Class WeakCounterImpl
- java.lang.Object
-
- org.infinispan.counter.impl.weak.WeakCounterImpl
-
- All Implemented Interfaces:
org.infinispan.counter.api.WeakCounter,CounterEventGenerator,TopologyChangeListener,InternalCounterAdmin
public class WeakCounterImpl extends Object implements org.infinispan.counter.api.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, org.infinispan.counter.api.CounterConfiguration configuration, CounterManagerNotificationManager notificationManager)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CompletableFuture<Void>add(long delta)<T extends org.infinispan.counter.api.CounterListener>
org.infinispan.counter.api.Handle<T>addListener(T listener)org.infinispan.counter.api.WeakCounterasWeakCounter()CompletionStage<Void>destroy()Destroys the counter.org.infinispan.counter.api.CounterEventgenerate(CounterKey key, CounterValue value)It generates theCounterEvent.org.infinispan.counter.api.CounterConfigurationgetConfiguration()WeakCounterKey[]getKeys()Debug only!StringgetName()WeakCounterKey[]getPreferredKeys()Debug only!longgetValue()CompletionStage<InternalCounterAdmin>init()Initializes the key set.booleanisWeakCounter()Checks if the counter is aWeakCounter.CompletableFuture<Void>remove()static CompletionStage<Void>removeWeakCounter(org.infinispan.Cache<WeakCounterKey,CounterValue> cache, org.infinispan.counter.api.CounterConfiguration configuration, String counterName)It removes a weak counter from thecache, identified by thecounterName.CompletableFuture<Void>reset()Resets the counter to its initial value.org.infinispan.counter.api.SyncWeakCountersync()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
-
-
-
-
Constructor Detail
-
WeakCounterImpl
public WeakCounterImpl(String counterName, org.infinispan.AdvancedCache<WeakCounterKey,CounterValue> cache, org.infinispan.counter.api.CounterConfiguration configuration, CounterManagerNotificationManager notificationManager)
-
-
Method Detail
-
removeWeakCounter
public static CompletionStage<Void> removeWeakCounter(org.infinispan.Cache<WeakCounterKey,CounterValue> cache, org.infinispan.counter.api.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 interfaceorg.infinispan.counter.api.WeakCounter
-
getValue
public long getValue()
- Specified by:
getValuein interfaceorg.infinispan.counter.api.WeakCounter
-
add
public CompletableFuture<Void> add(long delta)
- Specified by:
addin interfaceorg.infinispan.counter.api.WeakCounter
-
asWeakCounter
public org.infinispan.counter.api.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:InternalCounterAdminResets the counter to its initial value.- Specified by:
resetin interfaceInternalCounterAdmin- Specified by:
resetin interfaceorg.infinispan.counter.api.WeakCounter- 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 org.infinispan.counter.api.CounterListener> org.infinispan.counter.api.Handle<T> addListener(T listener)
- Specified by:
addListenerin interfaceorg.infinispan.counter.api.WeakCounter
-
getConfiguration
public org.infinispan.counter.api.CounterConfiguration getConfiguration()
- Specified by:
getConfigurationin interfaceorg.infinispan.counter.api.WeakCounter
-
generate
public org.infinispan.counter.api.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()
- Specified by:
removein interfaceorg.infinispan.counter.api.WeakCounter
-
sync
public org.infinispan.counter.api.SyncWeakCounter sync()
- Specified by:
syncin interfaceorg.infinispan.counter.api.WeakCounter
-
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!
-
-