Post

2 followers Follow
1
Avatar

Handling concurrent updates

A newbie question-

I have gemfire region defined as follows

Region> region;

I would like to write an update function. The crude implementation is as follows

void update(Integer key, Data d) {
Set dataSet = region.get(key);
if( dataSet == null ) {
dataSet = new HashSet();
} else {
dataSet = CopyHelper.copy(dataSet);
}
if( dataSet.add(d) == false ) {
dataSet.remove( d );
dataSet.add( d );
}
region.put( key, dataSet );
}

Questions -
A) if two threads try to add two different objects to same key, for example, update(1, dataObj1) and update(1, dataObj2), it is possible that the set might end up with only one object. How do I make the operation atomic ?
B) can I use the compute method in ConcurrentMap since region implements ConcurrentMap. Then again that will be in place update of an entry value which is not recommended by gemfire

Thanks,
SM

S M

Please sign in to leave a comment.

2 comments

0
Avatar

Hello SM,

For question A), since GemFire version 7 (the latest version is 9.0.x), GemFire automatically resolve data inconsistency caused with updating the same data entry by multiple threads from different location by default via "consistency checking" feature. It resolves data inconsistency for each region type differently. Please fake a look at the following docs.

[Consistency Checking by Region Type]
http://gemfire.docs.pivotal.io/geode/developing/distributed_regions/how_region_versioning_works.html

This realize "to make the operation atomic" as a result in terms of "eventually consistency."

If you want to realize "to make the operation atomic" in this context, you may apply "Global Region" or "Distributed Lock" although you need to accept some limitations. Please refer to the following docs regarding "Global Region."

[Locking in Global Regions]
http://gemfire.docs.pivotal.io/geode/developing/distributed_regions/locking_in_global_regions.html

You may also apply DistributedLockService API. But this is not only region level API but also any purpose of locking mechanism in the distributed system. 

[DistributedLockService]
http://gemfire-90-javadocs.docs.pivotal.io/org/apache/geode/distributed/DistributedLockService.html

You need much more effort to realize "to make the operation atomic" anyhow. You should be careful to use this API to avoid dead-lock situation.

Akihiro Kitada 0 votes
0
Avatar

Hello SM,

For question B), GemFire's region API does not determine special implementation for compute method derived from ConcurrentMap for Region interface. It works in terms of just putting data entries into GemFire's region based on a condition determined by a lambda formula or something but it means it may work atomically only for local region (only in a single JVM). It may be not considered to guarantee the atomic region operation across the distributed system.

Again, if you want to realize "to make the region operation atomic", you can choose three ways according to your requirements, mentioned at the previous comment of mine for your question A).

Regards. 

Akihiro Kitada 0 votes