|
| 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