|
| 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.mixed_node; |
| 9 | + |
| 10 | +import org.apache.http.HttpHost; |
| 11 | +import org.elasticsearch.client.Request; |
| 12 | +import org.elasticsearch.client.Response; |
| 13 | +import org.elasticsearch.client.RestClient; |
| 14 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 17 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
| 18 | +import org.elasticsearch.test.NotEqualMessageBuilder; |
| 19 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 20 | +import org.elasticsearch.xpack.ql.TestNode; |
| 21 | +import org.elasticsearch.xpack.ql.TestNodes; |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Before; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import static java.util.Arrays.asList; |
| 33 | +import static java.util.Collections.emptyMap; |
| 34 | +import static java.util.Collections.singletonList; |
| 35 | +import static java.util.Collections.singletonMap; |
| 36 | +import static java.util.Collections.unmodifiableList; |
| 37 | +import static org.elasticsearch.xpack.ql.TestUtils.buildNodeAndVersions; |
| 38 | +import static org.elasticsearch.xpack.ql.TestUtils.readResource; |
| 39 | + |
| 40 | +/** |
| 41 | + * Class testing the behavior of events and sequence queries in a mixed cluster scenario (during rolling upgrade). |
| 42 | + * The test is against a three-node cluster where one node is upgraded, the other two are on the old version. |
| 43 | + * |
| 44 | + */ |
| 45 | +public class EqlSearchIT extends ESRestTestCase { |
| 46 | + |
| 47 | + private static final String index = "test_eql_mixed_versions"; |
| 48 | + private static int numShards; |
| 49 | + private static int numReplicas = 1; |
| 50 | + private static int numDocs; |
| 51 | + private static TestNodes nodes; |
| 52 | + private static List<TestNode> newNodes; |
| 53 | + private static List<TestNode> bwcNodes; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void createIndex() throws IOException { |
| 57 | + nodes = buildNodeAndVersions(client()); |
| 58 | + numShards = nodes.size(); |
| 59 | + numDocs = randomIntBetween(numShards, 15); |
| 60 | + newNodes = new ArrayList<>(nodes.getNewNodes()); |
| 61 | + bwcNodes = new ArrayList<>(nodes.getBWCNodes()); |
| 62 | + |
| 63 | + String mappings = readResource(EqlSearchIT.class.getResourceAsStream("/eql_mapping.json")); |
| 64 | + createIndex( |
| 65 | + index, |
| 66 | + Settings.builder() |
| 67 | + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numShards) |
| 68 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numReplicas) |
| 69 | + .build(), |
| 70 | + mappings |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + @After |
| 75 | + public void cleanUpIndex() throws IOException { |
| 76 | + if (indexExists(index)) { |
| 77 | + deleteIndex(index); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public void testEventsWithRequestToOldNodes() throws Exception { |
| 82 | + assertEventsQueryOnNodes(bwcNodes); |
| 83 | + } |
| 84 | + |
| 85 | + public void testEventsWithRequestToUpgradedNodes() throws Exception { |
| 86 | + assertEventsQueryOnNodes(newNodes); |
| 87 | + } |
| 88 | + |
| 89 | + public void testSequencesWithRequestToOldNodes() throws Exception { |
| 90 | + assertSequncesQueryOnNodes(bwcNodes); |
| 91 | + } |
| 92 | + |
| 93 | + public void testSequencesWithRequestToUpgradedNodes() throws Exception { |
| 94 | + assertSequncesQueryOnNodes(newNodes); |
| 95 | + } |
| 96 | + |
| 97 | + private void assertEventsQueryOnNodes(List<TestNode> nodesList) throws Exception { |
| 98 | + final String event = randomEvent(); |
| 99 | + Map<String, Object> expectedResponse = prepareEventsTestData(event); |
| 100 | + try ( |
| 101 | + RestClient client = buildClient(restClientSettings(), |
| 102 | + nodesList.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new)) |
| 103 | + ) { |
| 104 | + // filter only the relevant bits of the response |
| 105 | + String filterPath = "filter_path=hits.events._source.@timestamp,hits.events._source.event_type,hits.events._source.sequence"; |
| 106 | + |
| 107 | + Request request = new Request("POST", index + "/_eql/search?" + filterPath); |
| 108 | + request.setJsonEntity("{\"query\":\"" + event + " where true\"}"); |
| 109 | + assertBusy(() -> { assertResponse(expectedResponse, runEql(client, request)); }); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void assertSequncesQueryOnNodes(List<TestNode> nodesList) throws Exception { |
| 114 | + Map<String, Object> expectedResponse = prepareSequencesTestData(); |
| 115 | + try ( |
| 116 | + RestClient client = buildClient(restClientSettings(), |
| 117 | + nodesList.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new)) |
| 118 | + ) { |
| 119 | + String filterPath = "filter_path=hits.sequences.join_keys,hits.sequences.events._id,hits.sequences.events._source"; |
| 120 | + String query = "sequence by `sequence` with maxspan=100ms [success where true] by correlation_success1, correlation_success2 " |
| 121 | + + "[failure where true] by correlation_failure1, correlation_failure2"; |
| 122 | + String filter = "{\"range\":{\"@timestamp\":{\"gte\":\"1970-05-01\"}}}"; |
| 123 | + |
| 124 | + Request request = new Request("POST", index + "/_eql/search?" + filterPath); |
| 125 | + request.setJsonEntity("{\"query\":\"" + query + "\",\"filter\":" + filter + "}"); |
| 126 | + assertBusy(() -> { assertResponse(expectedResponse, runEql(client, request)); }); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private String randomEvent() { |
| 131 | + return randomFrom("success", "failure"); |
| 132 | + } |
| 133 | + |
| 134 | + private Map<String, Object> prepareEventsTestData(String event) throws IOException { |
| 135 | + List<Map<String, Object>> sourceEvents = new ArrayList<Map<String, Object>>(); |
| 136 | + Map<String, Object> expectedResponse = singletonMap("hits", singletonMap("events", sourceEvents)); |
| 137 | + |
| 138 | + for (int i = 0; i < numDocs; i++) { |
| 139 | + StringBuilder builder = new StringBuilder(); |
| 140 | + final String randomEvent = randomEvent(); |
| 141 | + builder.append("{"); |
| 142 | + builder.append("\"@timestamp\":" + i + ","); |
| 143 | + builder.append("\"event_type\":\"" + randomEvent + "\","); |
| 144 | + builder.append("\"sequence\":" + i); |
| 145 | + builder.append("}"); |
| 146 | + if (randomEvent.equals(event)) { |
| 147 | + Map<String, Object> eventSource = new HashMap<>(); |
| 148 | + eventSource.put("@timestamp", i); |
| 149 | + eventSource.put("event_type", randomEvent); |
| 150 | + eventSource.put("sequence", i); |
| 151 | + sourceEvents.add(singletonMap("_source", eventSource)); |
| 152 | + } |
| 153 | + |
| 154 | + Request request = new Request("PUT", index + "/_doc/" + i); |
| 155 | + request.setJsonEntity(builder.toString()); |
| 156 | + assertOK(client().performRequest(request)); |
| 157 | + } |
| 158 | + if (sourceEvents.isEmpty()) { |
| 159 | + return emptyMap(); |
| 160 | + } |
| 161 | + return expectedResponse; |
| 162 | + } |
| 163 | + |
| 164 | + /* |
| 165 | + * Output to compare with looks like this: |
| 166 | + * { |
| 167 | + * "hits": { |
| 168 | + * "sequences": [ |
| 169 | + * { |
| 170 | + * "join_keys": [ |
| 171 | + * 44, |
| 172 | + * "C", |
| 173 | + * "D" |
| 174 | + * ], |
| 175 | + * "events": [ |
| 176 | + * { |
| 177 | + * "_id": "14", |
| 178 | + * "_source": { |
| 179 | + * ... |
| 180 | + * } |
| 181 | + * } |
| 182 | + * ] |
| 183 | + * } |
| 184 | + * } |
| 185 | + * } |
| 186 | + * |
| 187 | + */ |
| 188 | + private Map<String, Object> prepareSequencesTestData() throws IOException { |
| 189 | + Map<String, Object> event14 = new HashMap<>(); |
| 190 | + Map<String, Object> event14Source = new HashMap<>(); |
| 191 | + event14.put("_id", "14"); |
| 192 | + event14.put("_source", event14Source); |
| 193 | + event14Source.put("@timestamp", "12345678914"); |
| 194 | + event14Source.put("event_type", "success"); |
| 195 | + event14Source.put("sequence", 44); |
| 196 | + event14Source.put("correlation_success1", "C"); |
| 197 | + event14Source.put("correlation_success2", "D"); |
| 198 | + |
| 199 | + Map<String, Object> event15 = new HashMap<>(); |
| 200 | + Map<String, Object> event15Source = new HashMap<>(); |
| 201 | + event15.put("_id", "15"); |
| 202 | + event15.put("_source", event15Source); |
| 203 | + event15Source.put("@timestamp", "12345678999"); |
| 204 | + event15Source.put("event_type", "failure"); |
| 205 | + event15Source.put("sequence", 44); |
| 206 | + event15Source.put("correlation_failure1", "C"); |
| 207 | + event15Source.put("correlation_failure2", "D"); |
| 208 | + |
| 209 | + Map<String, Object> sequence = new HashMap<>(); |
| 210 | + List<Map<String, Object>> events = unmodifiableList(asList(event14, event15)); |
| 211 | + List<Map<String, Object>> sequences = singletonList(sequence); |
| 212 | + Map<String, Object> expectedResponse = singletonMap("hits", singletonMap("sequences", sequences)); |
| 213 | + |
| 214 | + sequence.put("join_keys", asList(44, "C", "D")); |
| 215 | + sequence.put("events", events); |
| 216 | + |
| 217 | + final String bulkEntries = readResource(EqlSearchIT.class.getResourceAsStream("/eql_data.json")); |
| 218 | + Request request = new Request("POST", index + "/_bulk?refresh"); |
| 219 | + request.setJsonEntity(bulkEntries); |
| 220 | + assertOK(client().performRequest(request)); |
| 221 | + |
| 222 | + return expectedResponse; |
| 223 | + } |
| 224 | + |
| 225 | + private void assertResponse(Map<String, Object> expected, Map<String, Object> actual) { |
| 226 | + if (false == expected.equals(actual)) { |
| 227 | + NotEqualMessageBuilder message = new NotEqualMessageBuilder(); |
| 228 | + message.compareMaps(actual, expected); |
| 229 | + fail("Response does not match:\n" + message.toString()); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + private Map<String, Object> runEql(RestClient client, Request request) throws IOException { |
| 234 | + Response response = client.performRequest(request); |
| 235 | + try (InputStream content = response.getEntity().getContent()) { |
| 236 | + return XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments