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 2 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,12 +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);
return Math.min(estimateEntryCountBySize(maxSizeBytes, readPosition, ledger), maxEntries);
}

static long estimateEntryCountBySize(long bytesSize, Position readPosition, ManagedLedgerImpl ml) {
// The minimum value is 1
static int estimateEntryCountBySize(long bytesSize, Position readPosition, ManagedLedgerImpl ml) {
Position posToRead = readPosition;
if (!ml.isValidPosition(readPosition)) {
posToRead = ml.getNextValidPosition(readPosition);
Expand Down Expand Up @@ -3855,7 +3854,8 @@ static long estimateEntryCountBySize(long bytesSize, Position readPosition, Mana
posToRead = ml.getNextValidPosition(PositionFactory.create(posToRead.getLedgerId(), Long.MAX_VALUE));
}
}
return Math.max(result, 1);
int resultInt = Long.valueOf(result).intValue();
return Math.max(resultInt < 0 ? Integer.MAX_VALUE : resultInt, 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5173,7 +5173,7 @@ public void findEntryFailed(ManagedLedgerException exception, Optional<Position>
public void testEstimateEntryCountBySize() throws Exception {
final String mlName = "ml-" + UUID.randomUUID().toString().replaceAll("-", "");
ManagedLedgerImpl ml = (ManagedLedgerImpl) factory.open(mlName);
long entryCount0 =
int entryCount0 =
ManagedCursorImpl.estimateEntryCountBySize(16, PositionFactory.create(ml.getCurrentLedger().getId(), 0), ml);
assertEquals(entryCount0, 1);
// Avoid trimming ledgers.
Expand Down Expand Up @@ -5206,44 +5206,52 @@ public void testEstimateEntryCountBySize() throws Exception {
assertEquals(average3, 4 + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY);

// Test: the individual ledgers.
long entryCount1 =
int entryCount1 =
ManagedCursorImpl.estimateEntryCountBySize(average1 * 16, PositionFactory.create(ledger1, 0), ml);
assertEquals(entryCount1, 16);
long entryCount2 =
int entryCount2 =
ManagedCursorImpl.estimateEntryCountBySize(average2 * 8, PositionFactory.create(ledger2, 0), ml);
assertEquals(entryCount2, 8);
long entryCount3 =
int entryCount3 =
ManagedCursorImpl.estimateEntryCountBySize(average3 * 4, PositionFactory.create(ledger3, 0), ml);
assertEquals(entryCount3, 4);

// Test: across ledgers.
long entryCount4 =
int entryCount4 =
ManagedCursorImpl.estimateEntryCountBySize((average1 * 100) + (average2 * 8), PositionFactory.create(ledger1, 0), ml);
assertEquals(entryCount4, 108);
long entryCount5 =
int entryCount5 =
ManagedCursorImpl.estimateEntryCountBySize((average2 * 100) + (average3 * 4), PositionFactory.create(ledger2, 0), ml);
assertEquals(entryCount5, 104);
long entryCount6 =
int entryCount6 =
ManagedCursorImpl.estimateEntryCountBySize((average1 * 100) + (average2 * 100) + (average3 * 4), PositionFactory.create(ledger1, 0), ml);
assertEquals(entryCount6, 204);

long entryCount7 =
int entryCount7 =
ManagedCursorImpl.estimateEntryCountBySize((average1 * 20) + (average2 * 8), PositionFactory.create(ledger1, 80), ml);
assertEquals(entryCount7, 28);
long entryCount8 =
int entryCount8 =
ManagedCursorImpl.estimateEntryCountBySize((average2 * 20) + (average3 * 4), PositionFactory.create(ledger2, 80), ml);
assertEquals(entryCount8, 24);
long entryCount9 =
int entryCount9 =
ManagedCursorImpl.estimateEntryCountBySize((average1 * 20) + (average2 * 100) + (average3 * 4), PositionFactory.create(ledger1, 80), ml);
assertEquals(entryCount9, 124);

// Test: read more than entries written.
long entryCount10 =
int entryCount10 =
ManagedCursorImpl.estimateEntryCountBySize((average1 * 100) + (average2 * 100) + (average3 * 100) + (average3 * 4) , PositionFactory.create(ledger1, 0), ml);
assertEquals(entryCount10, 304);

// cleanup.
ml.delete();

// test estimated long value convert to an int value
ml = (ManagedLedgerImpl) factory.open(mlName);
ml.addEntry(new byte[1000]);
int entryCount11 = ManagedCursorImpl.estimateEntryCountBySize(
Long.MAX_VALUE, PositionFactory.create(ml.getCurrentLedger().getId(), 0), ml);
assertTrue(entryCount11 > 1, "entryCount11 is " + entryCount11);
ml.delete();
}

@Test
Expand Down
Loading