|
19 | 19 | package org.apache.bookkeeper.mledger.impl;
|
20 | 20 |
|
21 | 21 | import static org.apache.bookkeeper.mledger.impl.cache.RangeEntryCacheImpl.BOOKKEEPER_READ_OVERHEAD_PER_ENTRY;
|
22 |
| -import static org.apache.bookkeeper.mledger.impl.cache.RangeEntryCacheImpl.DEFAULT_ESTIMATED_ENTRY_SIZE; |
23 | 22 | import static org.testng.Assert.assertEquals;
|
24 | 23 | import java.util.HashSet;
|
25 | 24 | import java.util.NavigableMap;
|
|
31 | 30 | import org.testng.annotations.Test;
|
32 | 31 |
|
33 | 32 | public class EntryCountEstimatorTest {
|
34 |
| - |
35 | 33 | private NavigableMap<Long, MLDataFormats.ManagedLedgerInfo.LedgerInfo> ledgersInfo;
|
36 | 34 | private Position readPosition;
|
37 | 35 | private Long lastLedgerId;
|
@@ -202,11 +200,39 @@ public void testWithOnlyLastLedgerWhichIsEmpty() {
|
202 | 200 | beforeLastKey.forEach(ledgersInfo::remove);
|
203 | 201 | lastLedgerTotalEntries = 0;
|
204 | 202 | lastLedgerTotalSize = 0;
|
| 203 | + int result = estimateEntryCountByBytesSize(Integer.MAX_VALUE); |
| 204 | + // expect that result is 1 because the estimation couldn't be done |
| 205 | + assertEquals(result, 1); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void testWithOnlySecondLastLedgerAndEmptyLastLedger() { |
| 210 | + readPosition = PositionImpl.LATEST; |
| 211 | + // remove all but the second last and last ledger |
| 212 | + long secondLastLedgerId = ledgersInfo.lowerKey(lastLedgerId); |
| 213 | + Set<Long> beforeSecondLastKey = new HashSet<>(ledgersInfo.headMap(secondLastLedgerId).keySet()); |
| 214 | + beforeSecondLastKey.forEach(ledgersInfo::remove); |
| 215 | + lastLedgerTotalEntries = 0; |
| 216 | + lastLedgerTotalSize = 0; |
| 217 | + long expectedEntries = 50; |
| 218 | + long requiredSize = |
| 219 | + expectedEntries * (2000 / 150 + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY); |
| 220 | + int result = estimateEntryCountByBytesSize(requiredSize); |
| 221 | + assertEquals(result, expectedEntries); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + public void testWithMultipleEmptyLedgers() { |
| 226 | + readPosition = PositionImpl.LATEST; |
| 227 | + long secondLastLedgerId = ledgersInfo.lowerKey(lastLedgerId); |
| 228 | + MLDataFormats.ManagedLedgerInfo.LedgerInfo secondLastLedgerInfo = ledgersInfo.get(secondLastLedgerId); |
| 229 | + // make the second last ledger empty |
| 230 | + ledgersInfo.put(secondLastLedgerId, secondLastLedgerInfo.toBuilder().setEntries(0).setSize(0).build()); |
| 231 | + lastLedgerTotalEntries = 0; |
| 232 | + lastLedgerTotalSize = 0; |
205 | 233 | long expectedEntries = 50;
|
206 |
| - // when last is empty, DEFAULT_ESTIMATED_ENTRY_SIZE + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY is used |
207 |
| - // for the average size per entry |
208 | 234 | long requiredSize =
|
209 |
| - expectedEntries * (DEFAULT_ESTIMATED_ENTRY_SIZE + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY); |
| 235 | + expectedEntries * (3000 / 200 + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY); |
210 | 236 | int result = estimateEntryCountByBytesSize(requiredSize);
|
211 | 237 | assertEquals(result, expectedEntries);
|
212 | 238 | }
|
|
0 commit comments