4 Replies Latest reply on Jan 30, 2012 7:45 AM by Vladimir Blagojevic

    How to get cache entries when clustering mode is LOCAL or REPL_SYNC

    sudheesh p Newbie

      Hi,

       

      When the cache is configured as LOCAL ro REPL_SYNC, how we get the cache entries?

       

      I tried using the following snipped for LOCAL :

       

      Configuration config =cache.getConfiguration();

                                              long expiryLifespan = config.getExpirationLifespan();

       

                                              if(expiryLifespan > 0){

                                                        System.out.println("expiryMillis extracted from entry is :"+expiryLifespan);

                                                        System.out.println("Time elapsed is :"+(expiryLifespan- System.currentTimeMillis()));

                                                        long newTTL = (expiryLifespan- System.currentTimeMillis())+ttlOffset;

                                                        System.out.println("newTTL is :"+newTTL);

                                                        config.setExpirationLifespan(newTTL);

                                              }

       

                                              else if(expiryLifespan == -1){

                                                        System.out.println("Entry is set with never expire; Do Nothing");

                                              }

       

      However i feel that if the client has put a value into a cache with a specific lifespan other than the one in the configuration, then this code will fail to statisfy the change of lifespan.

       

      Pls let me know how a mechanism where i can i access the cache map and apply the new lifespan value to all the cache entries.

      I was able to use mapReduce for a DIST configured cache and achieve the same

        • 1. Re: How to get cache entries when clustering mode is LOCAL or REPL_SYNC
          Galder Zamarreño Master

          First of all, that way of configuring Infinispan is deprecated in 5.1 (not 100% sure it'll have the desired effect btw). In the new configuration, expiration lifespan is not a dynamic property, although you can make a suggestion in https://issues.jboss.org/browse/ISPN

           

          Expiration values applied via Cache methods always have preference, so you can always traverse the entire cache and re-put with the desired lifespan (using map/reduce for example).

          • 2. Re: How to get cache entries when clustering mode is LOCAL or REPL_SYNC
            sudheesh p Newbie

            So i understand the expiration lifespan via configuration has been deprecated in Infinispan 5.1.

             

             

            Here i am trying a map reduce for a replicated cache whose configuration is :

             

            <clustering mode="replication"> <stateRetrieval timeout="20000" fetchInMemoryState="true"

                                          alwaysProvideInMemoryState="true" /> <sync replTimeout="30000" /> </clustering>

             

            public class MyMapReduceExample {

             

                        public static void main(String[] args) {

             

                                EmbeddedCacheManager manager;

                                long ttlOffset= 2000;

             

                                try {

                                          manager = new DefaultCacheManager("InfinispanCfgForNode1.xml");

                                          Cache cache = manager.getCache("InfinispanClientTest");

                                          Configuration.CacheMode cacheMode = cache.getConfiguration().getCacheMode();

                                          cache.put("LC", "Cat", 3000, TimeUnit.MILLISECONDS);

                                          MapReduceTask<Object, Object, Object, Object> t =

                                                                       new MapReduceTask<Object, Object, Object, Object>(cache);

                                        t.mappedWith(new MyShiftTTLMapper(manager, cache.getName(), ttlOffset)).reducedWith(new MyTestReducer());

                                        t.execute();

             

                                } catch (IOException e) {

                                          // TODO Auto-generated catch block

                                          e.printStackTrace();

                                }

             

                      }

             

             

            }

             

             

             

             

             

             

            public class MyShiftTTLMapper implements Mapper{

             

                      long ttlOffset;

                      private EmbeddedCacheManager manager;

                      private String cacheName;

             

                      public MyShiftTTLMapper(){

                                ttlOffset = 0;

                      }

                      public MyShiftTTLMapper(EmbeddedCacheManager manager, String cacheName,long ttlOffset){

                                System.out.println("Inside constructor of MyTestMapper");

                                this.manager = manager;

                                this.cacheName = cacheName;

                                this.ttlOffset = ttlOffset;

             

                      }

                      @Override

                      public void map(Object key, Object value, Collector arg2) {

                                Cache cache = this.manager.getCache(this.cacheName);

                                AdvancedCache advancedCache = cache.getAdvancedCache();

                                DataContainer dataContainer = advancedCache.getDataContainer();

                                InternalCacheEntry entry = dataContainer.get(key);

                                long expiryMillis = entry.getExpiryTime();

                                if (expiryMillis > 0) {

                                          long newTTL = (expiryMillis- System.currentTimeMillis())+ttlOffset;

                                          entry.setLifespan(newTTL);

             

                                }

                                if(expiryMillis == -1){

                                          System.out.println("Entry is set with never expire; Do Nothing");

                                          //Do nothing. Since this entry should not expiry at any time

                                }

                      }

             

            }

             

             

             

             

            While i tried to use the map reduce on the REPL_SYNC configured cache i get the following error:

            Exception in thread "main" java.lang.IllegalStateException: Cache mode should be DIST, rather than REPL_SYNC

                      at org.infinispan.distexec.mapreduce.MapReduceTask.ensureProperCacheState(MapReduceTask.java:443)

                      at org.infinispan.distexec.mapreduce.MapReduceTask.<init>(MapReduceTask.java:156)

                      at com.hp.ispan.mapreduce.ttl.MyMapReduceExample.main(MyMapReduceExample.java:14)

             

             

            This method works well when the cache configuration is DIST. However for REPLY_SYNC configurations i get errors. What should i do now?

            • 3. Re: How to get cache entries when clustering mode is LOCAL or REPL_SYNC
              Galder Zamarreño Master

              Hmmmm, don't think it makes sense to do distributed map/reduce with REPL because all nodes have the same data, so all you have to do is loop through one of the node's data and that's it

               

              With DIST, data is stored in a subset, so to work on all the cached data, data from all nodes needs to be computed.

               

              True that Map/Reduce maybe should support this more REPL or LOCAL modes more easily, but taken the burden from the user.

              • 4. Re: How to get cache entries when clustering mode is LOCAL or REPL_SYNC
                Vladimir Blagojevic Master

                Galder,

                 

                Nothing against supporting REPL unless we have to add a lot of additional code and maintain lots of special cases in order to support it. I can also foreesee this being useful to setup some basic testing scenarios to test MapReduce. I suggest user open a JIRA and we'll take it from there!

                 

                Regards,

                Vladimir