/** * Await for the result of the future and thrown bk related exceptions. * * @param result future to wait for * @return the result of future * @throws BKException when exceptions are thrown by the future. If there is unkown exceptions * thrown from the future, the exceptions will be wrapped into * {@link org.apache.bookkeeper.client.BKException.BKUnexpectedConditionException}. */ public static <T> T bkResult(Future<T> result) throws BKException { try { return Await.result(result); } catch (BKException bke) { throw bke; } catch (InterruptedException ie) { throw BKException.create(BKException.Code.InterruptedException); } catch (Exception e) { logger.warn("Encountered unexpected exception on waiting bookkeeper results : ", e); throw BKException.create(BKException.Code.UnexpectedConditionException); } }
@Override public void onFailure(final Throwable cause) { String errMsg = "Error reading entries [" + startEntryId + "-" + endEntryId + "] for reading record of " + streamName; promise.setException(new IOException(errMsg, BKException.create(FutureUtils.bkResultCode(cause)))); } };
@Override public void readLastConfirmedAndEntryComplete(int rc, long lac, LedgerEntry ledgerEntry, Object ctx) { if (BKException.Code.OK == rc) { promise.setValue(Pair.of(lac, ledgerEntry)); } else { promise.setException(BKException.create(rc)); } } }, null);
public long getLength(LedgerDescriptor ledgerDesc) throws BKException { RefCountedLedgerHandle refhandle = getLedgerHandle(ledgerDesc); if (null == refhandle) { LOG.error("Accessing ledger {} without opening.", ledgerDesc); throw BKException.create(BKException.Code.UnexpectedConditionException); } return refhandle.handle.getLength(); }
@Override public void onFailure(final Throwable cause) { String errMsg = "Error opening log segment [" + segment + "] for find record from " + logName; promise.setException(new IOException(errMsg, BKException.create(FutureUtils.bkResultCode(cause)))); } };
@Override public void onFailure(final Throwable cause) { String errMsg = "Error opening log segment [" + l + "] for reading record of " + streamName; promise.setException(new IOException(errMsg, BKException.create(FutureUtils.bkResultCode(cause)))); } };
@Override public void closeComplete(int rc, LedgerHandle ledgerHandle, Object ctx) { if (BKException.Code.OK == rc) { promise.setValue(null); } else { promise.setException(BKException.create(rc)); } } }, null);
@Override public void readLastConfirmedComplete(int rc, long lastAddConfirmed, Object ctx) { if (BKException.Code.OK == rc) { promise.setValue(lastAddConfirmed); } else { promise.setException(BKException.create(rc)); } } }, null);
@Override public void readComplete(int rc, LedgerHandle lh, Enumeration<LedgerEntry> entries, Object ctx) { if (BKException.Code.OK == rc) { promise.setValue(entries); } else { promise.setException(BKException.create(rc)); } } }, null);
/** * Get the last add confirmed of <code>ledgerDesc</code>. * * @param ledgerDesc * ledger descriptor. * @return last add confirmed of <code>ledgerDesc</code> * @throws BKException */ public long getLastAddConfirmed(LedgerDescriptor ledgerDesc) throws BKException { RefCountedLedgerHandle refhandle = getLedgerHandle(ledgerDesc); if (null == refhandle) { LOG.error("Accessing ledger {} without opening.", ledgerDesc); throw BKException.create(BKException.Code.UnexpectedConditionException); } return refhandle.handle.getLastAddConfirmed(); }
/** * Whether a ledger is closed or not. * * @param ledgerDesc * ledger descriptor. * @return true if a ledger is closed, otherwise false. * @throws BKException */ public boolean isLedgerHandleClosed(LedgerDescriptor ledgerDesc) throws BKException { RefCountedLedgerHandle refhandle = getLedgerHandle(ledgerDesc); if (null == refhandle) { LOG.error("Accessing ledger {} without opening.", ledgerDesc); throw BKException.create(BKException.Code.UnexpectedConditionException); } return refhandle.handle.isClosed(); }
@Override void process(int rc) { tracker.enterPhase(ReadAheadPhase.EXCEPTION_HANDLING); if (BKException.Code.InterruptedException == rc) { LOG.trace("ReadAhead Worker for {} is interrupted.", fullyQualifiedName); running = false; setReadAheadInterrupted(tracker); return; } else if (BKException.Code.ZKException == rc) { encounteredException = true; int numExceptions = bkcZkExceptions.incrementAndGet(); LOG.debug("ReadAhead Worker for {} encountered zookeeper exception : total exceptions are {}.", fullyQualifiedName, numExceptions); } else if (BKException.Code.OK != rc) { encounteredException = true; switch(rc) { case BKException.Code.NoSuchEntryException: case BKException.Code.LedgerRecoveryException: case BKException.Code.NoSuchLedgerExistsException: break; default: bkcUnExpectedExceptions.incrementAndGet(); } LOG.info("ReadAhead Worker for {} encountered exception : {}", fullyQualifiedName, BKException.create(rc)); } // schedule next read ahead next.process(BKException.Code.OK); } }
@Override public void deleteLedger(long lId) throws InterruptedException, BKException { checkProgrammedFail(); if (!ledgers.containsKey(lId)) { throw BKException.create(BKException.Code.NoSuchLedgerExistsException); } ledgers.remove(lId); }
public void failAfter(int steps, int rc) { promiseAfter(steps).completeExceptionally(BKException.create(rc)); }
/** * Async try read last confirmed. * * @param ledgerDesc * ledger descriptor * @return future presenting read last confirmed result. */ public Future<Long> asyncTryReadLastConfirmed(LedgerDescriptor ledgerDesc) { RefCountedLedgerHandle refHandle = handlesMap.get(ledgerDesc); if (null == refHandle) { LOG.error("Accessing ledger {} without opening.", ledgerDesc); return Future.exception(BKException.create(BKException.Code.UnexpectedConditionException)); } final Promise<Long> promise = new Promise<Long>(); refHandle.handle.asyncTryReadLastConfirmed(new AsyncCallback.ReadLastConfirmedCallback() { @Override public void readLastConfirmedComplete(int rc, long lastAddConfirmed, Object ctx) { if (BKException.Code.OK == rc) { promise.setValue(lastAddConfirmed); } else { promise.setException(BKException.create(rc)); } } }, null); return promise; }
@Override protected int runCmd() throws Exception { LedgerHandle lh = getBookKeeperClient().get().openLedgerNoRecovery( getLedgerID(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes(UTF_8)); final CountDownLatch doneLatch = new CountDownLatch(1); final AtomicInteger resultHolder = new AtomicInteger(-1234); BookkeeperInternalCallbacks.GenericCallback<Void> recoverCb = new BookkeeperInternalCallbacks.GenericCallback<Void>() { @Override public void operationComplete(int rc, Void result) { resultHolder.set(rc); doneLatch.countDown(); } }; try { BookKeeperAccessor.forceRecoverLedger(lh, recoverCb); doneLatch.await(); if (BKException.Code.OK != resultHolder.get()) { throw BKException.create(resultHolder.get()); } } finally { lh.close(); } return 0; }
@Override public void createComplete(int rc, LedgerHandle lh, Object ctx) { if (BKException.Code.OK == rc) { promise.updateIfEmpty(new Return<LedgerHandle>(lh)); } else { promise.updateIfEmpty(new Throw<LedgerHandle>(BKException.create(rc))); } } }, null);
@Override public long addEntry(byte[] data) throws InterruptedException, BKException { try { bk.checkProgrammedFail(); } catch (BKException e) { fenced = true; throw e; } if (fenced) { throw BKException.create(BKException.Code.LedgerFencedException); } lastEntry = entries.size(); entries.add(LedgerEntryImpl.create(ledgerId, lastEntry, data.length, Unpooled.wrappedBuffer(data))); return lastEntry; }
@Override public void deleteComplete(int rc, Object ctx) { if (BKException.Code.OK == rc) { promise.updateIfEmpty(new Return<Void>(null)); } else if (BKException.Code.NoSuchLedgerExistsException == rc) { if (ignoreNonExistentLedger) { promise.updateIfEmpty(new Return<Void>(null)); } else { promise.updateIfEmpty(new Throw<Void>(BKException.create(rc))); } } else { promise.updateIfEmpty(new Throw<Void>(BKException.create(rc))); } } }, null);
@Override public void openComplete(int rc, LedgerHandle lh, Object ctx) { if (BKException.Code.OK != rc) { promise.setException(BKException.create(rc)); return; } RefCountedLedgerHandle newRefHandle = new RefCountedLedgerHandle(lh); RefCountedLedgerHandle oldRefHandle = handlesMap.putIfAbsent(ledgerDesc, newRefHandle); if (null != oldRefHandle) { oldRefHandle.addRef(); if (newRefHandle.removeRef()) { newRefHandle.handle.asyncClose(new AsyncCallback.CloseCallback() { @Override public void closeComplete(int i, LedgerHandle ledgerHandle, Object o) { // No action necessary } }, null); } } promise.setValue(ledgerDesc); } }, null);