|
19 | 19 | package org.apache.bookkeeper.mledger.impl; |
20 | 20 |
|
21 | 21 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 22 | +import static org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.ENTRIES_ADDED_COUNTER_UPDATER; |
22 | 23 | import static org.testng.Assert.assertEquals; |
23 | 24 | import static org.testng.Assert.assertFalse; |
24 | 25 | import static org.testng.Assert.assertNotEquals; |
25 | 26 | import static org.testng.Assert.assertNotNull; |
26 | 27 | import static org.testng.Assert.assertTrue; |
27 | 28 | import static org.testng.Assert.fail; |
28 | | - |
29 | 29 | import com.google.common.collect.Iterables; |
30 | | - |
| 30 | +import io.netty.buffer.ByteBuf; |
31 | 31 | import java.nio.charset.Charset; |
32 | 32 | import java.nio.charset.StandardCharsets; |
33 | 33 | import java.util.ArrayList; |
|
37 | 37 | import java.util.concurrent.TimeUnit; |
38 | 38 | import java.util.concurrent.atomic.AtomicBoolean; |
39 | 39 | import java.util.concurrent.atomic.AtomicReference; |
40 | | - |
41 | | -import io.netty.buffer.ByteBuf; |
42 | 40 | import lombok.Cleanup; |
43 | 41 | import org.apache.bookkeeper.mledger.AsyncCallbacks; |
44 | 42 | import org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback; |
|
52 | 50 | import org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig; |
53 | 51 | import org.apache.bookkeeper.mledger.Position; |
54 | 52 | import org.apache.bookkeeper.test.MockedBookKeeperTestCase; |
| 53 | +import org.apache.commons.lang3.tuple.Pair; |
| 54 | +import org.apache.pulsar.common.api.proto.CommandSubscribe; |
| 55 | +import org.awaitility.Awaitility; |
| 56 | +import org.mockito.Mockito; |
| 57 | +import org.mockito.stubbing.Answer; |
55 | 58 | import org.slf4j.Logger; |
56 | 59 | import org.slf4j.LoggerFactory; |
57 | 60 | import org.testng.Assert; |
@@ -105,6 +108,58 @@ void testOpenNonDurableCursorAtNonExistentMessageId() throws Exception { |
105 | 108 | ledger.close(); |
106 | 109 | } |
107 | 110 |
|
| 111 | + @Test(timeOut = 20000) |
| 112 | + void testOpenNonDurableCursorWhileLedgerIsAddingFirstEntryAfterTrimmed() throws Exception { |
| 113 | + ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(1) |
| 114 | + .setRetentionTime(0, TimeUnit.MILLISECONDS); |
| 115 | + config.setMinimumRolloverTime(0, TimeUnit.MILLISECONDS); |
| 116 | + @Cleanup |
| 117 | + ManagedLedgerImpl ledgerSpy = |
| 118 | + Mockito.spy((ManagedLedgerImpl) factory.open("non_durable_cursor_while_ledger_trimmed", config)); |
| 119 | + |
| 120 | + ledgerSpy.addEntry("message1".getBytes()); |
| 121 | + |
| 122 | + ledgerSpy.rollCurrentLedgerIfFull(); |
| 123 | + Awaitility.waitAtMost(5, TimeUnit.SECONDS).until(() -> |
| 124 | + ledgerSpy.getLedgersInfoAsList().size() > 1 |
| 125 | + ); |
| 126 | + CompletableFuture<Void> trimFuture = new CompletableFuture<>(); |
| 127 | + ledgerSpy.trimConsumedLedgersInBackground(trimFuture); |
| 128 | + trimFuture.join(); |
| 129 | + |
| 130 | + // Use (currentLedgerId, -1) as startCursorPosition after ledger was trimmed |
| 131 | + PositionImpl startCursorPosition = PositionImpl.get(ledgerSpy.getCurrentLedger().getId(), -1); |
| 132 | + assertTrue(startCursorPosition.compareTo(ledgerSpy.lastConfirmedEntry) > 0); |
| 133 | + |
| 134 | + CountDownLatch getLastPositionLatch = new CountDownLatch(1); |
| 135 | + CountDownLatch newNonDurableCursorLatch = new CountDownLatch(1); |
| 136 | + Mockito.when(ledgerSpy.getLastPositionAndCounter()).then((Answer<Pair<Position, Long>>) invocation -> { |
| 137 | + newNonDurableCursorLatch.countDown(); |
| 138 | + getLastPositionLatch.await(); |
| 139 | + return Pair.of(ledgerSpy.lastConfirmedEntry, ENTRIES_ADDED_COUNTER_UPDATER.get(ledgerSpy)); |
| 140 | + }); |
| 141 | + |
| 142 | + CompletableFuture<ManagedCursor> cursorFuture = new CompletableFuture<ManagedCursor>() |
| 143 | + .completeAsync(() -> |
| 144 | + new NonDurableCursorImpl(bkc, ledgerSpy, "my_test_cursor", |
| 145 | + startCursorPosition, CommandSubscribe.InitialPosition.Latest, false) |
| 146 | + ); |
| 147 | + PositionImpl oldLastConfirmedEntry = ledgerSpy.lastConfirmedEntry; |
| 148 | + |
| 149 | + // Wait until NonDurableCursorImpl constructor invokes ManagedLedgerImpl.getLastPositionAndCounter |
| 150 | + newNonDurableCursorLatch.await(); |
| 151 | + // Add first entry after ledger was trimmed |
| 152 | + ledgerSpy.addEntry("message2".getBytes()); |
| 153 | + assertTrue(oldLastConfirmedEntry.compareTo(ledgerSpy.lastConfirmedEntry) < 0); |
| 154 | + |
| 155 | + // Unblock NonDurableCursorImpl constructor |
| 156 | + getLastPositionLatch.countDown(); |
| 157 | + |
| 158 | + // cursor should read from lastConfirmedEntry |
| 159 | + ManagedCursor cursor = cursorFuture.join(); |
| 160 | + assertEquals(cursor.getReadPosition(), ledgerSpy.lastConfirmedEntry); |
| 161 | + } |
| 162 | + |
108 | 163 | @Test(timeOut = 20000) |
109 | 164 | void testZNodeBypassed() throws Exception { |
110 | 165 | ManagedLedger ledger = factory.open("my_test_ledger"); |
|
0 commit comments