7 Replies Latest reply on Mar 17, 2015 5:52 PM by fly_

    Weird values when using embedded distributed cache with HotRod client/server

    fly_

      Hi JBoss community,

       

      I'm trying to use Infinispan (7.1.0.Final) as a distributed cache and to expose it through hotrod protocole.

      I started with the sample project from Tristan Tarrant (thanks to him) here https://github.com/tristantarrant/infinispan-playground-embedded-hotrod


      All worked fine until I tried to use a distributed cache. Now, all values I insert are weird (random) values (look like serialized data).

      When I used the cache as a local cache all is ok.


      Here is the code SimpleEmbeddedHotRodServer.java :

      public static void main(String[] args) throws Exception {

              // disabled for test

              //org.infinispan.configuration.cache.ConfigurationBuilder embeddedBuilder = new org.infinispan.configuration.cache.ConfigurationBuilder();

              //embeddedBuilder.dataContainer().keyEquivalence(new AnyServerEquivalence()).valueEquivalence(new AnyServerEquivalence());

              DefaultCacheManager defaultCacheManager = new DefaultCacheManager("distributed.xml");

       

              HotRodServerConfiguration build = new HotRodServerConfigurationBuilder().build();

              HotRodServer server = new HotRodServer();

              server.start(build, defaultCacheManager);

       

              ConfigurationBuilder remoteBuilder = new ConfigurationBuilder();

              remoteBuilder.addServers("localhost");

              RemoteCacheManager remoteCacheManager = new RemoteCacheManager(remoteBuilder.build());

              RemoteCache<String, String> remoteCache = remoteCacheManager.getCache();

       

              System.out.print("Inserting data into cache...");

              String s = "asRemote";

              remoteCache.put(s, s);

              System.out.print("\nVerifying data...");

              System.out.printf("%s...", remoteCache.get(s));

       

              System.out.print("\nNow using as a local cache");

              Cache<String, String> cache = defaultCacheManager.getCache();

              s = "asLocal";

              cache.put(s, s);

              System.out.print("\nVerifying data...");

              System.out.printf("%s...", cache.get(s));

             

              System.out.print("\nWhat is in my cache ?");      

              System.out.printf("\nCache have %s entries", cache.size());

              for(Map.Entry<String, String> entry : cache.entrySet()) {

                  System.out.printf("\n\t\tCache entry %s : value %s", entry.getKey(), entry.getValue());

              }

             

              System.out.println("\nDone !");

              remoteCacheManager.stop();

              server.stop();

              defaultCacheManager.stop();

      }

       

      and the conf distributed.xml :

      <?xml version="1.0" encoding="UTF-8"?>

      <infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

          xmlns="urn:infinispan:config:7.1"

          xsi:schemaLocation="urn:infinispan:config:7.1                 http://www.infinispan.org/schemas/infinispan-config-7.1.xsd ">

       

          <!-- minimal config -->

          <cache-container default-cache="my-cache" statistics="true" >

              <transport />

              <distributed-cache name="my-cache" l1-lifespan="30000" owners="2" mode="SYNC" statistics="true" />

          </cache-container>

       

      </infinispan>

       

      Now What I get when I mvn exec:java :

              ....

      Inserting data into cache...

      Verifying data...null...

      Now using as a local cache

      Verifying data...asLocal...

      What is in my cache ?

      Cache have 2 entries

                      Cache entry [B@1d422062 : value [B@31d7ed1d

                      Cache entry asLocal : value asLocal

      Done !

             ........

       

      I can't figured out what goes wrong.

      Any Idea ?

       

      Thanks for your help.

      Regards,

        • 1. Re: Weird values when using embedded distributed cache with HotRod client/server
          pruivo

          Hi Flavien,

           

          If you want to use multiple endpoints (like in your example, where you use embedded and hotrod), you have to enable the compatibility mode:

           

          <distributed-cache ...>
              <compatibility/>
          </distributed-cache>
          

           

          That should solve your problem.

           

          Cheers,

          Pedro

          • 2. Re: Weird values when using embedded distributed cache with HotRod client/server
            wdfink

            I don't understand what you want to achieve.

            Why you don't use a infinispan server and hot rod clients?

             

            Could you give a bit more details?

            • 3. Re: Weird values when using embedded distributed cache with HotRod client/server
              fly_

              Yes it works !

               

              In fact, I'm quite confused because I thought the "compatibility" configuration was only for multi endpoint access and at first, my probleme was only about remote access with HotRod (I tried to get data from embedded cache only for debug purpose) --> see from previous code :

                             ...

                      String s = "asRemote";

                      remoteCache.put(s, s);

                      System.out.print("\nVerifying data...");

                      System.out.printf("%s...", remoteCache.get(s));  // --> remoteCache.get(s) return null !!

                      ...

              Does it mean that if you want to use the HotRod endpoint (and only HotRod protocole) with distributed cache, you must absolutly add "compatibility" configuration to your cache (IMO this is not clear in the doc)?

               

              Many many thanks for your help !

              • 4. Re: Weird values when using embedded distributed cache with HotRod client/server
                nadirx

                Does it mean that if you want to use the HotRod endpoint (and only HotRod protocole) with distributed cache, you must absolutly add "compatibility" configuration to your cache (IMO this is not clear in the doc)?

                 

                No, if it's HotRod only. But here you're also using Embedded, so those are two different access modes.

                 

                Tristan

                • 5. Re: Weird values when using embedded distributed cache with HotRod client/server
                  pruivo

                  I think I got it.

                   

                  you are getting null because you didn't configure any Equivalence. You have to use the AnyServerEquivalence:

                   

                  <distributed-cache ...>
                       <data-container key-equivalence="org.infinispan.commons.equivalence.AnyServerEquivalence" value-equivalence="org.infinispan.commons.equivalence.AnyServerEquivalence"/>
                  </distributed-cache>
                  

                   

                  The Equivalence is needed because hotrod stores the data in byte[]. In java, the equals() method only returns true if the byte[] are is same. The AnyServerEquivalence will solve that problem by comparing the content of the array.

                  Cheers,

                  Pedro

                  • 6. Re: Weird values when using embedded distributed cache with HotRod client/server
                    fly_

                    you are perfectly right ! It works, too.

                     

                    I didn't pay enough attention to the configuration from Tristan Tarrant sample which is using an AnyServerEquivalence class.

                    I thought that this comparison behavior would be "built in" but I think that HotRod, as a multi language server, don't care about java concern.

                     

                    One more thing : I didn't find org.infinispan.commons.equivalence.AnyServerEquivalence in infinispan-server-hotrod:7.1.1.Final maven dependency (including its childs artifacts). I found it in org.jboss.as.clustering.infinispan.equivalence.AnyServerEquivalence but it's part of the Infinispan Server binaries.

                     

                    Maybe this class should be included in infinispan-server-hotrod artifacts (or maybe one of its child)

                     

                    I found this bug ISPN-4212 that talk about the same probleme.

                     

                    Once again thanks for your help,

                    Regards,

                    • 7. Re: Weird values when using embedded distributed cache with HotRod client/server
                      fly_

                      Why you don't use a infinispan server and hot rod clients?

                       

                      Actually I also tried to launch Infinispan server nodes. HotRod part worked well but I get an other problem (maybe trivial) with the REST endpoint and security (I would like to have those two protocoles)

                       

                      (created SO question here : Infinispan Server say "No security context found" when using REST endpoint - Stack Overflow)

                       

                      So finally launching a hotrod server in my own java processes seems to be a good alternative, I will be able to create my own REST endpoint with specific operations.

                       

                      Regards