org.apache.bookkeeper.client.AsyncCallback Java Examples

The following examples show how to use org.apache.bookkeeper.client.AsyncCallback. 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 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 #2
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 #3
Source File: LedgerHandleCache.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Async Read Entries
 *
 * @param ledgerDesc
 *          ledger descriptor
 * @param first
 *          first entry
 * @param last
 *          second entry
 */
public Future<Enumeration<LedgerEntry>> asyncReadEntries(
        LedgerDescriptor ledgerDesc, long first, long last) {
    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<Enumeration<LedgerEntry>> promise = new Promise<Enumeration<LedgerEntry>>();
    refHandle.handle.asyncReadEntries(first, last, new AsyncCallback.ReadCallback() {
        @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);
    return promise;
}
 
Example #4
Source File: LedgerHandleCache.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Async read last confirmed and entry
 *
 * @param ledgerDesc
 *          ledger descriptor
 * @param entryId
 *          entry id to read
 * @param timeOutInMillis
 *          time out if no newer entry available
 * @param parallel
 *          whether to read from replicas in parallel
 */
public Future<Pair<Long, LedgerEntry>> asyncReadLastConfirmedAndEntry(
        LedgerDescriptor ledgerDesc,
        long entryId,
        long timeOutInMillis,
        boolean parallel) {
    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<Pair<Long, LedgerEntry>> promise = new Promise<Pair<Long, LedgerEntry>>();
    refHandle.handle.asyncReadLastConfirmedAndEntry(entryId, timeOutInMillis, parallel,
            new AsyncCallback.ReadLastConfirmedAndEntryCallback() {
                @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);
    return promise;
}
 
Example #5
Source File: LedgerHandleCache.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #6
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 #7
Source File: BookKeeperClient.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Void> deleteLedger(long lid,
                                 final boolean ignoreNonExistentLedger) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return FutureUtils.exception(ioe);
    }
    final CompletableFuture<Void> promise = new CompletableFuture<Void>();
    bk.asyncDeleteLedger(lid, new AsyncCallback.DeleteCallback() {
        @Override
        public void deleteComplete(int rc, Object ctx) {
            if (BKException.Code.OK == rc) {
                promise.complete(null);
            } else if (BKException.Code.NoSuchLedgerExistsException == rc) {
                if (ignoreNonExistentLedger) {
                    promise.complete(null);
                } else {
                    promise.completeExceptionally(BKException.create(rc));
                }
            } else {
                promise.completeExceptionally(BKException.create(rc));
            }
        }
    }, null);
    return promise;
}
 
Example #8
Source File: LedgerHandleCache.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * Close the ledger asynchronously.
 *
 * @param ledgerDesc
 *          ledger descriptor.
 * @return future presenting the closing result.
 */
public Future<Void> asyncCloseLedger(LedgerDescriptor ledgerDesc) {
    final Promise<Void> promise = new Promise<Void>();

    RefCountedLedgerHandle refhandle = getLedgerHandle(ledgerDesc);
    if ((null != refhandle) && (refhandle.removeRef())) {
        refhandle = handlesMap.remove(ledgerDesc);
        if (refhandle.getRefCount() > 0) {
            // In the rare race condition that a ref count was added immediately
            // after the close de-refed it and the remove was called

            // Try to put the handle back in the map
            handlesMap.putIfAbsent(ledgerDesc, refhandle);

            // ReadOnlyLedgerHandles don't have much overhead, so lets just leave
            // the handle open even if it had already been replaced
            promise.setValue(null);
        } else {
            refhandle.handle.asyncClose(new AsyncCallback.CloseCallback() {
                @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);
        }
    } else {
        promise.setValue(null);
    }
    return promise;
}
 
Example #9
Source File: BookKeeperClient.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public Future<Void> deleteLedger(long lid,
                                 final boolean ignoreNonExistentLedger) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return Future.exception(ioe);
    }
    final Promise<Void> promise = new Promise<Void>();
    bk.asyncDeleteLedger(lid, new AsyncCallback.DeleteCallback() {
        @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);
    return promise;
}
 
Example #10
Source File: LedgerHandleCache.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * Open the given ledger <i>ledgerDesc</i>.
 *
 * @param ledgerDesc
 *          ledger description
 * @param callback
 *          open callback.
 * @param ctx
 *          callback context
 */
private void asyncOpenLedger(LedgerDescriptor ledgerDesc, AsyncCallback.OpenCallback callback, Object ctx) {
    try {
        if (!ledgerDesc.isFenced()) {
            bkc.get().asyncOpenLedgerNoRecovery(ledgerDesc.getLedgerId(),
                    BookKeeper.DigestType.CRC32, digestpw.getBytes(UTF_8), callback, ctx);
        } else {
            bkc.get().asyncOpenLedger(ledgerDesc.getLedgerId(),
                    BookKeeper.DigestType.CRC32, digestpw.getBytes(UTF_8), callback, ctx);
        }
    } catch (IOException ace) {
        // :) when we can't get bkc, it means bookie handle not available
        callback.openComplete(BKException.Code.BookieHandleNotAvailableException, null, ctx);
    }
}
 
Example #11
Source File: BKLogSegmentEntryStore.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<LogSegmentRandomAccessEntryReader> openRandomAccessReader(final LogSegmentMetadata segment,
                                                                        final boolean fence) {
    final BookKeeper bk;
    try {
        bk = this.bkc.get();
    } catch (IOException e) {
        return FutureUtils.exception(e);
    }
    final CompletableFuture<LogSegmentRandomAccessEntryReader> openPromise =
            new CompletableFuture<LogSegmentRandomAccessEntryReader>();
    AsyncCallback.OpenCallback openCallback = new AsyncCallback.OpenCallback() {
        @Override
        public void openComplete(int rc, LedgerHandle lh, Object ctx) {
            if (BKException.Code.OK != rc) {
                FutureUtils.completeExceptionally(
                        openPromise,
                        new BKTransmitException("Failed to open ledger handle for log segment " + segment, rc));
                return;
            }
            LogSegmentRandomAccessEntryReader reader = new BKLogSegmentRandomAccessEntryReader(
                    segment,
                    lh,
                    conf);
            FutureUtils.complete(openPromise, reader);
        }
    };
    if (segment.isInProgress() && !fence) {
        bk.asyncOpenLedgerNoRecovery(
                segment.getLogSegmentId(),
                BookKeeper.DigestType.CRC32,
                passwd,
                openCallback,
                null);
    } else {
        bk.asyncOpenLedger(
                segment.getLogSegmentId(),
                BookKeeper.DigestType.CRC32,
                passwd,
                openCallback,
                null);
    }
    return openPromise;
}
 
Example #12
Source File: BKLogSegmentEntryWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void asyncAddEntry(byte[] data, int offset, int length,
                          AsyncCallback.AddCallback callback, Object ctx) {
    lh.asyncAddEntry(data, offset, length, callback, ctx);
}
 
Example #13
Source File: BKLogSegmentEntryWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void asyncClose(AsyncCallback.CloseCallback callback, Object ctx) {
    lh.asyncClose(callback, ctx);
}
 
Example #14
Source File: BKLogSegmentEntryWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void asyncClose(AsyncCallback.CloseCallback callback, Object ctx) {
    lh.asyncClose(callback, ctx);
}
 
Example #15
Source File: LedgerHandleCache.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
/**
 * Open the log segment.
 *
 * @param metadata
 *          the log segment metadata
 * @param fence
 *          whether to fence the log segment during open
 * @return a future presenting the open result.
 */
public Future<LedgerDescriptor> asyncOpenLedger(LogSegmentMetadata metadata, boolean fence) {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    final OpStatsLogger openStatsLogger = fence ? openStats : openNoRecoveryStats;
    final Promise<LedgerDescriptor> promise = new Promise<LedgerDescriptor>();
    final LedgerDescriptor ledgerDesc = new LedgerDescriptor(metadata.getLedgerId(), metadata.getLogSegmentSequenceNumber(), fence);
    RefCountedLedgerHandle refhandle = handlesMap.get(ledgerDesc);
    if (null == refhandle) {
        asyncOpenLedger(ledgerDesc, new AsyncCallback.OpenCallback() {
            @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);
    } else {
        refhandle.addRef();
        promise.setValue(ledgerDesc);
    }
    return promise.addEventListener(new FutureEventListener<LedgerDescriptor>() {
        @Override
        public void onSuccess(LedgerDescriptor value) {
            openStatsLogger.registerSuccessfulEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS));
        }

        @Override
        public void onFailure(Throwable cause) {
            openStatsLogger.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS));
        }
    });
}
 
Example #16
Source File: ReadAheadWorker.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
ReadLastConfirmedAndEntryCallbackWithNotification(
        long lac, AsyncCallback.ReadLastConfirmedAndEntryCallback cb, Object ctx) {
    super(lac, cb, ctx);
}
 
Example #17
Source File: BKLogWriteHandler.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private Future<LogSegmentMetadata> deleteLogSegment(
        final LogSegmentMetadata ledgerMetadata) {
    LOG.info("Deleting ledger {} for {}", ledgerMetadata, getFullyQualifiedName());
    final Promise<LogSegmentMetadata> promise = new Promise<LogSegmentMetadata>();
    final Stopwatch stopwatch = Stopwatch.createStarted();
    promise.addEventListener(new FutureEventListener<LogSegmentMetadata>() {
        @Override
        public void onSuccess(LogSegmentMetadata segment) {
            deleteOpStats.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        }

        @Override
        public void onFailure(Throwable cause) {
            deleteOpStats.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        }
    });
    try {
        bookKeeperClient.get().asyncDeleteLedger(ledgerMetadata.getLedgerId(), new AsyncCallback.DeleteCallback() {
            @Override
            public void deleteComplete(int rc, Object ctx) {
                if (BKException.Code.NoSuchLedgerExistsException == rc) {
                    LOG.warn("No ledger {} found to delete for {} : {}.",
                            new Object[]{ledgerMetadata.getLedgerId(), getFullyQualifiedName(),
                                    ledgerMetadata});
                } else if (BKException.Code.OK != rc) {
                    BKException bke = BKException.create(rc);
                    LOG.error("Couldn't delete ledger {} from bookkeeper for {} : ",
                            new Object[]{ledgerMetadata.getLedgerId(), getFullyQualifiedName(), bke});
                    promise.setException(bke);
                    return;
                }
                // after the ledger is deleted, we delete the metadata znode
                scheduler.submit(new Runnable() {
                    @Override
                    public void run() {
                        deleteLogSegmentMetadata(ledgerMetadata, promise);
                    }
                });
            }
        }, null);
    } catch (IOException e) {
        promise.setException(BKException.create(BKException.Code.BookieHandleNotAvailableException));
    }
    return promise;
}
 
Example #18
Source File: BKLogSegmentEntryWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void asyncAddEntry(ByteBuf entry,
                          AsyncCallback.AddCallback callback, Object ctx) {
    lh.asyncAddEntry(entry, callback, ctx);
}
 
Example #19
Source File: LogSegmentEntryWriter.java    From distributedlog with Apache License 2.0 2 votes vote down vote up
/**
 * Close the entry writer.
 */
void asyncClose(AsyncCallback.CloseCallback callback, Object ctx);
 
Example #20
Source File: LogSegmentEntryWriter.java    From distributedlog with Apache License 2.0 2 votes vote down vote up
/**
 * Async add entry to the log segment.
 * <p>The implementation semantic follows
 * {@link org.apache.bookkeeper.client.LedgerHandle#asyncAddEntry(
 * byte[], int, int, AsyncCallback.AddCallback, Object)}
 *
 * @param data
 *          data to add
 * @param offset
 *          offset in the data
 * @param length
 *          length of the data
 * @param callback
 *          callback
 * @param ctx
 *          ctx
 * @see org.apache.bookkeeper.client.LedgerHandle#asyncAddEntry(
 * byte[], int, int, AsyncCallback.AddCallback, Object)
 */
void asyncAddEntry(byte[] data, int offset, int length,
                   AsyncCallback.AddCallback callback, Object ctx);
 
Example #21
Source File: LogSegmentEntryWriter.java    From distributedlog with Apache License 2.0 2 votes vote down vote up
/**
 * Close the entry writer.
 */
void asyncClose(AsyncCallback.CloseCallback callback, Object ctx);
 
Example #22
Source File: LogSegmentEntryWriter.java    From distributedlog with Apache License 2.0 2 votes vote down vote up
/**
 * Async add entry to the log segment.
 *
 *  <p>The implementation semantic follows
 * {@link org.apache.bookkeeper.client.LedgerHandle#asyncAddEntry(
 * byte[], int, int, AsyncCallback.AddCallback, Object)}
 *
 * @param entry
 *          data to add
 * @param callback
 *          callback
 * @param ctx
 *          ctx
 * @see org.apache.bookkeeper.client.LedgerHandle#asyncAddEntry(
 * byte[], int, int, AsyncCallback.AddCallback, Object)
 */
void asyncAddEntry(ByteBuf entry,
                   AsyncCallback.AddCallback callback, Object ctx);