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 {