|
21 | 21 | import static java.nio.charset.StandardCharsets.UTF_8; |
22 | 22 | import static org.mockito.ArgumentMatchers.any; |
23 | 23 | import static org.mockito.ArgumentMatchers.anyInt; |
| 24 | +import static org.mockito.ArgumentMatchers.anyMap; |
24 | 25 | import static org.mockito.ArgumentMatchers.anyString; |
| 26 | +import static org.mockito.ArgumentMatchers.eq; |
25 | 27 | import static org.mockito.Mockito.atLeast; |
26 | 28 | import static org.mockito.Mockito.doAnswer; |
27 | 29 | import static org.mockito.Mockito.doNothing; |
|
55 | 57 | import java.util.HashMap; |
56 | 58 | import java.util.HashSet; |
57 | 59 | import java.util.Iterator; |
| 60 | +import java.util.LinkedList; |
58 | 61 | import java.util.List; |
59 | 62 | import java.util.Map; |
60 | 63 | import java.util.NavigableMap; |
61 | 64 | import java.util.Optional; |
| 65 | +import java.util.Queue; |
62 | 66 | import java.util.Set; |
63 | 67 | import java.util.UUID; |
64 | 68 | import java.util.concurrent.BlockingQueue; |
@@ -4151,4 +4155,55 @@ private long calculatePendingTaskCount(OrderedScheduler orderedScheduler) { |
4151 | 4155 | } |
4152 | 4156 | return taskCounter; |
4153 | 4157 | } |
| 4158 | + |
| 4159 | + @Test |
| 4160 | + public void testNoCleanupOffloadLedgerWhenMetadataExceptionHappens() throws Exception { |
| 4161 | + ManagedLedgerConfig config = spy(new ManagedLedgerConfig()); |
| 4162 | + ManagedLedgerImpl ml = spy((ManagedLedgerImpl) factory.open("testNoCleanupOffloadLedger", config)); |
| 4163 | + |
| 4164 | + // mock the ledger offloader |
| 4165 | + LedgerOffloader ledgerOffloader = mock(NullLedgerOffloader.class); |
| 4166 | + when(config.getLedgerOffloader()).thenReturn(ledgerOffloader); |
| 4167 | + when(ledgerOffloader.getOffloadDriverName()).thenReturn("mock"); |
| 4168 | + |
| 4169 | + // There will have two put call to the metadata store, the first time is prepare the offload. |
| 4170 | + // And the second is the complete the offload. This case is testing when completing the offload, |
| 4171 | + // the metadata store meets an exception. |
| 4172 | + AtomicInteger metadataPutCallCount = new AtomicInteger(0); |
| 4173 | + metadataStore.failConditional(new MetadataStoreException("mock completion error"), |
| 4174 | + (key, value) -> key.equals(FaultInjectionMetadataStore.OperationType.PUT) && |
| 4175 | + metadataPutCallCount.incrementAndGet() == 2); |
| 4176 | + |
| 4177 | + // prepare the arguments for the offloadLoop method |
| 4178 | + CompletableFuture<PositionImpl> future = new CompletableFuture<>(); |
| 4179 | + Queue<LedgerInfo> ledgersToOffload = new LinkedList<>(); |
| 4180 | + LedgerInfo ledgerInfo = LedgerInfo.getDefaultInstance().toBuilder().setLedgerId(1).setEntries(10).build(); |
| 4181 | + ledgersToOffload.add(ledgerInfo); |
| 4182 | + PositionImpl firstUnoffloaded = new PositionImpl(1, 0); |
| 4183 | + Optional<Throwable> firstError = Optional.empty(); |
| 4184 | + |
| 4185 | + // mock the read handle to make the offload successful |
| 4186 | + CompletableFuture<ReadHandle> readHandle = new CompletableFuture<>(); |
| 4187 | + readHandle.complete(mock(ReadHandle.class)); |
| 4188 | + when(ml.getLedgerHandle(eq(ledgerInfo.getLedgerId()))).thenReturn(readHandle); |
| 4189 | + when(ledgerOffloader.offload(any(), any(), anyMap())).thenReturn(CompletableFuture.completedFuture(null)); |
| 4190 | + |
| 4191 | + ml.ledgers.put(ledgerInfo.getLedgerId(), ledgerInfo); |
| 4192 | + |
| 4193 | + // do the offload |
| 4194 | + ml.offloadLoop(future, ledgersToOffload, firstUnoffloaded, firstError); |
| 4195 | + |
| 4196 | + // waiting for the offload complete |
| 4197 | + try { |
| 4198 | + future.join(); |
| 4199 | + fail("The offload should fail"); |
| 4200 | + } catch (Exception e) { |
| 4201 | + // the offload should fail |
| 4202 | + assertTrue(e.getCause().getMessage().contains("mock completion error")); |
| 4203 | + } |
| 4204 | + |
| 4205 | + // the ledger deletion shouldn't happen |
| 4206 | + verify(ledgerOffloader, times(0)) |
| 4207 | + .deleteOffloaded(eq(ledgerInfo.getLedgerId()), any(), anyMap()); |
| 4208 | + } |
4154 | 4209 | } |
0 commit comments