Search in sources :

Example 1 with DistributedLock

use of com.alibaba.otter.shared.arbitrate.impl.zookeeper.lock.DistributedLock in project otter by alibaba.

the class DistributedLockTest method test_lock.

@Test
protected void test_lock() {
    ExecutorService exeucotr = Executors.newCachedThreadPool();
    final int count = 50;
    final CountDownLatch latch = new CountDownLatch(count);
    final DistributedLock[] nodes = new DistributedLock[count];
    for (int i = 0; i < count; i++) {
        final DistributedLock node = new DistributedLock(dir);
        nodes[i] = node;
        exeucotr.submit(new Runnable() {

            public void run() {
                try {
                    node.lock();
                    Thread.sleep(100 + RandomUtils.nextInt(100));
                    System.out.println("id: " + node.getId() + " is leader: " + node.isOwner());
                } catch (InterruptedException e) {
                    want.fail();
                } catch (KeeperException e) {
                    want.fail();
                } finally {
                    latch.countDown();
                    try {
                        node.unlock();
                    } catch (KeeperException e) {
                        want.fail();
                    }
                }
            }
        });
    }
    try {
        latch.await();
    } catch (InterruptedException e) {
        want.fail();
    }
    exeucotr.shutdown();
}
Also used : DistributedLock(com.alibaba.otter.shared.arbitrate.impl.zookeeper.lock.DistributedLock) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException) BaseEventTest(com.alibaba.otter.shared.arbitrate.BaseEventTest) Test(org.testng.annotations.Test)

Example 2 with DistributedLock

use of com.alibaba.otter.shared.arbitrate.impl.zookeeper.lock.DistributedLock in project otter by alibaba.

the class DistributedLockTest method test_try_lock.

@Test
protected void test_try_lock() {
    ExecutorService exeucotr = Executors.newCachedThreadPool();
    final int count = 50;
    final CountDownLatch latch = new CountDownLatch(count);
    final DistributedLock[] nodes = new DistributedLock[count];
    for (int i = 0; i < count; i++) {
        final DistributedLock node = new DistributedLock(dir);
        nodes[i] = node;
        exeucotr.submit(new Runnable() {

            public void run() {
                try {
                    while (node.tryLock() == false) {
                        Thread.sleep(100 + RandomUtils.nextInt(100));
                    }
                    System.out.println("id: " + node.getId() + " is leader: " + node.isOwner());
                } catch (InterruptedException e) {
                    want.fail();
                } catch (KeeperException e) {
                    want.fail();
                } finally {
                    latch.countDown();
                    try {
                        node.unlock();
                    } catch (KeeperException e) {
                        want.fail();
                    }
                }
            }
        });
    }
    try {
        latch.await();
    } catch (InterruptedException e) {
        want.fail();
    }
    exeucotr.shutdown();
}
Also used : DistributedLock(com.alibaba.otter.shared.arbitrate.impl.zookeeper.lock.DistributedLock) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException) BaseEventTest(com.alibaba.otter.shared.arbitrate.BaseEventTest) Test(org.testng.annotations.Test)

Example 3 with DistributedLock

use of com.alibaba.otter.shared.arbitrate.impl.zookeeper.lock.DistributedLock in project otter by alibaba.

the class ErrorTerminProcess method process.

public boolean process(TerminEventData data) {
    DistributedLock lock = new DistributedLock(StagePathUtils.getLoadLock(data.getPipelineId()));
    try {
        // 尝试进行锁定,等待当前的load操作完成
        boolean locked = lock.tryLock();
        if (!locked) {
            return false;
        }
        processChain(data);
        return true;
    } catch (KeeperException e) {
        throw new ArbitrateException("Termin_process", e);
    } finally {
        try {
            // 马上进行释放
            lock.unlock();
        } catch (KeeperException e1) {
        // ignore
        }
    }
}
Also used : DistributedLock(com.alibaba.otter.shared.arbitrate.impl.zookeeper.lock.DistributedLock) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

DistributedLock (com.alibaba.otter.shared.arbitrate.impl.zookeeper.lock.DistributedLock)3 KeeperException (org.apache.zookeeper.KeeperException)3 BaseEventTest (com.alibaba.otter.shared.arbitrate.BaseEventTest)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 Test (org.testng.annotations.Test)2 ArbitrateException (com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)1