Skip to content

Commit e0b6721

Browse files
authored
Add dedicated test for chain replication (#34497)
This commit adds a dedicated test that chain replication leader -> middle -> follow is successful.
1 parent 75c973f commit e0b6721

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import org.elasticsearch.gradle.test.RestIntegTestTask
2+
3+
apply plugin: 'elasticsearch.standalone-test'
4+
5+
dependencies {
6+
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
7+
testCompile project(path: xpackModule('ccr'), configuration: 'runtime')
8+
}
9+
10+
task leaderClusterTest(type: RestIntegTestTask) {
11+
mustRunAfter(precommit)
12+
}
13+
14+
leaderClusterTestCluster {
15+
numNodes = 1
16+
clusterName = 'leader-cluster'
17+
setting 'xpack.license.self_generated.type', 'trial'
18+
setting 'node.name', 'leader'
19+
}
20+
21+
leaderClusterTestRunner {
22+
systemProperty 'tests.target_cluster', 'leader'
23+
}
24+
25+
task middleClusterTest(type: RestIntegTestTask) {}
26+
27+
middleClusterTestCluster {
28+
dependsOn leaderClusterTestRunner
29+
numNodes = 1
30+
clusterName = 'middle-cluster'
31+
setting 'xpack.license.self_generated.type', 'trial'
32+
setting 'cluster.remote.leader_cluster.seeds', "\"${-> leaderClusterTest.nodes.get(0).transportUri()}\""
33+
setting 'node.name', 'middle'
34+
}
35+
36+
middleClusterTestRunner {
37+
systemProperty 'tests.target_cluster', 'middle'
38+
systemProperty 'tests.leader_host', "${-> leaderClusterTest.nodes.get(0).httpUri()}"
39+
}
40+
41+
task followClusterTest(type: RestIntegTestTask) {}
42+
43+
followClusterTestCluster {
44+
dependsOn middleClusterTestRunner
45+
numNodes = 1
46+
clusterName = 'follow-cluster'
47+
setting 'xpack.license.self_generated.type', 'trial'
48+
setting 'cluster.remote.middle_cluster.seeds', "\"${-> middleClusterTest.nodes.get(0).transportUri()}\""
49+
setting 'node.name', 'follow'
50+
}
51+
52+
followClusterTestRunner {
53+
systemProperty 'tests.target_cluster', 'follow'
54+
systemProperty 'tests.leader_host', "${-> leaderClusterTest.nodes.get(0).httpUri()}"
55+
systemProperty 'tests.middle_host', "${-> middleClusterTest.nodes.get(0).httpUri()}"
56+
finalizedBy 'leaderClusterTestCluster#stop'
57+
finalizedBy 'middleClusterTestCluster#stop'
58+
}
59+
60+
check.dependsOn followClusterTest
61+
test.enabled = false
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.ccr;
8+
9+
import org.apache.http.HttpHost;
10+
import org.apache.http.util.EntityUtils;
11+
import org.elasticsearch.client.Request;
12+
import org.elasticsearch.client.Response;
13+
import org.elasticsearch.client.RestClient;
14+
import org.elasticsearch.common.Strings;
15+
import org.elasticsearch.common.settings.Settings;
16+
import org.elasticsearch.common.xcontent.XContentBuilder;
17+
import org.elasticsearch.common.xcontent.XContentHelper;
18+
import org.elasticsearch.common.xcontent.json.JsonXContent;
19+
import org.elasticsearch.common.xcontent.support.XContentMapValues;
20+
import org.elasticsearch.test.rest.ESRestTestCase;
21+
22+
import java.io.IOException;
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
27+
import static org.hamcrest.Matchers.equalTo;
28+
29+
public class ChainIT extends ESRestTestCase {
30+
31+
private final String targetCluster = System.getProperty("tests.target_cluster");
32+
33+
@Override
34+
protected boolean preserveClusterUponCompletion() {
35+
return true;
36+
}
37+
38+
public void testFollowIndex() throws Exception {
39+
final int numDocs = 128;
40+
final String leaderIndexName = "leader";
41+
final String middleIndexName = "middle";
42+
if ("leader".equals(targetCluster)) {
43+
logger.info("Running against leader cluster");
44+
String mapping = "";
45+
if (randomBoolean()) { // randomly do source filtering on indexing
46+
mapping =
47+
"\"_doc\": {" +
48+
" \"_source\": {" +
49+
" \"includes\": [\"field\"]," +
50+
" \"excludes\": [\"filtered_field\"]" +
51+
" }"+
52+
"}";
53+
}
54+
Settings indexSettings = Settings.builder()
55+
.put("index.soft_deletes.enabled", true)
56+
.build();
57+
createIndex(leaderIndexName, indexSettings, mapping);
58+
for (int i = 0; i < numDocs; i++) {
59+
logger.info("Indexing doc [{}]", i);
60+
index(client(), leaderIndexName, Integer.toString(i), "field", i, "filtered_field", "true");
61+
}
62+
refresh(leaderIndexName);
63+
verifyDocuments(leaderIndexName, numDocs);
64+
} else if ("middle".equals(targetCluster)) {
65+
logger.info("Running against middle cluster");
66+
followIndex("leader_cluster:" + leaderIndexName, middleIndexName);
67+
assertBusy(() -> verifyDocuments(middleIndexName, numDocs));
68+
try (RestClient leaderClient = buildLeaderClient()) {
69+
int id = numDocs;
70+
index(leaderClient, leaderIndexName, Integer.toString(id), "field", id, "filtered_field", "true");
71+
index(leaderClient, leaderIndexName, Integer.toString(id + 1), "field", id + 1, "filtered_field", "true");
72+
index(leaderClient, leaderIndexName, Integer.toString(id + 2), "field", id + 2, "filtered_field", "true");
73+
}
74+
assertBusy(() -> verifyDocuments(middleIndexName, numDocs + 3));
75+
} else if ("follow".equals(targetCluster)) {
76+
logger.info("Running against follow cluster");
77+
final String followIndexName = "follow";
78+
followIndex("middle_cluster:" + middleIndexName, followIndexName);
79+
assertBusy(() -> verifyDocuments(followIndexName, numDocs + 3));
80+
81+
try (RestClient leaderClient = buildLeaderClient()) {
82+
int id = numDocs + 3;
83+
index(leaderClient, leaderIndexName, Integer.toString(id), "field", id, "filtered_field", "true");
84+
index(leaderClient, leaderIndexName, Integer.toString(id + 1), "field", id + 1, "filtered_field", "true");
85+
index(leaderClient, leaderIndexName, Integer.toString(id + 2), "field", id + 2, "filtered_field", "true");
86+
}
87+
88+
try (RestClient middleClient = buildMiddleClient()) {
89+
assertBusy(() -> verifyDocuments(middleIndexName, numDocs + 6, middleClient));
90+
}
91+
92+
assertBusy(() -> verifyDocuments(followIndexName, numDocs + 6));
93+
} else {
94+
fail("unexpected target cluster [" + targetCluster + "]");
95+
}
96+
}
97+
98+
private static void index(RestClient client, String index, String id, Object... fields) throws IOException {
99+
XContentBuilder document = jsonBuilder().startObject();
100+
for (int i = 0; i < fields.length; i += 2) {
101+
document.field((String) fields[i], fields[i + 1]);
102+
}
103+
document.endObject();
104+
final Request request = new Request("POST", "/" + index + "/_doc/" + id);
105+
request.setJsonEntity(Strings.toString(document));
106+
assertOK(client.performRequest(request));
107+
}
108+
109+
private static void refresh(String index) throws IOException {
110+
assertOK(client().performRequest(new Request("POST", "/" + index + "/_refresh")));
111+
}
112+
113+
private static void resumeFollow(String leaderIndex, String followIndex) throws IOException {
114+
final Request request = new Request("POST", "/" + followIndex + "/_ccr/resume_follow");
115+
request.setJsonEntity("{\"leader_index\": \"" + leaderIndex + "\", \"poll_timeout\": \"10ms\"}");
116+
assertOK(client().performRequest(request));
117+
}
118+
119+
private static void followIndex(String leaderIndex, String followIndex) throws IOException {
120+
final Request request = new Request("PUT", "/" + followIndex + "/_ccr/follow");
121+
request.setJsonEntity("{\"leader_index\": \"" + leaderIndex + "\", \"poll_timeout\": \"10ms\"}");
122+
assertOK(client().performRequest(request));
123+
}
124+
125+
private static void pauseFollow(String followIndex) throws IOException {
126+
assertOK(client().performRequest(new Request("POST", "/" + followIndex + "/_ccr/pause_follow")));
127+
}
128+
129+
private static void verifyDocuments(String index, int expectedNumDocs) throws IOException {
130+
verifyDocuments(index, expectedNumDocs, client());
131+
}
132+
133+
private static void verifyDocuments(final String index, final int expectedNumDocs, final RestClient client) throws IOException {
134+
final Request request = new Request("GET", "/" + index + "/_search");
135+
request.addParameter("size", Integer.toString(expectedNumDocs));
136+
request.addParameter("sort", "field:asc");
137+
request.addParameter("q", "filtered_field:true");
138+
Map<String, ?> response = toMap(client.performRequest(request));
139+
140+
int numDocs = (int) XContentMapValues.extractValue("hits.total", response);
141+
assertThat(numDocs, equalTo(expectedNumDocs));
142+
143+
List<?> hits = (List<?>) XContentMapValues.extractValue("hits.hits", response);
144+
assertThat(hits.size(), equalTo(expectedNumDocs));
145+
for (int i = 0; i < expectedNumDocs; i++) {
146+
int value = (int) XContentMapValues.extractValue("_source.field", (Map<?, ?>) hits.get(i));
147+
assertThat(i, equalTo(value));
148+
}
149+
}
150+
151+
private static Map<String, Object> toMap(Response response) throws IOException {
152+
return toMap(EntityUtils.toString(response.getEntity()));
153+
}
154+
155+
private static Map<String, Object> toMap(String response) {
156+
return XContentHelper.convertToMap(JsonXContent.jsonXContent, response, false);
157+
}
158+
159+
private RestClient buildLeaderClient() throws IOException {
160+
assert "leader".equals(targetCluster) == false;
161+
return buildClient(System.getProperty("tests.leader_host"));
162+
}
163+
164+
private RestClient buildMiddleClient() throws IOException {
165+
assert "middle".equals(targetCluster) == false;
166+
return buildClient(System.getProperty("tests.middle_host"));
167+
}
168+
169+
private RestClient buildClient(final String url) throws IOException {
170+
int portSeparator = url.lastIndexOf(':');
171+
HttpHost httpHost = new HttpHost(url.substring(0, portSeparator),
172+
Integer.parseInt(url.substring(portSeparator + 1)), getProtocol());
173+
return buildClient(Settings.EMPTY, new HttpHost[]{httpHost});
174+
}
175+
176+
}

0 commit comments

Comments
 (0)