diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java index 7c0d13108b1c4..62f5e3ab7554b 100644 --- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java +++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java @@ -1129,7 +1129,7 @@ public boolean hasMoreEntries() { @Override public long getNumberOfEntries() { - if (readPosition.compareTo(ledger.getLastPosition().getNext()) > 0) { + if (readPosition.compareTo(ledger.getLastPosition()) > 0) { if (log.isDebugEnabled()) { log.debug("[{}] [{}] Read position {} is ahead of last position {}. There are no entries to read", ledger.getName(), name, readPosition, ledger.getLastPosition()); diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java index b4ab673facb26..4966cc82570db 100644 --- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java +++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java @@ -444,6 +444,10 @@ void readFromClosedLedger() throws Exception { void testNumberOfEntries() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2)); + ManagedCursor c0 = ledger.openCursor("c0"); + assertEquals(c0.getNumberOfEntries(), 0); + assertFalse(c0.hasMoreEntries()); + ManagedCursor c1 = ledger.openCursor("c1"); ledger.addEntry("dummy-entry-1".getBytes(Encoding)); ManagedCursor c2 = ledger.openCursor("c2");