/** * Returns a ZooKeeper session. Calls should be made within run of ZooRunnable after caches are * checked. This will be performed at each retry of the run method. Calls to this method should be * made, ideally, after cache checks since other threads may have succeeded when updating the * cache. Doing this will ensure that we don't pay the cost of retrieving a ZooKeeper session on * each retry until we've ensured the caches aren't populated for a given node. * * @return ZooKeeper session. */ private ZooKeeper getZooKeeper() { return zReader.getZooKeeper(); }
@Override public void sync(final String path) throws KeeperException, InterruptedException { final AtomicInteger rc = new AtomicInteger(); final CountDownLatch waiter = new CountDownLatch(1); getZooKeeper().sync(path, (code, arg1, arg2) -> { rc.set(code); waiter.countDown(); }, null); waiter.await(); Code code = Code.get(rc.get()); if (code != KeeperException.Code.OK) { throw KeeperException.create(code); } }
@Override public boolean exists(String zPath) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().exists(zPath, false) != null; } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public Stat getStatus(String zPath, Watcher watcher) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().exists(zPath, watcher); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public List<String> getChildren(String zPath) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().getChildren(zPath, false); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public Stat getStatus(String zPath) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().exists(zPath, false); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public byte[] getData(String zPath, Watcher watcher, Stat stat) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().getData(zPath, watcher, stat); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public List<String> getChildren(String zPath, Watcher watcher) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().getChildren(zPath, watcher); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public boolean exists(String zPath, Watcher watcher) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().exists(zPath, watcher) != null; } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public byte[] getData(String zPath, boolean watch, Stat stat) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().getData(zPath, watch, stat); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
/** * Returns a ZooKeeper session. Calls should be made within run of ZooRunnable after caches are * checked. This will be performed at each retry of the run method. Calls to * {@link #getZooKeeper()} should be made, ideally, after cache checks since other threads may * have succeeded when updating the cache. Doing this will ensure that we don't pay the cost of * retrieving a ZooKeeper session on each retry until we've ensured the caches aren't populated * for a given node. * * @return ZooKeeper session. */ private ZooKeeper getZooKeeper() { return zReader.getZooKeeper(); }
@Override public void sync(final String path) throws KeeperException, InterruptedException { final AtomicInteger rc = new AtomicInteger(); final CountDownLatch waiter = new CountDownLatch(1); getZooKeeper().sync(path, new VoidCallback() { @Override public void processResult(int code, String arg1, Object arg2) { rc.set(code); waiter.countDown(); } }, null); waiter.await(); Code code = Code.get(rc.get()); if (code != KeeperException.Code.OK) { throw KeeperException.create(code); } }
@Override public byte[] getData(String zPath, boolean watch, Stat stat) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().getData(zPath, watch, stat); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public Stat getStatus(String zPath) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().exists(zPath, false); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public List<String> getChildren(String zPath) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().getChildren(zPath, false); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public boolean exists(String zPath) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().exists(zPath, false) != null; } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public byte[] getData(String zPath, Watcher watcher, Stat stat) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().getData(zPath, watcher, stat); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public Stat getStatus(String zPath, Watcher watcher) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().exists(zPath, watcher); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public List<String> getChildren(String zPath, Watcher watcher) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().getChildren(zPath, watcher); } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }
@Override public boolean exists(String zPath, Watcher watcher) throws KeeperException, InterruptedException { final Retry retry = getRetryFactory().createRetry(); while (true) { try { return getZooKeeper().exists(zPath, watcher) != null; } catch (KeeperException e) { final Code code = e.code(); if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); } else { throw e; } } retry.waitForNextAttempt(); } }