Skip to content

Commit 9864c86

Browse files
committed
[CCR] Add metadata to keep track of the index uuid of the leader index in the follow index (#33367)
The follow index api checks if the recorded uuid in the follow index matches with uuid of the leader index and fails otherwise. This validation will prevent a follow index from following an incompatible leader index. The create_and_follow api will automatically add this custom index metadata when it creates the follow index. Closes #31505
1 parent 7791c89 commit 9864c86

File tree

5 files changed

+89
-52
lines changed

5 files changed

+89
-52
lines changed

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/Ccr.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class Ccr extends Plugin implements ActionPlugin, PersistentTaskPlugin, E
8888
public static final String CCR_THREAD_POOL_NAME = "ccr";
8989
public static final String CCR_CUSTOM_METADATA_KEY = "ccr";
9090
public static final String CCR_CUSTOM_METADATA_LEADER_INDEX_SHARD_HISTORY_UUIDS = "leader_index_shard_history_uuids";
91+
public static final String CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY = "leader_index_uuid";
9192

9293
private final boolean enabled;
9394
private final Settings settings;

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportCreateAndFollowIndexAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public ClusterState execute(final ClusterState currentState) throws Exception {
183183
// Adding the leader index uuid for each shard as custom metadata:
184184
Map<String, String> metadata = new HashMap<>();
185185
metadata.put(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_SHARD_HISTORY_UUIDS, String.join(",", historyUUIDs));
186+
metadata.put(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY, leaderIndexMetaData.getIndexUUID());
186187
imdBuilder.putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, metadata);
187188

188189
// Copy all settings, but overwrite a few settings.

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportFollowIndexAction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ static void validate(
251251
if (followIndex == null) {
252252
throw new IllegalArgumentException("follow index [" + request.getFollowerIndex() + "] does not exist");
253253
}
254+
String leaderIndexUUID = leaderIndex.getIndex().getUUID();
255+
String recordedLeaderIndexUUID = followIndex
256+
.getCustomData(Ccr.CCR_CUSTOM_METADATA_KEY)
257+
.get(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY);
258+
if (leaderIndexUUID.equals(recordedLeaderIndexUUID) == false) {
259+
throw new IllegalArgumentException("follow index [" + request.getFollowerIndex() + "] should reference [" + leaderIndexUUID +
260+
"] as leader index but instead reference [" + recordedLeaderIndexUUID + "] as leader index");
261+
}
254262

255263
String[] recordedHistoryUUIDs = extractIndexShardHistoryUUIDs(followIndex);
256264
assert recordedHistoryUUIDs.length == leaderIndexHistoryUUID.length;

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/ShardChangesIT.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.elasticsearch.action.index.IndexRequest;
1818
import org.elasticsearch.action.search.SearchRequest;
1919
import org.elasticsearch.action.search.SearchResponse;
20-
import org.elasticsearch.action.support.ActiveShardCount;
2120
import org.elasticsearch.analysis.common.CommonAnalysisPlugin;
2221
import org.elasticsearch.cluster.ClusterState;
2322
import org.elasticsearch.cluster.metadata.MappingMetaData;
@@ -313,10 +312,7 @@ public void testFollowIndexAndCloseNode() throws Exception {
313312
internalCluster().ensureAtLeastNumDataNodes(3);
314313
String leaderIndexSettings = getIndexSettings(3, 1, singletonMap(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "true"));
315314
assertAcked(client().admin().indices().prepareCreate("index1").setSource(leaderIndexSettings, XContentType.JSON));
316-
317-
String followerIndexSettings = getIndexSettings(3, 1, singletonMap(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), "true"));
318-
assertAcked(client().admin().indices().prepareCreate("index2").setSource(followerIndexSettings, XContentType.JSON));
319-
ensureGreen("index1", "index2");
315+
ensureGreen("index1");
320316

321317
AtomicBoolean run = new AtomicBoolean(true);
322318
Thread thread = new Thread(() -> {
@@ -338,7 +334,7 @@ public void testFollowIndexAndCloseNode() throws Exception {
338334
final FollowIndexAction.Request followRequest = new FollowIndexAction.Request("index1", "index2", randomIntBetween(32, 2048),
339335
randomIntBetween(2, 10), Long.MAX_VALUE, randomIntBetween(2, 10),
340336
FollowIndexAction.DEFAULT_MAX_WRITE_BUFFER_SIZE, TimeValue.timeValueMillis(500), TimeValue.timeValueMillis(10));
341-
client().execute(FollowIndexAction.INSTANCE, followRequest).get();
337+
client().execute(CreateAndFollowIndexAction.INSTANCE, new CreateAndFollowIndexAction.Request(followRequest)).get();
342338

343339
long maxNumDocsReplicated = Math.min(1000, randomLongBetween(followRequest.getMaxBatchOperationCount(),
344340
followRequest.getMaxBatchOperationCount() * 10));
@@ -415,33 +411,6 @@ public void testFollowNonExistentIndex() throws Exception {
415411
expectThrows(IndexNotFoundException.class, () -> client().execute(FollowIndexAction.INSTANCE, followRequest3).actionGet());
416412
}
417413

418-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/33379")
419-
public void testValidateFollowingIndexSettings() throws Exception {
420-
assertAcked(client().admin().indices().prepareCreate("test-leader")
421-
.setSettings(Settings.builder().put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true)));
422-
// TODO: indexing should be optional but the current mapping logic requires for now.
423-
client().prepareIndex("test-leader", "doc", "id").setSource("{\"f\": \"v\"}", XContentType.JSON).get();
424-
assertAcked(client().admin().indices().prepareCreate("test-follower").get());
425-
IllegalArgumentException followError = expectThrows(IllegalArgumentException.class, () -> client().execute(
426-
FollowIndexAction.INSTANCE, createFollowRequest("test-leader", "test-follower")).actionGet());
427-
assertThat(followError.getMessage(), equalTo("the following index [test-follower] is not ready to follow;" +
428-
" the setting [index.xpack.ccr.following_index] must be enabled."));
429-
// updating the `following_index` with an open index must not be allowed.
430-
IllegalArgumentException updateError = expectThrows(IllegalArgumentException.class, () -> {
431-
client().admin().indices().prepareUpdateSettings("test-follower")
432-
.setSettings(Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true)).get();
433-
});
434-
assertThat(updateError.getMessage(), containsString("Can't update non dynamic settings " +
435-
"[[index.xpack.ccr.following_index]] for open indices [[test-follower/"));
436-
assertAcked(client().admin().indices().prepareClose("test-follower"));
437-
assertAcked(client().admin().indices().prepareUpdateSettings("test-follower")
438-
.setSettings(Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true)));
439-
assertAcked(client().admin().indices().prepareOpen("test-follower").setWaitForActiveShards(ActiveShardCount.DEFAULT));
440-
assertAcked(client().execute(FollowIndexAction.INSTANCE,
441-
createFollowRequest("test-leader", "test-follower")).actionGet());
442-
unfollowIndex("test-follower");
443-
}
444-
445414
public void testFollowIndex_lowMaxTranslogBytes() throws Exception {
446415
final String leaderIndexSettings = getIndexSettings(1, between(0, 1),
447416
singletonMap(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "true"));
@@ -476,6 +445,37 @@ public void testFollowIndex_lowMaxTranslogBytes() throws Exception {
476445
unfollowIndex("index2");
477446
}
478447

448+
public void testDontFollowTheWrongIndex() throws Exception {
449+
String leaderIndexSettings = getIndexSettings(1, 0,
450+
Collections.singletonMap(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "true"));
451+
assertAcked(client().admin().indices().prepareCreate("index1").setSource(leaderIndexSettings, XContentType.JSON));
452+
ensureGreen("index1");
453+
assertAcked(client().admin().indices().prepareCreate("index3").setSource(leaderIndexSettings, XContentType.JSON));
454+
ensureGreen("index3");
455+
456+
FollowIndexAction.Request followRequest = new FollowIndexAction.Request("index1", "index2", 1024, 1, 1024L,
457+
1, 10240, TimeValue.timeValueMillis(500), TimeValue.timeValueMillis(10));
458+
CreateAndFollowIndexAction.Request createAndFollowRequest = new CreateAndFollowIndexAction.Request(followRequest);
459+
client().execute(CreateAndFollowIndexAction.INSTANCE, createAndFollowRequest).get();
460+
461+
followRequest = new FollowIndexAction.Request("index3", "index4", 1024, 1, 1024L,
462+
1, 10240, TimeValue.timeValueMillis(500), TimeValue.timeValueMillis(10));
463+
createAndFollowRequest = new CreateAndFollowIndexAction.Request(followRequest);
464+
client().execute(CreateAndFollowIndexAction.INSTANCE, createAndFollowRequest).get();
465+
unfollowIndex("index2", "index4");
466+
467+
FollowIndexAction.Request wrongRequest1 = new FollowIndexAction.Request("index1", "index4", 1024, 1, 1024L,
468+
1, 10240, TimeValue.timeValueMillis(500), TimeValue.timeValueMillis(10));
469+
Exception e = expectThrows(IllegalArgumentException.class,
470+
() -> client().execute(FollowIndexAction.INSTANCE, wrongRequest1).actionGet());
471+
assertThat(e.getMessage(), containsString("follow index [index4] should reference"));
472+
473+
FollowIndexAction.Request wrongRequest2 = new FollowIndexAction.Request("index3", "index2", 1024, 1, 1024L,
474+
1, 10240, TimeValue.timeValueMillis(500), TimeValue.timeValueMillis(10));
475+
e = expectThrows(IllegalArgumentException.class, () -> client().execute(FollowIndexAction.INSTANCE, wrongRequest2).actionGet());
476+
assertThat(e.getMessage(), containsString("follow index [index2] should reference"));
477+
}
478+
479479
private CheckedRunnable<Exception> assertTask(final int numberOfPrimaryShards, final Map<ShardId, Long> numDocsPerShard) {
480480
return () -> {
481481
final ClusterState clusterState = client().admin().cluster().prepareState().get().getState();
@@ -512,10 +512,12 @@ private CheckedRunnable<Exception> assertTask(final int numberOfPrimaryShards, f
512512
};
513513
}
514514

515-
private void unfollowIndex(String index) throws Exception {
516-
final UnfollowIndexAction.Request unfollowRequest = new UnfollowIndexAction.Request();
517-
unfollowRequest.setFollowIndex(index);
518-
client().execute(UnfollowIndexAction.INSTANCE, unfollowRequest).get();
515+
private void unfollowIndex(String... indices) throws Exception {
516+
for (String index : indices) {
517+
final UnfollowIndexAction.Request unfollowRequest = new UnfollowIndexAction.Request();
518+
unfollowRequest.setFollowIndex(index);
519+
client().execute(UnfollowIndexAction.INSTANCE, unfollowRequest).get();
520+
}
519521
assertBusy(() -> {
520522
final ClusterState clusterState = client().admin().cluster().prepareState().get().getState();
521523
final PersistentTasksCustomMetaData tasks = clusterState.getMetaData().custom(PersistentTasksCustomMetaData.TYPE);

0 commit comments

Comments
 (0)