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

[fix] Avoid negative estimated entry count #24055

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -3811,9 +3811,11 @@ public int applyMaxSizeCap(int maxEntries, long maxSizeBytes) {
if (maxSizeBytes == NO_MAX_SIZE_LIMIT) {
return maxEntries;
}
int maxEntriesBasedOnSize =
Long.valueOf(estimateEntryCountBySize(maxSizeBytes, readPosition, ledger)).intValue();
return Math.min(maxEntriesBasedOnSize, maxEntries);
long estimatedEntryCount = estimateEntryCountBySize(maxSizeBytes, readPosition, ledger);
if (estimatedEntryCount > Integer.MAX_VALUE) {
return maxEntries;
}
return Math.min((int) estimatedEntryCount, maxEntries);
}

static long estimateEntryCountBySize(long bytesSize, Position readPosition, ManagedLedgerImpl ml) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5246,6 +5246,16 @@ public void testEstimateEntryCountBySize() throws Exception {
ml.delete();
}

@Test
public void testApplyMaxSizeCap() throws Exception {
var ml = factory.open("testApplyMaxSizeCap");
var cursor = ml.openCursor("c1");
ml.addEntry(new byte[1000]);
assertEquals(cursor.applyMaxSizeCap(200, Long.MAX_VALUE), 200);
ml.deleteCursor("c1");
ml.delete();
}

@Test
void testForceCursorRecovery() throws Exception {
TestPulsarMockBookKeeper bk = new TestPulsarMockBookKeeper(executor);
Expand Down
Loading