Zookeper簡單使用

獲取鎖的過程步驟:

1.每個線程進入獲取鎖方法之後,直接調用zookeeper創建對應的節點數據;你下爲/rrrrww節點下對應的線程所創建的節點

[zk: localhost:2181(CONNECTED) 4] ls /rrrrw
[_c_73f6f5a1-3172-435a-8cb7-cb685edd5850-lock-0000014332, _c_6f6e059a-44c2-4452-a6fe-a8fd46e86b2f-lock-0000014339, _c_1ba53a24-7700-4f8c-a91f-8db5c036c50c-lock-0000014338, _c_2123b109-e7d2-4d04-9a6c-2df0b627be15-lock-0000014335, _c_33672d76-265e-4998-9bb3-16218d9eca21-lock-0000014328, _c_f7ffbf6b-e0dd-4ef9-925f-233912cdff74-lock-0000014337, _c_d60409a6-190e-4aa9-a40b-b354bfacc29a-lock-0000014336, _c_89994cb9-9c19-4fa3-b07f-2af48d823d08-lock-0000014334]

2.獲取節點下所有已經創建的子節點,來處理第一個進入的節點,第一個進入的節點,則會獲取到鎖;

3.如果沒有獲取到鎖,則watcher當前的節點變化,讓當前線程等待,當節點變化時,則notifyall所有的線程,則繼續獲取鎖;

4.增加序列之後,則release鎖;

以下代碼根據zookeeper實現了分佈式鎖獲取全局序列:

package com.freeplatform.common.core.seq;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.CloseableUtils;
import org.apache.zookeeper.data.Stat;

/**
 * 
 * <p>Description: </p>
 * @date 2016年5月12日
 * @author 李世佳 
 */
public class DistributedLockSeq {

    public static final String LOCK_ZNODE = "/rrrrw";

    public static CuratorFramework client;

    public static CuratorFrameworkFactory.Builder builder;

    static {
        client = CuratorFrameworkFactory.newClient("172.16.35.9:2181", new ExponentialBackoffRetry(1000, 3));

        builder = CuratorFrameworkFactory.builder().connectString("172.16.35.9:2181")
                .retryPolicy(new ExponentialBackoffRetry(1000, 3));
        // etc. etc.
    }

    public static void main(String[] args) {

        final ExecutorService service = Executors.newFixedThreadPool(20);

        for (int i = 0; i < 1; i++) {
            service.execute(new SeqTask("[Concurrent-" + i + "]"));
        }

        if (!service.isShutdown()) {
            try {
                service.shutdown();
                if (!service.awaitTermination(10, TimeUnit.SECONDS)) {
                    service.shutdownNow();
                }
            } catch (InterruptedException e) {
                service.shutdownNow();
                System.out.println(e.getMessage());
            }
        }
    }

    // 藉助curatorFramework利用Zookeeper實現分佈式seq生成
    public static class SeqTask implements Runnable {

        private final String seqTaskName;

        public SeqTask(String seqTaskName) {
            this.seqTaskName = seqTaskName;
        }

        @Override
        public void run() {
            CuratorFramework client = builder.build();
            client.start();
            // 鎖對象 client 鎖節點
            InterProcessMutex lock = new InterProcessMutex(client, LOCK_ZNODE);
            try {
                boolean retry = true;
                int i = 0;
                do {
                    i++;
                    System.out.println(seqTaskName + " recome:" + i);
                    // 索取鎖,設置時長1s,如果獲取不到,則繼續獲取
                    if (lock.acquire(1000, TimeUnit.MILLISECONDS)) {
                        Stat stat = client.checkExists().forPath(LOCK_ZNODE);
                        if (stat != null) {
                            // 獲取鎖操作則增加序列
                            byte[] oldData = client.getData().storingStatIn(stat).forPath(LOCK_ZNODE);
                            String s = new String(oldData);
                            int d = Integer.parseInt(s);
                            d = d + 1;
                            s = String.valueOf(d);
                            byte[] newData = s.getBytes();
                            client.setData().forPath(LOCK_ZNODE, newData);
                            System.out.println(seqTaskName + " obtain seq :" + new String(newData));
                        }
                        retry = false;
                    }
                } while (retry);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (lock.isAcquiredInThisProcess()) {
                        lock.release();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    CloseableUtils.closeQuietly(client);
                }
            }
        }
    }
}
複製代碼
複製代碼
/**
     * Acquire the mutex - blocks until it's available or the given time expires. Note: the same thread
     * can call acquire re-entrantly. Each call to acquire that returns true must be balanced by a call
     * to {@link #release()}
     *
     * @param time time to wait
     * @param unit time unit
     * @return true if the mutex was acquired, false if not
     * @throws Exception ZK errors, connection interruptions
     */
    @Override
    public boolean acquire(long time, TimeUnit unit) throws Exception
    {
        return internalLock(time, unit);
    }

  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;
    }
複製代碼


複製代碼
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
            {
                if ( localLockNodeBytes != null )
                {
                    ourPath = client.create().creatingParentsIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path, localLockNodeBytes);
                }
                else
                {
                    ourPath = client.create().creatingParentsIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path);
                }
                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;
    }
複製代碼
複製代碼
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();

                //獲取當前線程的節點名稱
                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
                {
                     //沒有索取到鎖,則讓線程等待,並且watcher當前節點,當節點有變化的之後,則notifyAll當前等待的線程,讓它再次進入來爭搶鎖
                    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 )
        {
            doDelete = true;
            throw e;
        }
        finally
        {
            if ( doDelete )
            {
                deleteOurPath(ourPath);
            }
        }
        return haveTheLock;
    }    
複製代碼
複製代碼
@Override
    public PredicateResults getsTheLock(CuratorFramework client, List<String> children, String sequenceNodeName, int maxLeases) throws Exception
    {
        //maxLeases=1,即表示第一個進入的線程所創建的節點獲取鎖,其他則無,線程等待,watcher節點,節點變化,繼續搶佔鎖
        int             ourIndex = children.indexOf(sequenceNodeName);
        validateOurIndex(sequenceNodeName, ourIndex);

        boolean         getsTheLock = ourIndex < maxLeases;
        String          pathToWatch = getsTheLock ? null : children.get(ourIndex - maxLeases);

        return new PredicateResults(pathToWatch, getsTheLock);
    }
複製代碼


複製代碼
private final Watcher watcher = new Watcher()
    {
        @Override
        public void process(WatchedEvent event)
        {
            notifyFromWatcher();
        }
    };

private synchronized void notifyFromWatcher()
    {
        notifyAll();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章