-
Notifications
You must be signed in to change notification settings - Fork 1k
/
QueryBackend.java
105 lines (95 loc) · 5.1 KB
/
QueryBackend.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.graylog.plugins.enterprise.search.engine;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableSet;
import org.graylog.plugins.enterprise.search.Parameter;
import org.graylog.plugins.enterprise.search.Query;
import org.graylog.plugins.enterprise.search.QueryMetadata;
import org.graylog.plugins.enterprise.search.QueryResult;
import org.graylog.plugins.enterprise.search.SearchJob;
import org.graylog.plugins.enterprise.search.errors.QueryError;
import org.graylog.plugins.enterprise.search.searchtypes.pivot.PivotResult;
import org.graylog2.plugin.indexer.searches.timeranges.AbsoluteRange;
import org.graylog2.plugin.indexer.searches.timeranges.RelativeRange;
import org.graylog2.plugin.indexer.searches.timeranges.TimeRange;
import org.joda.time.DateTime;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* A search backend that is capable of generating and executing search jobs
*
* @param <T> the type of the generated query
*/
public interface QueryBackend<T extends GeneratedQueryContext> {
/**
* Generate a backend-specific query out of the logical query structure.
*
* @param job currently executing job
* @param query the graylog query structure
* @param predecessorResults the query result of the preceding queries
* @return a backend specific generated query
*/
T generate(SearchJob job, Query query, Set<QueryResult> predecessorResults);
default boolean isAllMessages(TimeRange timeRange) {
return timeRange instanceof RelativeRange && ((RelativeRange)timeRange).range() == 0;
}
default AbsoluteRange effectiveTimeRangeForResult(Query query, QueryResult queryResult) {
if (isAllMessages(query.timerange())) {
final Optional<AbsoluteRange> effectiveRange = queryResult.searchTypes().values().stream()
.filter(result -> result instanceof PivotResult)
.map(result -> ((PivotResult) result).effectiveTimerange())
.reduce((prev, next) -> {
final DateTime from = prev.from().compareTo(next.from()) < 0 ? prev.from() : next.from();
final DateTime to = prev.to().compareTo(next.to()) < 0 ? next.to() : prev.to();
return AbsoluteRange.create(from, to);
});
if (effectiveRange.isPresent()) {
return effectiveRange.get();
}
}
return AbsoluteRange.create(query.timerange().getFrom(), query.timerange().getTo());
}
// TODO we can probably push job, query and predecessorResults into the GeneratedQueryContext to simplify the signature
default QueryResult run(SearchJob job, Query query, GeneratedQueryContext generatedQueryContext, Set<QueryResult> predecessorResults) {
try {
final Stopwatch stopwatch = Stopwatch.createStarted();
final QueryExecutionStats.Builder statsBuilder = QueryExecutionStats.builderWithCurrentTime();
// https://www.ibm.com/developerworks/java/library/j-jtp04298/index.html#3.0
//noinspection unchecked
final QueryResult result = doRun(job, query, (T) generatedQueryContext, predecessorResults);
stopwatch.stop();
return result.toBuilder()
.executionStats(
statsBuilder.duration(stopwatch.elapsed(TimeUnit.MILLISECONDS))
.effectiveTimeRange(effectiveTimeRangeForResult(query, result))
.build())
.build();
} catch (Exception e) {
// the backend has very likely created a more specific error and added it to the context, but we fall
// back to a generic error so we never throw exceptions into the engine.
final QueryError queryError = new QueryError(query, e);
generatedQueryContext.addError(queryError);
return QueryResult.failedQueryWithError(query, queryError);
}
}
/**
* Run the generated query as part of the given query job.
* <p>
* This method is typically being run in an executor and can safely block.
*
* @param job currently executing job
* @param query the individual query to run from the current job
* @param queryContext the generated query by {@link #generate(SearchJob, Query, Set)}
* @param predecessorResults the query result of the preceding queries
* @return the result for the query
* @throws RuntimeException if the query could not be executed for some reason
*/
QueryResult doRun(SearchJob job, Query query, T queryContext, Set<QueryResult> predecessorResults);
/**
* Parse the query and return structural information about it.
* <p>
* This method decomposes the backend-specific query and returns information about used parameters, optionally the
* AST for syntax highlight and other information the UI can use to offer help.
*/
QueryMetadata parse(ImmutableSet<Parameter> parameters, Query query);
}