Skip to content

Commit 49f6236

Browse files
authored
[fix] Avoid negative estimated entry count (#24055)
1 parent 302c1d5 commit 49f6236

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -3811,9 +3811,11 @@ public int applyMaxSizeCap(int maxEntries, long maxSizeBytes) {
38113811
if (maxSizeBytes == NO_MAX_SIZE_LIMIT) {
38123812
return maxEntries;
38133813
}
3814-
int maxEntriesBasedOnSize =
3815-
Long.valueOf(estimateEntryCountBySize(maxSizeBytes, readPosition, ledger)).intValue();
3816-
return Math.min(maxEntriesBasedOnSize, maxEntries);
3814+
long estimatedEntryCount = estimateEntryCountBySize(maxSizeBytes, readPosition, ledger);
3815+
if (estimatedEntryCount > Integer.MAX_VALUE) {
3816+
return maxEntries;
3817+
}
3818+
return Math.min((int) estimatedEntryCount, maxEntries);
38173819
}
38183820

38193821
static long estimateEntryCountBySize(long bytesSize, Position readPosition, ManagedLedgerImpl ml) {

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

+10
Original file line numberDiff line numberDiff line change
@@ -5246,6 +5246,16 @@ public void testEstimateEntryCountBySize() throws Exception {
52465246
ml.delete();
52475247
}
52485248

5249+
@Test
5250+
public void testApplyMaxSizeCap() throws Exception {
5251+
var ml = factory.open("testApplyMaxSizeCap");
5252+
var cursor = ml.openCursor("c1");
5253+
ml.addEntry(new byte[1000]);
5254+
assertEquals(cursor.applyMaxSizeCap(200, Long.MAX_VALUE), 200);
5255+
ml.deleteCursor("c1");
5256+
ml.delete();
5257+
}
5258+
52495259
@Test
52505260
void testForceCursorRecovery() throws Exception {
52515261
TestPulsarMockBookKeeper bk = new TestPulsarMockBookKeeper(executor);

0 commit comments

Comments
 (0)