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 Details

  • Method Details

    • 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 the cache, identified by the counterName.
      Parameters:
      cache - The Cache to 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

      public String getName()
      Specified by:
      getName in interface org.infinispan.counter.api.WeakCounter
    • getValue

      public long getValue()
      Specified by:
      getValue in interface org.infinispan.counter.api.WeakCounter
    • add

      public CompletableFuture<Void> add(long delta)
      Specified by:
      add in interface org.infinispan.counter.api.WeakCounter
    • asWeakCounter

      public org.infinispan.counter.api.WeakCounter asWeakCounter()
      Specified by:
      asWeakCounter in interface InternalCounterAdmin
      Returns:
      The WeakCounter instance or throws an CounterException if the counter is not a WeakCounter.
    • destroy

      public CompletionStage<Void> destroy()
      Description copied from interface: InternalCounterAdmin
      Destroys the counter.

      It drops the counter's value and all listeners registered.

      Specified by:
      destroy in interface InternalCounterAdmin
      Returns:
      A CompletionStage instance which is completed when the operation finishes.
    • reset

      public CompletableFuture<Void> reset()
      Description copied from interface: InternalCounterAdmin
      Resets the counter to its initial value.
      Specified by:
      reset in interface InternalCounterAdmin
      Specified by:
      reset in interface org.infinispan.counter.api.WeakCounter
      Returns:
      A CompletionStage instance which is completed when the operation finishes.
    • value

      public CompletionStage<Long> value()
      Description copied from interface: InternalCounterAdmin
      Returns the counter's value.
      Specified by:
      value in interface InternalCounterAdmin
      Returns:
      A CompletionStage instance which is completed when the operation finishes.
    • isWeakCounter

      public boolean isWeakCounter()
      Description copied from interface: InternalCounterAdmin
      Checks if the counter is a WeakCounter.

      If true, ensures InternalCounterAdmin.asWeakCounter() never throws an CounterException. Otherwise, it ensures InternalCounterAdmin.asStrongCounter() never throws an CounterException.

      Specified by:
      isWeakCounter in interface InternalCounterAdmin
      Returns:
      true if the counter is WeakCounter.
    • addListener

      public <T extends org.infinispan.counter.api.CounterListener> org.infinispan.counter.api.Handle<T> addListener(T listener)
      Specified by:
      addListener in interface org.infinispan.counter.api.WeakCounter
    • getConfiguration

      public org.infinispan.counter.api.CounterConfiguration getConfiguration()
      Specified by:
      getConfiguration in interface org.infinispan.counter.api.WeakCounter
    • generate

      public org.infinispan.counter.api.CounterEvent generate(CounterKey key, CounterValue value)
      Description copied from interface: CounterEventGenerator
      It generates the CounterEvent.

      The value is the new value of CounterEvent.

      Specified by:
      generate in interface CounterEventGenerator
      Parameters:
      key - The counter's key.
      value - The counter's most recent CounterValue.
      Returns:
      The CounterEvent with the updated value.
    • remove

      public CompletableFuture<Void> remove()
      Specified by:
      remove in interface org.infinispan.counter.api.WeakCounter
    • sync

      public org.infinispan.counter.api.SyncWeakCounter sync()
      Specified by:
      sync in interface org.infinispan.counter.api.WeakCounter
    • topologyChanged

      public void topologyChanged()
      Description copied from interface: TopologyChangeListener
      It notifies the cache topology change.
      Specified by:
      topologyChanged in interface TopologyChangeListener
    • getPreferredKeys

      public WeakCounterKey[] getPreferredKeys()
      Debug only!
    • getKeys

      public WeakCounterKey[] getKeys()
      Debug only!
    • toString

      public String toString()
      Overrides:
      toString in class Object