Skip to content

Commit

Permalink
In managed ledger ReadOnlyCursor, optimize for when there's a new led…
Browse files Browse the repository at this point in the history
…ger created (apache#3148)

### Motivation

In managed ledger, the check `Cursor.hasMoreEntries()` is very cheap when the last ledger has at least some entries (eg: just longs comparison). If it's empty, we need to do a more expensive check that involve looking at the ledgers list to make sure the answer is always correct.

When creating a read-only cursor, we get a snapshot of the current state. If the topic was left with a new ledger created and no entry into that, the `hasMoreEntries()` will be always expensive.

To avoid that, if the last ledger is empty, initialize the last written position on the last entry of previous ledger.
  • Loading branch information
merlimat authored and sijie committed Dec 10, 2018
1 parent f2f7ba9 commit 20fbb5d
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,21 @@ public void operationFailed(MetaStoreException e) {
}

private ReadOnlyCursor createReadOnlyCursor(PositionImpl startPosition) {
lastConfirmedEntry = ledgers.size() == 0 ? PositionImpl.earliest
: new PositionImpl(ledgers.lastKey(), ledgers.lastEntry().getValue().getEntries() - 1);
if (ledgers.isEmpty()) {
lastConfirmedEntry = PositionImpl.earliest;
} else if (ledgers.lastEntry().getValue().getEntries() > 0) {
// Last ledger has some of the entries
lastConfirmedEntry = new PositionImpl(ledgers.lastKey(), ledgers.lastEntry().getValue().getEntries() - 1);
} else {
// Last ledger is empty. If there is a previous ledger, position on the last entry of that ledger
if (ledgers.size() > 1) {
long lastLedgerId = ledgers.lastKey();
LedgerInfo li = ledgers.headMap(lastLedgerId, false).lastEntry().getValue();
lastConfirmedEntry = new PositionImpl(li.getLedgerId(), li.getEntries() - 1);
} else {
lastConfirmedEntry = PositionImpl.earliest;
}
}

ReadOnlyCursorImpl cursor = new ReadOnlyCursorImpl(bookKeeper, config, this, startPosition, "read-only-cursor");
return cursor;
Expand Down

0 comments on commit 20fbb5d

Please sign in to comment.