Skip to content

Commit

Permalink
Rework AutoFollowIT.testAutoFollowSearchableSnapshotsFails (elastic#7…
Browse files Browse the repository at this point in the history
…4498)

The test AutoFollowIT.testAutoFollowSearchableSnapshotsFails was
added in elastic#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 elastic#74486
  • Loading branch information
tlrx committed Jun 28, 2021
1 parent 8716b39 commit 3bc22dc
Showing 1 changed file with 47 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
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;
import org.elasticsearch.common.util.concurrent.ThreadContext;
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;
Expand All @@ -39,7 +39,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;
Expand Down Expand Up @@ -777,38 +777,21 @@ public void testAutoFollowSearchableSnapshotsFails() throws Exception {
}

final String testPrefix = getTestName().toLowerCase(Locale.ROOT);
int initialNumberOfSuccessfulFollowedIndicesInFollowCluster = getNumberOfSuccessfulFollowedIndices();
final String repository = testPrefix + "-repository";
final String snapshot = testPrefix + "-snapshot";

final String autoFollowPattern = "pattern-" + testPrefix;
createAutoFollowPattern(client(), autoFollowPattern, testPrefix + "-*", "leader_cluster");

// Create a regular index on leader
final String regularIndex = testPrefix + "-regular";
{
try (RestClient leaderClient = buildLeaderClient()) {
for (int i = 0; i < 10; i++) {
Request indexRequest = new Request("POST", "/" + regularIndex + "/_doc");
indexRequest.addParameter("refresh", "true");
indexRequest.setJsonEntity("{\"value\":" + i + "}");
assertOK(leaderClient.performRequest(indexRequest));
}
verifyDocuments(leaderClient, regularIndex, 10);
}
}

// 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 (RestClient 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++) {
Request indexRequest = new Request("POST", "/" + indexName + "/_doc");
indexRequest.addParameter("refresh", "true");
Expand All @@ -817,33 +800,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(singletonList(indexName), emptyList(), emptyList());
}

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 (RestClient leaderClient = buildLeaderClient()) {
for (int i = 0; i < 10; i++) {
Request 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 (RestClient 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(asList(regularIndex, mountedIndex), emptyList(), emptyList());
cleanUpFollower(singletonList(regularIndex), emptyList(), singletonList(autoFollowPattern));
}
}

private int getNumberOfSuccessfulFollowedIndices() throws IOException {
Expand Down

0 comments on commit 3bc22dc

Please sign in to comment.