Java Code Examples for org.apache.bookkeeper.client.BookKeeper#asyncCreateLedger()

The following examples show how to use org.apache.bookkeeper.client.BookKeeper#asyncCreateLedger() . 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: BookKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public CompletableFuture<LedgerHandle> createLedger(int ensembleSize,
                                                    int writeQuorumSize,
                                                    int ackQuorumSize) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return FutureUtils.exception(ioe);
    }
    final CompletableFuture<LedgerHandle> promise = new CompletableFuture<LedgerHandle>();
    bk.asyncCreateLedger(ensembleSize, writeQuorumSize, ackQuorumSize,
            BookKeeper.DigestType.CRC32, passwd, new AsyncCallback.CreateCallback() {
                @Override
                public void createComplete(int rc, LedgerHandle lh, Object ctx) {
                    if (BKException.Code.OK == rc) {
                        promise.complete(lh);
                    } else {
                        promise.completeExceptionally(BKException.create(rc));
                    }
                }
            }, null, Collections.emptyMap());
    return promise;
}
 
Example 2
Source File: BookKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public Future<LedgerHandle> createLedger(int ensembleSize,
                                         int writeQuorumSize,
                                         int ackQuorumSize) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return Future.exception(ioe);
    }
    final Promise<LedgerHandle> promise = new Promise<LedgerHandle>();
    bk.asyncCreateLedger(ensembleSize, writeQuorumSize, ackQuorumSize,
            BookKeeper.DigestType.CRC32, passwd, new AsyncCallback.CreateCallback() {
                @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);
    return promise;
}
 
Example 3
Source File: TwoPhaseCompactor.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<LedgerHandle> createLedger(BookKeeper bk, Map<String,byte[]> metadata) {
    CompletableFuture<LedgerHandle> bkf = new CompletableFuture<>();

    try {
        bk.asyncCreateLedger(conf.getManagedLedgerDefaultEnsembleSize(),
                conf.getManagedLedgerDefaultWriteQuorum(),
                conf.getManagedLedgerDefaultAckQuorum(),
                Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE,
                Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD,
                (rc, ledger, ctx) -> {
                    if (rc != BKException.Code.OK) {
                        bkf.completeExceptionally(BKException.create(rc));
                    } else {
                        bkf.complete(ledger);
                    }
                }, null, metadata);
    } catch (Throwable t) {
        log.error("Encountered unexpected error when creating compaction ledger", t);
        return FutureUtil.failedFuture(t);
    }
    return bkf;
}
 
Example 4
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);
}