zookeeper分布式锁基于curator源码(二)

  1. 在 apache封装的zk客户端操作组件curator中,有不同的锁
    在这里插入图片描述
    InterProcessMutex:分布式可重入排它锁
    InterProcessSemaphoreMutex:分布式排它锁
    InterProcessReadWriteLock:分布式读写锁

  2. 以InterProcessMutex为例

    public class LockDemo {
    
        public static final String host = "ip:2181,ip:2181,ip:2181";
        public static final String lock = "/locks";
    
        public static final CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(host)
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
    
        public static void main(String[] args) {
            curatorFramework.start();
            final InterProcessMutex interProcessMutex = new InterProcessMutex(curatorFramework, lock);
    		
    		//模拟多个进程抢锁
            for (int i = 0; i < 10; i++) {
                new Thread(() -> {
                    try {
                        System.out.println("线程开始抢锁" + Thread.currentThread().getName());
                        interProcessMutex.acquire();
                        System.out.println("线程" + Thread.currentThread().getName() + "抢到");
    					//睡三秒
                        Thread.sleep(3000);
    
                        interProcessMutex.release();
                        System.out.println("线程" + Thread.currentThread().getName() + "释放锁");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }, "Thread" + i).start();
    
            }
        }
    }
    
  3. zk的分布式锁是使用临时有序节点实现的

    1.从interProcessMutex.acquire();方法开始
    @Override
    public void acquire() throws Exception
    {
        if ( !internalLock(-1, null) )
        {
            throw new IOException("Lost connection while trying to acquire lock: " + basePath);
        }
    }
    2. internalLock()
    private boolean internalLock(long time, TimeUnit unit) throws Exception {
        /*
           Note on concurrency: a given lockData instance
           can be only acted on by a single thread so locking isn't necessary
        */
    	//记录当前线程,是可重入的
        Thread currentThread = Thread.currentThread();
    
        LockData lockData = threadData.get(currentThread);
        if ( lockData != null )
        {
            // re-entering
            lockData.lockCount.incrementAndGet();
            return true;
        }
    	//尝试获取锁
        String lockPath = internals.attemptLock(time, unit, getLockNodeBytes());
        if ( lockPath != null )
        {
            LockData newLockData = new LockData(currentThread, lockPath);
            threadData.put(currentThread, newLockData);
            return true;
        }
    
        return false;
    }
    
    
    3.LockInternals的attemptLock方法
    String attemptLock(long time, TimeUnit unit, byte[] lockNodeBytes) throws Exception {
        final long      startMillis = System.currentTimeMillis();
        final Long      millisToWait = (unit != null) ? unit.toMillis(time) : null;
        final byte[]    localLockNodeBytes = (revocable.get() != null) ? new byte[0] : lockNodeBytes;
        int             retryCount = 0;
    
        String          ourPath = null;
        boolean         hasTheLock = false;
        boolean         isDone = false;
        while ( !isDone )
        {
            isDone = true;
    
            try
            {
                ourPath = driver.createsTheLock(client, path, localLockNodeBytes);
                hasTheLock = internalLockLoop(startMillis, millisToWait, ourPath);
            }
            catch ( KeeperException.NoNodeException e )
            {
                // gets thrown by StandardLockInternalsDriver when it can't find the lock node
                // this can happen when the session expires, etc. So, if the retry allows, just try it all again
                if ( client.getZookeeperClient().getRetryPolicy().allowRetry(retryCount++, System.currentTimeMillis() - startMillis, RetryLoop.getDefaultRetrySleeper()) )
                {
                    isDone = false;
                }
                else
                {
                    throw e;
                }
            }
        }
    
        if ( hasTheLock )
        {
            return ourPath;
        }
    
        return null;
    }
    
    4. driver在InterProcessMutex构造器中初始化为StandardLockInternalsDriver
    @Override
    public String createsTheLock(CuratorFramework client, String path, byte[] lockNodeBytes) throws Exception
    {
        String ourPath;
        if ( lockNodeBytes != null )
        {
            ourPath = client.create().creatingParentContainersIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path, lockNodeBytes);
        }
        else
        {
        	//创建container类型的临时有序znode,子节点如果没了,/locks就会被删除
        	//ourPath=/locks/_c_892e4db6-f463-4ec3-9d92-51a9cf236f9d-lock-0000000000,序号是递增的
            ourPath = client.create().creatingParentContainersIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path);
        }
        return ourPath;
    }
    
    5. LockInternals的internalLockLoop方法
    private boolean internalLockLoop(long startMillis, Long millisToWait, String ourPath) throws Exception
    {
        boolean     haveTheLock = false;
        boolean     doDelete = false;
        try
        {
            if ( revocable.get() != null )
            {
                client.getData().usingWatcher(revocableWatcher).forPath(ourPath);
            }
    
            while ( (client.getState() == CuratorFrameworkState.STARTED) && !haveTheLock )
            {
            	//将上一步获取的子节点排序,小到大
                List<String>        children = getSortedChildren();
                //sequenceNodeName = _c_892e4db6-f463-4ec3-9d92-51a9cf236f9d-lock-0000000000
                String              sequenceNodeName = ourPath.substring(basePath.length() + 1); // +1 to include the slash
    			//step 6.
                PredicateResults    predicateResults = driver.getsTheLock(client, children, sequenceNodeName, maxLeases);
                if ( predicateResults.getsTheLock() )
                {
                    haveTheLock = true;
                }
                else
                {
                    String  previousSequencePath = basePath + "/" + predicateResults.getPathToWatch();
    
                    synchronized(this)
                    {
                        try
                        {
                            // use getData() instead of exists() to avoid leaving unneeded watchers which is a type of resource leak
                            //监听前一个节点变化
                            client.getData().usingWatcher(watcher).forPath(previousSequencePath);
                            if ( millisToWait != null )
                            {
                                millisToWait -= (System.currentTimeMillis() - startMillis);
                                startMillis = System.currentTimeMillis();
                                if ( millisToWait <= 0 )
                                {
                                    doDelete = true;    // timed out - delete our node
                                    break;
                                }
    
                                wait(millisToWait);
                            }
                            else
                            {
                            	//阻塞
                                wait();
                            }
                        }
                        catch ( KeeperException.NoNodeException e )
                        {
                            // it has been deleted (i.e. lock released). Try to acquire again
                        }
                    }
                }
            }
        }
        catch ( Exception e )
        {
            ThreadUtils.checkInterrupted(e);
            doDelete = true;
            throw e;
        }
        finally
        {
            if ( doDelete )
            {
                deleteOurPath(ourPath);
            }
        }
        return haveTheLock;
    }
    
    6.
    @Override
    public PredicateResults getsTheLock(CuratorFramework client, List<String> children, String sequenceNodeName, int maxLeases) throws Exception
    {
    	//子节点索引位置
        int             ourIndex = children.indexOf(sequenceNodeName);
        //校验节点是否有效,如果小于0,则会话丢失或者连接失败,zk服务端会将临时节点删除,向外抛出NoNodeException
        validateOurIndex(sequenceNodeName, ourIndex);
        
    	//maxleases也是构造器初始化为1, 索引是0的话,前面没有节点,不需要watcher监听, 临时顺序节点是每个节点监听前一个节点数据变化,可以防止惊群效应,避免释放锁瞬间大量客户端进行抢锁。
        boolean         getsTheLock = ourIndex < maxLeases;
        String          pathToWatch = getsTheLock ? null : children.get(ourIndex - maxLeases);
    
        return new PredicateResults(pathToWatch, getsTheLock);
    }
    
    7. 监听节点时的watcher,有变化事件会回调,最终调用notifyAll方法唤醒所有在wait的线程
     private final Watcher watcher = new Watcher()
    {
        @Override
        public void process(WatchedEvent event)
        {
            client.postSafeNotify(LockInternals.this);
        }
    };
    
    8.释放锁interProcessMutex.release();
     @Override
    public void release() throws Exception
    {
        /*
            Note on concurrency: a given lockData instance
            can be only acted on by a single thread so locking isn't necessary
         */
    
        Thread currentThread = Thread.currentThread();
        LockData lockData = threadData.get(currentThread);
        if ( lockData == null )
        {
            throw new IllegalMonitorStateException("You do not own the lock: " + basePath);
        }
    
        int newLockCount = lockData.lockCount.decrementAndGet();
        if ( newLockCount > 0 )
        {
            return;
        }
        if ( newLockCount < 0 )
        {
            throw new IllegalMonitorStateException("Lock count has gone negative for lock: " + basePath);
        }
        try
        {
            internals.releaseLock(lockData.lockPath);
        }
        finally
        {
            threadData.remove(currentThread);
        }
    }
    
    9.LockInternals的releaseLock方法移除watcher删除节点
    final void releaseLock(String lockPath) throws Exception
    {
        client.removeWatchers();
        revocable.set(null);
        deleteOurPath(lockPath);
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值