Skip to content

Commit ab5237d

Browse files
gaoran10mukesh-ctds
authored andcommitted
[fix] Avoid negative estimated entry count (apache#24055)
(cherry picked from commit 49f6236) (cherry picked from commit a913874)
1 parent 5ff986c commit ab5237d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -3711,9 +3711,11 @@ public int applyMaxSizeCap(int maxEntries, long maxSizeBytes) {
37113711
if (maxSizeBytes == NO_MAX_SIZE_LIMIT) {
37123712
return maxEntries;
37133713
}
3714-
int maxEntriesBasedOnSize =
3715-
Long.valueOf(estimateEntryCountBySize(maxSizeBytes, readPosition, ledger)).intValue();
3716-
return Math.min(maxEntriesBasedOnSize, maxEntries);
3714+
long estimatedEntryCount = estimateEntryCountBySize(maxSizeBytes, readPosition, ledger);
3715+
if (estimatedEntryCount > Integer.MAX_VALUE) {
3716+
return maxEntries;
3717+
}
3718+
return Math.min((int) estimatedEntryCount, maxEntries);
37173719
}
37183720

37193721
static long estimateEntryCountBySize(long bytesSize, PositionImpl readPosition, ManagedLedgerImpl ml) {

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

+10
Original file line numberDiff line numberDiff line change
@@ -4880,5 +4880,15 @@ public void testEstimateEntryCountBySize() throws Exception {
48804880
ml.delete();
48814881
}
48824882

4883+
@Test
4884+
public void testApplyMaxSizeCap() throws Exception {
4885+
var ml = factory.open("testApplyMaxSizeCap");
4886+
var cursor = ml.openCursor("c1");
4887+
ml.addEntry(new byte[1000]);
4888+
assertEquals(((ManagedCursorImpl) cursor).applyMaxSizeCap(200, Long.MAX_VALUE), 200);
4889+
ml.deleteCursor("c1");
4890+
ml.delete();
4891+
}
4892+
48834893
private static final Logger log = LoggerFactory.getLogger(ManagedCursorTest.class);
48844894
}

0 commit comments

Comments
 (0)