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();
}
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();
}
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
}
}
}
Aggregations