分布式锁之Zookeeper

zookeeper

zookeeper介绍

ZooKeeper是一种用于分布式应用程序的分布式开源协调服务。它公开了一组简单的原语,分布式应用程序可以构建使用这些原语,以实现更高级别的服务,如实现同步,配置维护以及组和命名。 它被设计为易于编程,并使用在熟悉的文件系统目录树结构之后设计的数据模型。

Curator介绍

Curator是ZooKeeper的一个Java客户端框架, 它将zk本身提供的原子功能进行功能性封装, 提供诸如客户端断线自动重连, 分布式锁, 选举, 服务发现等开箱即用的功能;

锁的实现代码

  1. Curator提供了一个跨JVM进程的可重入互斥锁实现-InterProcessMutex;
  • 申请获取锁
    a. 获取锁, 支持重入, 并且有超时获取时间
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;
    }

b. 本地内存中标识当前线程没有获得锁, 那就要去zk中去获取锁了;

    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
            {
  // 在zk指定namespace下的锁节点中创建当前线程的锁节点
                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;
    }

c. 因为上一步创建的锁节点是有序的, 所以这里设置当前线程获取锁znode监听它的前节点状态然后进行wait状态, 等待被唤醒; 如果获取锁带了超时时间就会进行带超时时间的等待状态;

    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        children = getSortedChildren();
                String              sequenceNodeName = ourPath.substring(basePath.length() + 1); // +1 to include the slash
                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;
    }

d. 判断当前线程是否能够获得锁, 即判断它的锁节点znode是否在顺序第一位;

    @Override
    public PredicateResults getsTheLock(CuratorFramework client, List children, String sequenceNodeName, int maxLeases) throws Exception
    {
        int             ourIndex = children.indexOf(sequenceNodeName);
        validateOurIndex(sequenceNodeName, ourIndex);
        // maxLeases默认为1, 如果当前线程znode的索引为0, 即表示能够获得锁
        boolean         getsTheLock = ourIndex < maxLeases;
        String          pathToWatch = getsTheLock ? null : children.get(ourIndex - maxLeases);

        return new PredicateResults(pathToWatch, getsTheLock);
    }
  • 释放锁
    a. 先处理锁重入的释放, 直接对锁重入的计数减一; 当锁重入数为0时, 那就要进行zk进行节点删除释放锁了;
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);
        }
    }

b. 删除锁关联的znode

    private void deleteOurPath(String ourPath) throws Exception
    {
        try
        {
            client.delete().guaranteed().forPath(ourPath);
        }
        catch ( KeeperException.NoNodeException e )
        {
            // ignore - already deleted (possibly expired session, etc.)
        }
    }

Zookeeper实现分布式锁的原理

  • 借助zk集群模式, 实现高可用;
  • 通过临时的Znode模式, 保证了获得锁的线程意外挂掉后, 锁可以被zk主动释放掉, 不会发生死锁;
  • 使用本地进程内记录锁的信息, 实现锁重入;
  • 节点监听机制, 实现锁释放被动通知;

你可能感兴趣的:(分布式锁,缓存,事务)