이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 14. Clustered Counters
Clustered counters are distributed and shared across nodes in a Red Hat JBoss Data Grid cluster. Clustered counters allow you to record the count of objects.
Clustered counters are identified by their names and are initialized with a value, which defaults to 0. Clustered counters can also be persisted so that the values are kept after cluster restarts.
There are two types of clustered counter:
-
Strong
stores the counter value in a single key for consistency. During updates to the counter, the value is known. Updates to the counter value are performed under the key lock. However, reads of the current value of the counter do not acquire any lock. Strong counters allow the counter value to be bounded and provide atomic operations such ascompareAndSet
orcompareAndSwap
. -
Weak
stores the counter value in multiple keys. Each key stores a partial state of the counter value and can be updated concurrently. During updates to the counter, the value is not known. Retrieving the counter value does not always return the current, up to date value.
Both strong and weak clustered counters support updating the counter value, return the current value of a counter, and provide events when a counter value is updated.
14.1. The Counter API 링크 복사링크가 클립보드에 복사되었습니다!
The counter
API consists of the following:
-
EmbeddedCounterManagerFactory
initializes a counter manager from an embedded cache manager. -
RemoteCounterManagerFactory
initializes a counter manager from a remote cache manager. -
CounterManager
provides methods to create, define, and return counters. -
StrongCounter
implements strong counters. This interface provides atomic updates for a counter. All operations are performed asynchronously and use theCompletableFuture
class for completion logic. -
SyncStrongCounter
implements synchronous strong counters. -
WeakCounter
implements weak counters. All operations are performed asynchronously and use theCompletableFuture
class for completion logic. -
SyncWeakCounter
implements synchronous weak counters. -
CounterListener
listens for changes to strong counters. -
CounterEvent
returns events when changes to strong counters occur. -
Handle
extends theCounterListener
interface.
14.2. Adding Maven Dependencies 링크 복사링크가 클립보드에 복사되었습니다!
To start using clustered counters, add the following dependency to pom.xml
:
pom.xml
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-clustered-counter</artifactId> <version>...</version> <!-- 7.2.0 or later --> </dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-clustered-counter</artifactId>
<version>...</version> <!-- 7.2.0 or later -->
</dependency>
14.3. Retrieving the CounterManager Interface 링크 복사링크가 클립보드에 복사되었습니다!
To use clustered counters in Red Hat JBoss Data Grid embedded mode, do the following:
// Create or obtain an EmbeddedCacheManager. EmbeddedCacheManager manager = ...; // Retrieve the CounterManager interface. CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(manager);
// Create or obtain an EmbeddedCacheManager.
EmbeddedCacheManager manager = ...;
// Retrieve the CounterManager interface.
CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(manager);
To use clustered counters with a Hot Rod client that interacts with a Red Hat JBoss Data Grid remote server, do the following:
// Create or obtain a RemoteCacheManager. RemoteCacheManager manager = ...; // Retrieve the CounterManager interface. CounterManager counterManager = RemoteCounterManagerFactory.asCounterManager(manager);
// Create or obtain a RemoteCacheManager.
RemoteCacheManager manager = ...;
// Retrieve the CounterManager interface.
CounterManager counterManager = RemoteCounterManagerFactory.asCounterManager(manager);
14.4. Using Clustered Counters 링크 복사링크가 클립보드에 복사되었습니다!
You can define and configure clustered counters in the cache-container XML configuration or programmatically.
14.4.1. XML Configuration for Clustered Counters 링크 복사링크가 클립보드에 복사되었습니다!
The following XML snippet provides an example of a clustered counters configuration:
14.4.1.1. XML Definition 링크 복사링크가 클립보드에 복사되었습니다!
The counters
element configures counters for a cluster and has the following attributes:
-
num-owners
sets the number of copies of each counter to store across the cluster. A smaller number results in faster update operations but supports a lower number of server crashes. The value must be a positive number. The default value is2
. reliability
sets the counter update behavior in a network partition and takes the following values:-
AVAILABLE
all partitions can read and update the value of the counter. This is the default value. -
CONSISTENT
the primary partition can read and update the value of the counter. The remaining partitions can only read the value of the counter.
-
The strong-counter
element creates and defines a strong clustered counter. The weak-counter
element creates and defines a weak clustered counter. The following attributes are common to both elements:
-
initial-value
sets the initial value of the counter. The default value is0
. storage
configures how counter values are stored. This attribute determines if the counter values are saved after the cluster shuts down and restarts. This attribute takes the following values:-
VOLATILE
stores the value of the counter in memory. The value of the counter is discarded when the cluster shuts down. This is the default value. -
PERSISTENT
stores the value of the counter in a private, local persistence store. The value of the counter is saved when the cluster shuts down and restarts.
-
Attributes specific to the strong-counter
element are as follows:
-
lower-bound
sets the lower bound of a strong counter. The default value isLong.MIN_VALUE
. -
upper-bound
sets the upper bound of a strong counter. The default value isLong.MAX_VALUE
.
The value of the initial-value
attribute must be between the lower-bound
value and the upper-bound
value. If you do not specify a lower and upper bound for a strong counter, the counter is not bounded.
Attributes specific to the weak-counter
element are as follows:
-
concurrency-level
sets the maximum number of concurrent updates to the value of a counter. The value must be a positive number. The default value is16
.
14.4.2. Run-time Configuration of Clustered Counters 링크 복사링크가 클립보드에 복사되었습니다!
You can configure clustered counters on-demand at run-time after the EmbeddedCacheManager
is initialized, as in the following example:
The defineCounter()
method returns true
if the counter is defined successfully or false
if not. If the counter configuration is not valid, a CounterConfigurationException
exception is thrown.
Use the isDefined()
method to determine if a counter is already defined, as in the following example:
CounterManager manager = ... if (!manager.isDefined("someCounter")) { manager.define("someCounter", ...); }
CounterManager manager = ...
if (!manager.isDefined("someCounter")) {
manager.define("someCounter", ...);
}
14.4.3. Programmatic Configuration of Clustered Counters 링크 복사링크가 클립보드에 복사되었습니다!
The following code sample illustrates how to configure clustered counters programmatically with the GlobalConfigurationBuilder
:
14.4.3.1. Using Clustered Counters 링크 복사링크가 클립보드에 복사되었습니다!
The following code example illustrates how you can use clustered counters that you create and define programmatically: