|
15 | 15 | import org.opensearch.cluster.metadata.IndexMetadata; |
16 | 16 | import org.opensearch.common.settings.ClusterSettings; |
17 | 17 | import org.opensearch.common.settings.Settings; |
| 18 | +import org.opensearch.common.util.CancellableThreads; |
18 | 19 | import org.opensearch.index.engine.NRTReplicationEngineFactory; |
19 | 20 | import org.opensearch.index.shard.IndexShard; |
20 | 21 | import org.opensearch.index.shard.IndexShardTestCase; |
|
29 | 30 | import java.util.concurrent.TimeUnit; |
30 | 31 |
|
31 | 32 | import static org.mockito.ArgumentMatchers.any; |
| 33 | +import static org.mockito.Mockito.doReturn; |
32 | 34 | import static org.mockito.Mockito.mock; |
33 | 35 | import static org.mockito.Mockito.when; |
34 | 36 | import static org.mockito.Mockito.doAnswer; |
|
37 | 39 | import static org.mockito.Mockito.times; |
38 | 40 | import static org.mockito.Mockito.spy; |
39 | 41 | import static org.mockito.Mockito.eq; |
| 42 | +import static org.opensearch.indices.replication.SegmentReplicationState.Stage.CANCELLED; |
40 | 43 |
|
41 | 44 | public class SegmentReplicationTargetServiceTests extends IndexShardTestCase { |
42 | 45 |
|
@@ -215,24 +218,25 @@ public void testOnNewCheckpointFromNewPrimaryCancelOngoingReplication() throws I |
215 | 218 | // Mocking response when startReplication is called on targetSpy we send a new checkpoint to serviceSpy and later reduce countdown |
216 | 219 | // of latch. |
217 | 220 | doAnswer(invocation -> { |
218 | | - final ActionListener<Void> listener = invocation.getArgument(0); |
| 221 | + // short circuit loop on new checkpoint request |
| 222 | + doReturn(null).when(serviceSpy).startReplication(eq(newPrimaryCheckpoint), eq(replicaShard), any()); |
219 | 223 | // a new checkpoint arrives before we've completed. |
220 | 224 | serviceSpy.onNewCheckpoint(newPrimaryCheckpoint, replicaShard); |
221 | | - listener.onResponse(null); |
222 | | - latch.countDown(); |
| 225 | + try { |
| 226 | + invocation.callRealMethod(); |
| 227 | + } catch (CancellableThreads.ExecutionCancelledException e) { |
| 228 | + latch.countDown(); |
| 229 | + } |
223 | 230 | return null; |
224 | 231 | }).when(targetSpy).startReplication(any()); |
225 | | - doNothing().when(targetSpy).onDone(); |
226 | 232 |
|
227 | 233 | // start replication. This adds the target to on-ongoing replication collection |
228 | 234 | serviceSpy.startReplication(targetSpy); |
229 | | - |
| 235 | + latch.await(); |
230 | 236 | // wait for the new checkpoint to arrive, before the listener completes. |
231 | | - latch.await(5, TimeUnit.SECONDS); |
232 | | - doNothing().when(targetSpy).startReplication(any()); |
| 237 | + assertEquals(CANCELLED, targetSpy.state().getStage()); |
233 | 238 | verify(targetSpy, times(1)).cancel("Cancelling stuck target after new primary"); |
234 | 239 | verify(serviceSpy, times(1)).startReplication(eq(newPrimaryCheckpoint), eq(replicaShard), any()); |
235 | | - closeShards(replicaShard); |
236 | 240 | } |
237 | 241 |
|
238 | 242 | public void testNewCheckpointBehindCurrentCheckpoint() { |
|
0 commit comments