Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Broker] Fix the backlog issue with --precise-backlog=true #10966

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -883,13 +883,13 @@ public long getNumberOfEntriesInBacklog(boolean isPrecise) {
messagesConsumedCounter, markDeletePosition, readPosition);
}
if (isPrecise) {
return getNumberOfEntries(Range.closed(markDeletePosition, ledger.getLastPosition())) - 1;
return getNumberOfEntries(Range.openClosed(markDeletePosition, ledger.getLastPosition()));
eolivelli marked this conversation as resolved.
Show resolved Hide resolved
}

long backlog = ManagedLedgerImpl.ENTRIES_ADDED_COUNTER_UPDATER.get(ledger) - messagesConsumedCounter;
if (backlog < 0) {
// In some case the counters get incorrect values, fall back to the precise backlog count
backlog = getNumberOfEntries(Range.closed(markDeletePosition, ledger.getLastPosition())) - 1;
backlog = getNumberOfEntries(Range.openClosed(markDeletePosition, ledger.getLastPosition()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we handle this situation inside the method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The range openClosed may be the appropriate select due to that the backlog metric doesn't include the markDeletePosition position.

}

return backlog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -82,6 +83,7 @@
import org.apache.bookkeeper.mledger.Position;
import org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.VoidCallback;
import org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback;
import org.apache.bookkeeper.mledger.proto.MLDataFormats;
import org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo;
import org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo;
import org.apache.bookkeeper.test.MockedBookKeeperTestCase;
Expand Down Expand Up @@ -3526,5 +3528,34 @@ public void testCursorCheckReadPositionChanged() throws Exception {
ledger.close();
}


@Test
public void testCursorGetBacklog() throws Exception {
ManagedLedgerConfig managedLedgerConfig = new ManagedLedgerConfig();
managedLedgerConfig.setMaxEntriesPerLedger(2);
managedLedgerConfig.setMinimumRolloverTime(0, TimeUnit.MILLISECONDS);
ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("get-backlog", managedLedgerConfig);
ManagedCursor managedCursor = ledger.openCursor("test");

Position position = ledger.addEntry("test".getBytes(Encoding));
ledger.addEntry("test".getBytes(Encoding));
Position position1 = ledger.addEntry("test".getBytes(Encoding));
ledger.addEntry("test".getBytes(Encoding));

Assert.assertEquals(managedCursor.getNumberOfEntriesInBacklog(true), 4);
Assert.assertEquals(managedCursor.getNumberOfEntriesInBacklog(false), 4);
Field field = ManagedLedgerImpl.class.getDeclaredField("ledgers");
field.setAccessible(true);

((ConcurrentSkipListMap<Long, MLDataFormats.ManagedLedgerInfo.LedgerInfo>) field.get(ledger)).remove(position.getLedgerId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to change the markDeletePosition at the same time, due to the range start position is markDeletePosition.

Copy link
Contributor Author

@congbobo184 congbobo184 Jun 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is for when the ledger have been deleted and the markDeletePosition is still the deleted ledger we can get the correct backlog.

field = ManagedCursorImpl.class.getDeclaredField("markDeletePosition");
field.setAccessible(true);
field.set(managedCursor, PositionImpl.get(position1.getLedgerId(), -1));


Assert.assertEquals(managedCursor.getNumberOfEntriesInBacklog(true), 2);
Assert.assertEquals(managedCursor.getNumberOfEntriesInBacklog(false), 4);
}

private static final Logger log = LoggerFactory.getLogger(ManagedCursorTest.class);
}