48
48
49
49
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
50
50
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
51
+ import static org .hamcrest .Matchers .containsString ;
51
52
import static org .hamcrest .Matchers .equalTo ;
52
53
import static org .hamcrest .Matchers .notNullValue ;
53
54
import static org .hamcrest .Matchers .nullValue ;
@@ -245,44 +246,21 @@ public void testSyncMappings() throws Exception {
245
246
.get ("index2" ).get ("doc" );
246
247
assertThat (XContentMapValues .extractValue ("properties.f.type" , mappingMetaData .sourceAsMap ()), equalTo ("integer" ));
247
248
assertThat (XContentMapValues .extractValue ("properties.k.type" , mappingMetaData .sourceAsMap ()), equalTo ("long" ));
248
-
249
- final UnfollowIndexAction .Request unfollowRequest = new UnfollowIndexAction .Request ();
250
- unfollowRequest .setFollowIndex ("index2" );
251
- client ().execute (UnfollowIndexAction .INSTANCE , unfollowRequest ).get ();
252
-
253
- assertBusy (() -> {
254
- final ClusterState clusterState = client ().admin ().cluster ().prepareState ().get ().getState ();
255
- final PersistentTasksCustomMetaData tasks = clusterState .getMetaData ().custom (PersistentTasksCustomMetaData .TYPE );
256
- assertThat (tasks .tasks ().size (), equalTo (0 ));
257
-
258
- ListTasksRequest listTasksRequest = new ListTasksRequest ();
259
- listTasksRequest .setDetailed (true );
260
- ListTasksResponse listTasksResponse = client ().admin ().cluster ().listTasks (listTasksRequest ).get ();
261
- int numNodeTasks = 0 ;
262
- for (TaskInfo taskInfo : listTasksResponse .getTasks ()) {
263
- if (taskInfo .getAction ().startsWith (ListTasksAction .NAME ) == false ) {
264
- numNodeTasks ++;
265
- }
266
- }
267
- assertThat (numNodeTasks , equalTo (0 ));
268
- });
249
+ unfollowIndex ("index2" );
269
250
}
270
251
271
252
public void testFollowIndexWithNestedField () throws Exception {
272
253
final String leaderIndexSettings =
273
254
getIndexSettingsWithNestedMapping (1 , Collections .singletonMap (IndexSettings .INDEX_SOFT_DELETES_SETTING .getKey (), "true" ));
274
255
assertAcked (client ().admin ().indices ().prepareCreate ("index1" ).setSource (leaderIndexSettings , XContentType .JSON ));
256
+ ensureGreen ("index1" );
275
257
276
- final String followerIndexSettings =
277
- getIndexSettingsWithNestedMapping (1 , Collections .singletonMap (CcrSettings .CCR_FOLLOWING_INDEX_SETTING .getKey (), "true" ));
278
- assertAcked (client ().admin ().indices ().prepareCreate ("index2" ).setSource (followerIndexSettings , XContentType .JSON ));
279
-
280
- ensureGreen ("index1" , "index2" );
281
-
282
- final FollowIndexAction .Request followRequest = new FollowIndexAction .Request ();
258
+ FollowIndexAction .Request followRequest = new FollowIndexAction .Request ();
283
259
followRequest .setLeaderIndex ("index1" );
284
260
followRequest .setFollowIndex ("index2" );
285
- client ().execute (FollowIndexAction .INSTANCE , followRequest ).get ();
261
+ CreateAndFollowIndexAction .Request createAndFollowRequest = new CreateAndFollowIndexAction .Request ();
262
+ createAndFollowRequest .setFollowRequest (followRequest );
263
+ client ().execute (CreateAndFollowIndexAction .INSTANCE , createAndFollowRequest ).get ();
286
264
287
265
final int numDocs = randomIntBetween (2 , 64 );
288
266
for (int i = 0 ; i < numDocs ; i ++) {
@@ -311,16 +289,7 @@ public void testFollowIndexWithNestedField() throws Exception {
311
289
equalTo (Collections .singletonList (value )));
312
290
});
313
291
}
314
-
315
- final UnfollowIndexAction .Request unfollowRequest = new UnfollowIndexAction .Request ();
316
- unfollowRequest .setFollowIndex ("index2" );
317
- client ().execute (UnfollowIndexAction .INSTANCE , unfollowRequest ).get ();
318
-
319
- assertBusy (() -> {
320
- final ClusterState clusterState = client ().admin ().cluster ().prepareState ().get ().getState ();
321
- final PersistentTasksCustomMetaData tasks = clusterState .getMetaData ().custom (PersistentTasksCustomMetaData .TYPE );
322
- assertThat (tasks .tasks ().size (), equalTo (0 ));
323
- });
292
+ unfollowIndex ("index2" );
324
293
}
325
294
326
295
public void testUnfollowNonExistingIndex () {
@@ -347,6 +316,46 @@ public void testFollowNonExistentIndex() throws Exception {
347
316
expectThrows (IllegalArgumentException .class , () -> client ().execute (FollowIndexAction .INSTANCE , followRequest ).actionGet ());
348
317
}
349
318
319
+ public void testDontFollowTheWrongIndex () throws Exception {
320
+ String leaderIndexSettings = getIndexSettings (1 ,
321
+ Collections .singletonMap (IndexSettings .INDEX_SOFT_DELETES_SETTING .getKey (), "true" ));
322
+ assertAcked (client ().admin ().indices ().prepareCreate ("index1" ).setSource (leaderIndexSettings , XContentType .JSON ));
323
+ ensureGreen ("index1" );
324
+ assertAcked (client ().admin ().indices ().prepareCreate ("index3" ).setSource (leaderIndexSettings , XContentType .JSON ));
325
+ ensureGreen ("index3" );
326
+
327
+ FollowIndexAction .Request followRequest = new FollowIndexAction .Request ();
328
+ followRequest .setLeaderIndex ("index1" );
329
+ followRequest .setFollowIndex ("index2" );
330
+
331
+ CreateAndFollowIndexAction .Request createAndFollowRequest = new CreateAndFollowIndexAction .Request ();
332
+ createAndFollowRequest .setFollowRequest (followRequest );
333
+ client ().execute (CreateAndFollowIndexAction .INSTANCE , createAndFollowRequest ).get ();
334
+
335
+ followRequest = new FollowIndexAction .Request ();
336
+ followRequest .setLeaderIndex ("index3" );
337
+ followRequest .setFollowIndex ("index4" );
338
+
339
+ createAndFollowRequest = new CreateAndFollowIndexAction .Request ();
340
+ createAndFollowRequest .setFollowRequest (followRequest );
341
+ client ().execute (CreateAndFollowIndexAction .INSTANCE , createAndFollowRequest ).get ();
342
+
343
+ unfollowIndex ("index2" , "index4" );
344
+
345
+ FollowIndexAction .Request wrongRequest1 = new FollowIndexAction .Request ();
346
+ wrongRequest1 .setLeaderIndex ("index1" );
347
+ wrongRequest1 .setFollowIndex ("index4" );
348
+ Exception e = expectThrows (IllegalArgumentException .class ,
349
+ () -> client ().execute (FollowIndexAction .INSTANCE , wrongRequest1 ).actionGet ());
350
+ assertThat (e .getMessage (), containsString ("follow index [index4] should reference" ));
351
+
352
+ FollowIndexAction .Request wrongRequest2 = new FollowIndexAction .Request ();
353
+ wrongRequest2 .setLeaderIndex ("index3" );
354
+ wrongRequest2 .setFollowIndex ("index2" );
355
+ e = expectThrows (IllegalArgumentException .class , () -> client ().execute (FollowIndexAction .INSTANCE , wrongRequest2 ).actionGet ());
356
+ assertThat (e .getMessage (), containsString ("follow index [index2] should reference" ));
357
+ }
358
+
350
359
private CheckedRunnable <Exception > assertTask (final int numberOfPrimaryShards , final Map <ShardId , Long > numDocsPerShard ) {
351
360
return () -> {
352
361
final ClusterState clusterState = client ().admin ().cluster ().prepareState ().get ().getState ();
@@ -383,10 +392,12 @@ private CheckedRunnable<Exception> assertTask(final int numberOfPrimaryShards, f
383
392
};
384
393
}
385
394
386
- private void unfollowIndex (String index ) throws Exception {
387
- final UnfollowIndexAction .Request unfollowRequest = new UnfollowIndexAction .Request ();
388
- unfollowRequest .setFollowIndex (index );
389
- client ().execute (UnfollowIndexAction .INSTANCE , unfollowRequest ).get ();
395
+ private void unfollowIndex (String ... indices ) throws Exception {
396
+ for (String index : indices ) {
397
+ final UnfollowIndexAction .Request unfollowRequest = new UnfollowIndexAction .Request ();
398
+ unfollowRequest .setFollowIndex (index );
399
+ client ().execute (UnfollowIndexAction .INSTANCE , unfollowRequest ).get ();
400
+ }
390
401
assertBusy (() -> {
391
402
final ClusterState clusterState = client ().admin ().cluster ().prepareState ().get ().getState ();
392
403
final PersistentTasksCustomMetaData tasks = clusterState .getMetaData ().custom (PersistentTasksCustomMetaData .TYPE );
0 commit comments