public class CounterService { // Channel stack must include COUNTER protocol. public CounterService (JChannel ch) { ... } public SyncCounter getOrCreateSyncCounter (String name, long initial_value) { ... } public CompletionStage<AsyncCounter> getOrCreateAsyncCounter (String name, long initial_value) { ... } public void deleteCounter (String name) { ... } ... } public interface SyncCounter extends BaseCounter { long get (); void set (long new_value); long addAndGet (long delta); long incrementAndGet (); long decrementAndGet (); long compareAndSwap (long expect, long update); boolean compareAndSet (long expect, long update); // Useful for complex updates under high contention. <T extends Streamable> T update (CounterFunction<T> updateFunction); } public interface AsyncCounter extends BaseCounter { CompletionStage<Long> get (); CompletionStage<Void> set (long new_value); ... }
Cluster coordinator stores and updates counter values
Cluster coordinator can have backup coordinators
Counter values include version also sent to clients
Client value with latest version used to recover from coordinator failure