-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSegmentReplicationTargetServiceTests.java
611 lines (545 loc) · 27.5 KB
/
SegmentReplicationTargetServiceTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.indices.replication;
import org.apache.lucene.store.AlreadyClosedException;
import org.opensearch.OpenSearchException;
import org.opensearch.Version;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.routing.RoutingTable;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.CancellableThreads;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.core.transport.TransportResponse;
import org.opensearch.index.engine.NRTReplicationEngineFactory;
import org.opensearch.index.replication.TestReplicationSource;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.index.shard.IndexShardTestCase;
import org.opensearch.index.store.StoreFileMetadata;
import org.opensearch.indices.IndicesService;
import org.opensearch.indices.recovery.ForceSyncRequest;
import org.opensearch.indices.recovery.RecoverySettings;
import org.opensearch.indices.replication.checkpoint.ReplicationCheckpoint;
import org.opensearch.indices.replication.common.CopyState;
import org.opensearch.indices.replication.common.ReplicationCollection;
import org.opensearch.indices.replication.common.ReplicationFailedException;
import org.opensearch.indices.replication.common.ReplicationLuceneIndex;
import org.opensearch.indices.replication.common.ReplicationType;
import org.opensearch.test.transport.CapturingTransport;
import org.opensearch.threadpool.TestThreadPool;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.EmptyTransportResponseHandler;
import org.opensearch.transport.TransportRequestOptions;
import org.opensearch.transport.TransportService;
import org.junit.Assert;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
public class SegmentReplicationTargetServiceTests extends IndexShardTestCase {
private IndexShard replicaShard;
private IndexShard primaryShard;
private ReplicationCheckpoint checkpoint;
private SegmentReplicationTargetService sut;
private ReplicationCheckpoint aheadCheckpoint;
private ReplicationCheckpoint newPrimaryCheckpoint;
private TransportService transportService;
private TestThreadPool testThreadPool;
private DiscoveryNode localNode;
private IndicesService indicesService;
private SegmentReplicationState state;
private ReplicationCheckpoint initialCheckpoint;
private static final long TRANSPORT_TIMEOUT = 30000;// 30sec
@Override
public void setUp() throws Exception {
super.setUp();
final Settings settings = Settings.builder()
.put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT)
.put("node.name", SegmentReplicationTargetServiceTests.class.getSimpleName())
.build();
primaryShard = newStartedShard(true, settings);
String primaryCodec = primaryShard.getLatestReplicationCheckpoint().getCodec();
replicaShard = newShard(false, settings, new NRTReplicationEngineFactory());
recoverReplica(replicaShard, primaryShard, true, getReplicationFunc(replicaShard));
checkpoint = new ReplicationCheckpoint(
replicaShard.shardId(),
0L,
0L,
0L,
replicaShard.getLatestReplicationCheckpoint().getCodec()
);
testThreadPool = new TestThreadPool("test", Settings.EMPTY);
localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
CapturingTransport transport = new CapturingTransport();
transportService = transport.createTransportService(
Settings.EMPTY,
testThreadPool,
TransportService.NOOP_TRANSPORT_INTERCEPTOR,
boundAddress -> localNode,
null,
Collections.emptySet()
);
transportService.start();
transportService.acceptIncomingRequests();
indicesService = mock(IndicesService.class);
ClusterService clusterService = mock(ClusterService.class);
ClusterState clusterState = mock(ClusterState.class);
RoutingTable mockRoutingTable = mock(RoutingTable.class);
when(clusterService.state()).thenReturn(clusterState);
when(clusterState.routingTable()).thenReturn(mockRoutingTable);
when(mockRoutingTable.shardRoutingTable(any())).thenReturn(primaryShard.getReplicationGroup().getRoutingTable());
when(clusterState.nodes()).thenReturn(DiscoveryNodes.builder().add(localNode).build());
sut = prepareForReplication(primaryShard, replicaShard, transportService, indicesService, clusterService);
initialCheckpoint = primaryShard.getLatestReplicationCheckpoint();
aheadCheckpoint = new ReplicationCheckpoint(
initialCheckpoint.getShardId(),
initialCheckpoint.getPrimaryTerm(),
initialCheckpoint.getSegmentsGen(),
initialCheckpoint.getSegmentInfosVersion() + 1,
primaryCodec
);
newPrimaryCheckpoint = new ReplicationCheckpoint(
initialCheckpoint.getShardId(),
initialCheckpoint.getPrimaryTerm() + 1,
initialCheckpoint.getSegmentsGen(),
initialCheckpoint.getSegmentInfosVersion() + 1,
primaryCodec
);
state = new SegmentReplicationState(
replicaShard.routingEntry(),
new ReplicationLuceneIndex(),
0L,
"",
new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT)
);
}
@Override
public void tearDown() throws Exception {
closeShards(primaryShard, replicaShard);
ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS);
testThreadPool = null;
super.tearDown();
}
public void testsSuccessfulReplication_listenerCompletes() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
sut.startReplication(
replicaShard,
primaryShard.getLatestReplicationCheckpoint(),
new SegmentReplicationTargetService.SegmentReplicationListener() {
@Override
public void onReplicationDone(SegmentReplicationState state) {
assertEquals(SegmentReplicationState.Stage.DONE, state.getStage());
latch.countDown();
}
@Override
public void onReplicationFailure(SegmentReplicationState state, ReplicationFailedException e, boolean sendShardFailure) {
logger.error("Unexpected error", e);
Assert.fail("Test should succeed");
}
}
);
latch.await(2, TimeUnit.SECONDS);
assertEquals(0, latch.getCount());
}
public void testReplicationFails() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
final OpenSearchException expectedError = new OpenSearchException("Fail");
SegmentReplicationSource source = new TestReplicationSource() {
@Override
public void getCheckpointMetadata(
long replicationId,
ReplicationCheckpoint checkpoint,
ActionListener<CheckpointInfoResponse> listener
) {
listener.onFailure(expectedError);
}
@Override
public void getSegmentFiles(
long replicationId,
ReplicationCheckpoint checkpoint,
List<StoreFileMetadata> filesToFetch,
IndexShard indexShard,
ActionListener<GetSegmentFilesResponse> listener
) {
Assert.fail("Should not be called");
}
};
final SegmentReplicationTarget target = new SegmentReplicationTarget(
replicaShard,
primaryShard.getLatestReplicationCheckpoint(),
source,
new SegmentReplicationTargetService.SegmentReplicationListener() {
@Override
public void onReplicationDone(SegmentReplicationState state) {
Assert.fail();
}
@Override
public void onReplicationFailure(SegmentReplicationState state, ReplicationFailedException e, boolean sendShardFailure) {
// failures leave state object in last entered stage.
assertEquals(SegmentReplicationState.Stage.GET_CHECKPOINT_INFO, state.getStage());
assertEquals(expectedError, e.getCause());
latch.countDown();
}
}
);
sut.startReplication(target);
latch.await(2, TimeUnit.SECONDS);
assertEquals(0, latch.getCount());
}
public void testAlreadyOnNewCheckpoint() {
SegmentReplicationTargetService spy = spy(sut);
spy.onNewCheckpoint(replicaShard.getLatestReplicationCheckpoint(), replicaShard);
verify(spy, times(0)).startReplication(any(), any(), any());
}
@AwaitsFix(bugUrl = "https://github.com/opensearch-project/OpenSearch/issues/8928")
public void testShardAlreadyReplicating() {
CountDownLatch blockGetCheckpointMetadata = new CountDownLatch(1);
SegmentReplicationSource source = new TestReplicationSource() {
@Override
public void getCheckpointMetadata(
long replicationId,
ReplicationCheckpoint checkpoint,
ActionListener<CheckpointInfoResponse> listener
) {
try {
blockGetCheckpointMetadata.await();
final CopyState copyState = new CopyState(
ReplicationCheckpoint.empty(primaryShard.shardId(), primaryShard.getLatestReplicationCheckpoint().getCodec()),
primaryShard
);
listener.onResponse(
new CheckpointInfoResponse(copyState.getCheckpoint(), copyState.getMetadataMap(), copyState.getInfosBytes())
);
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void getSegmentFiles(
long replicationId,
ReplicationCheckpoint checkpoint,
List<StoreFileMetadata> filesToFetch,
IndexShard indexShard,
ActionListener<GetSegmentFilesResponse> listener
) {
listener.onResponse(new GetSegmentFilesResponse(Collections.emptyList()));
}
};
final SegmentReplicationTarget target = spy(
new SegmentReplicationTarget(
replicaShard,
primaryShard.getLatestReplicationCheckpoint(),
source,
mock(SegmentReplicationTargetService.SegmentReplicationListener.class)
)
);
final SegmentReplicationTargetService spy = spy(sut);
doReturn(false).when(spy).processLatestReceivedCheckpoint(eq(replicaShard), any());
// Start first round of segment replication.
spy.startReplication(target);
// Start second round of segment replication, this should fail to start as first round is still in-progress
spy.onNewCheckpoint(newPrimaryCheckpoint, replicaShard);
verify(spy, times(1)).processLatestReceivedCheckpoint(eq(replicaShard), any());
blockGetCheckpointMetadata.countDown();
}
public void testOnNewCheckpointFromNewPrimaryCancelOngoingReplication() throws InterruptedException {
// Create a spy of Target Service so that we can verify invocation of startReplication call with specific checkpoint on it.
SegmentReplicationTargetService serviceSpy = spy(sut);
doNothing().when(serviceSpy).updateVisibleCheckpoint(anyLong(), any());
// skip post replication actions so we can assert execution counts. This will continue to process bc replica's pterm is not advanced
// post replication.
doReturn(true).when(serviceSpy).processLatestReceivedCheckpoint(any(), any());
// Create a Mockito spy of target to stub response of few method calls.
CountDownLatch latch = new CountDownLatch(1);
SegmentReplicationSource source = new TestReplicationSource() {
ActionListener<CheckpointInfoResponse> listener;
@Override
public void getCheckpointMetadata(
long replicationId,
ReplicationCheckpoint checkpoint,
ActionListener<CheckpointInfoResponse> listener
) {
// set the listener, we will only fail it once we cancel the source.
this.listener = listener;
latch.countDown();
// do not resolve this listener yet, wait for cancel to hit.
}
@Override
public void getSegmentFiles(
long replicationId,
ReplicationCheckpoint checkpoint,
List<StoreFileMetadata> filesToFetch,
IndexShard indexShard,
ActionListener<GetSegmentFilesResponse> listener
) {
Assert.fail("Unreachable");
}
@Override
public void cancel() {
// simulate listener resolving, but only after we have issued a cancel from beforeIndexShardClosed .
final RuntimeException exception = new CancellableThreads.ExecutionCancelledException("retryable action was cancelled");
listener.onFailure(exception);
}
};
final ReplicationCheckpoint updatedCheckpoint = new ReplicationCheckpoint(
initialCheckpoint.getShardId(),
initialCheckpoint.getPrimaryTerm(),
initialCheckpoint.getSegmentsGen(),
initialCheckpoint.getSegmentInfosVersion() + 1,
primaryShard.getDefaultCodecName()
);
final SegmentReplicationTarget targetSpy = spy(
new SegmentReplicationTarget(
replicaShard,
updatedCheckpoint,
source,
mock(SegmentReplicationTargetService.SegmentReplicationListener.class)
)
);
// start replication. This adds the target to on-ongoing replication collection
serviceSpy.startReplication(targetSpy);
// wait until we get to getCheckpoint step.
latch.await();
// new checkpoint arrives with higher pterm.
serviceSpy.onNewCheckpoint(newPrimaryCheckpoint, replicaShard);
// ensure the old target is cancelled. and new iteration kicks off.
verify(targetSpy, times(1)).cancel("Cancelling stuck target after new primary");
verify(serviceSpy, times(1)).startReplication(eq(replicaShard), any(), any());
}
public void testNewCheckpointBehindCurrentCheckpoint() {
SegmentReplicationTargetService spy = spy(sut);
spy.onNewCheckpoint(checkpoint, replicaShard);
verify(spy, times(0)).startReplication(any(), any(), any());
}
public void testShardNotStarted() throws IOException {
SegmentReplicationTargetService spy = spy(sut);
IndexShard shard = newShard(false);
spy.onNewCheckpoint(checkpoint, shard);
verify(spy, times(0)).startReplication(any(), any(), any());
closeShards(shard);
}
/**
* here we are starting a new shard in PrimaryMode and testing that we don't process a checkpoint on shard when it is in PrimaryMode.
*/
public void testRejectCheckpointOnShardPrimaryMode() throws IOException {
SegmentReplicationTargetService spy = spy(sut);
// Starting a new shard in PrimaryMode.
IndexShard primaryShard = newStartedShard(true);
IndexShard spyShard = spy(primaryShard);
spy.onNewCheckpoint(aheadCheckpoint, spyShard);
// Verify that checkpoint is not processed as shard is in PrimaryMode.
verify(spy, times(0)).startReplication(any(), any(), any());
closeShards(primaryShard);
}
public void testAfterIndexShardStartedDoesNothingForDocrepIndex() throws IOException {
SegmentReplicationTargetService spy = spy(sut);
final IndexShard indexShard = newStartedShard();
spy.afterIndexShardStarted(indexShard);
verify(spy, times(0)).processLatestReceivedCheckpoint(eq(replicaShard), any());
closeShards(indexShard);
}
public void testAfterIndexShardStartedProcessesLatestReceivedCheckpoint() {
sut.updateLatestReceivedCheckpoint(aheadCheckpoint, replicaShard);
SegmentReplicationTargetService spy = spy(sut);
doNothing().when(spy).onNewCheckpoint(any(), any());
spy.afterIndexShardStarted(replicaShard);
verify(spy, times(1)).processLatestReceivedCheckpoint(eq(replicaShard), any());
}
public void testStartReplicationListenerSuccess() throws InterruptedException {
sut.updateLatestReceivedCheckpoint(aheadCheckpoint, replicaShard);
SegmentReplicationTargetService spy = spy(sut);
CountDownLatch latch = new CountDownLatch(1);
doAnswer(i -> {
((SegmentReplicationTargetService.SegmentReplicationListener) i.getArgument(2)).onReplicationDone(state);
latch.countDown();
return null;
}).when(spy).startReplication(any(), any(), any());
doNothing().when(spy).updateVisibleCheckpoint(eq(0L), any());
spy.afterIndexShardStarted(replicaShard);
latch.await(2, TimeUnit.SECONDS);
verify(spy, (atLeastOnce())).updateVisibleCheckpoint(eq(0L), eq(replicaShard));
}
public void testStartReplicationListenerFailure() throws InterruptedException {
sut.updateLatestReceivedCheckpoint(aheadCheckpoint, replicaShard);
SegmentReplicationTargetService spy = spy(sut);
CountDownLatch latch = new CountDownLatch(1);
doAnswer(i -> {
((SegmentReplicationTargetService.SegmentReplicationListener) i.getArgument(2)).onReplicationFailure(
state,
new ReplicationFailedException(replicaShard, null),
false
);
latch.countDown();
return null;
}).when(spy).startReplication(any(), any(), any());
doNothing().when(spy).updateVisibleCheckpoint(eq(0L), any());
spy.afterIndexShardStarted(replicaShard);
latch.await(2, TimeUnit.SECONDS);
verify(spy, (never())).updateVisibleCheckpoint(eq(0L), eq(replicaShard));
}
public void testDoNotProcessLatestCheckpointIfItIsbehind() {
sut.updateLatestReceivedCheckpoint(replicaShard.getLatestReplicationCheckpoint(), replicaShard);
assertFalse(sut.processLatestReceivedCheckpoint(replicaShard, null));
}
public void testOnNewCheckpointInvokedOnClosedShardDoesNothing() throws IOException {
closeShards(replicaShard);
SegmentReplicationTargetService spy = spy(sut);
spy.onNewCheckpoint(aheadCheckpoint, replicaShard);
verify(spy, times(0)).updateLatestReceivedCheckpoint(any(), any());
}
public void testBeforeIndexShardClosed_DoesNothingForDocRepIndex() throws IOException {
final SegmentReplicationSourceFactory sourceFactory = mock(SegmentReplicationSourceFactory.class);
final IndicesService indicesService = mock(IndicesService.class);
final ClusterService clusterService = mock(ClusterService.class);
final ReplicationCollection<SegmentReplicationTarget> ongoingReplications = mock(ReplicationCollection.class);
final SegmentReplicationTargetService targetService = new SegmentReplicationTargetService(
threadPool,
new RecoverySettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)),
mock(TransportService.class),
sourceFactory,
indicesService,
clusterService,
ongoingReplications
);
final Settings settings = Settings.builder().put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT).build();
IndexShard shard = newStartedShard(false, settings);
targetService.beforeIndexShardClosed(shard.shardId(), shard, shard.indexSettings().getSettings());
verifyNoInteractions(ongoingReplications);
closeShards(shard);
}
public void testShardRoutingChanged_DoesNothingForDocRepIndex() throws IOException {
final SegmentReplicationSourceFactory sourceFactory = mock(SegmentReplicationSourceFactory.class);
final IndicesService indicesService = mock(IndicesService.class);
final ClusterService clusterService = mock(ClusterService.class);
final ReplicationCollection<SegmentReplicationTarget> ongoingReplications = mock(ReplicationCollection.class);
final SegmentReplicationTargetService targetService = new SegmentReplicationTargetService(
threadPool,
new RecoverySettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)),
mock(TransportService.class),
sourceFactory,
indicesService,
clusterService,
ongoingReplications
);
final Settings settings = Settings.builder().put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT).build();
IndexShard shard = newStartedShard(false, settings);
targetService.shardRoutingChanged(shard, shard.routingEntry(), primaryShard.routingEntry());
verifyNoInteractions(ongoingReplications);
closeShards(shard);
}
public void testUpdateLatestReceivedCheckpoint() {
final SegmentReplicationTargetService spy = spy(sut);
sut.updateLatestReceivedCheckpoint(checkpoint, replicaShard);
sut.updateLatestReceivedCheckpoint(aheadCheckpoint, replicaShard);
assertEquals(sut.latestReceivedCheckpoint.get(replicaShard.shardId()), aheadCheckpoint);
}
public void testForceSegmentSyncHandler() throws Exception {
ForceSyncRequest forceSyncRequest = new ForceSyncRequest(1L, 1L, replicaShard.shardId());
when(indicesService.getShardOrNull(forceSyncRequest.getShardId())).thenReturn(replicaShard);
TransportResponse response = transportService.submitRequest(
localNode,
SegmentReplicationTargetService.Actions.FORCE_SYNC,
forceSyncRequest,
TransportRequestOptions.builder().withTimeout(TRANSPORT_TIMEOUT).build(),
EmptyTransportResponseHandler.INSTANCE_SAME
).txGet();
assertEquals(TransportResponse.Empty.INSTANCE, response);
}
public void testForceSegmentSyncHandlerWithFailure() throws Exception {
allowShardFailures();
IndexShard spyReplicaShard = spy(replicaShard);
ForceSyncRequest forceSyncRequest = new ForceSyncRequest(1L, 1L, replicaShard.shardId());
when(indicesService.getShardOrNull(forceSyncRequest.getShardId())).thenReturn(spyReplicaShard);
IOException exception = new IOException("dummy failure");
doThrow(exception).when(spyReplicaShard).finalizeReplication(any());
// prevent shard failure to avoid test setup assertion
doNothing().when(spyReplicaShard).failShard(eq("replication failure"), any());
Exception finalizeException = expectThrows(Exception.class, () -> {
transportService.submitRequest(
localNode,
SegmentReplicationTargetService.Actions.FORCE_SYNC,
forceSyncRequest,
TransportRequestOptions.builder().withTimeout(TRANSPORT_TIMEOUT).build(),
EmptyTransportResponseHandler.INSTANCE_SAME
).txGet();
});
Throwable nestedException = finalizeException.getCause().getCause();
assertTrue(nestedException instanceof IOException);
assertTrue(nestedException.getMessage().contains("dummy failure"));
}
public void testForceSync_ShardDoesNotExist() {
ForceSyncRequest forceSyncRequest = new ForceSyncRequest(1L, 1L, new ShardId("no", "", 0));
when(indicesService.getShardOrNull(forceSyncRequest.getShardId())).thenReturn(null);
transportService.submitRequest(
localNode,
SegmentReplicationTargetService.Actions.FORCE_SYNC,
forceSyncRequest,
TransportRequestOptions.builder().withTimeout(TRANSPORT_TIMEOUT).build(),
EmptyTransportResponseHandler.INSTANCE_SAME
).txGet();
}
public void testForceSegmentSyncHandlerWithFailure_AlreadyClosedException_swallowed() throws Exception {
IndexShard spyReplicaShard = spy(replicaShard);
ForceSyncRequest forceSyncRequest = new ForceSyncRequest(1L, 1L, replicaShard.shardId());
when(indicesService.getShardOrNull(forceSyncRequest.getShardId())).thenReturn(spyReplicaShard);
AlreadyClosedException exception = new AlreadyClosedException("shard closed");
doThrow(exception).when(spyReplicaShard).finalizeReplication(any());
// prevent shard failure to avoid test setup assertion
doNothing().when(spyReplicaShard).failShard(eq("replication failure"), any());
transportService.submitRequest(
localNode,
SegmentReplicationTargetService.Actions.FORCE_SYNC,
forceSyncRequest,
TransportRequestOptions.builder().withTimeout(TRANSPORT_TIMEOUT).build(),
EmptyTransportResponseHandler.INSTANCE_SAME
).txGet();
}
public void testTargetCancelledBeforeStartInvoked() {
final SegmentReplicationTarget target = new SegmentReplicationTarget(
replicaShard,
primaryShard.getLatestReplicationCheckpoint(),
mock(SegmentReplicationSource.class),
new SegmentReplicationTargetService.SegmentReplicationListener() {
@Override
public void onReplicationDone(SegmentReplicationState state) {
Assert.fail();
}
@Override
public void onReplicationFailure(SegmentReplicationState state, ReplicationFailedException e, boolean sendShardFailure) {
// failures leave state object in last entered stage.
assertEquals(SegmentReplicationState.Stage.GET_CHECKPOINT_INFO, state.getStage());
assertTrue(e.getCause() instanceof CancellableThreads.ExecutionCancelledException);
}
}
);
target.cancel("test");
sut.startReplication(target);
}
}