Hazelcast MapStoreConfig ignored

人走茶凉 提交于 2019-12-10 17:35:59

问题


I'm using a map store to persist my hazelcast distributed map in a database.

In my test case, I start three hazelcast instances, each configured the same way:

Config cfg = new Config();
cfg.setInstanceName("name");
hazelcast = Hazelcast.newHazelcastInstance(cfg);    
MapConfig mapConfig = new MapConfig("myMapName");
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setClassName(MyMapStore.class.getName());
mapStoreConfig.setWriteDelaySeconds(0);
mapStoreConfig.setEnabled(true);
mapConfig.setMapStoreConfig(mapStoreConfig);
mapConfig.setBackupCount(2);
hazelcast.getConfig().addMapConfig(mapConfig);
IMap myMap = hazelcast.getMap("myMapName");

However, when I add values to the map, only the first Cluster member writes into the database, the MapStoreConfig is just set to Default values on each other node. But, if I change the code to the following, it works:

Config cfg = new Config();
cfg.setInstanceName("name");
MapConfig mapConfig = new MapConfig("myMapName");
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setClassName(MyMapStore.class.getName());
mapStoreConfig.setWriteDelaySeconds(0);
mapStoreConfig.setEnabled(true);
mapConfig.setMapStoreConfig(mapStoreConfig);
mapConfig.setBackupCount(2);
cfg.addMapConfig(mapConfig);
hazelcast = Hazelcast.newHazelcastInstance(cfg);
IMap myMap = hazelcast.getMap("myMapName");

Seems like the line hazelcast.getConfig().addMapConfig(mapConfig); is ignored. Tested with hazelcast v3.1.5.


回答1:


To my understanding, the Config instance should be considered only as a sort of "template", which is used to create one instance, but may not be used to afterwards modify this instance. Although, according to a quick websearch and a look at the documentation, this is not stated explicitly, but

  1. all examples that I have seen so far follow the pattern of FIRST creating and the complete Config, and THEN creating the corresponding instance and
  2. the addMapConfig method (in https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/config/Config.java ) does not involve any notification mechanism. So there would at least have to be some mechanism to inform the Hazelcast instance about the changed config, but I think that such a mechanism does not exist.

From what I have read in the source code ( e.g. in https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/instance/Node.java and https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/instance/HazelcastInstanceImpl.java ), the Config is only read at construction time, and it does not seem to be possible to change the configuration after the Hazelcast instance has been created.




回答2:


The ability to configure Hazelcast at runtime is convenient, but very misleading. I use Spring, so I've always had crystal clarity about the concept of the configuration. In most systems (Hazelcast being no exception) configuration should be specified before instantiating the "engine".



来源:https://stackoverflow.com/questions/21379171/hazelcast-mapstoreconfig-ignored

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!