Skip to content

Commit 50e9199

Browse files
poorbarcodeganesh-ctds
authored andcommitted
[fix][broker]Incorrect backlog that is larger than expected (apache#25037)
(cherry picked from commit 587031e)
1 parent 322fddb commit 50e9199

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3785,6 +3785,7 @@ public long getNumberOfEntries(Range<Position> range) {
37853785
// than the LAC.
37863786
// To support this case, use "Long.MAX_VALUE" if the ledger is the last one.
37873787
long entriesInLedger = comparePositions(toPosition, lastConfirmedEntry) >= 0
3788+
|| toPosition.getLedgerId() == lastConfirmedEntry.getLedgerId()
37883789
? Long.MAX_VALUE : toLedger.getEntries();
37893790
count += Math.min(toPosition.getEntryId(), entriesInLedger - 1);
37903791
count += toIncluded ? 1 : 0;
@@ -3793,12 +3794,12 @@ public long getNumberOfEntries(Range<Position> range) {
37933794
// 2. Add the entries in the ledger pointed by fromPosition.
37943795
// Add nothing if "toPosition.entryId < 0".
37953796
// Add nothing if "toPosition" does not exit in "ledgers".
3796-
LedgerInfo formLedger = ledgers.get(fromPosition.getLedgerId());
3797-
if (formLedger != null) {
3797+
LedgerInfo fromLedger = ledgers.get(fromPosition.getLedgerId());
3798+
if (fromLedger != null) {
37983799
if (fromPosition.getEntryId() < 0) {
3799-
count += formLedger.getEntries();
3800+
count += fromLedger.getEntries();
38003801
} else {
3801-
count += formLedger.getEntries() - (fromPosition.getEntryId() + 1);
3802+
count += fromLedger.getEntries() - (fromPosition.getEntryId() + 1);
38023803
count += fromIncluded ? 1 : 0;
38033804
}
38043805
}

managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4682,13 +4682,15 @@ public void testGetNumberOfEntriesWithRangeParam() throws Exception {
46824682
assertEquals(ml.getNumberOfEntries(range26), 29);
46834683

46844684
// Normal case: end with current ledger.
4685-
Range<Position> range27 = Range.closed(positions.get(0), positions.get(34));
4686-
assertEquals(ml.getNumberOfEntries(range27), 35);
4685+
Range<Position> range27 = Range.closed(positions.get(0), positions.get(31));
4686+
assertEquals(ml.getNumberOfEntries(range27), 32);
4687+
Range<Position> range28 = Range.closed(positions.get(0), positions.get(34));
4688+
assertEquals(ml.getNumberOfEntries(range28), 35);
46874689
// Cover the following case.
46884690
// The use case "cursor.getNumberOfEntries()", which will use a "toPosition" that with an entry
46894691
// id that is larger than the LAC.
4690-
Range<Position> range28 = Range.closed(positions.get(0), PositionFactory.create(ledger4.getLedgerId(), 100));
4691-
assertEquals(ml.getNumberOfEntries(range28), 131);
4692+
Range<Position> range29 = Range.closed(positions.get(0), PositionFactory.create(ledger4.getLedgerId(), 100));
4693+
assertEquals(ml.getNumberOfEntries(range29), 131);
46924694

46934695
// From position that entry id is "-1" & positions in the same ledger.
46944696
Range<Position> range31 = Range.closed(PositionFactory.create(ledger1.getLedgerId(), -1),

0 commit comments

Comments
 (0)