Skip to content

Commit

Permalink
Rework AutoFollowIT.testAutoFollowSearchableSnapshotsFails (#74498)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tlrx authored Jun 28, 2021
1 parent 54738fc commit d03a69c
Showing 1 changed file with 47 additions and 42 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 @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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 {
Expand Down

0 comments on commit d03a69c

Please sign in to comment.