Class WeakCounterImpl
- All Implemented Interfaces:
WeakCounter,CounterEventGenerator,TopologyChangeListener,InternalCounterAdmin
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
ConstructorsConstructorDescriptionWeakCounterImpl(String counterName, org.infinispan.AdvancedCache<WeakCounterKey, CounterValue> cache, CounterConfiguration configuration, CounterManagerNotificationManager notificationManager) -
Method Summary
Modifier and TypeMethodDescriptionadd(long delta) Adds the given value to the new value.<T extends CounterListener>
Handle<T>addListener(T listener) Adds aCounterListenerto this counter.destroy()Destroys the counter.generate(CounterKey key, CounterValue value) It generates theCounterEvent.getKeys()Debug only!getName()Debug only!longgetValue()It returns the counter's value.init()Initializes the key set.booleanChecks if the counter is aWeakCounter.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.reset()Resets the counter to its initial value.sync()It returns a synchronous weak counter for this instance.voidIt notifies the cache topology change.toString()value()Returns the counter's value.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.infinispan.counter.impl.manager.InternalCounterAdmin
asStrongCounterMethods inherited from interface org.infinispan.counter.api.WeakCounter
decrement, increment
-
Constructor Details
-
WeakCounterImpl
public WeakCounterImpl(String counterName, org.infinispan.AdvancedCache<WeakCounterKey, CounterValue> cache, CounterConfiguration configuration, CounterManagerNotificationManager notificationManager)
-
-
Method Details
-
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
Initializes the key set.Only one key will have the initial value and the remaining is zero.
-
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
Description copied from interface:WeakCounterAdds the given value to the new value.- Specified by:
addin interfaceWeakCounter- Parameters:
delta- the value to add.
-
asWeakCounter
- Specified by:
asWeakCounterin interfaceInternalCounterAdmin- Returns:
- The
WeakCounterinstance or throws anCounterExceptionif the counter is not aWeakCounter.
-
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
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
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
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
- Specified by:
getConfigurationin interfaceWeakCounter- Returns:
- the
CounterConfigurationused by this counter.
-
generate
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
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
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
Debug only! -
getKeys
Debug only! -
toString
-