From d03a69cbeac0569330ddce5aad5970c148b90482 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Mon, 28 Jun 2021 10:04:22 +0200 Subject: [PATCH] Rework AutoFollowIT.testAutoFollowSearchableSnapshotsFails (#74498) The test AutoFollowIT.testAutoFollowSearchableSnapshotsFails was added in #70580 in order to test that mounted indices of a leader cluster are not auto-followed in a follower cluster using CCR. This test sometimes fails because it expects 2 indices to be followed (the -regular and the -index indices) but not the mounted one. This looks wrong as the -index index is deleted soon after it is snapshotted, and this index only exist to create a snapshot that can be later mounted as an index in the leader cluster. This commit changes the test so that the -index index, the repository and the snapshot are created at the beginning of the test. Then the test creates the mounted index and the regular one and can now asserts that only the regular one was auto-followed. Closes #74486 --- .../elasticsearch/xpack/ccr/AutoFollowIT.java | 89 ++++++++++--------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java index 255fe889b9895..33c6a65924e9e 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java @@ -14,7 +14,6 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; @@ -22,6 +21,7 @@ import org.elasticsearch.common.xcontent.ObjectPath; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.rest.RestStatus; import java.io.IOException; @@ -36,7 +36,7 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.emptyOrNullString; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasSize; @@ -774,38 +774,20 @@ public void testAutoFollowSearchableSnapshotsFails() throws Exception { } final String testPrefix = getTestName().toLowerCase(Locale.ROOT); - int initialNumberOfSuccessfulFollowedIndicesInFollowCluster = getNumberOfSuccessfulFollowedIndices(); - - final String autoFollowPattern = "pattern-" + testPrefix; - createAutoFollowPattern(client(), autoFollowPattern, testPrefix + "-*", "leader_cluster"); - - // Create a regular index on leader - final String regularIndex = testPrefix + "-regular"; - { - try (var leaderClient = buildLeaderClient()) { - for (int i = 0; i < 10; i++) { - var indexRequest = new Request("POST", "/" + regularIndex + "/_doc"); - indexRequest.addParameter("refresh", "true"); - indexRequest.setJsonEntity("{\"value\":" + i + "}"); - assertOK(leaderClient.performRequest(indexRequest)); - } - verifyDocuments(leaderClient, regularIndex, 10); - } - } + final String repository = testPrefix + "-repository"; + final String snapshot = testPrefix + "-snapshot"; - // Create a snapshot backed index on leader - final String mountedIndex = testPrefix + "-mounted"; - { + // Create a repository and a snapshot of a 5 docs index on leader + final String indexName = testPrefix + "-index"; + try { try (var leaderClient = buildLeaderClient()) { final String systemPropertyRepoPath = System.getProperty("tests.leader_cluster_repository_path"); assertThat("Missing system property [tests.leader_cluster_repository_path]", systemPropertyRepoPath, not(emptyOrNullString())); final String repositoryPath = systemPropertyRepoPath + '/' + testPrefix; - final String repository = testPrefix + "-repository"; registerRepository(leaderClient, repository, "fs", true, Settings.builder().put("location", repositoryPath).build()); - final String indexName = testPrefix + "-index"; for (int i = 0; i < 5; i++) { var indexRequest = new Request("POST", "/" + indexName + "/_doc"); indexRequest.addParameter("refresh", "true"); @@ -814,33 +796,56 @@ public void testAutoFollowSearchableSnapshotsFails() throws Exception { } verifyDocuments(leaderClient, indexName, 5); - final String snapshot = testPrefix + "-snapshot"; deleteSnapshot(leaderClient, repository, snapshot, true); createSnapshot(leaderClient, repository, snapshot, true); - deleteIndex(leaderClient, indexName); + } + } finally { + cleanUpLeader(List.of(indexName), List.of(), List.of()); + } + final String autoFollowPattern = "pattern-" + testPrefix; + final String regularIndex = testPrefix + "-regular"; + final String mountedIndex = testPrefix + "-mounted"; + + try { + createAutoFollowPattern(client(), autoFollowPattern, testPrefix + "-*", "leader_cluster"); + + // Create a regular index on leader + try (var leaderClient = buildLeaderClient()) { + for (int i = 0; i < 10; i++) { + var indexRequest = new Request("POST", "/" + regularIndex + "/_doc"); + indexRequest.addParameter("refresh", "true"); + indexRequest.setJsonEntity("{\"value\":" + i + "}"); + assertOK(leaderClient.performRequest(indexRequest)); + } + verifyDocuments(leaderClient, regularIndex, 10); + } + + // Mount the snapshot on leader + try (var leaderClient = buildLeaderClient()) { final Request mountRequest = new Request(HttpPost.METHOD_NAME, "/_snapshot/" + repository + '/' + snapshot + "/_mount"); mountRequest.setJsonEntity("{\"index\": \"" + indexName + "\",\"renamed_index\": \"" + mountedIndex + "\"}"); final Response mountResponse = leaderClient.performRequest(mountRequest); assertThat(mountResponse.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); ensureYellow(mountedIndex, leaderClient); } - } - assertLongBusy(() -> { - Map response = toMap(getAutoFollowStats()); - assertThat(eval("auto_follow_stats.number_of_successful_follow_indices", response), - equalTo(initialNumberOfSuccessfulFollowedIndicesInFollowCluster + 2)); - assertThat(eval("auto_follow_stats.recent_auto_follow_errors", response), - hasSize(greaterThan(0))); - assertThat(eval("auto_follow_stats.recent_auto_follow_errors.0.auto_follow_exception.reason", response), - containsString("index to follow [" + mountedIndex + "] is a searchable snapshot index and cannot be used " + - "for cross-cluster replication purpose")); - ensureYellow(regularIndex); - verifyDocuments(client(), regularIndex, 10); - }); - - deleteAutoFollowPattern(client(), autoFollowPattern); + assertLongBusy(() -> { + Map response = toMap(getAutoFollowStats()); + assertThat(eval("auto_follow_stats.number_of_failed_follow_indices", response), + greaterThanOrEqualTo(1)); + assertThat(eval("auto_follow_stats.recent_auto_follow_errors", response), + hasSize(greaterThanOrEqualTo(1))); + assertThat(eval("auto_follow_stats.recent_auto_follow_errors.0.auto_follow_exception.reason", response), + containsString("index to follow [" + mountedIndex + "] is a searchable snapshot index and cannot be used " + + "for cross-cluster replication purpose")); + ensureYellow(regularIndex); + verifyDocuments(client(), regularIndex, 10); + }); + } finally { + cleanUpLeader(List.of(regularIndex, mountedIndex), List.of(), List.of()); + cleanUpFollower(List.of(regularIndex), List.of(), List.of(autoFollowPattern)); + } } private int getNumberOfSuccessfulFollowedIndices() throws IOException {