/** * 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 closeComplete(int rc, LedgerHandle lh, Object ctx) { if (BKException.Code.OK != rc && BKException.Code.LedgerClosedException != rc) { if (!abort) { throwExc.set(new IOException("Failed to close ledger for " + fullyQualifiedLogSegment + " : " + BKException.getMessage(rc))); } } completeClosePromise(abort, throwExc, closePromise); } }, null);
@Override public void run() { while (!ledgers.isEmpty()) { long lid = -1L; try { lid = ledgers.take(); System.out.println("Recovering ledger " + lid); bkAdmin.recoverBookieData(lid, bookieAddrs, dryrun, skipOpenLedgers); System.out.println("Recovered ledger completed : " + lid + ", " + numPendings.decrementAndGet() + " left"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); doneLatch.countDown(); break; } catch (BKException ke) { System.out.println("Recovered ledger failed : " + lid + ", rc = " + BKException.getMessage(ke.getCode())); } } doneLatch.countDown(); } };
+ "Heard from {} : bitset = {}, Error = '{}'. First unread entry is ({}, rc = {})", lh.getId(), startEntryId, endEntryId, sentToHosts, heardFromHosts, heardFromHostsBitSet, BKException.getMessage(code), firstUnread, firstRc); clientCtx.getClientStats().getReadOpLogger().registerFailedEvent(latencyNanos, TimeUnit.NANOSECONDS); future.completeExceptionally(BKException.create(code)); } else { clientCtx.getClientStats().getReadOpLogger().registerSuccessfulEvent(latencyNanos, TimeUnit.NANOSECONDS);
/** * Return the bk exception return code for a <i>throwable</i>. * * @param throwable the cause of the exception * @return the bk exception return code. if the exception isn't bk exceptions, * it would return bk exception code. */ public static int bkResultCode(Throwable throwable) { if (throwable instanceof BKException) { return ((BKException)throwable).getCode(); } return BKException.Code.UnexpectedConditionException; }
private void replicateLedgerFragment(LedgerHandle lh, final LedgerFragment ledgerFragment, final Map<Integer, BookieSocketAddress> targetBookieAddresses) throws InterruptedException, BKException { CompletableFuture<Void> result = new CompletableFuture<>(); ResultCallBack resultCallBack = new ResultCallBack(result); SingleFragmentCallback cb = new SingleFragmentCallback( resultCallBack, lh, bkc.getLedgerManager(), ledgerFragment.getFirstEntryId(), getReplacementBookiesMap(ledgerFragment, targetBookieAddresses)); Set<BookieSocketAddress> targetBookieSet = Sets.newHashSet(); targetBookieSet.addAll(targetBookieAddresses.values()); asyncRecoverLedgerFragment(lh, ledgerFragment, cb, targetBookieSet); try { SyncCallbackUtils.waitForResult(result); } catch (BKException err) { throw BKException.create(bkc.getReturnRc(err.getCode())); } }
/** * Handle the Response Code and transform it to a BKException. * * @param <T> * @param rc * @param result * @param future */ public static <T> void finish(int rc, T result, CompletableFuture<? super T> future) { if (rc != BKException.Code.OK) { future.completeExceptionally(BKException.create(rc).fillInStackTrace()); } else { future.complete(result); } }
public void recoverBookieData(final long lid, final Set<BookieSocketAddress> bookiesSrc, boolean dryrun, boolean skipOpenLedgers) throws InterruptedException, BKException { SyncObject sync = new SyncObject(); // Call the async method to recover bookie data. asyncRecoverBookieData(lid, bookiesSrc, dryrun, skipOpenLedgers, (rc, ctx) -> { LOG.info("Recover bookie for {} completed with rc : {}", lid, BKException.codeLogger(rc)); SyncObject syncObject = (SyncObject) ctx; synchronized (syncObject) { syncObject.rc = rc; syncObject.value = true; syncObject.notify(); } }, sync); // Wait for the async method to complete. synchronized (sync) { while (!sync.value) { sync.wait(); } } if (sync.rc != BKException.Code.OK) { throw BKException.create(sync.rc); } }
/** * Extract an exception code from an BKException, or use a default if it's another type. * The throwable is null, assume that no exception took place and return * {@link BKException.Code.OK}. */ public static int getExceptionCode(Throwable t, int defaultCode) { if (t == null) { return BKException.Code.OK; } else if (t instanceof BKException) { return ((BKException) t).getCode(); } else if (t.getCause() != null) { return getExceptionCode(t.getCause(), defaultCode); } else { return defaultCode; } }
@Override public void closeComplete(int rc, LedgerHandle lh, Object ctx) { if (rc != BKException.Code.OK) { LOG.warn("Close failed: {}", BKException.codeLogger(rc)); } // noop } }
private boolean closeCurrentLedgerHandle() { if (currentLH == null) { return true; } boolean retVal = false; LedgerDescriptor ld = currentLH; try { handleCache.closeLedger(ld); currentLH = null; retVal = true; } catch (BKException bke) { LOG.debug("BK Exception during closing {} : ", ld, bke); handleException(ReadAheadPhase.CLOSE_LEDGER, bke.getCode()); } return retVal; }
@Override public void recoverComplete(int rc, Object ctx) { LOG.info("Recover bookie operation completed with rc: {}", BKException.codeLogger(rc)); SyncObject syncObj = (SyncObject) ctx; synchronized (syncObj) { syncObj.rc = rc; syncObj.value = true; syncObj.notify(); } } }, sync);
@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);
/** * Create transmit exception from transmit result. * * @param transmitResult * transmit result (basically bk exception code) * @return transmit exception */ public static BKTransmitException transmitException(int transmitResult) { return new BKTransmitException("Failed to write to bookkeeper; Error is (" + transmitResult + ") " + BKException.getMessage(transmitResult), transmitResult); }
static int getExceptionCode(Throwable t) { if (t instanceof BKException) { return ((BKException) t).getCode(); } else if (t.getCause() != null) { return getExceptionCode(t.getCause()); } else { return BKException.Code.UnexpectedConditionException; } }
@Override public void getBookieInfoComplete(int rc, BookieInfo bInfo, Object ctx) { BookieSocketAddress b = (BookieSocketAddress) ctx; if (rc != BKException.Code.OK) { if (LOG.isErrorEnabled()) { LOG.error("Reading bookie info from bookie {} failed due to {}", b, BKException.codeLogger(rc)); } } else { if (LOG.isDebugEnabled()) { LOG.debug("Free disk space on bookie {} is {}.", b, bInfo.getFreeDiskSpace()); } map.put(b, bInfo); } if (totalCompleted.incrementAndGet() == totalSent.get()) { latch.countDown(); } } }, b);
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(); }
public static ManagedLedgerException createManagedLedgerException(int bkErrorCode) { if (bkErrorCode == BKException.Code.TooManyRequestsException) { return new TooManyRequestsException("Too many request error from bookies"); } else if (isBkErrorNotRecoverable(bkErrorCode)) { return new NonRecoverableLedgerException(BKException.getMessage(bkErrorCode)); } else { return new ManagedLedgerException(BKException.getMessage(bkErrorCode)); } }
/** * Wait for the result for a given <i>duration</i>. * <p>If the result is not ready within `duration`, an IOException will thrown wrapping with * corresponding {@link com.twitter.util.TimeoutException}. * * @param result result to wait * @param duration duration to wait * @return the result * @throws IOException when encountered exceptions on the result or waiting for the result. */ public static <T> T result(Future<T> result, Duration duration) throws IOException { try { return Await.result(result, duration); } catch (KeeperException ke) { throw new ZKException("Encountered zookeeper exception on waiting result", ke); } catch (BKException bke) { throw new BKTransmitException("Encountered bookkeeper exception on waiting result", bke.getCode()); } catch (IOException ioe) { throw ioe; } catch (InterruptedException ie) { throw new DLInterruptedException("Interrupted on waiting result", ie); } catch (Exception e) { throw new IOException("Encountered exception on waiting result", e); } }
@Override public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) { if (rc != BKException.Code.OK) { LOG.error("Failure {} while writing entry: {} while recovering ledger: {}", BKException.codeLogger(rc), entryId + 1, lh.ledgerId); submitCallback(rc); return; } long numAdd = writeCount.incrementAndGet(); if (readDone && readCount.get() == numAdd) { submitCallback(rc); } }