use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.
the class ManagedLedgerImpl method asyncReadEntry.
void asyncReadEntry(PositionImpl position, ReadEntryCallback callback, Object ctx) {
LedgerHandle currentLedger = this.currentLedger;
if (log.isDebugEnabled()) {
log.debug("[{}] Reading entry ledger {}: {}", name, position.getLedgerId(), position.getEntryId());
}
if (position.getLedgerId() == currentLedger.getId()) {
LedgerHandle ledger = currentLedger;
entryCache.asyncReadEntry(ledger, position, callback, ctx);
} else {
getLedgerHandle(position.getLedgerId()).thenAccept(ledger -> {
entryCache.asyncReadEntry(ledger, position, callback, ctx);
}).exceptionally(ex -> {
log.error("[{}] Error opening ledger for reading at position {} - {}", name, position, ex.getMessage());
callback.readEntryFailed(ManagedLedgerException.getManagedLedgerException(ex.getCause()), ctx);
return null;
});
}
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.
the class ManagedLedgerImpl method checkBackloggedCursors.
@Override
public void checkBackloggedCursors() {
// activate caught up cursors
cursors.forEach(cursor -> {
if (cursor.getNumberOfEntries() < maxActiveCursorBacklogEntries) {
cursor.setActive();
}
});
// deactivate backlog cursors
Iterator<ManagedCursor> cursors = activeCursors.iterator();
while (cursors.hasNext()) {
ManagedCursor cursor = cursors.next();
long backlogEntries = cursor.getNumberOfEntries();
if (backlogEntries > maxActiveCursorBacklogEntries) {
PositionImpl readPosition = (PositionImpl) cursor.getReadPosition();
readPosition = isValidPosition(readPosition) ? readPosition : getNextValidPosition(readPosition);
if (readPosition == null) {
if (log.isDebugEnabled()) {
log.debug("[{}] Couldn't find valid read position [{}] {}", name, cursor.getName(), cursor.getReadPosition());
}
continue;
}
try {
asyncReadEntry(readPosition, new ReadEntryCallback() {
@Override
public void readEntryFailed(ManagedLedgerException e, Object ctx) {
log.warn("[{}] Failed while reading entries on [{}] {}", name, cursor.getName(), e.getMessage(), e);
}
@Override
public void readEntryComplete(Entry entry, Object ctx) {
MessageMetadata msgMetadata = null;
try {
msgMetadata = Commands.parseMessageMetadata(entry.getDataBuffer());
long msgTimeSincePublish = (System.currentTimeMillis() - msgMetadata.getPublishTime());
if (msgTimeSincePublish > maxMessageCacheRetentionTimeMillis) {
cursor.setInactive();
}
} finally {
if (msgMetadata != null) {
msgMetadata.recycle();
}
entry.release();
}
}
}, null);
} catch (Exception e) {
log.warn("[{}] Failed while reading entries from cache on [{}] {}", name, cursor.getName(), e.getMessage(), e);
}
}
}
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.
the class ManagedCursorImpl method getNthEntry.
@Override
public Entry getNthEntry(int n, IndividualDeletedEntries deletedEntries) throws InterruptedException, ManagedLedgerException {
final CountDownLatch counter = new CountDownLatch(1);
class Result {
ManagedLedgerException exception = null;
Entry entry = null;
}
final Result result = new Result();
asyncGetNthEntry(n, deletedEntries, new ReadEntryCallback() {
@Override
public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
result.exception = exception;
counter.countDown();
}
@Override
public void readEntryComplete(Entry entry, Object ctx) {
result.entry = entry;
counter.countDown();
}
}, null);
counter.await();
if (result.exception != null) {
throw result.exception;
}
return result.entry;
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.
the class PersistentReplicator method peekNthMessage.
public CompletableFuture<Entry> peekNthMessage(int messagePosition) {
CompletableFuture<Entry> future = new CompletableFuture<>();
if (log.isDebugEnabled()) {
log.debug("[{}][{} -> {}] Getting message at position {}", topicName, localCluster, remoteCluster, messagePosition);
}
cursor.asyncGetNthEntry(messagePosition, IndividualDeletedEntries.Exclude, new ReadEntryCallback() {
@Override
public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
future.completeExceptionally(exception);
}
@Override
public void readEntryComplete(Entry entry, Object ctx) {
future.complete(entry);
}
}, null);
return future;
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.
the class PersistentSubscription method peekNthMessage.
@Override
public CompletableFuture<Entry> peekNthMessage(int messagePosition) {
CompletableFuture<Entry> future = new CompletableFuture<>();
if (log.isDebugEnabled()) {
log.debug("[{}][{}] Getting message at position {}", topicName, subName, messagePosition);
}
cursor.asyncGetNthEntry(messagePosition, IndividualDeletedEntries.Exclude, new ReadEntryCallback() {
@Override
public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
future.completeExceptionally(exception);
}
@Override
public void readEntryComplete(Entry entry, Object ctx) {
future.complete(entry);
}
}, null);
return future;
}
Aggregations