@Override protected Lock initialValue() { return new ReentrantDistributedLock(zkClient, IDS_BASEPATH + "/lock"); } };
/** * Acquires a distributed lock through ZooKeeper. It's the same as calling * {@link #acquire(boolean, boolean, long, java.util.concurrent.TimeUnit)} with {@link Long#MAX_VALUE} as timeout. */ private boolean acquire(boolean interruptible, boolean waitForLock) throws InterruptedException, ExecutionException { try { return acquire(interruptible, waitForLock, Long.MAX_VALUE, TimeUnit.SECONDS); } catch (TimeoutException e) { // Should never happen throw Throwables.propagate(e); } }
@Test (timeout = 20000) public void testReentrant() { // Test the lock is reentrant from the same thread ZKClientService zkClient = createZKClient(); try { ReentrantDistributedLock lock = new ReentrantDistributedLock(zkClient, "reentrant"); lock.lock(); try { try { lock.lock(); } finally { lock.unlock(); } } finally { lock.unlock(); } } finally { zkClient.stopAndWait(); } }
ZKClientService zkClient2 = createZKClient(); try { final ReentrantDistributedLock lock1 = new ReentrantDistributedLock(zkClient1, "/multiTrylock"); final ReentrantDistributedLock lock2 = new ReentrantDistributedLock(zkClient2, "/multiTrylock"); lock1.lock(); try { t.start(); Assert.assertTrue(lockFailed.await(5000, TimeUnit.SECONDS)); } finally { lock1.unlock(); Assert.assertTrue(lock1.tryLock()); lock1.unlock(); } finally { zkClient1.stopAndWait();
@Override public void run() { try { lock2.lockInterruptibly(); try { lockAcquired.countDown(); } finally { lock2.unlock(); } } catch (InterruptedException e) { lockInterrupted.countDown(); } } };
@Override public void run() { try { if (!lock.tryLock()) { lockFailed.countDown(); } if (!lock.tryLock(1, TimeUnit.SECONDS)) { lockFailed.countDown(); } if (lock.tryLock(5, TimeUnit.SECONDS)) { // Test the reentrant park as well if (lock.tryLock(1, TimeUnit.SECONDS)) { if (lock.tryLock()) { lockAcquired.countDown(); lock.unlock(); } lock.unlock(); } lock.unlock(); } } catch (InterruptedException e) { // Shouldn't happen LOG.error(e.getMessage(), e); } } };
ZKClientService zkClient2 = createZKClient(); try { final ReentrantDistributedLock lock1 = new ReentrantDistributedLock(zkClient1, "/multiInterrupt"); final ReentrantDistributedLock lock2 = new ReentrantDistributedLock(zkClient2, "/multiInterrupt"); lock1.lock(); lock1.unlock(); lock2.lock(); lock2.unlock();
final ReentrantDistributedLock lock = new ReentrantDistributedLock(zkClient, "/trylock"); lock.lock(); try { t.start(); Assert.assertTrue(lockFailed.await(5, TimeUnit.SECONDS)); } finally { lock.unlock(); Assert.assertTrue(lock.tryLock()); lock.unlock(); } finally { zkClient.stopAndWait();
@Override public void run() { try { lock.lockInterruptibly(); try { lockAcquired.countDown(); } finally { lock.unlock(); } } catch (InterruptedException e) { lockInterrupted.countDown(); } } };
@Override public void run() { try { if (!lock2.tryLock()) { lockFailed.countDown(); } if (!lock2.tryLock(1, TimeUnit.SECONDS)) { lockFailed.countDown(); } if (lock2.tryLock(5000, TimeUnit.SECONDS)) { // Test the reentrant park as well if (lock2.tryLock(1, TimeUnit.SECONDS)) { if (lock2.tryLock()) { lockAcquired.countDown(); lock2.unlock(); } lock2.unlock(); } lock2.unlock(); } } catch (InterruptedException e) { // Shouldn't happen LOG.error(e.getMessage(), e); } } };
try { final ReentrantDistributedLock lock = new ReentrantDistributedLock(zkClient, "multiThreads"); final CountDownLatch acquired = new CountDownLatch(1); Thread t = new Thread() { lock.lock(); try { t.start(); lock.unlock();
@Override protected Lock initialValue() { return new ReentrantDistributedLock(zkClient, basePath); } };
@Override public void lock() { lock.lock(); try { acquire(false, true); } catch (Exception e) { lock.unlock(); throw Throwables.propagate(e); } }
final ReentrantDistributedLock lock = new ReentrantDistributedLock(zkClient, "/interrupt"); lock.lock(); try { final CountDownLatch lockAcquired = new CountDownLatch(1); Assert.assertTrue(lockInterrupted.await(2, TimeUnit.SECONDS)); } finally { lock.unlock();
public Lock getTenantProvisionerLock() { return new ReentrantDistributedLock(zkClient, Constants.Lock.TENANT_NAMESPACE); } }
@Override public boolean tryLock() { if (!lock.tryLock()) { return false; } try { if (acquire(false, false)) { return true; } lock.unlock(); return false; } catch (Exception e) { lock.unlock(); throw Throwables.propagate(e); } }
try { ReentrantDistributedLock lock1 = new ReentrantDistributedLock(zkClient1, "multiClients"); final ReentrantDistributedLock lock2 = new ReentrantDistributedLock(zkClient2, "multiClients"); lock1.lock(); try { lock1.lock(); lock1.unlock(); lock1.unlock();
public SynchronizedZKMap(ZKClient zkClient, Serializer<T> serializer) { this.zkClient = zkClient; this.serializer = serializer; this.currentView = Maps.newHashMap(); this.currentViewVersion = -1; this.globalLock = new ReentrantDistributedLock(zkClient, LOCK_PATH); }