|
18 | 18 | */
|
19 | 19 | package org.apache.bookkeeper.mledger.impl;
|
20 | 20 |
|
| 21 | +import static org.apache.bookkeeper.mledger.impl.cache.RangeEntryCacheImpl.BOOKKEEPER_READ_OVERHEAD_PER_ENTRY; |
21 | 22 | import static org.mockito.ArgumentMatchers.anyInt;
|
22 | 23 | import static org.mockito.Mockito.any;
|
23 | 24 | import static org.mockito.Mockito.doAnswer;
|
@@ -681,13 +682,15 @@ void testAsyncReadWithMaxSizeByte() throws Exception {
|
681 | 682 | ManagedCursor cursor = ledger.openCursor("c1");
|
682 | 683 |
|
683 | 684 | for (int i = 0; i < 100; i++) {
|
684 |
| - ledger.addEntry(new byte[1024]); |
| 685 | + ledger.addEntry(new byte[(int) (1024)]); |
685 | 686 | }
|
686 | 687 |
|
687 |
| - // First time, since we don't have info, we'll get 1 single entry |
688 |
| - readAndCheck(cursor, 10, 3 * 1024, 1); |
| 688 | + // Since https://github.com/apache/pulsar/pull/23931 improved the performance of delivery, the consumer |
| 689 | + // will get more messages than before(it only receives 1 messages at the first delivery), |
| 690 | + int avg = (int) (BOOKKEEPER_READ_OVERHEAD_PER_ENTRY + 1024); |
| 691 | + readAndCheck(cursor, 10, 3 * avg, 3); |
689 | 692 | // We should only return 3 entries, based on the max size
|
690 |
| - readAndCheck(cursor, 20, 3 * 1024, 3); |
| 693 | + readAndCheck(cursor, 20, 3 * avg, 3); |
691 | 694 | // If maxSize is < avg, we should get 1 entry
|
692 | 695 | readAndCheck(cursor, 10, 500, 1);
|
693 | 696 | }
|
@@ -3885,13 +3888,15 @@ public void testReadEntriesOrWaitWithMaxSize() throws Exception {
|
3885 | 3888 | ledger.addEntry(new byte[1024]);
|
3886 | 3889 | }
|
3887 | 3890 |
|
3888 |
| - // First time, since we don't have info, we'll get 1 single entry |
3889 |
| - List<Entry> entries = c.readEntriesOrWait(10, 3 * 1024); |
3890 |
| - assertEquals(entries.size(), 1); |
| 3891 | + // Since https://github.com/apache/pulsar/pull/23931 improved the performance of delivery, the consumer |
| 3892 | + // will get more messages than before(it only receives 1 messages at the first delivery), |
| 3893 | + int avg = (int) (1024 + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY); |
| 3894 | + List<Entry> entries = c.readEntriesOrWait(10, 3 * avg); |
| 3895 | + assertEquals(entries.size(), 3); |
3891 | 3896 | entries.forEach(Entry::release);
|
3892 | 3897 |
|
3893 | 3898 | // We should only return 3 entries, based on the max size
|
3894 |
| - entries = c.readEntriesOrWait(10, 3 * 1024); |
| 3899 | + entries = c.readEntriesOrWait(10, 3 * avg); |
3895 | 3900 | assertEquals(entries.size(), 3);
|
3896 | 3901 | entries.forEach(Entry::release);
|
3897 | 3902 |
|
@@ -4798,5 +4803,82 @@ public void operationFailed(ManagedLedgerException exception) {
|
4798 | 4803 | assertEquals(cursor.getReadPosition(), markDeletedPosition.getNext());
|
4799 | 4804 | }
|
4800 | 4805 |
|
| 4806 | + @Test |
| 4807 | + public void testEstimateEntryCountBySize() throws Exception { |
| 4808 | + final String mlName = "ml-" + UUID.randomUUID().toString().replaceAll("-", ""); |
| 4809 | + ManagedLedgerImpl ml = (ManagedLedgerImpl) factory.open(mlName); |
| 4810 | + long entryCount0 = |
| 4811 | + ManagedCursorImpl.estimateEntryCountBySize(16, PositionImpl.get(ml.getCurrentLedger().getId(), 0), ml); |
| 4812 | + assertEquals(entryCount0, 1); |
| 4813 | + // Avoid trimming ledgers. |
| 4814 | + ml.openCursor("c1"); |
| 4815 | + |
| 4816 | + // Build data. |
| 4817 | + for (int i = 0; i < 100; i++) { |
| 4818 | + ml.addEntry(new byte[]{1}); |
| 4819 | + } |
| 4820 | + long ledger1 = ml.getCurrentLedger().getId(); |
| 4821 | + ml.getCurrentLedger().close(); |
| 4822 | + ml.ledgerClosed(ml.getCurrentLedger()); |
| 4823 | + for (int i = 0; i < 100; i++) { |
| 4824 | + ml.addEntry(new byte[]{1, 2}); |
| 4825 | + } |
| 4826 | + long ledger2 = ml.getCurrentLedger().getId(); |
| 4827 | + ml.getCurrentLedger().close(); |
| 4828 | + ml.ledgerClosed(ml.getCurrentLedger()); |
| 4829 | + for (int i = 0; i < 100; i++) { |
| 4830 | + ml.addEntry(new byte[]{1, 2, 3, 4}); |
| 4831 | + } |
| 4832 | + long ledger3 = ml.getCurrentLedger().getId(); |
| 4833 | + MLDataFormats.ManagedLedgerInfo.LedgerInfo ledgerInfo1 = ml.getLedgersInfo().get(ledger1); |
| 4834 | + MLDataFormats.ManagedLedgerInfo.LedgerInfo ledgerInfo2 = ml.getLedgersInfo().get(ledger2); |
| 4835 | + long average1 = ledgerInfo1.getSize() / ledgerInfo1.getEntries() + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY; |
| 4836 | + long average2 = ledgerInfo2.getSize() / ledgerInfo2.getEntries() + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY; |
| 4837 | + long average3 = ml.getCurrentLedgerSize() / ml.getCurrentLedgerEntries() + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY; |
| 4838 | + assertEquals(average1, 1 + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY); |
| 4839 | + assertEquals(average2, 2 + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY); |
| 4840 | + assertEquals(average3, 4 + BOOKKEEPER_READ_OVERHEAD_PER_ENTRY); |
| 4841 | + |
| 4842 | + // Test: the individual ledgers. |
| 4843 | + long entryCount1 = |
| 4844 | + ManagedCursorImpl.estimateEntryCountBySize(average1 * 16, PositionImpl.get(ledger1, 0), ml); |
| 4845 | + assertEquals(entryCount1, 16); |
| 4846 | + long entryCount2 = |
| 4847 | + ManagedCursorImpl.estimateEntryCountBySize(average2 * 8, PositionImpl.get(ledger2, 0), ml); |
| 4848 | + assertEquals(entryCount2, 8); |
| 4849 | + long entryCount3 = |
| 4850 | + ManagedCursorImpl.estimateEntryCountBySize(average3 * 4, PositionImpl.get(ledger3, 0), ml); |
| 4851 | + assertEquals(entryCount3, 4); |
| 4852 | + |
| 4853 | + // Test: across ledgers. |
| 4854 | + long entryCount4 = |
| 4855 | + ManagedCursorImpl.estimateEntryCountBySize((average1 * 100) + (average2 * 8), PositionImpl.get(ledger1, 0), ml); |
| 4856 | + assertEquals(entryCount4, 108); |
| 4857 | + long entryCount5 = |
| 4858 | + ManagedCursorImpl.estimateEntryCountBySize((average2 * 100) + (average3 * 4), PositionImpl.get(ledger2, 0), ml); |
| 4859 | + assertEquals(entryCount5, 104); |
| 4860 | + long entryCount6 = |
| 4861 | + ManagedCursorImpl.estimateEntryCountBySize((average1 * 100) + (average2 * 100) + (average3 * 4), PositionImpl.get(ledger1, 0), ml); |
| 4862 | + assertEquals(entryCount6, 204); |
| 4863 | + |
| 4864 | + long entryCount7 = |
| 4865 | + ManagedCursorImpl.estimateEntryCountBySize((average1 * 20) + (average2 * 8), PositionImpl.get(ledger1, 80), ml); |
| 4866 | + assertEquals(entryCount7, 28); |
| 4867 | + long entryCount8 = |
| 4868 | + ManagedCursorImpl.estimateEntryCountBySize((average2 * 20) + (average3 * 4), PositionImpl.get(ledger2, 80), ml); |
| 4869 | + assertEquals(entryCount8, 24); |
| 4870 | + long entryCount9 = |
| 4871 | + ManagedCursorImpl.estimateEntryCountBySize((average1 * 20) + (average2 * 100) + (average3 * 4), PositionImpl.get(ledger1, 80), ml); |
| 4872 | + assertEquals(entryCount9, 124); |
| 4873 | + |
| 4874 | + // Test: read more than entries written. |
| 4875 | + long entryCount10 = |
| 4876 | + ManagedCursorImpl.estimateEntryCountBySize((average1 * 100) + (average2 * 100) + (average3 * 100) + (average3 * 4) , PositionImpl.get(ledger1, 0), ml); |
| 4877 | + assertEquals(entryCount10, 304); |
| 4878 | + |
| 4879 | + // cleanup. |
| 4880 | + ml.delete(); |
| 4881 | + } |
| 4882 | + |
4801 | 4883 | private static final Logger log = LoggerFactory.getLogger(ManagedCursorTest.class);
|
4802 | 4884 | }
|
0 commit comments