|
| 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 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.eql.qa.ccs_rolling_upgrade; |
| 9 | + |
| 10 | +import org.apache.http.HttpHost; |
| 11 | +import org.apache.http.util.EntityUtils; |
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.apache.logging.log4j.Logger; |
| 14 | +import org.elasticsearch.Version; |
| 15 | +import org.elasticsearch.action.index.IndexRequest; |
| 16 | +import org.elasticsearch.client.Request; |
| 17 | +import org.elasticsearch.client.RequestOptions; |
| 18 | +import org.elasticsearch.client.Response; |
| 19 | +import org.elasticsearch.client.RestClient; |
| 20 | +import org.elasticsearch.client.RestHighLevelClient; |
| 21 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 22 | +import org.elasticsearch.common.settings.Settings; |
| 23 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 24 | +import org.elasticsearch.test.rest.ObjectPath; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.UncheckedIOException; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +import static org.hamcrest.Matchers.empty; |
| 35 | +import static org.hamcrest.Matchers.hasSize; |
| 36 | +import static org.hamcrest.Matchers.not; |
| 37 | + |
| 38 | +/** |
| 39 | + * This test ensures that EQL can process CCS requests correctly when the local and remote clusters |
| 40 | + * have different but compatible versions. |
| 41 | + */ |
| 42 | +@SuppressWarnings("removal") |
| 43 | +public class EqlCcsRollingUpgradeIT extends ESRestTestCase { |
| 44 | + |
| 45 | + private static final Logger LOGGER = LogManager.getLogger(EqlCcsRollingUpgradeIT.class); |
| 46 | + private static final String CLUSTER_ALIAS = "remote_cluster"; |
| 47 | + |
| 48 | + record Node(String id, String name, Version version, String transportAddress, String httpAddress, Map<String, Object> attributes) {} |
| 49 | + |
| 50 | + static List<Node> getNodes(RestClient restClient) throws IOException { |
| 51 | + Response response = restClient.performRequest(new Request("GET", "_nodes")); |
| 52 | + ObjectPath objectPath = ObjectPath.createFromResponse(response); |
| 53 | + final Map<String, Object> nodeMap = objectPath.evaluate("nodes"); |
| 54 | + final List<Node> nodes = new ArrayList<>(); |
| 55 | + for (String id : nodeMap.keySet()) { |
| 56 | + final String name = objectPath.evaluate("nodes." + id + ".name"); |
| 57 | + final Version version = Version.fromString(objectPath.evaluate("nodes." + id + ".version")); |
| 58 | + final String transportAddress = objectPath.evaluate("nodes." + id + ".transport.publish_address"); |
| 59 | + final String httpAddress = objectPath.evaluate("nodes." + id + ".http.publish_address"); |
| 60 | + final Map<String, Object> attributes = objectPath.evaluate("nodes." + id + ".attributes"); |
| 61 | + nodes.add(new Node(id, name, version, transportAddress, httpAddress, attributes)); |
| 62 | + } |
| 63 | + return nodes; |
| 64 | + } |
| 65 | + |
| 66 | + static List<HttpHost> parseHosts(String props) { |
| 67 | + final String address = System.getProperty(props); |
| 68 | + assertNotNull("[" + props + "] is not configured", address); |
| 69 | + String[] stringUrls = address.split(","); |
| 70 | + List<HttpHost> hosts = new ArrayList<>(stringUrls.length); |
| 71 | + for (String stringUrl : stringUrls) { |
| 72 | + int portSeparator = stringUrl.lastIndexOf(':'); |
| 73 | + if (portSeparator < 0) { |
| 74 | + throw new IllegalArgumentException("Illegal cluster url [" + stringUrl + "]"); |
| 75 | + } |
| 76 | + String host = stringUrl.substring(0, portSeparator); |
| 77 | + int port = Integer.parseInt(stringUrl.substring(portSeparator + 1)); |
| 78 | + hosts.add(new HttpHost(host, port, "http")); |
| 79 | + } |
| 80 | + assertThat("[" + props + "] is empty", hosts, not(empty())); |
| 81 | + return hosts; |
| 82 | + } |
| 83 | + |
| 84 | + public static void configureRemoteClusters(List<Node> remoteNodes) throws Exception { |
| 85 | + assertThat(remoteNodes, hasSize(3)); |
| 86 | + final String remoteClusterSettingPrefix = "cluster.remote." + CLUSTER_ALIAS + "."; |
| 87 | + try (RestClient localClient = newLocalClient().getLowLevelClient()) { |
| 88 | + final Settings remoteConnectionSettings; |
| 89 | + if (randomBoolean()) { |
| 90 | + final List<String> seeds = remoteNodes.stream() |
| 91 | + .filter(n -> n.attributes.containsKey("gateway")) |
| 92 | + .map(n -> n.transportAddress) |
| 93 | + .collect(Collectors.toList()); |
| 94 | + assertThat(seeds, hasSize(2)); |
| 95 | + LOGGER.info("--> use sniff mode with seed [{}], remote nodes [{}]", seeds, remoteNodes); |
| 96 | + remoteConnectionSettings = Settings.builder() |
| 97 | + .putNull(remoteClusterSettingPrefix + "proxy_address") |
| 98 | + .put(remoteClusterSettingPrefix + "mode", "sniff") |
| 99 | + .putList(remoteClusterSettingPrefix + "seeds", seeds) |
| 100 | + .build(); |
| 101 | + } else { |
| 102 | + final Node proxyNode = randomFrom(remoteNodes); |
| 103 | + LOGGER.info("--> use proxy node [{}], remote nodes [{}]", proxyNode, remoteNodes); |
| 104 | + remoteConnectionSettings = Settings.builder() |
| 105 | + .putNull(remoteClusterSettingPrefix + "seeds") |
| 106 | + .put(remoteClusterSettingPrefix + "mode", "proxy") |
| 107 | + .put(remoteClusterSettingPrefix + "proxy_address", proxyNode.transportAddress) |
| 108 | + .build(); |
| 109 | + } |
| 110 | + updateClusterSettings(localClient, remoteConnectionSettings); |
| 111 | + assertBusy(() -> { |
| 112 | + final Response resp = localClient.performRequest(new Request("GET", "/_remote/info")); |
| 113 | + assertOK(resp); |
| 114 | + final ObjectPath objectPath = ObjectPath.createFromResponse(resp); |
| 115 | + assertNotNull(objectPath.evaluate(CLUSTER_ALIAS)); |
| 116 | + assertTrue(objectPath.evaluate(CLUSTER_ALIAS + ".connected")); |
| 117 | + }, 60, TimeUnit.SECONDS); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + static RestHighLevelClient newLocalClient() { |
| 122 | + final List<HttpHost> hosts = parseHosts("tests.rest.cluster"); |
| 123 | + final int index = random().nextInt(hosts.size()); |
| 124 | + LOGGER.info("Using client node {}", index); |
| 125 | + return new RestHighLevelClient(RestClient.builder(hosts.get(index))); |
| 126 | + } |
| 127 | + |
| 128 | + static RestHighLevelClient newRemoteClient() { |
| 129 | + return new RestHighLevelClient(RestClient.builder(randomFrom(parseHosts("tests.rest.remote_cluster")))); |
| 130 | + } |
| 131 | + |
| 132 | + static int indexDocs(RestHighLevelClient client, String index, int numDocs) throws IOException { |
| 133 | + for (int i = 0; i < numDocs; i++) { |
| 134 | + client.index(new IndexRequest(index).id("id_" + i).source("f", i, "@timestamp", i), RequestOptions.DEFAULT); |
| 135 | + } |
| 136 | + |
| 137 | + refresh(client.getLowLevelClient(), index); |
| 138 | + return numDocs; |
| 139 | + } |
| 140 | + |
| 141 | + void verify(String localIndex, int localNumDocs, String remoteIndex, int remoteNumDocs) { |
| 142 | + try (RestClient localClient = newLocalClient().getLowLevelClient()) { |
| 143 | + |
| 144 | + Request request = new Request("POST", "/" + randomFrom(remoteIndex, localIndex + "," + remoteIndex) + "/_eql/search"); |
| 145 | + int size = between(1, 100); |
| 146 | + int id1 = between(0, 5); |
| 147 | + int id2 = between(6, Math.min(localNumDocs - 1, remoteNumDocs - 1)); |
| 148 | + request.setJsonEntity( |
| 149 | + "{\"query\": \"sequence [any where f == " + id1 + "] [any where f == " + id2 + "] \", \"size\": " + size + "}" |
| 150 | + ); |
| 151 | + Response response = localClient.performRequest(request); |
| 152 | + String responseText = EntityUtils.toString(response.getEntity()); |
| 153 | + assertTrue(responseText.contains("\"sequences\":[{")); |
| 154 | + assertTrue(responseText.contains("\"_id\":\"id_" + id1 + "\"")); |
| 155 | + assertTrue(responseText.contains("\"_id\":\"id_" + id2 + "\"")); |
| 156 | + } catch (IOException e) { |
| 157 | + throw new UncheckedIOException(e); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public void testSequences() throws Exception { |
| 162 | + String localIndex = "test_bwc_search_states_index"; |
| 163 | + String remoteIndex = "test_bwc_search_states_remote_index"; |
| 164 | + try (RestHighLevelClient localClient = newLocalClient(); RestHighLevelClient remoteClient = newRemoteClient()) { |
| 165 | + createIndex( |
| 166 | + localClient.getLowLevelClient(), |
| 167 | + localIndex, |
| 168 | + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5)).build(), |
| 169 | + "{\"properties\": {\"@timestamp\": {\"type\": \"date\"}}}", |
| 170 | + null |
| 171 | + ); |
| 172 | + int localNumDocs = indexDocs(localClient, localIndex, between(10, 100)); |
| 173 | + createIndex( |
| 174 | + remoteClient.getLowLevelClient(), |
| 175 | + remoteIndex, |
| 176 | + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5)).build(), |
| 177 | + "{\"properties\": {\"@timestamp\": {\"type\": \"date\"}}}", |
| 178 | + null |
| 179 | + ); |
| 180 | + int remoteNumDocs = indexDocs(remoteClient, remoteIndex, between(10, 100)); |
| 181 | + |
| 182 | + configureRemoteClusters(getNodes(remoteClient.getLowLevelClient())); |
| 183 | + int iterations = between(1, 20); |
| 184 | + for (int i = 0; i < iterations; i++) { |
| 185 | + verify(localIndex, localNumDocs, CLUSTER_ALIAS + ":" + remoteIndex, remoteNumDocs); |
| 186 | + } |
| 187 | + deleteIndex(localClient.getLowLevelClient(), localIndex); |
| 188 | + deleteIndex(remoteClient.getLowLevelClient(), remoteIndex); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments