Este conteúdo não está disponível no idioma selecionado.
Chapter 11. Using clustered counters
Data Grid provides counters that record the count of objects and are distributed across all nodes in a cluster.
11.1. Clustered Counters Copiar o linkLink copiado para a área de transferência!
Clustered counters are counters which are distributed and shared among all nodes in the Data Grid cluster. Counters can have different consistency levels: strong and weak.
Although a strong/weak consistent counter has separate interfaces, both support updating its value, return the current value and they provide events when its value is updated. Details are provided below in this document to help you choose which one fits best your uses-case.
11.1.1. Installation and Configuration Copiar o linkLink copiado para a área de transferência!
In order to start using the counters, you needs to add the dependency in your Maven pom.xml file:
pom.xml
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-clustered-counter</artifactId> </dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-clustered-counter</artifactId>
</dependency>
The counters can be configured Data Grid configuration file or on-demand via the CounterManager interface detailed later in this document. A counters configured in Data Grid configuration file is created at boot time when the EmbeddedCacheManager is starting. These counters are started eagerly and they are available in all the cluster’s nodes.
configuration.xml
or programmatically, in the GlobalConfigurationBuilder:
On other hand, the counters can be configured on-demand, at any time after the EmbeddedCacheManager is initialized.
CounterConfiguration is immutable and can be reused.
The method defineCounter() will return true if the counter is successful configured or false otherwise. However, if the configuration is invalid, the method will throw a CounterConfigurationException. To find out if a counter is already defined, use the method isDefined().
CounterManager manager = ...
if (!manager.isDefined("someCounter")) {
manager.define("someCounter", ...);
}
CounterManager manager = ...
if (!manager.isDefined("someCounter")) {
manager.define("someCounter", ...);
}
11.1.1.1. List counter names Copiar o linkLink copiado para a área de transferência!
To list all the counters defined, the method CounterManager.getCounterNames() returns a collection of all counter names created cluster-wide.
11.1.2. CounterManager interface Copiar o linkLink copiado para a área de transferência!
The CounterManager interface is the entry point to define, retrieve and remove counters.
Embedded deployments
CounterManager automatically listen to the creation of EmbeddedCacheManager and proceeds with the registration of an instance of it per EmbeddedCacheManager. It starts the caches needed to store the counter state and configures the default counters.
Retrieving the CounterManager is as simple as invoke the EmbeddedCounterManagerFactory.asCounterManager(EmbeddedCacheManager) as shown in the example below:
// create or obtain your EmbeddedCacheManager EmbeddedCacheManager manager = ...; // retrieve the CounterManager CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(manager);
// create or obtain your EmbeddedCacheManager
EmbeddedCacheManager manager = ...;
// retrieve the CounterManager
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(manager);
Server deployments
For Hot Rod clients, the CounterManager is registered in the RemoteCacheManager and can be retrieved as follows:
// create or obtain your RemoteCacheManager RemoteCacheManager manager = ...; // retrieve the CounterManager CounterManager counterManager = RemoteCounterManagerFactory.asCounterManager(manager);
// create or obtain your RemoteCacheManager
RemoteCacheManager manager = ...;
// retrieve the CounterManager
CounterManager counterManager = RemoteCounterManagerFactory.asCounterManager(manager);
11.1.2.1. Remove a counter via CounterManager Copiar o linkLink copiado para a área de transferência!
There is a difference between remove a counter via the Strong/WeakCounter interfaces and the CounterManager. The CounterManager.remove(String) removes the counter value from the cluster and removes all the listeners registered in the counter in the local counter instance. In addition, the counter instance is no longer reusable and it may return an invalid results.
On the other side, the Strong/WeakCounter removal only removes the counter value. The instance can still be reused and the listeners still works.
The counter is re-created if it is accessed after a removal.
11.1.3. The Counter Copiar o linkLink copiado para a área de transferência!
A counter can be strong (StrongCounter) or weakly consistent (WeakCounter) and both is identified by a name. They have a specific interface but they share some logic, namely, both of them are asynchronous ( a CompletableFuture is returned by each operation), provide an update event and can be reset to its initial value.
If you don’t want to use the async API, it is possible to return a synchronous counter via sync() method. The API is the same but without the CompletableFuture return value.
The following methods are common to both interfaces:
-
getName()returns the counter name (identifier). -
getValue()returns the current counter’s value. -
reset()allows to reset the counter’s value to its initial value. -
addListener()register a listener to receive update events. More details about it in the Notification and Events section. -
getConfiguration()returns the configuration used by the counter. -
remove()removes the counter value from the cluster. The instance can still be used and the listeners are kept. -
sync()creates a synchronous counter.
The counter is re-created if it is accessed after a removal.
11.1.3.1. The StrongCounter interface: when the consistency or bounds matters. Copiar o linkLink copiado para a área de transferência!
The strong counter provides uses a single key stored in Data Grid cache to provide the consistency needed. All the updates are performed under the key lock to updates its values. On other hand, the reads don’t acquire any locks and reads the current value. Also, with this scheme, it allows to bound the counter value and provide atomic operations like compare-and-set/swap.
A StrongCounter can be retrieved from the CounterManager by using the getStrongCounter() method. As an example:
CounterManager counterManager = ...
StrongCounter aCounter = counterManager.getStrongCounter("my-counter");
CounterManager counterManager = ...
StrongCounter aCounter = counterManager.getStrongCounter("my-counter");
Since every operation will hit a single key, the StrongCounter has a higher contention rate.
The StrongCounter interface adds the following method:
-
incrementAndGet()increments the counter by one and returns the new value. -
decrementAndGet()decrements the counter by one and returns the new value. -
addAndGet()adds a delta to the counter’s value and returns the new value. -
compareAndSet()andcompareAndSwap()atomically set the counter’s value if the current value is the expected.
A operation is considered completed when the CompletableFuture is completed.
The difference between compare-and-set and compare-and-swap is that the former returns true if the operation succeeds while the later returns the previous value. The compare-and-swap is successful if the return value is the same as the expected.
11.1.3.1.1. Bounded StrongCounter Copiar o linkLink copiado para a área de transferência!
When bounded, all the update method above will throw a CounterOutOfBoundsException when they reached the lower or upper bound. The exception has the following methods to check which side bound has been reached:
public boolean isUpperBoundReached(); public boolean isLowerBoundReached();
public boolean isUpperBoundReached();
public boolean isLowerBoundReached();
11.1.3.1.2. Uses cases Copiar o linkLink copiado para a área de transferência!
The strong counter fits better in the following uses cases:
- When counter’s value is needed after each update (example, cluster-wise ids generator or sequences)
- When a bounded counter is needed (example, rate limiter)
11.1.3.1.3. Usage Examples Copiar o linkLink copiado para a área de transferência!
And below, there is another example using a bounded counter:
Compare-and-set vs Compare-and-swap examples:
With compare-and-swap, it saves one invocation counter invocation (counter.getValue())
To use a strong counter as a rate limiter, configure upper-bound and lifespan parameters as follows:
The lifespan parameter is an experimental capability and may be removed in a future version.
11.1.3.2. The WeakCounter interface: when speed is needed Copiar o linkLink copiado para a área de transferência!
The WeakCounter stores the counter’s value in multiple keys in Data Grid cache. The number of keys created is configured by the concurrency-level attribute. Each key stores a partial state of the counter’s value and it can be updated concurrently. It main advantage over the StrongCounter is the lower contention in the cache. On other hand, the read of its value is more expensive and bounds are not allowed.
The reset operation should be handled with caution. It is not atomic and it produces intermediates values. These value may be seen by a read operation and by any listener registered.
A WeakCounter can be retrieved from the CounterManager by using the getWeakCounter() method. As an example:
CounterManager counterManager = ...
StrongCounter aCounter = counterManager.getWeakCounter("my-counter);
CounterManager counterManager = ...
StrongCounter aCounter = counterManager.getWeakCounter("my-counter);
11.1.3.2.1. Weak Counter Interface Copiar o linkLink copiado para a área de transferência!
The WeakCounter adds the following methods:
They are similar to the `StrongCounter’s methods but they don’t return the new value.
11.1.3.2.2. Uses cases Copiar o linkLink copiado para a área de transferência!
The weak counter fits best in uses cases where the result of the update operation is not needed or the counter’s value is not required too often. Collecting statistics is a good example of such an use case.
11.1.3.2.3. Examples Copiar o linkLink copiado para a área de transferência!
Below, there is an example of the weak counter usage.
11.1.4. Notifications and Events Copiar o linkLink copiado para a área de transferência!
Both strong and weak counter supports a listener to receive its updates events. The listener must implement CounterListener and it can be registered by the following method:
<T extends CounterListener> Handle<T> addListener(T listener);
<T extends CounterListener> Handle<T> addListener(T listener);
The CounterListener has the following interface:
public interface CounterListener {
void onUpdate(CounterEvent entry);
}
public interface CounterListener {
void onUpdate(CounterEvent entry);
}
The Handle object returned has the main goal to remove the CounterListener when it is not longer needed. Also, it allows to have access to the CounterListener instance that is it handling. It has the following interface:
public interface Handle<T extends CounterListener> {
T getCounterListener();
void remove();
}
public interface Handle<T extends CounterListener> {
T getCounterListener();
void remove();
}
Finally, the CounterEvent has the previous and current value and state. It has the following interface:
The state is always State.VALID for unbounded strong counter and weak counter. State.LOWER_BOUND_REACHED and State.UPPER_BOUND_REACHED are only valid for bounded strong counters.
The weak counter reset() operation will trigger multiple notification with intermediate values.