Class WeakCounterImpl
- java.lang.Object
-
- org.infinispan.counter.impl.weak.WeakCounterImpl
-
- All Implemented Interfaces:
WeakCounter,CounterEventGenerator,TopologyChangeListener,InternalCounterAdmin
public class WeakCounterImpl extends Object implements WeakCounter, CounterEventGenerator, TopologyChangeListener, InternalCounterAdmin
A weak consistent counter implementation.Implementation: The counter is split in multiple keys and they are stored in the cache.
Write: A write operation will pick a key to update. If the node is a primary owner of one of the key, that key is chosen based on thread-id. This will take advantage of faster write operations. If the node is not a primary owner, one of the key in key set is chosen.
Read: A read operation needs to read all the key set (including the remote keys). This is slower than atomic counter.
Weak Read: A snapshot of all the keys values is kept locally and they are updated via cluster listeners.
Reset: The reset operation is not atomic and intermediate results may be observed.
- Since:
- 9.0
- Author:
- Pedro Ruivo
-
-
Constructor Summary
Constructors Constructor Description WeakCounterImpl(String counterName, org.infinispan.AdvancedCache<WeakCounterKey,CounterValue> cache, CounterConfiguration configuration, CounterManagerNotificationManager notificationManager)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CompletableFuture<Void>add(long delta)Adds the given value to the new value.<T extends CounterListener>
Handle<T>addListener(T listener)Adds aCounterListenerto this counter.WeakCounterasWeakCounter()CompletionStage<Void>destroy()Destroys the counter.CounterEventgenerate(CounterKey key, CounterValue value)It generates theCounterEvent.CounterConfigurationgetConfiguration()WeakCounterKey[]getKeys()Debug only!StringgetName()WeakCounterKey[]getPreferredKeys()Debug only!longgetValue()It returns the counter's value.CompletionStage<InternalCounterAdmin>init()Initializes the key set.booleanisWeakCounter()Checks if the counter is aWeakCounter.CompletableFuture<Void>remove()It removes this counter from the cluster.static CompletionStage<Void>removeWeakCounter(org.infinispan.Cache<WeakCounterKey,CounterValue> cache, CounterConfiguration configuration, String counterName)It removes a weak counter from thecache, identified by thecounterName.CompletableFuture<Void>reset()Resets the counter to its initial value.SyncWeakCountersync()It returns a synchronous weak counter for this instance.voidtopologyChanged()It notifies the cache topology change.StringtoString()CompletionStage<Long>value()Returns the counter's value.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.infinispan.counter.impl.manager.InternalCounterAdmin
asStrongCounter
-
Methods inherited from interface org.infinispan.counter.api.WeakCounter
decrement, increment
-
-
-
-
Constructor Detail
-
WeakCounterImpl
public WeakCounterImpl(String counterName, org.infinispan.AdvancedCache<WeakCounterKey,CounterValue> cache, CounterConfiguration configuration, CounterManagerNotificationManager notificationManager)
-
-
Method Detail
-
removeWeakCounter
public static CompletionStage<Void> removeWeakCounter(org.infinispan.Cache<WeakCounterKey,CounterValue> cache, CounterConfiguration configuration, String counterName)
It removes a weak counter from thecache, identified by thecounterName.- Parameters:
cache- TheCacheto remove the counter from.configuration- The counter's configuration.counterName- The counter's name.
-
init
public CompletionStage<InternalCounterAdmin> init()
Initializes the key set.Only one key will have the initial value and the remaining is zero.
-
getName
public String getName()
- Specified by:
getNamein interfaceWeakCounter- Returns:
- The counter name.
-
getValue
public long getValue()
Description copied from interface:WeakCounterIt returns the counter's value.This value may be not the mot up-to-data value.
- Specified by:
getValuein interfaceWeakCounter- Returns:
- The counter's value.
-
add
public CompletableFuture<Void> add(long delta)
Description copied from interface:WeakCounterAdds the given value to the new value.- Specified by:
addin interfaceWeakCounter- Parameters:
delta- the value to add.
-
asWeakCounter
public WeakCounter asWeakCounter()
- Specified by:
asWeakCounterin interfaceInternalCounterAdmin- Returns:
- The
WeakCounterinstance or throws anCounterExceptionif the counter is not aWeakCounter.
-
destroy
public CompletionStage<Void> destroy()
Description copied from interface:InternalCounterAdminDestroys the counter.It drops the counter's value and all listeners registered.
- Specified by:
destroyin interfaceInternalCounterAdmin- Returns:
- A
CompletionStageinstance which is completed when the operation finishes.
-
reset
public CompletableFuture<Void> reset()
Description copied from interface:WeakCounterResets the counter to its initial value.- Specified by:
resetin interfaceInternalCounterAdmin- Specified by:
resetin interfaceWeakCounter- Returns:
- A
CompletionStageinstance which is completed when the operation finishes.
-
value
public CompletionStage<Long> value()
Description copied from interface:InternalCounterAdminReturns the counter's value.- Specified by:
valuein interfaceInternalCounterAdmin- Returns:
- A
CompletionStageinstance which is completed when the operation finishes.
-
isWeakCounter
public boolean isWeakCounter()
Description copied from interface:InternalCounterAdminChecks if the counter is aWeakCounter.If
true, ensuresInternalCounterAdmin.asWeakCounter()never throws anCounterException. Otherwise, it ensuresInternalCounterAdmin.asStrongCounter()never throws anCounterException.- Specified by:
isWeakCounterin interfaceInternalCounterAdmin- Returns:
trueif the counter isWeakCounter.
-
addListener
public <T extends CounterListener> Handle<T> addListener(T listener)
Description copied from interface:WeakCounterAdds aCounterListenerto this counter.- Specified by:
addListenerin interfaceWeakCounter- Type Parameters:
T- The type of the listener. It must implementCounterListener.- Parameters:
listener- The listener to add.- Returns:
- A
Handlethat allows to remove the listener viaHandle.remove()()}.
-
getConfiguration
public CounterConfiguration getConfiguration()
- Specified by:
getConfigurationin interfaceWeakCounter- Returns:
- the
CounterConfigurationused by this counter.
-
generate
public CounterEvent generate(CounterKey key, CounterValue value)
Description copied from interface:CounterEventGeneratorIt generates theCounterEvent.The
valueis the new value ofCounterEvent.- Specified by:
generatein interfaceCounterEventGenerator- Parameters:
key- The counter's key.value- The counter's most recentCounterValue.- Returns:
- The
CounterEventwith the updated value.
-
remove
public CompletableFuture<Void> remove()
Description copied from interface:WeakCounterIt removes this counter from the cluster.Note that it doesn't remove the counter from the
CounterManager. If you want to remove the counter from theCounterManageruseCounterManager.remove(String).- Specified by:
removein interfaceWeakCounter- Returns:
- The
CompletableFuturethat is completed when the counter is removed from the cluster.
-
sync
public SyncWeakCounter sync()
Description copied from interface:WeakCounterIt returns a synchronous weak counter for this instance.- Specified by:
syncin interfaceWeakCounter- Returns:
- a
SyncWeakCounter.
-
topologyChanged
public void topologyChanged()
Description copied from interface:TopologyChangeListenerIt notifies the cache topology change.- Specified by:
topologyChangedin interfaceTopologyChangeListener
-
getPreferredKeys
public WeakCounterKey[] getPreferredKeys()
Debug only!
-
getKeys
public WeakCounterKey[] getKeys()
Debug only!
-
-