Comments on the source code of zookeeper's two distributed locks

Comments on the source code of zookeeper's two distributed locks

 

The idea of ​​​​implementing the lock yourself is

 

All things that are not well distributed can be registered to a functional middleware, and then this middleware is unified and managed

 

For threads that need lock control sequence, first establish a marked node before execution, and finally determine the sequence of thread execution according to the sequence of nodes (all on the same zk)

 

This is also the distributed lock principle of zk

 

 

The source code of zk lock (sequence lock)

 

After each process is connected to zk, it creates nodes in sequence, and then judges whether the node is the first sequence (created first), if so, execute it first, and other nodes wait at their respective nodes (1 smaller than themselves). Node establishes monitoring

, once the node you listen to is removed (lock release), you will call back your own process method and do more things yourself (at this time, you no longer need to acquire the lock, and the timing lock will be established in turn for the first time))

 

 public void connectZookeeper() throws Exception {

        zk = new ZooKeeper(hosts, SESSION_TIMEOUT, new Watcher() {

            public void process(WatchedEvent event) {

                try {

                    // When the connection is established, open the latch and wake up the thread waiting on the latch

                    if (event.getState() == KeeperState.SyncConnected) {

                        latch.countDown();////Ensure that the connection is complete before going down (the countdown starts at 0)

 

                    }

 

                    // The delete event of waitPath occurred

                    if (event.getType() == EventType.NodeDeleted && event.getPath().equals(waitPath)) {

                        doSomething();

                    }

                } catch (Exception e) {

                    e.printStackTrace ();

                }

            }

        });

 

        // wait for the connection to be established

        latch.await();////Ensure that the connection is complete before going down

 

 

 

 

 

All locks are executed sequentially by means of callback

 // Register the listener on waitPath, when waitPath is deleted, zookeeper will call back the listener's process method

                zk.getData(waitPath, true, new Stat());

 

 

When many processes need to access shared resources, we can implement distributed locks through zk. The main steps are: 

1. Create a node, if named: lock. The node type is persistent node (PERSISTENT) 

2. Whenever a process needs to access a shared resource, it will call the lock() or tryLock() method of the distributed lock to obtain the lock. At this time, a corresponding sequential child node will be created under the lock node created in the first step. The node type is Temporary sequence node (EPHEMERAL_SEQUENTIAL), by forming a specific name name+lock+sequence number. 

3. After the child node is established, sort all the child nodes starting with name under the lock, and judge whether the sequence number of the child node just created is the smallest node. If it is the smallest node, the lock is obtained to access the resource. 

4. If it is not the node, get the last sequential node of the node, and give the node whether there is a registered listening event. Also blocked here. Waiting for the occurrence of the listening event to gain control of the lock. 

5. After calling the shared resource, call the unlock() method to close the zk, which can then trigger the listening event and release the lock. 

The implemented distributed locks are strictly sequential access concurrent locks.

 

 

http://blog.csdn.net/a925907195/article/details/39639357

 

 

 

exclusive lock

 

Go to create a lock with the same name, delete it after use, before it is created successfully, and wait for the unobtained (first judge whether it is the smallest), waiting for a timeout will cause a timeout exception, but it will continue to iterate until it is obtained. Lock

 

 

  

 

 

 

 

 

 

 

private boolean waitForLock(String lower, long waitTime, TimeUnit unit,boolean isBreak) throws InterruptedException, KeeperException {  

        long beginTime = System.currentTimeMillis();  

        Stat stat = zk.exists(root + "/" + lower,true);  

        //Determine whether a node smaller than yourself exists, if not, there is no need to wait for the lock, and register the listener at the same time  

        if(stat != null){  

            logger.debug("Thread " + Thread.currentThread().getId() + " waiting for " + root + "/" + lower);  

            this.latch = new CountDownLatch(1);  

            this.latch.await(waitTime, unit);  

            if(this.latch.getCount() == 1){  

                 this.latch = null;  

                 if(isBreak){  

                     throw new LockException("The default time to apply for a lock resource is: "+defaultWaitTime+"ms, waiting for the lock to time out");  

                 }else{  

                     return false;  

                 }  

            }  

            this.latch = null;  

        }  

          

        //Remove all locks of lockName  

        List<String> lockObjNodes = this.getChildrenListSort();  

        logger.debug(myZnode + "==" + lockObjNodes.get(0));  

        if(myZnode.equals(root+"/"+lockObjNodes.get(0))){  

            //If it is the smallest node, it means that the lock is acquired  

            return true;  

        }else{  

             //If it is not the smallest node, find a node that is 1 smaller than itself  

            String subMyZnode = myZnode.substring(myZnode.lastIndexOf("/") + 1);  

            waitNode = lockObjNodes.get(Collections.binarySearch(lockObjNodes, subMyZnode) - 1);  

            long endTime = System.currentTimeMillis();  

            waitTime = waitTime - (endTime - beginTime) ;  

            if(waitTime <= 0) return false;  

            return waitForLock(lower, waitTime, unit, false);//waiting for lock (iteration)

        }  

    }

 

 

 

 

http://blog.csdn.net/qq_18167901/article/details/50681429

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326290595&siteId=291194637