From aa908efe075b8662a477c7423f1c9d2892784df1 Mon Sep 17 00:00:00 2001 From: Lari Hotari Date: Wed, 26 Mar 2025 15:28:16 +0200 Subject: [PATCH] [fix][ml] Return 1 when input max bytes size is 0 or negative for entry count estimation --- .../bookkeeper/mledger/impl/EntryCountEstimator.java | 4 ++-- .../bookkeeper/mledger/impl/EntryCountEstimatorTest.java | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryCountEstimator.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryCountEstimator.java index b95b2c846fa2c..511379a7ae525 100644 --- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryCountEstimator.java +++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryCountEstimator.java @@ -73,8 +73,8 @@ static int internalEstimateEntryCountByBytesSize(int maxEntries, long maxSizeByt Long lastLedgerId, long lastLedgerTotalEntries, long lastLedgerTotalSize) { if (maxSizeBytes <= 0) { - // If the specified maximum size is invalid (e.g., non-positive), return 0 - return 0; + // If the specified maximum size is invalid (e.g., non-positive), return 1 + return 1; } // If the maximum size is Long.MAX_VALUE, return the maximum number of entries diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCountEstimatorTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCountEstimatorTest.java index 2016c97f1abbc..c1c1b8dd2c133 100644 --- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCountEstimatorTest.java +++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/EntryCountEstimatorTest.java @@ -83,7 +83,13 @@ private int estimateEntryCountByBytesSize(long maxSizeBytes) { @Test public void testZeroMaxSize() { int result = estimateEntryCountByBytesSize(0); - assertEquals(result, 0, "Should return 0 when max size is 0"); + assertEquals(result, 1, "Should return 1 when max size is 0"); + } + + @Test + public void testNegativeMaxSize() { + int result = estimateEntryCountByBytesSize(-1); + assertEquals(result, 1, "Should return 1 when max size is negative"); } @Test