4 Replies Latest reply on Feb 25, 2013 11:54 AM by mircea.markus

    Atomic Numbers

    paradigmza

      Hello,

       

      I have been playing around with Hazelcast and I found a useful feature for atomic numbers across a distributed cache.

       

       

      AtomicNumber number = hazelCastInstance.getAtomicNumber("somekey");
      long result = number.addAndGet(1);
      

       

      How can I do this with Infinispan? 

       

      I found something called an atomic map, but I am not quite sure how to use that to emulate the above code.

       

      Thanks,

      Sean.

        • 1. Re: Atomic Numbers
          alexgad

          Were you able to find a solution to the above?  I an currently investigating Infinispan for a project and distributed atomic increment/decrement is an important requirement.  Thanks.

          • 2. Re: Atomic Numbers
            an1310

            Hi Sean,

             

            While ISPN doesn't offer this as part of the API, I've implemented something similar in my application.  It's actually pretty easy and very performant if you use the distributed executor framework.  Here are some rough guidelines:

             

            • You assuredly will either need a lock/increment/release pattern to get counter values.  However, if you execute a task on the data owner, you're acquiring local locks.
            • You will need to look at the cache configuration options for eager locks.
            • You may want to consider allocating "chunks" of possible counter values for your clients to use locally.  This will reduce contention on the cache, while guaranteeing uniqueness.
            • (Don't forget about overflow conditions)

             

            Erik

            • 3. Re: Atomic Numbers
              paolodt

              It's pretty easy to implement using an distributed map. 

               

              Keep the last value in a map. When you need the next value, get the last value from the map, increment it, and save the new value using the ConcurrentMap#replace method.

               

              If replace fails, continue to incremente the next value . .

               

              Cheers,

              Paolo

              • 4. Re: Atomic Numbers
                mircea.markus

                Both suggeestions should work.

                We do plan to add native support for atomic operations in future, please see/vore for https://issues.jboss.org/browse/ISPN-924.