[infinispan-dev] Distributed Counter Discussion

Pedro Ruivo pedro at infinispan.org
Mon Mar 14 15:14:26 EDT 2016


Hi everybody,

Discussion about distributed counters.

== Public API ==

interface Counter

String getName() //counter name
long get()	 //current value. may return stale value due to concurrent 
operations to other nodes.
void increment() //async or sync increment. default add(1)
void decrement() //async or sync decrement. default add(-1)
void add(long)   //async or sync add.
void reset()     //resets to initial value

Note: Tried to make the interface as simple as possible with support for 
sync and async operations. To avoid any confusion, I consider an async 
operation as happening somewhat in the future, i.e. eventually 
increments/decrements.
The sync operation happens somewhat during the method execution.

interface AtomiCounter extends Counter

long addAndGet()       //adds a returns the new value. sync operation
long incrementAndGet() //increments and returns the new value. sync 
operation. default addAndGet(1)
long decrementAndGet() //decrements and returns the new value. sync 
operation. default addAndGet(-1)

interface AdvancedCounter extends Counter

long getMin/MaxThreshold() //returns the min and max threshold value
void add/removeListener()  //adds a listener that is invoked when the 
value change. Can be extended to notify when it is "reseted" and when 
the threshold is reached.

Note: should this interface be splitted?

== Details ==

This is what I have in mind. Two counter managers: one based on JGroups 
counter and another one based on Infinispan cache.
The first one creates AtomicCounters and it first perfectly. All 
counters are created with an initial value (zero by default)
The second generates counters with all the options available. It can mix 
sync/async operation and all counters will be in the same cache. The 
cache will be configure by us and it would be an internal cache. This 
will use all the features available in the cache.

Configuration-wise, I'm thinking about 2 parameters: number of backups 
and timeout (for sync operations).

So, comment bellow and let me know alternatives, improvement or if I 
missed something.

ps. I also consider implement a counter based on JGroups-raft but I 
believe it is an overkill.
ps2. sorry for the long email :( I tried to be shorter as possible.

Cheers,
Pedro


More information about the infinispan-dev mailing list