Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
poorbarcode committed Feb 6, 2025
1 parent f75a0c9 commit 5be6980
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3815,7 +3815,7 @@ public int applyMaxSizeCap(int maxEntries, long maxSizeBytes) {
return Math.min(maxEntriesBasedOnSize, maxEntries);
}

private static long estimateEntryCountBySize(long bytesSize, Position readPosition, ManagedLedgerImpl ml) {
static long estimateEntryCountBySize(long bytesSize, Position readPosition, ManagedLedgerImpl ml) {
Position posToRead = readPosition;
if (!ml.isValidPosition(readPosition)) {
posToRead = ml.getNextValidPosition(readPosition);
Expand All @@ -3825,12 +3825,12 @@ private static long estimateEntryCountBySize(long bytesSize, Position readPositi

while (remainingBytesSize > 0) {
// Last ledger.
if (posToRead.getLedgerId() == ml.currentLedger.getId()) {
if (ml.currentLedgerSize == 0 || ml.currentLedgerEntries == 0) {
if (posToRead.getLedgerId() == ml.getCurrentLedger().getId()) {
if (ml.getCurrentLedgerSize() == 0 || ml.getCurrentLedgerEntries() == 0) {
// Only read 1 entry if no entries to read.
return 1;
}
long avg = Math.max(1, ml.currentLedgerSize / ml.currentLedgerEntries);
long avg = Math.max(1, ml.getCurrentLedgerSize() / ml.getCurrentLedgerEntries());
result += remainingBytesSize / avg;
break;
}
Expand All @@ -3842,14 +3842,14 @@ private static long estimateEntryCountBySize(long bytesSize, Position readPositi
}
// Calculate entries by average of ledgers.
long avg = Math.max(1, ledgerInfo.getSize() / ledgerInfo.getEntries());
long remainEntriesOfLedger = ledgerInfo.getEntries() - posToRead.getEntryId() + 1;
long remainEntriesOfLedger = ledgerInfo.getEntries() - posToRead.getEntryId();
if (remainEntriesOfLedger * avg >= remainingBytesSize) {
result += remainingBytesSize / avg;
break;
} else {
// Calculate for the next ledger.
result += remainEntriesOfLedger / avg;
remainingBytesSize -= remainEntriesOfLedger;
result += remainEntriesOfLedger;
remainingBytesSize -= remainEntriesOfLedger * avg;
posToRead = ml.getNextValidPosition(PositionFactory.create(posToRead.getLedgerId(), Long.MAX_VALUE));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5169,8 +5169,9 @@ public void testEstimateEntryCountBySize() throws Exception {
final String mlName = "ml-" + UUID.randomUUID().toString().replaceAll("-", "");
ManagedLedgerImpl ml = (ManagedLedgerImpl) factory.open(mlName);
// Verify: no entry to read

long entryCount0 =
ManagedCursorImpl.estimateEntryCountBySize(16, PositionFactory.create(ml.currentLedger.getId(), 0), ml);
ManagedCursorImpl.estimateEntryCountBySize(16, PositionFactory.create(ml.getCurrentLedger().getId(), 0), ml);
assertEquals(entryCount0, 1);
// Avoid trimming ledgers.
ml.openCursor("c1");
Expand All @@ -5179,19 +5180,19 @@ public void testEstimateEntryCountBySize() throws Exception {
for (int i = 0; i < 100; i++) {
ml.addEntry(new byte[]{1});
}
long ledger1 = ml.currentLedger.getId();
ml.currentLedger.close();
ml.ledgerClosed(ml.currentLedger);
long ledger1 = ml.getCurrentLedger().getId();
ml.getCurrentLedger().close();
ml.ledgerClosed(ml.getCurrentLedger());
for (int i = 0; i < 100; i++) {
ml.addEntry(new byte[]{1, 2});
}
long ledger2 = ml.currentLedger.getId();
ml.currentLedger.close();
ml.ledgerClosed(ml.currentLedger);
long ledger2 = ml.getCurrentLedger().getId();
ml.getCurrentLedger().close();
ml.ledgerClosed(ml.getCurrentLedger());
for (int i = 0; i < 100; i++) {
ml.addEntry(new byte[]{1, 2, 3, 4});
}
long ledger3 = ml.currentLedger.getId();
long ledger3 = ml.getCurrentLedger().getId();
MLDataFormats.ManagedLedgerInfo.LedgerInfo ledgerInfo1 = ml.getLedgersInfo().get(ledger1);
MLDataFormats.ManagedLedgerInfo.LedgerInfo ledgerInfo2 = ml.getLedgersInfo().get(ledger2);
long average1 = ledgerInfo1.getSize() / ledgerInfo1.getEntries();
Expand Down Expand Up @@ -5233,7 +5234,6 @@ public void testEstimateEntryCountBySize() throws Exception {
ManagedCursorImpl.estimateEntryCountBySize(236, PositionFactory.create(ledger1, 80), ml);
assertEquals(entryCount9, 124);


// cleanup.
ml.delete();
}
Expand Down

0 comments on commit 5be6980

Please sign in to comment.