Chapter 15. Clustered Locks

Clustered locks are data structures that are distributed and shared across nodes in a Red Hat JBoss Data Grid cluster. Clustered locks allow you to run code that is synchronized between the nodes in a cluster.

15.1. The Lock API

The lock API consists of the following:

  • EmbeddedClusteredLockManagerFactory initializes a clustered lock manager from an embedded cache manager.
  • ClusteredLockManager provides methods to define, configure, retrieve, and remove clustered locks.
  • ClusteredLock provides methods to implement clustered locks.
Note

Clustered locks are available in Red Hat JBoss Data Grid embedded mode only.

15.2. Supported Configuration

As of this release, Red Hat JBoss Data Grid supports NODE ownership and non-reentrant clustered locks.

NODE ownership allows all nodes in the Red Hat JBoss Data Grid cluster to use a lock.

Reentrant locks allow the node that owns the lock to acquire it again while the node has ownership of the lock. Non-reentrant locks allow any node to acquire the lock. As a result, if two consecutive lock calls are sent for the same owner, the first call acquires the lock if it is available and the second call is blocked.

15.3. Adding Maven Dependencies

To start using clustered locks, add the following dependency to pom.xml:

pom.xml

<dependency>
   <groupId>org.infinispan</groupId>
   <artifactId>infinispan-clustered-lock</artifactId>
   <version>...</version> <!-- 7.2.0 or later -->
</dependency>

15.4. Using Clustered Locks

The following code sample illustrates how to use clustered locks:

// Set up a clustered cache manager.
GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();

// Configure the cache mode as distributed and synchronous.
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.clustering().cacheMode(CacheMode.DIST_SYNC);

// Initialize a new default cache manager.
DefaultCacheManager cm = new DefaultCacheManager(global.build(), builder.build());

// Initialize a clustered lock manager from the cache manager.
ClusteredLockManager clm1 = EmbeddedClusteredLockManagerFactory.from(cm);

// Define a clustered lock named 'lock' with the default configuration.
clm1.defineLock("lock");

// Get a lock from each node in the cluster.
ClusteredLock lock = clm1.get("lock");

AtomicInteger counter = new AtomicInteger(0);

// Acquire the lock as follows.
// Each 'lock.tryLock(1, TimeUnit.SECONDS)'' method attempts to acquire the lock.
// If the lock is not available, the method waits for the timeout period to elapse. When the lock is acquired, other calls to acquire the lock are blocked until the lock is released.
CompletableFuture<Boolean> call1 = lock.tryLock(1, TimeUnit.SECONDS).whenComplete((r, ex) -> {
    if (r) {
        System.out.println("lock is acquired by the call 1");
        lock.unlock().whenComplete((nil, ex2) -> {
            System.out.println("lock is released by the call 1");
            counter.incrementAndGet();
        });
    }
});

CompletableFuture<Boolean> call2 = lock.tryLock(1, TimeUnit.SECONDS).whenComplete((r, ex) -> {
    if (r) {
        System.out.println("lock is acquired by the call 2");
        lock.unlock().whenComplete((nil, ex2) -> {
            System.out.println("lock is released by the call 2");
            counter.incrementAndGet();
        });
    }
});

CompletableFuture<Boolean> call3 = lock.tryLock(1, TimeUnit.SECONDS).whenComplete((r, ex) -> {
    if (r) {
        System.out.println("lock is acquired by the call 3");
        lock.unlock().whenComplete((nil, ex2) -> {
            System.out.println("lock is released by the call 3");
            counter.incrementAndGet();
        });
    }
});

CompletableFuture.allOf(call1, call2, call3).whenComplete((r, ex) -> {
    // Print the value of the counter.
    System.out.println("Value of the counter is " + counter.get());

    // Stop the cache manager.
    cm.stop();
});