hazelcasthazelcast-imaphazelcast-aggregate

UnsupportedOperationException when looping over a mapConfig created from a ClientConfig instance of Hazelcast


May I ask your help on how to loop over a mapConfig to change the backup-count of a running Hazelcast that is setup from a ClientConfig class?

I have checked the hazelcast code examples but all cases, the backup-count is set over configuration I also tried to apply the backup-count code from this stackoverflow (which, btw, was asked by myself) but I'm facing a UnsupportedOperationException because it seems a Hazelcast ClientConfig instance cannot change in runtime, is it correct? Is there any workaround for it/suggestion on how to overcome it?

I have the following implementation:

ClientConfig cfg = new ClientConfig();
if (hazelcastConfPath != null) {
    cfg = new XmlClientConfigBuilder(hazelcastConfPath).build();
}
cfg.setProperty("hazelcast.logging.type", "slf4j");
hazelcastInstance = HazelcastClient.newHazelcastClient(cfg);

int clusterSize = hazelcastInstance.getCluster().getMembers().size();
logger.info("Cluster size: {}", clusterSize);

for (Entry<String, MapConfig> currMap : hazelcastInstance.getConfig().getMapConfigs().entrySet()) {
    if (clusterSize - 1 > 1) {
        try {
            currMap.getValue().setBackupCount(clusterSize);

        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }
    }

    logger.info("Map {} configured with {}", currMap.getKey(), currMap.getValue().getBackupCount());
}

and the Hazelcast XML configuration:

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast ...>
    <network>
        <port auto-increment="false">${hazelcast.port}</port>
    </network>
    <management-center enabled="true" update-interval="3">
        ${hazelcast.mancenter.url}
    </management-center>
    <group>
        <name>${group.name}</name>
        <password>${group.password}</password>
    </group>
    <map name="*token">
        <max-idle-seconds>120</max-idle-seconds>
        <backup-count>1</backup-count>
        <eviction-policy>LRU</eviction-policy>
    </map>
    <map name="*actionSessionRedirUrlMap">
        <max-idle-seconds>30</max-idle-seconds>
        <backup-count>1</backup-count>
        <eviction-policy>LRU</eviction-policy>
    </map>
</hazelcast>

Doing this, I have the UnsupportedOperationException, it seems it is not possible :'(

java.lang.UnsupportedOperationException: Client config object only supports adding new data structure configurations
at com.hazelcast.client.impl.clientside.ClientDynamicClusterConfig.getMapConfigs(ClientDynamicClusterConfig.java:591)
...

Thank You Very Much, Geraldo Netto


Solution

  • You can achieve this either through management-center (https://docs.hazelcast.org/docs/management-center/latest/manual/html/index.html#deploying-and-starting) or by starting a lite-member with a new MapConfig having the same name but different backup count. See below for an example:

    MapConfig mapConfig = new MapConfig("map_a");
    mapConfig.setBackupCount(3);
    
    Config config = new Config();
    config.setLiteMember(true);
    config.addMapConfig(mapConfig);
    
    HazelcastInstance lite = Hazelcast.newHazelcastInstance(config);