org.apache.bookkeeper.client.BKException.Code Java Examples

The following examples show how to use org.apache.bookkeeper.client.BKException.Code. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void rollCurrentLedgerIfFull() {
    log.info("[{}] Start checking if current ledger is full", name);
    if (currentLedgerEntries > 0 && currentLedgerIsFull()) {
        STATE_UPDATER.set(this, State.ClosingLedger);
        currentLedger.asyncClose(new AsyncCallback.CloseCallback() {
            @Override
            public void closeComplete(int rc, LedgerHandle lh, Object o) {
                checkArgument(currentLedger.getId() == lh.getId(), "ledgerId %s doesn't match with acked ledgerId %s",
                        currentLedger.getId(),
                        lh.getId());

                if (rc == BKException.Code.OK) {
                    log.debug("Successfuly closed ledger {}", lh.getId());
                } else {
                    log.warn("Error when closing ledger {}. Status={}", lh.getId(), BKException.getMessage(rc));
                }

                ledgerClosed(lh);
                createLedgerAfterClosed();
            }
        }, System.nanoTime());
    }
}
 
Example #2
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void asyncDeleteLedger(long ledgerId, long retry) {
    if (retry <= 0) {
        log.warn("[{}] Failed to delete ledger after retries {}", name, ledgerId);
        return;
    }
    bookKeeper.asyncDeleteLedger(ledgerId, (rc, ctx) -> {
        if (isNoSuchLedgerExistsException(rc)) {
            log.warn("[{}] Ledger was already deleted {}", name, ledgerId);
        } else if (rc != BKException.Code.OK) {
            log.error("[{}] Error deleting ledger {} : {}", name, ledgerId, BKException.getMessage(rc));
            scheduledExecutor.schedule(safeRun(() -> asyncDeleteLedger(ledgerId, retry - 1)), DEFAULT_LEDGER_DELETE_BACKOFF_TIME_SEC, TimeUnit.SECONDS);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Deleted ledger {}", name, ledgerId);
            }
        }
    }, null);
}
 
Example #3
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * check if ledger-op task is already completed by timeout-task. If completed then delete the created ledger
 *
 * @param rc
 * @param lh
 * @param ctx
 * @return
 */
protected boolean checkAndCompleteLedgerOpTask(int rc, LedgerHandle lh, Object ctx) {
    if (ctx instanceof AtomicBoolean) {
        // ledger-creation is already timed out and callback is already completed so, delete this ledger and return.
        if (((AtomicBoolean) (ctx)).compareAndSet(false, true)) {
            return false;
        } else {
            if (rc == BKException.Code.OK) {
                log.warn("[{}]-{} ledger creation timed-out, deleting ledger", this.name, lh.getId());
                asyncDeleteLedger(lh.getId(), DEFAULT_LEDGER_DELETE_RETRIES);
            }
            return true;
        }
    }
    return false;
}
 
Example #4
Source File: LedgerReader.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public void readLacs(final LedgerHandle lh, long eid,
                     final GenericCallback<Set<ReadResult<Long>>> callback) {
    WriteSet writeSet = lh.distributionSchedule.getWriteSet(eid);
    final AtomicInteger numBookies = new AtomicInteger(writeSet.size());
    final Set<ReadResult<Long>> readResults = new HashSet<ReadResult<Long>>();
    ReadEntryCallback readEntryCallback = (rc, lid, eid1, buffer, ctx) -> {
        InetSocketAddress bookieAddress = (InetSocketAddress) ctx;
        ReadResult<Long> rr;
        if (BKException.Code.OK != rc) {
            rr = new ReadResult<Long>(eid1, rc, null, bookieAddress);
        } else {
            try {
                DigestManager.RecoveryData data = lh.macManager.verifyDigestAndReturnLastConfirmed(buffer);
                rr = new ReadResult<Long>(eid1, BKException.Code.OK, data.lastAddConfirmed, bookieAddress);
            } catch (BKException.BKDigestMatchException e) {
                rr = new ReadResult<Long>(eid1, BKException.Code.DigestMatchException, null, bookieAddress);
            }
        }
        readResults.add(rr);
        if (numBookies.decrementAndGet() == 0) {
            callback.operationComplete(BKException.Code.OK, readResults);
        }
    };

    ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsemble(eid);
    for (int i = 0; i < writeSet.size(); i++) {
        int idx = writeSet.get(i);
        bookieClient.readEntry(ensemble.get(idx), lh.getId(), eid, readEntryCallback, ensemble.get(idx));
    }
}
 
Example #5
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * return BK error codes that are considered not likely to be recoverable.
 */
private static boolean isBkErrorNotRecoverable(int rc) {
    switch (rc) {
    case Code.NoSuchLedgerExistsException:
    case Code.NoSuchLedgerExistsOnMetadataServerException:
    case Code.NoSuchEntryException:
        return true;

    default:
        return false;
    }
}
 
Example #6
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
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));
    }
}
 
Example #7
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Create ledger async and schedule a timeout task to check ledger-creation is complete else it fails the callback
 * with TimeoutException.
 *
 * @param bookKeeper
 * @param config
 * @param digestType
 * @param cb
 * @param metadata
 */
protected void asyncCreateLedger(BookKeeper bookKeeper, ManagedLedgerConfig config, DigestType digestType,
        CreateCallback cb, Map<String, byte[]> metadata) {
    AtomicBoolean ledgerCreated = new AtomicBoolean(false);
    Map<String, byte[]> finalMetadata = new HashMap<>();
    finalMetadata.putAll(ledgerMetadata);
    finalMetadata.putAll(metadata);
    log.info("[{}] Creating ledger, metadata: {} - metadata ops timeout : {} seconds",
        name, finalMetadata, config.getMetadataOperationsTimeoutSeconds());
    try {
        bookKeeper.asyncCreateLedger(config.getEnsembleSize(), config.getWriteQuorumSize(), config.getAckQuorumSize(),
            digestType, config.getPassword(), cb, ledgerCreated, finalMetadata);
    } catch (Throwable cause) {
        log.error("[{}] Encountered unexpected error when creating ledger",
            name, cause);
        cb.createComplete(Code.UnexpectedConditionException, null, ledgerCreated);
        return;
    }
    scheduledExecutor.schedule(() -> {
        if (!ledgerCreated.get()) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Timeout creating ledger", name);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Ledger already created when timeout task is triggered", name);
            }
        }
        cb.createComplete(BKException.Code.TimeoutException, null, ledgerCreated);
    }, config.getMetadataOperationsTimeoutSeconds(), TimeUnit.SECONDS);
}
 
Example #8
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void checkReadTimeout() {
    long timeoutSec = config.getReadEntryTimeoutSeconds();
    if (timeoutSec < 1) {
        return;
    }
    ReadEntryCallbackWrapper callback = this.lastReadCallback;
    long readOpCount = callback != null ? callback.readOpCount : 0;
    boolean timeout = callback != null && (TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - callback.createdTime) >= timeoutSec);
    if (readOpCount > 0 && timeout) {
        log.warn("[{}]-{}-{} read entry timeout after {} sec", this.name, this.lastReadCallback.ledgerId,
                this.lastReadCallback.entryId, timeoutSec);
        callback.readFailed(createManagedLedgerException(BKException.Code.TimeoutException), readOpCount);
        LAST_READ_CALLBACK_UPDATER.compareAndSet(this, callback, null);
    }
}
 
Example #9
Source File: BKExceptionStatsLogger.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
public static String getMessage(int code) {
    switch (code) {
        case Code.OK:
            return "OK";
        case Code.ReadException:
            return "ReadException";
        case Code.QuorumException:
            return "QuorumException";
        case Code.NoBookieAvailableException:
            return "NoBookieAvailableException";
        case Code.DigestNotInitializedException:
            return "DigestNotInitializedException";
        case Code.DigestMatchException:
            return "DigestMatchException";
        case Code.NotEnoughBookiesException:
            return "NotEnoughBookiesException";
        case Code.NoSuchLedgerExistsException:
            return "NoSuchLedgerExistsException";
        case Code.BookieHandleNotAvailableException:
            return "BookieHandleNotAvailableException";
        case Code.ZKException:
            return "ZKException";
        case Code.LedgerRecoveryException:
            return "LedgerRecoveryException";
        case Code.LedgerClosedException:
            return "LedgerClosedException";
        case Code.WriteException:
            return "WriteException";
        case Code.NoSuchEntryException:
            return "NoSuchEntryException";
        case Code.IncorrectParameterException:
            return "IncorrectParameterException";
        case Code.InterruptedException:
            return "InterruptedException";
        case Code.ProtocolVersionException:
            return "ProtocolVersionException";
        case Code.MetadataVersionException:
            return "MetadataVersionException";
        case Code.LedgerFencedException:
            return "LedgerFencedException";
        case Code.UnauthorizedAccessException:
            return "UnauthorizedAccessException";
        case Code.UnclosedFragmentException:
            return "UnclosedFragmentException";
        case Code.WriteOnReadOnlyBookieException:
            return "WriteOnReadOnlyBookieException";
        case Code.IllegalOpException:
            return "IllegalOpException";
        default:
            return "UnexpectedException";
    }
}
 
Example #10
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private synchronized void internalAsyncAddEntry(OpAddEntry addOperation) {
    pendingAddEntries.add(addOperation);
    final State state = STATE_UPDATER.get(this);
    if (state == State.Fenced) {
        addOperation.failed(new ManagedLedgerFencedException());
        return;
    } else if (state == State.Terminated) {
        addOperation.failed(new ManagedLedgerTerminatedException("Managed ledger was already terminated"));
        return;
    } else if (state == State.Closed) {
        addOperation.failed(new ManagedLedgerAlreadyClosedException("Managed ledger was already closed"));
        return;
    } else if (state == State.WriteFailed) {
        pendingAddEntries.remove(addOperation);
        addOperation.failed(new ManagedLedgerAlreadyClosedException("Waiting to recover from failure"));
        return;
    }

    if (state == State.ClosingLedger || state == State.CreatingLedger) {
        // We don't have a ready ledger to write into
        // We are waiting for a new ledger to be created
        if (log.isDebugEnabled()) {
            log.debug("[{}] Queue addEntry request", name);
        }
        if (State.CreatingLedger == state) {
            long elapsedMs = System.currentTimeMillis() - this.lastLedgerCreationInitiationTimestamp;
            if (elapsedMs > TimeUnit.SECONDS.toMillis(2 * config.getMetadataOperationsTimeoutSeconds())) {
                log.info("[{}] Ledger creation was initiated {} ms ago but it never completed" +
                    " and creation timeout task didn't kick in as well. Force to fail the create ledger operation ...");
                this.createComplete(Code.TimeoutException, null, null);
            }
        }
    } else if (state == State.ClosedLedger) {
        // No ledger and no pending operations. Create a new ledger
        log.info("[{}] Creating a new ledger", name);
        if (STATE_UPDATER.compareAndSet(this, State.ClosedLedger, State.CreatingLedger)) {
            this.lastLedgerCreationInitiationTimestamp = System.currentTimeMillis();
            mbean.startDataLedgerCreateOp();
            asyncCreateLedger(bookKeeper, config, digestType, this, Collections.emptyMap());
        }
    } else {
        checkArgument(state == State.LedgerOpened, "ledger=%s is not opened", state);

        // Write into lastLedger
        addOperation.setLedger(currentLedger);

        ++currentLedgerEntries;
        currentLedgerSize += addOperation.data.readableBytes();

        if (log.isDebugEnabled()) {
            log.debug("[{}] Write into current ledger lh={} entries={}", name, currentLedger.getId(),
                    currentLedgerEntries);
        }

        if (currentLedgerIsFull()) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Closing current ledger lh={}", name, currentLedger.getId());
            }
            // This entry will be the last added to current ledger
            addOperation.setCloseWhenDone(true);
            STATE_UPDATER.set(this, State.ClosingLedger);
        }

        addOperation.initiate();
    }
}
 
Example #11
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void asyncTerminate(TerminateCallback callback, Object ctx) {
    if (state == State.Fenced) {
        callback.terminateFailed(new ManagedLedgerFencedException(), ctx);
        return;
    } else if (state == State.Terminated) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Ignoring request to terminate an already terminated managed ledger", name);
        }
        callback.terminateComplete(lastConfirmedEntry, ctx);
        return;
    }

    log.info("[{}] Terminating managed ledger", name);
    state = State.Terminated;

    LedgerHandle lh = currentLedger;
    if (log.isDebugEnabled()) {
        log.debug("[{}] Closing current writing ledger {}", name, lh.getId());
    }

    mbean.startDataLedgerCloseOp();
    lh.asyncClose((rc, lh1, ctx1) -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Close complete for ledger {}: rc = {}", name, lh.getId(), rc);
        }
        mbean.endDataLedgerCloseOp();
        if (rc != BKException.Code.OK) {
            callback.terminateFailed(createManagedLedgerException(rc), ctx);
        } else {
            lastConfirmedEntry = new PositionImpl(lh.getId(), lh.getLastAddConfirmed());
            // Store the new state in metadata
            store.asyncUpdateLedgerIds(name, getManagedLedgerInfo(), ledgersStat, new MetaStoreCallback<Void>() {
                @Override
                public void operationComplete(Void result, Stat stat) {
                    ledgersStat = stat;
                    log.info("[{}] Terminated managed ledger at {}", name, lastConfirmedEntry);
                    callback.terminateComplete(lastConfirmedEntry, ctx);
                }

                @Override
                public void operationFailed(MetaStoreException e) {
                    log.error("[{}] Failed to terminate managed ledger: {}", name, e.getMessage());
                    callback.terminateFailed(new ManagedLedgerException(e), ctx);
                }
            });
        }
    }, null);
}
 
Example #12
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void asyncClose(final CloseCallback callback, final Object ctx) {
    State state = STATE_UPDATER.get(this);
    if (state == State.Fenced) {
        factory.close(this);
        callback.closeFailed(new ManagedLedgerFencedException(), ctx);
        return;
    } else if (state == State.Closed) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Ignoring request to close a closed managed ledger", name);
        }
        callback.closeComplete(ctx);
        return;
    }

    log.info("[{}] Closing managed ledger", name);

    factory.close(this);
    STATE_UPDATER.set(this, State.Closed);

    LedgerHandle lh = currentLedger;

    if (lh == null) {
        // No ledger to close, proceed with next step
        closeAllCursors(callback, ctx);
        return;
    }

    if (log.isDebugEnabled()) {
        log.debug("[{}] Closing current writing ledger {}", name, lh.getId());
    }

    mbean.startDataLedgerCloseOp();
    lh.asyncClose((rc, lh1, ctx1) -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Close complete for ledger {}: rc = {}", name, lh.getId(), rc);
        }
        mbean.endDataLedgerCloseOp();
        if (rc != BKException.Code.OK) {
            callback.closeFailed(createManagedLedgerException(rc), ctx);
            return;
        }

        closeAllCursors(callback, ctx);
    }, null);

    if (this.timeoutTask != null) {
        this.timeoutTask.cancel(false);
    }

}
 
Example #13
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void createComplete(int rc, final LedgerHandle lh, Object ctx) {
    if (log.isDebugEnabled()) {
        log.debug("[{}] createComplete rc={} ledger={}", name, rc, lh != null ? lh.getId() : -1);
    }

    if (checkAndCompleteLedgerOpTask(rc, lh, ctx)) {
        return;
    }

    mbean.endDataLedgerCreateOp();
    if (rc != BKException.Code.OK) {
        log.error("[{}] Error creating ledger rc={} {}", name, rc, BKException.getMessage(rc));
        ManagedLedgerException status = createManagedLedgerException(rc);

        // no pending entries means that creating this new ledger is NOT caused by write failure
        if (pendingAddEntries.isEmpty()) {
            STATE_UPDATER.set(this, State.ClosedLedger);
        } else {
            STATE_UPDATER.set(this, State.WriteFailed);
        }

        // Empty the list of pending requests and make all of them fail
        clearPendingAddEntries(status);
        lastLedgerCreationFailureTimestamp = clock.millis();
    } else {
        log.info("[{}] Created new ledger {}", name, lh.getId());
        ledgers.put(lh.getId(), LedgerInfo.newBuilder().setLedgerId(lh.getId()).setTimestamp(0).build());
        currentLedger = lh;
        currentLedgerEntries = 0;
        currentLedgerSize = 0;

        final MetaStoreCallback<Void> cb = new MetaStoreCallback<Void>() {
            @Override
            public void operationComplete(Void v, Stat stat) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Updating of ledgers list after create complete. version={}", name, stat);
                }
                ledgersStat = stat;
                metadataMutex.unlock();
                updateLedgersIdsComplete(stat);
                synchronized (ManagedLedgerImpl.this) {
                    mbean.addLedgerSwitchLatencySample(System.currentTimeMillis() - lastLedgerCreationInitiationTimestamp,
                            TimeUnit.MILLISECONDS);
                }
            }

            @Override
            public void operationFailed(MetaStoreException e) {
                if (e instanceof BadVersionException) {
                    synchronized (ManagedLedgerImpl.this) {
                        log.error(
                                "[{}] Failed to update ledger list. z-node version mismatch. Closing managed ledger",
                                name);
                        STATE_UPDATER.set(ManagedLedgerImpl.this, State.Fenced);
                        // Return ManagedLedgerFencedException to addFailed callback
                        // to indicate that the ledger is now fenced and topic needs to be closed
                        clearPendingAddEntries(new ManagedLedgerFencedException(e));
                        // Do not need to unlock ledgersListMutex here because we are going to close to topic anyways
                        return;
                    }
                }

                log.warn("[{}] Error updating meta data with the new list of ledgers: {}", name, e.getMessage());

                // Remove the ledger, since we failed to update the list
                ledgers.remove(lh.getId());
                mbean.startDataLedgerDeleteOp();
                bookKeeper.asyncDeleteLedger(lh.getId(), (rc1, ctx1) -> {
                    mbean.endDataLedgerDeleteOp();
                    if (rc1 != BKException.Code.OK) {
                        log.warn("[{}] Failed to delete ledger {}: {}", name, lh.getId(),
                                BKException.getMessage(rc1));
                    }
                }, null);

                metadataMutex.unlock();

                synchronized (ManagedLedgerImpl.this) {
                    lastLedgerCreationFailureTimestamp = clock.millis();
                    STATE_UPDATER.set(ManagedLedgerImpl.this, State.ClosedLedger);
                    clearPendingAddEntries(e);
                }
            }
        };

        updateLedgersListAfterRollover(cb);
    }
}
 
Example #14
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("checkstyle:fallthrough")
private void deleteAllLedgers(DeleteLedgerCallback callback, Object ctx) {
    List<LedgerInfo> ledgers = Lists.newArrayList(ManagedLedgerImpl.this.ledgers.values());
    AtomicInteger ledgersToDelete = new AtomicInteger(ledgers.size());
    if (ledgers.isEmpty()) {
        // No ledgers to delete, proceed with deleting metadata
        deleteMetadata(callback, ctx);
        return;
    }

    for (LedgerInfo ls : ledgers) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Deleting ledger {}", name, ls);
        }
        bookKeeper.asyncDeleteLedger(ls.getLedgerId(), (rc, ctx1) -> {
            switch (rc) {
            case Code.NoSuchLedgerExistsException:
            case Code.NoSuchLedgerExistsOnMetadataServerException:
                log.warn("[{}] Ledger {} not found when deleting it", name, ls.getLedgerId());
                // Continue anyway

            case BKException.Code.OK:
                if (ledgersToDelete.decrementAndGet() == 0) {
                    // All ledgers deleted, now remove ML metadata
                    deleteMetadata(callback, ctx);
                }
                break;

            default:
                // Handle error
                log.warn("[{}] Failed to delete ledger {} -- {}", name, ls.getLedgerId(),
                        BKException.getMessage(rc));
                int toDelete = ledgersToDelete.get();
                if (toDelete != -1 && ledgersToDelete.compareAndSet(toDelete, -1)) {
                    // Trigger callback only once
                    callback.deleteLedgerFailed(createManagedLedgerException(rc), ctx);
                }
            }
        }, null);
    }
}