@Override public Lock createLock(String name) { return new ReentrantDistributedLock(zkClient, "/lock/" + name); }
/** * Acquires a distributed lock through ZooKeeper. It's the same as calling * {@link #acquire(boolean, boolean, long, 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); } }
@Override public Lock createLock(String name) { return new ReentrantDistributedLock(zkClient, "/lock/" + name); }
/** * Acquires a distributed lock through ZooKeeper. It's the same as calling * {@link #acquire(boolean, boolean, long, 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); } }
@Override protected Lock getLock(StreamId streamId) { // It's ok to create new locks every time as it's backed by ZK for distributed lock ZKClient lockZKClient = ZKClients.namespace(zkClient, "/" + Constants.Service.STREAMS + "/locks"); return new ReentrantDistributedLock(lockZKClient, streamId.toString()); }
@Override public void lock() { lock.lock(); try { acquire(false, true); } catch (Exception e) { lock.unlock(); throw Throwables.propagate(e); } }
this.logAppenderInitializer = injector.getInstance(LogAppenderInitializer.class); this.zkClient = injector.getInstance(ZKClientService.class); this.shutdownLock = new ReentrantDistributedLock(zkClient, "/lock/" + Constants.Service.MASTER_SERVICES);
@Override public void lock() { lock.lock(); try { acquire(false, true); } catch (Exception e) { lock.unlock(); throw Throwables.propagate(e); } }
@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); } }
@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); } }
@Override public void lockInterruptibly() throws InterruptedException { lock.lockInterruptibly(); try { acquire(true, true); } catch (Exception e) { lock.unlock(); Throwables.propagateIfInstanceOf(e, InterruptedException.class); throw Throwables.propagate(e); } }
@Override public void lockInterruptibly() throws InterruptedException { lock.lockInterruptibly(); try { acquire(true, true); } catch (Exception e) { lock.unlock(); Throwables.propagateIfInstanceOf(e, InterruptedException.class); throw Throwables.propagate(e); } }
@Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { long startTime = System.nanoTime(); if (!lock.tryLock(time, unit)) { return false; } long timeoutNano = unit.toNanos(time) - (System.nanoTime() - startTime); try { if (acquire(true, true, timeoutNano, TimeUnit.NANOSECONDS)) { return true; } lock.unlock(); return false; } catch (ExecutionException e) { lock.unlock(); throw Throwables.propagate(e.getCause()); } catch (TimeoutException e) { lock.unlock(); return false; } }
@Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { long startTime = System.nanoTime(); if (!lock.tryLock(time, unit)) { return false; } long timeoutNano = unit.toNanos(time) - (System.nanoTime() - startTime); try { if (acquire(true, true, timeoutNano, TimeUnit.NANOSECONDS)) { return true; } lock.unlock(); return false; } catch (ExecutionException e) { lock.unlock(); throw Throwables.propagate(e.getCause()); } catch (TimeoutException e) { lock.unlock(); return false; } }