1

I am trying to implement a counter system using infinispan cache. Multiple threads are trying to access a same key and increment the value against that key.

The counter operation begins with lock(key) so that no other thread can write to it. The lock-acquired thread performs get(key), increments the value and puts it back against the same key.

Another thread , which was waiting for lock release , calls get(key) . Instead of the expected updated counter value , the get(KEY) returns a previous value.

I am beginning a new transaction against the cache in each thread. The transaction is auto-committed.

I tried using auto-commit as well as explicitly committing after put. I also Made sure to clear the file persistence and db persistence before every run.

This is my infinispan cache configuration.

<distributed-cache name="COUNTER_CACHE">
<transaction transaction-manager-lookup="com.suntecgroup.tbms.container.services.cluster.ContainerCacheTxManagerLookup" mode="FULL_XA" locking="PESSIMISTIC"/>
<state-transfer timeout="360000" await-initial-transfer="true" enabled="true"/>
<locking isolation="READ_COMMITTED" acquire-timeout="60000" concurrency-level="100" striping="false"/>
<memory>
<off-heap size="1000" eviction="COUNT"/>
 <!--  Determines the max entries to hold in the cache  -->
</memory>
<persistence passivation="false">
<file-store path="./infinispan_persistance_tmp/" max-entries="1000"/>
<store class="com.suntecgroup.tbms.container.cache.store.ispn.InfinispanCounterCacheStore" fetch-state="false" preload="true" shared="false" purge="false" read-only="false" singleton="false"> </store>
</persistence>
</distributed-cache>

The expected result is that , any update to the value made by a thread should be reflected when another tries to get the same value.

My best guess is that it has to do something with the transaction, which is created per thread.

I cannot share the actual code but following code approximates the process. Hope this helps

class CounterThread implements Runnable{

    public Integer getCounterVal(String key){
        int counter = cacheDelegate.get(key);
        if(counter==null){
                synchronized (Counter.class) {
                    if(counter = cacheDelegate.beginTransactionIfNoneAndGetObjectWithLock(key)){                
                    counter = 0;
                    cacheDelegate.putAndCommit(key,counter);
                    }
            }
        }   else
        counter = cacheDelegate.beginTransactionIfNoneAndGetObjectWithLock(key);
        return  counter;
    }

    public void putCounterVal(String key,int val){
        val++;
        cacheDelegate.putAndCommit(key,val);
    }

    public void run(){
        int i = 0;
        while(i<100){
            int counter = getCounterVal("KEY");
            putCounterVal("KEY",counter);
            i++;
        }
    }
}

// this method is used in beginTransactionIfNoneAndGetObjectWithLock to create a transaction
public void begin()
            throws ContainerPlatformServicesException {
        try {
            if (getCache("COUNTER_CACHE").getAdvancedCache().getTransactionManager()
                    .getTransaction() == null) {
                getCache("COUNTER_CACHE").getAdvancedCache().getTransactionManager()
                        .setTransactionTimeout(transactionTimeOut);
                getCache("COUNTER_CACHE").getAdvancedCache().getTransactionManager()
                        .begin();
            }
        }catch(TimeoutException e){
            logger.warn("TimeoutException exception caught. Required node is suspected. Thus waiting for suspect completion, to begin transaction.");
        } 
        catch (Exception e) {
            logger.error("Failed to begin transaction. Details:-", e);
        }
    }


// Only two threads are running.
Alan Lal
  • 147
  • 2
  • 8

0 Answers0