preface
Distributed semaphores. Redisson’s semaphores maintain counting in Redis. Now let’s see how a Curator of ZooKeeper implements semaphores.
Use the Demo
public class CuratorDemo {
public static void main(String[] args) throws Exception {
String connectString = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000.3);
CuratorFramework client = CuratorFrameworkFactory
.builder()
.connectString(connectString)
.retryPolicy(retryPolicy)
.build();
client.start();
InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/semaphores/semaphore_01".3);
for (int i = 0; i < 10; i++) {
new Thread(() -> {
try {
System.out.println(Thread.currentThread() + "Thread start -" + LocalTime.now());
Lease lease = semaphore.acquire();
System.out.println(Thread.currentThread() + "Thread execute -" + LocalTime.now());
Thread.sleep(3000);
System.out.println(Thread.currentThread() + "Thread over -" + LocalTime.now());
semaphore.returnLease(lease);
} catch (Exception e) {
}
}).start();
}
Thread.sleep(1000000); }}Copy the code
The console output data is as follows:
The source code
To obtain credentials
InterProcessSemaphoreV2#internalAcquire1Lease
Here only introduces the general logic, interested partners can read the source code.
Lock is the InterProcessMutex, InterProcessSemaphoreV2 semaphore, also with the help of the most basic locking.
You can also see from the figure that with InterProcessSemaphoreV2, the /semaphores/ semaphoRE_01 path is created first and locks nodes are created under the path. The /semaphores/ semaphoRE_01 /locks path has 10 temporary sequential nodes.
Leases nodes are immediately created under the /semaphores/semaphore_01 path, so the creation of temporary order nodes for locks is followed by the creation of temporary order nodes under /semaphores/semaphore_01/leases.
Listen on the /semaphores/semaphore_01/leases node and get the number of child nodes under /semaphores/semaphore_01/leases.
- If the number of child nodes is less than or equal to the semaphore count, the loop ends directly.
- If greater than, wait to wake up.
Release the documents
To release credentials is to call Lease’s close method and drop nodes so that listeners on /semaphores/semaphore_01/leases fire and other threads fetch credentials.
The mutex
The mutex InterProcessSemaphoreMutex, pragmatic tired, other and reentrant lock and no difference. Is based on InterProcessSemaphoreV2 implementation.
The leases value maxLeases is set to 1.
conclusion
The semaphore InterProcessSemaphoreV2 controls the semaphore by judging the number of children under the node, and internal locking is based on the reentrapable lock InterProcessMutex.
The mutex InterProcessSemaphoreMutex is the semaphore technology is set to 1 to achieve the function mutually exclusive.
Related to recommend
- ZooKeeper distributed lock concurrent lock
- ZooKeeper distributed lock Curator 02: reentrant lock repeat lock and lock release
- ZooKeeper distributed lock Curator 01: reentrant lock