Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement distributed exclusive Lock in zookeeper

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to achieve distributed exclusive lock in zookeeper, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

One: distributed mutex

Distributed locks are mainly used to synchronize access to shared resources in distributed systems. In zookeeper, there is no Synchronized or ReentrantLock mechanism to implement locking mechanism as in JAVA, but in zookeeper, it is easier to implement:

We can say that a data node of zk represents a lock. When multiple clients call create () node to create a node at the same time, zookeeper will ensure that only one client will be created successfully, so we can let the successful client hold the lock, while other clients register Watcher listening.

When the client that holds the lock releases the lock, the listening client receives a Watcher notification and then attempts to acquire the lock over and over again.

Second, the general process

Three: code examples

Import java.util.Collections;import java.util.List;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.EventType;import org.apache.zookeeper.Watcher.Event.KeeperState;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.data.Stat / * Mutex based on zookeeper * * / public class DistributedClient {private static final int SESSION_TIMEOUT = 5000; private String hosts = "192.168.8.88 private String hosts 2182192.168.88 private String hosts 2182192.168.88"; private String groupNode = "locks"; private String subNode = "sub"; private ZooKeeper zk; / / the child node private volatile String thisPath created by the current client / / current client waiting child node private volatile String waitPath; private CountDownLatch latch = new CountDownLatch (1) / * Connect zookeeper * * @ param countDownLatch * / public void connectZookeeper (final CountDownLatch countDownLatch) throws Exception {zk = new ZooKeeper (hosts, SESSION_TIMEOUT) New Watcher () {@ Override public void process (WatchedEvent event) {try {if (event.getState () = = KeeperState.SyncConnected) {latch.countDown () } / / A delete event for waitPath has occurred / * assume that a client hangs before acquiring the lock, because the node created by client is of type ephemeral * so this node will also be deleted, causing the client after the client to acquire the lock in advance. * at this time, multiple client will access the shared resource at the same time. How to solve this problem? Upon receiving the deletion notification, * make a confirmation to confirm whether the current thisPath is really the smallest node in the list. * / if (event.getType () = = EventType.NodeDeleted & & event.getPath () .equals (waitPath)) {/ / confirm whether thisPath is really the smallest node in the list List childrenNodes = zk.getChildren ("/" + groupNode False) String thisNode = thisPath .substring (("/" >

The possible running results are as follows:

Current thread: pool-1-thread-16 acquired lock: / locks/sub0000000053 current thread: pool-1-thread-16 has released the lock, giving other clients a chance to acquire, / locks/sub0000000053 current thread: pool-1-thread-20-EventThread acquired lock: / locks/sub0000000054 current thread: pool-1-thread-20-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000054 current thread: pool-1-thread-5-EventThread acquired lock: / locks/sub0000000055 current thread: pool-1-thread-5-EventThread has released the lock, giving other clients a chance to acquire, / locks/sub0000000055 current thread: pool-1-thread-2-EventThread acquired lock: / locks/sub0000000056 current thread: pool-1-thread-2-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000056 current thread: pool-1-thread-6-EventThread acquired lock: / locks/sub0000000057 current thread: pool-1-thread-6-EventThread has released the lock, giving other clients a chance to acquire, / locks/sub0000000057 current thread: pool-1-thread-10-EventThread acquired lock: / locks/sub0000000058 current thread: pool-1-thread-10-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000058 current thread: pool-1-thread-3-EventThread acquired lock: / locks/sub0000000059 current thread: pool-1-thread-3-EventThread has released the lock, giving other clients a chance to acquire, / locks/sub0000000059 current thread: pool-1-thread-11-EventThread acquired lock: / locks/sub0000000060 current thread: pool-1-thread-11-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000060 current thread: pool-1-thread-7-EventThread acquired lock: / locks/sub0000000061 current thread: pool-1-thread-7-EventThread has released the lock, giving other clients a chance to acquire, / locks/sub0000000061 current thread: pool-1-thread-13-EventThread acquired lock: / locks/sub0000000062 current thread: pool-1-thread-13-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000062 current thread: pool-1-thread-15-EventThread acquired lock: / locks/sub0000000063 current thread: pool-1-thread-15-EventThread has released the lock, giving other clients a chance to acquire, / locks/sub0000000063 current thread: pool-1-thread-1-EventThread acquired lock: / locks/sub0000000064 current thread: pool-1-thread-1-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000064 current thread: pool-1-thread-18-EventThread acquired lock: / locks/sub0000000065 current thread: pool-1-thread-18-EventThread has released the lock, giving other clients a chance to acquire, / locks/sub0000000065 current thread: pool-1-thread-4-EventThread acquired lock: / locks/sub0000000066 current thread: pool-1-thread-4-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000066 current thread: pool-1-thread-19-EventThread acquired lock: / locks/sub0000000067 current thread: pool-1-thread-19-EventThread has released the lock, giving other clients a chance to acquire, / locks/sub0000000067 current thread: pool-1-thread-14-EventThread acquired lock: / locks/sub0000000068 current thread: pool-1-thread-14-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000068 current thread: pool-1-thread-9-EventThread acquired lock: / locks/sub0000000069 current thread: pool-1-thread-9-EventThread has released the lock, giving other clients a chance to acquire, / locks/sub0000000069 current thread: pool-1-thread-8-EventThread acquired lock: / locks/sub0000000070 current thread: pool-1-thread-8-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000070 current thread: pool-1-thread-12-EventThread acquired lock: / locks/sub0000000071 current thread: pool-1-thread-12-EventThread has released the lock, giving other clients a chance to acquire, / locks/sub0000000071 current thread: pool-1-thread-17-EventThread acquired lock: / locks/sub0000000072 current thread: pool-1-thread-17-EventThread has released the lock, giving other clients a chance to acquire it / locks/sub0000000072, is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report