final List<LogSegmentMetadata> ledgers = new ArrayList<LogSegmentMetadata>(); final AtomicInteger result = new AtomicInteger(-1); final CountDownLatch latch = new CountDownLatch(1); Stopwatch stopwatch = Stopwatch.createStarted(); asyncGetLedgerListInternal(comparator, segmentFilter, null, new GenericCallback<List<LogSegmentMetadata>>() { latch.await(); } catch (InterruptedException e) { forceGetListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS)); KeeperException.Code rc = KeeperException.Code.get(result.get()); if (rc == KeeperException.Code.OK) { forceGetListStat.registerSuccessfulEvent(elapsedMicros);
throw new DLInterruptedException("Interrupted on checking if log " + path + " exists", e); final CountDownLatch syncLatch = new CountDownLatch(1); final AtomicInteger syncResult = new AtomicInteger(0); zk.sync(path, new AsyncCallback.VoidCallback() { @Override public void processResult(int rc, String path, Object ctx) { syncLatch.await(); } catch (InterruptedException e) { throw new DLInterruptedException("Interrupted on syncing zookeeper connection", e); if (KeeperException.Code.OK.intValue() != syncResult.get()) { throw new ZKException("Error syncing zookeeper connection ", KeeperException.Code.get(syncResult.get()));
private void createIfDoesntExist(String path, byte[] data, boolean force, ZooKeeper zooKeeper) throws InterruptedException, KeeperException { try { zooKeeper.create(path, data, defaultAcl(), CreateMode.PERSISTENT); } catch (KeeperException ke) { //Explicit Overwrite if (KeeperException.Code.NODEEXISTS.equals(ke.code())) { if (force) { zooKeeper.delete(path, -1); zooKeeper.create(path, data, defaultAcl(), CreateMode.PERSISTENT); } } else { throw ke; } } }
@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); } }
@Test(timeout = 60000) public void testProcessNullResults() throws Exception { ZooKeeperClient zkc = mock(ZooKeeperClient.class); ZKTransaction transaction = new ZKTransaction(zkc); int numOps = 3; final CountDownLatch commitLatch = new CountDownLatch(numOps); final CountDownLatch abortLatch = new CountDownLatch(numOps); for (int i = 0; i < numOps; i++) { transaction.addOp(new CountDownZKOp(commitLatch, abortLatch)); } transaction.processResult( KeeperException.Code.CONNECTIONLOSS.intValue(), "test-path", null, null); abortLatch.await(); assertEquals(0, abortLatch.getCount()); assertEquals(numOps, commitLatch.getCount()); }
@Test(timeout = 60000) public void testAbortOpResult() throws Exception { final AtomicReference<Throwable> exception = new AtomicReference<Throwable>(); final CountDownLatch latch = new CountDownLatch(1); ZKVersionedSetOp versionedSetOp = new ZKVersionedSetOp(mock(Op.class), new Transaction.OpListener<Version>() { @Override public void onCommit(Version r) { // no-op } @Override public void onAbort(Throwable t) { exception.set(t); latch.countDown(); } }); KeeperException ke = KeeperException.create(KeeperException.Code.SESSIONEXPIRED); OpResult opResult = new OpResult.ErrorResult(KeeperException.Code.NONODE.intValue()); versionedSetOp.abortOpResult(ke, opResult); latch.await(); assertTrue(exception.get() instanceof KeeperException.NoNodeException); } }
Stat stat = new Stat(); startSignal = new CountDownLatch(1); zk = new DisconnectableZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, new MyWatcher("testSession"), zk.getSessionId(), zk.getSessionPasswd()); startSignal.await(); Assert.assertEquals(KeeperException.Code.SESSIONEXPIRED.toString(), cb.toString());
@Override public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { if (KeeperException.Code.OK.intValue() != rc) { numFailures.incrementAndGet(); latch.countDown(); return; } Versioned<byte[]> allocatorData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion())); SimpleLedgerAllocator allocator = new SimpleLedgerAllocator(path, allocatorData, quorumConfigProvider, zkc, bkc); allocator.start(); pendingList.add(allocator); if (numPendings.decrementAndGet() == 0 && numFailures.get() == 0) { latch.countDown(); } } };
void waitForFinish() throws IOException { try { countDownLatch.await(); } catch (InterruptedException e) { throw new DLInterruptedException("Interrupted on getting ledgers list for " + path, e); } if (KeeperException.Code.OK.intValue() != rc) { if (KeeperException.Code.NONODE.intValue() == rc) { if (!allowEmpty) { throw new LogNotFoundException("Log " + path + " is not found"); } } else { throw new MetadataException("Error getting ledgers list for " + path); } } } }
); final CountDownLatch latch = new CountDownLatch(1); curator.getCuratorListenable().addListener( new CuratorListener() Assert.assertEquals(1, results.size()); final CuratorTransactionResult result = results.iterator().next(); Assert.assertEquals(Code.OK.intValue(), result.getError()); // assert delete
zk = createClient(); try { zk.addAuthInfo("digest", "ben:passwd".getBytes()); zk.create("/ben", new byte[0], Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT, this, results); zk.create("/ben/2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results); zk.delete("/ben", -1, this, results); zk.create("/ben2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results); Assert.assertEquals(Code.NOAUTH, Code.get(results.get(1))); Assert.assertEquals(0, (int) results.get(2)); Assert.assertEquals(0, (int) results.get(3)); zk.addAuthInfo("digest", "ben:passwd2".getBytes()); try { zk.getData("/ben2", false, new Stat()); Assert.fail("Should have received a permission error"); } catch (KeeperException e) { Assert.assertEquals(Code.NOAUTH, e.code()); try { zk.addAuthInfo("digest", "ben:passwd".getBytes()); zk.getData("/ben2", false, new Stat()); } finally { zk.close();
while (true) { try { children = getZooKeeper(info).getChildren(zPath, false); break; } catch (KeeperException e) { final Code c = e.code(); if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); while (true) { try { stat = getZooKeeper(info).exists(zPath, null); getZooKeeper(info).delete(zPath, -1); return; } catch (NoNodeException e) { final Code c = e.code(); if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) { retryOrThrow(retry, e); if (policy.equals(NodeMissingPolicy.SKIP) && e.code().equals(KeeperException.Code.NONODE)) return; throw e;
@Override public void getCursors(final String ledgerName, final MetaStoreCallback<List<String>> callback) { if (log.isDebugEnabled()) { log.debug("[{}] Get cursors list", ledgerName); } zk.getChildren(prefix + ledgerName, false, (rc, path, ctx, children, stat) -> executor.executeOrdered(ledgerName, safeRun(() -> { if (log.isDebugEnabled()) { log.debug("[{}] getConsumers complete rc={} children={}", ledgerName, Code.get(rc), children); } if (rc != Code.OK.intValue()) { callback.operationFailed(new MetaStoreException(KeeperException.create(Code.get(rc)))); return; } if (log.isDebugEnabled()) { log.debug("[{}] Get childrend completed version={}", ledgerName, stat.getVersion()); } callback.operationComplete(children, new ZKStat(stat)); })), null); }
@Override public void processResult(int rc, String path, Object ctx, Stat stat) { if (KeeperException.Code.OK.intValue() == rc) { ZKAccessControl.this.zkVersion = stat.getVersion(); promise.setValue(ZKAccessControl.this); } else { promise.setException(KeeperException.create(KeeperException.Code.get(rc))); } } }, null);
private String ensureNodeExists(ZooKeeper zooKeeper, String path, CreateMode createMode) { try { LOGGER.debug("attempting to create node at {}", path); final ArrayList<ACL> acls = ZooDefs.Ids.OPEN_ACL_UNSAFE; final String createNodePath = zooKeeper.create(path, new byte[0], acls, createMode); LOGGER.info("created node at {}, acls: {}, createMode: {}", createNodePath, acls, createMode); return createNodePath; } catch (KeeperException e) { if (KeeperException.Code.NONODE.equals(e.code())) { final List<String> pathTokens = Splitter.on('/').omitEmptyStrings().trimResults().splitToList(path); final String parentPath = "/" + Joiner.on('/').skipNulls().join(pathTokens.subList(0, pathTokens.size() - 1)); LOGGER.debug("node doesn't exist, recursively attempting to create node at {}", parentPath); ensureNodeExists(zooKeeper, parentPath, CreateMode.PERSISTENT); LOGGER.debug("recursively created node at {}", parentPath); LOGGER.debug("retrying attempt to create node at {}", path); return ensureNodeExists(zooKeeper, path, createMode); } else if (KeeperException.Code.NODEEXISTS.equals(e.code())) { return path; } else { throw new RuntimeException(String.format("unable to create node at path %s, ZooKeeper returned %s", path, e.code()), e); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(String.format("unable to create node at path %s", path), e); } }
@Override public void removeManagedLedger(String ledgerName, MetaStoreCallback<Void> callback) { log.info("[{}] Remove ManagedLedger", ledgerName); zk.delete(prefix + ledgerName, -1, (rc, path, ctx) -> executor.executeOrdered(ledgerName, safeRun(() -> { if (log.isDebugEnabled()) { log.debug("[{}] zk delete done. rc={}", ledgerName, Code.get(rc)); } if (rc == Code.OK.intValue()) { callback.operationComplete(null, null); } else { callback.operationFailed(new MetaStoreException(KeeperException.create(Code.get(rc)))); } })), null); }
return FAILED_TO_OWN_TASK; latestZKVersion = stat.getVersion(); SplitLogCounters.tot_wkr_task_heartbeat.increment(); return latestZKVersion; } catch (KeeperException e) { if (!isFirstTime) { if (e.code().equals(KeeperException.Code.NONODE)) { LOG.warn("NONODE failed to assert ownership for " + task, e); } else if (e.code().equals(KeeperException.Code.BADVERSION)) { LOG.warn("BADVERSION failed to assert ownership for " + task, e); } else {
@Override public void asyncGetCursorInfo(String ledgerName, String consumerName, final MetaStoreCallback<ManagedCursorInfo> callback) { String path = prefix + ledgerName + "/" + consumerName; if (log.isDebugEnabled()) { log.debug("Reading from {}", path); } zk.getData(path, false, (rc, path1, ctx, data, stat) -> executor.executeOrdered(ledgerName, safeRun(() -> { if (rc != Code.OK.intValue()) { callback.operationFailed(new MetaStoreException(KeeperException.create(Code.get(rc)))); } else { try { ManagedCursorInfo info = parseManagedCursorInfo(data); callback.operationComplete(info, new ZKStat(stat)); } catch (ParseException | InvalidProtocolBufferException e) { callback.operationFailed(new MetaStoreException(e)); } } })), null); if (log.isDebugEnabled()) { log.debug("Reading from {} ok", path); } }
@Override public void getData(final String path, final Watcher watcher, final DataCallback cb, final Object ctx) { executor.execute(() -> { checkReadOpDelay(); mutex.lock(); if (getProgrammedFailStatus()) { mutex.unlock(); cb.processResult(failReturnCode.intValue(), path, ctx, null, null); return; } else if (stopped) { mutex.unlock(); cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null, null); return; } Pair<byte[], Integer> value = tree.get(path); if (value == null) { mutex.unlock(); cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null, null); } else { if (watcher != null) { watchers.put(path, watcher); } Stat stat = new Stat(); stat.setVersion(value.getRight()); mutex.unlock(); cb.processResult(0, path, ctx, value.getLeft(), stat); } }); }