Hazelcast is calling loadAll() TWICE for the same key

579 Views Asked by At

As far as I'm aware, when using a MapLoader, Hazelcast calls loadAllKeys() once on the node which owns the partition which owns the map's name.

loadAll(Collection<Long> keys) is then only called on partitions which own a given key retrieved from loadAllKeys(). After this, the values are distributed as-needed around the cluster.

I'm performing a basic test with one node, one map and one record in my persistent store.

What I'm finding is loadAllKeys() is correctly called once, however, loadAll(Collection<Long> keys) is called twice. Why is this the case?

My implementation of loadAll(Collection<Long> keys) is as follows:

@Override
public synchronized Map<Long, MyCacheEntry> loadAll(Collection<Long> keys) {

    return myCacheRepository.loadMyCacheDataForKeys(keys)
        .stream()
        .collect(Collectors.toMap(MyCacheEntity::getId,
                                  entity -> gson.fromJson(entity.getValue(), MyCacheEntry.class)
                ));

}

This means that I am doing two lookups to my persistent storage instead of one. Seeing as though I have one record in my database I would expect loadAll(Collection<Long> keys) to only be called once.

What is happening here?

My crude, test Hazelcast/Spring configuration is as follows:

@Configuration
public class HazelcastConfiguration {

  private final MyMapStore myMapStore;

  @Inject
  HazelcastConfiguration(@Lazy MyMapStore myMapStore) {
    this.myMapStore = myMapStore;
  }

  @PreDestroy
  public void shutdown() {
    Hazelcast.shutdownAll();
  }

  @Bean
  public HazelcastInstance hazelcastInstance() {

    Config config = new Config();
    config.getGroupConfig().setName("MyGroup");

    NetworkConfig networkConfig = config.getNetworkConfig();
    networkConfig.setPortAutoIncrement(false);

    JoinConfig joinConfig = networkConfig.getJoin();
    joinConfig.getMulticastConfig().setEnabled(false);
    joinConfig.getTcpIpConfig().setEnabled(true).setMembers(Collections.singletonList("127.0.0.1"));

    MapConfig mapConfig = new MapConfig("MyMap");
    mapConfig.setBackupCount(1);
    mapConfig.setAsyncBackupCount(1);
    mapConfig.setInMemoryFormat(InMemoryFormat.OBJECT);
    mapConfig.setTimeToLiveSeconds(0);

    EntryListenerConfig entryListenerConfig = new EntryListenerConfig();
    entryListenerConfig.setImplementation(new MyCacheEntryListener());
    mapConfig.addEntryListenerConfig(entryListenerConfig);

    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setInitialLoadMode(MapStoreConfig.InitialLoadMode.EAGER);

    mapStoreConfig.setWriteDelaySeconds(1);
    mapStoreConfig.setImplementation(myMapStore);

    mapConfig.setMapStoreConfig(mapStoreConfig);

    config.addMapConfig(mapConfig);

    return Hazelcast.newHazelcastInstance(config);
  }
}
0

There are 0 best solutions below