Skip to content

Commit 8abc10f

Browse files
gaoran10lhotari
authored andcommitted
[fix] Avoid negative estimated entry count (#24055)
(cherry picked from commit 49f6236)
1 parent bba5bc8 commit 8abc10f

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,9 +3719,11 @@ public int applyMaxSizeCap(int maxEntries, long maxSizeBytes) {
37193719
if (maxSizeBytes == NO_MAX_SIZE_LIMIT) {
37203720
return maxEntries;
37213721
}
3722-
int maxEntriesBasedOnSize =
3723-
Long.valueOf(estimateEntryCountBySize(maxSizeBytes, readPosition, ledger)).intValue();
3724-
return Math.min(maxEntriesBasedOnSize, maxEntries);
3722+
long estimatedEntryCount = estimateEntryCountBySize(maxSizeBytes, readPosition, ledger);
3723+
if (estimatedEntryCount > Integer.MAX_VALUE) {
3724+
return maxEntries;
3725+
}
3726+
return Math.min((int) estimatedEntryCount, maxEntries);
37253727
}
37263728

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

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

Lines changed: 10 additions & 0 deletions
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)