1 Reply Latest reply on Feb 25, 2014 9:06 AM by Tristan Tarrant

    BasicCache loses value ?

    Chao Wang Newbie

      Hello, I'm using infinispan-core-5.3.0.Final, I found that in some case BasicCache can't properly persist data into cache, I made a simple test as codes below to simply insert 5 key-value into cache, however, cache size in the end is 4, my output of main method execution:

       

      Feb 25, 2014 2:39:49 PM org.infinispan.factories.GlobalComponentRegistry start

      INFO: ISPN000128: Infinispan version: Infinispan 'Tactical Nuclear Penguin' 5.3.0.Final

      Feb 25, 2014 2:39:50 PM org.infinispan.jmx.CacheJmxRegistration start

      INFO: ISPN000031: MBeans were successfully registered to the platform MBean server.

      basicCache : 4

      basicCache keySet: [923, 837, 937, 950]

       

      its simple test, but cache should contains all 5 elements I put, did I miss any configuration here?

      please see README.txt in attached zip file to compile and run the codes

       

      package org.jboss.overview;
      
      import org.infinispan.api.BasicCache;
      import org.infinispan.api.BasicCacheContainer;
      import org.infinispan.configuration.cache.CacheMode;
      import org.infinispan.configuration.cache.Configuration;
      import org.infinispan.configuration.cache.ConfigurationBuilder;
      import org.infinispan.configuration.global.GlobalConfiguration;
      import org.infinispan.configuration.global.GlobalConfigurationBuilder;
      import org.infinispan.eviction.EvictionStrategy;
      import org.infinispan.manager.DefaultCacheManager;
      
      public class MainCacheTest {
      
          private static BasicCacheContainer manager;
      
          public static BasicCacheContainer getCacheContainer() {
              if (manager == null) {
                  GlobalConfiguration glob = new GlobalConfigurationBuilder().nonClusteredDefault().globalJmxStatistics().enable()
                          .jmxDomain("overview").build();
                  Configuration loc = new ConfigurationBuilder().clustering().cacheMode(CacheMode.LOCAL).eviction().maxEntries(100)
                          .strategy(EvictionStrategy.LIRS).loaders().passivation(false).build();
                  manager = new DefaultCacheManager(glob, loc, true);
              }
              return manager;
          }
      
          public static void main(String[] args) {
              BasicCache<Integer, String> basicCache = getCacheContainer().getCache("CACHE_NAME");
              // HashMap<Integer, String> cache = new HashMap<Integer, String>();
              // add 5 key-value to each caches
              basicCache.put(965, "value965");
              basicCache.put(950, "value950");
              basicCache.put(937, "value937");
              basicCache.put(923, "value923");
              basicCache.put(837, "value837");
      
              // size should be 5, but output is 4
              System.out.println("basicCache : " + basicCache.size());
              System.out.println("basicCache keySet: " + basicCache.keySet());
          }
      
      }
      
        • 1. Re: BasicCache loses value ?
          Tristan Tarrant Master

          Hi Chao,

           

          this is because the internal Infinispan container uses a concurrent hashmap divided into segments and the number you set in eviction().maxEntries(x) is "spread" across the segments. This means that if you set 100 (as you have) with 32 segments (a number determined by locking().concurrencyLevel()), each segment will contain at most 3 entries and then they will be evicted. To workaround this, tune the concurrencyLevel accordingly: make it smaller and each segment will be "larger", make it bigger and each segment will be smaller (but there will be more of them). Which buckets gets which data is determined by the hashing of your keys.