-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EQL: Introduce support for sequences #56300
Changes from 6 commits
70d7ce8
9d1ead0
efb8239
3515f88
2b608e4
e6386bf
d3e2671
45250f8
f58a5a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,19 @@ | |
package org.elasticsearch.test.eql; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
||
import org.elasticsearch.Build; | ||
import org.elasticsearch.action.bulk.BulkRequest; | ||
import org.elasticsearch.action.bulk.BulkResponse; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.action.support.WriteRequest; | ||
import org.elasticsearch.client.EqlClient; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.elasticsearch.client.eql.EqlSearchRequest; | ||
import org.elasticsearch.client.eql.EqlSearchResponse; | ||
import org.elasticsearch.client.eql.EqlSearchResponse.Hits; | ||
import org.elasticsearch.client.eql.EqlSearchResponse.Sequence; | ||
import org.elasticsearch.client.indices.CreateIndexRequest; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.io.Streams; | ||
|
@@ -32,7 +36,9 @@ | |
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
|
||
public abstract class CommonEqlActionTestCase extends ESRestTestCase { | ||
|
@@ -121,14 +127,15 @@ public void cleanup() throws Exception { | |
|
||
@ParametersFactory(shuffle = false, argumentFormatting = PARAM_FORMATTING) | ||
public static List<Object[]> readTestSpecs() throws Exception { | ||
List<Object[]> testSpecs = new ArrayList<>(); | ||
|
||
// Load EQL validation specs | ||
List<EqlSpec> specs = EqlSpecLoader.load("/test_queries.toml", true); | ||
specs.addAll(EqlSpecLoader.load("/test_queries_supported.toml", true)); | ||
List<EqlSpec> unsupportedSpecs = EqlSpecLoader.load("/test_queries_unsupported.toml", false); | ||
|
||
// Validate only currently supported specs | ||
List<EqlSpec> filteredSpecs = new ArrayList<>(); | ||
|
||
for (EqlSpec spec : specs) { | ||
boolean supported = true; | ||
// Check if spec is supported, simple iteration, cause the list is short. | ||
|
@@ -140,18 +147,25 @@ public static List<Object[]> readTestSpecs() throws Exception { | |
} | ||
|
||
if (supported) { | ||
String name = spec.description(); | ||
if (Strings.isNullOrEmpty(name)) { | ||
name = spec.note(); | ||
} | ||
if (Strings.isNullOrEmpty(name)) { | ||
name = spec.query(); | ||
} | ||
|
||
testSpecs.add(new Object[]{++counter, name, spec}); | ||
filteredSpecs.add(spec); | ||
} | ||
} | ||
return testSpecs; | ||
counter = specs.size(); | ||
return asArray(filteredSpecs); | ||
} | ||
|
||
public static List<Object[]> asArray(List<EqlSpec> specs) { | ||
AtomicInteger counter = new AtomicInteger(); | ||
return specs.stream().map(spec -> { | ||
String name = spec.description(); | ||
if (Strings.isNullOrEmpty(name)) { | ||
name = spec.note(); | ||
} | ||
if (Strings.isNullOrEmpty(name)) { | ||
name = spec.query(); | ||
} | ||
return new Object[] { counter.incrementAndGet(), name, spec }; | ||
}).collect(toList()); | ||
} | ||
|
||
private final int num; | ||
|
@@ -165,9 +179,29 @@ public CommonEqlActionTestCase(int num, String name, EqlSpec spec) { | |
} | ||
|
||
public void test() throws Exception { | ||
EqlSearchRequest request = new EqlSearchRequest(testIndexName, spec.query()); | ||
EqlSearchResponse response = highLevelClient().eql().search(request, RequestOptions.DEFAULT); | ||
assertSpec(response.hits().events()); | ||
assertResponse(runQuery(testIndexName, spec.query())); | ||
} | ||
|
||
protected void assertResponse(EqlSearchResponse response) { | ||
Hits hits = response.hits(); | ||
if (hits.events() != null) { | ||
assertSearchHits(hits.events()); | ||
} | ||
else if (hits.sequences() != null) { | ||
assertSequences(hits.sequences()); | ||
} | ||
else { | ||
fail("No events or sequences found"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is an odd formatting for if else if .... seems like the same line is more common in the current code |
||
} | ||
|
||
protected EqlSearchResponse runQuery(String index, String query) throws Exception { | ||
EqlSearchRequest request = new EqlSearchRequest(testIndexName, query); | ||
return eqlClient().search(request, RequestOptions.DEFAULT); | ||
} | ||
|
||
protected EqlClient eqlClient() { | ||
return highLevelClient().eql(); | ||
} | ||
|
||
private static long[] extractIds(List<SearchHit> events) { | ||
|
@@ -179,11 +213,18 @@ private static long[] extractIds(List<SearchHit> events) { | |
return ids; | ||
} | ||
|
||
private void assertSpec(List<SearchHit> events) { | ||
protected void assertSearchHits(List<SearchHit> events) { | ||
assertNotNull(events); | ||
assertArrayEquals("unexpected result for spec: [" + spec.toString() + "]", spec.expectedEventIds(), extractIds(events)); | ||
} | ||
|
||
protected void assertSequences(List<Sequence> sequences) { | ||
List<SearchHit> events = sequences.stream() | ||
.flatMap(s -> s.events().stream()) | ||
.collect(toList()); | ||
assertSearchHits(events); | ||
} | ||
|
||
private RestHighLevelClient highLevelClient() { | ||
if (highLevelClient == null) { | ||
highLevelClient = new RestHighLevelClient( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,3 +118,26 @@ query = "file where 66.0 / serial_event_id == 1" | |
[[queries]] | ||
expected_event_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 46] | ||
query = "process where serial_event_id + ((1 + 3) * 2 / (3 - 1)) * 2 == 54 or 70 + serial_event_id < 100" | ||
|
||
[[queries]] | ||
query = ''' | ||
sequence | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently these are getting dropped by CommonEqlActionTestCase.readTestSpecs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, they still need to be removed from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And since these are duplicates of test_queries.toml, you can remove them here too |
||
[process where serial_event_id = 1] | ||
[process where serial_event_id = 2] | ||
''' | ||
expected_event_ids = [1, 2] | ||
|
||
[[queries]] | ||
query = ''' | ||
sequence | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also add another test where the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearly there needs to be more testing in the future and polishing but that's future work. the PR is already large enough - a current issue with the current dataset is that it relies on a custom tie-breaker something not yet implemented. |
||
[process where serial_event_id=1] by unique_pid | ||
[process where true] by unique_ppid''' | ||
expected_event_ids = [1, 2] | ||
|
||
[[queries]] | ||
query = ''' | ||
sequence | ||
[process where serial_event_id<3] by unique_pid | ||
[process where true] by unique_ppid | ||
''' | ||
expected_event_ids = [1, 2, 2, 3] | ||
rw-access marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.eql.execution.assembler; | ||
|
||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.elasticsearch.xpack.ql.execution.search.extractor.HitExtractor; | ||
|
||
import java.util.List; | ||
|
||
public class Criterion { | ||
|
||
private final SearchSourceBuilder searchSource; | ||
private final List<HitExtractor> keyExtractors; | ||
private final HitExtractor timestampExtractor; | ||
|
||
public Criterion(SearchSourceBuilder searchSource, List<HitExtractor> searchAfterExractors, HitExtractor timestampExtractor) { | ||
this.searchSource = searchSource; | ||
this.keyExtractors = searchAfterExractors; | ||
this.timestampExtractor = timestampExtractor; | ||
} | ||
|
||
public SearchSourceBuilder searchSource() { | ||
return searchSource; | ||
} | ||
|
||
public List<HitExtractor> keyExtractors() { | ||
return keyExtractors; | ||
} | ||
|
||
public HitExtractor timestampExtractor() { | ||
return timestampExtractor; | ||
} | ||
|
||
public void fromTimestamp(long timestampMarker) { | ||
// TODO: this is likely to be rewritten afterwards | ||
searchSource.searchAfter(new Object[] { timestampMarker }); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.eql.execution.assembler; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.xpack.eql.session.Results; | ||
|
||
public interface Executable { | ||
|
||
void execute(ActionListener<Results> resultsListener); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why AtomicInteger where int will do just fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int doesn't work since the stream pipeline requires a final/non-enclosed object. The
AtomicLong
is essentially a hack to increment the counter while keeping the same reference.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit of misuse for AtomicInteger only for a Holder functionality but since it's a test it's not important.