当只有一部分节点能够填充Map数据时,我如何配置Hazelcast read-through Map



假设我有两种类型的Hazelcast节点在集群上运行:

  • "领导者">节点–这些节点能够加载并填充Hazelcast映射M。领导者还将不时更新M中的值(基于外部资源)
  • "追随者节点–这些节点需要从M中读取

我的意图是让Follower节点触发将丢失的元素加载到M中(因此需要在Leader侧进行加载)。

粗略地说,从地图中获取元素的步骤可能看起来像这样:

IMap m = hazelcastInstance.getMap("M");
if (!m.containsKey(k)) {  
if (iAmLeader()) { 
Object fresh = loadByKey(k); // loading from external resource 
return m.put(k, fresh);
} else {
makeSomeLeaderPopulateValueForKey(k);
}
}
return m.get(k);

你能建议什么方法?


注释

  1. 我希望追随者充当节点,而不仅仅是客户端,因为追随者实例将远远多于领导者,我希望他们参与负载分配
  2. 我可以只构建另一个服务级别,它只在Leader节点上运行,并提供用请求的密钥填充映射的接口。但这意味着要增加额外的通信和配置层,我希望上述需求可以在单个Hazelcast集群中解决

我想我可能已经在MapLoader的形式中找到了答案(自最初发布以来,我已经确认这确实是一种方法)。

final Config config = new Config();
config.getMapConfig("MY_MAP_NAME").setMapStoreConfig(
new MapStoreConfig().setImplementation(new MapLoader<KeyType, ValueType>(){
@Override
public ValueType load(final KeyType key) {
//when a client asks for data for corresponding key of type
//KeyType that isn't already loaded
//this function will be invoked and give you a chance
//to load it and return it
ValueType rv = ...;
return rv;
}
@Override
public Map<KeyType, ValueType> loadAll(
final Collection<KeyType> keys) {
//Similar to MapLoader#load(KeyType), except this is
//a batched version of it for performance gains.
//this gets called on first access to the cache,
//where MapLoader#loadAllKeys() is called to get
//the keys parameter for this funcion
Map<KeyType, ValueType> rv = new HashMap<>();
keys.foreach((key)->{
rv.put(key, /*figure out what key means*/);
});
return rv;
}
@Override
public Set<KeyType> loadAllKeys() {
//Prepopulate all the keys. My understanding is that
//this is an initialization step, to give you a chance
//to load data on startup so an initial set of datas
//will be available to anyone using the cache. Any keys
//returned here are sent to MapLoader#loadAll(Collection)
Set<KeyType> rv = new HashSet<>();
//figure out what keys need to be in the return value
//to load a key into cache at first access to this map,
//named "MY_MAP_NAME" in this example
return rv;
}
}));
config.getGroupConfig().setName("MY_INSTANCE_NAME").setPassword("my_password");
final HazelcastInstance hazelcast = Hazelcast
.getOrCreateHazelcastInstance(config);

最新更新