/
CompositeAggregationCursor.java
245 lines (206 loc) · 8.56 KB
/
CompositeAggregationCursor.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* 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.sql.execution.search;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregation;
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.execution.search.extractor.BucketExtractor;
import org.elasticsearch.xpack.sql.querydsl.agg.Aggs;
import org.elasticsearch.xpack.sql.session.Configuration;
import org.elasticsearch.xpack.sql.session.Cursor;
import org.elasticsearch.xpack.sql.session.RowSet;
import org.elasticsearch.xpack.sql.util.StringUtils;
import java.io.IOException;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Cursor for composite aggregation (GROUP BY).
* Stores the query that gets updated/slides across requests.
*/
public class CompositeAggregationCursor implements Cursor {
private final Logger log = LogManager.getLogger(getClass());
public static final String NAME = "c";
private final String[] indices;
private final byte[] nextQuery;
private final List<BucketExtractor> extractors;
private final BitSet mask;
private final int limit;
CompositeAggregationCursor(byte[] next, List<BucketExtractor> exts, BitSet mask, int remainingLimit, String... indices) {
this.indices = indices;
this.nextQuery = next;
this.extractors = exts;
this.mask = mask;
this.limit = remainingLimit;
}
public CompositeAggregationCursor(StreamInput in) throws IOException {
indices = in.readStringArray();
nextQuery = in.readByteArray();
limit = in.readVInt();
extractors = in.readNamedWriteableList(BucketExtractor.class);
mask = BitSet.valueOf(in.readByteArray());
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeStringArray(indices);
out.writeByteArray(nextQuery);
out.writeVInt(limit);
out.writeNamedWriteableList(extractors);
out.writeByteArray(mask.toByteArray());
}
@Override
public String getWriteableName() {
return NAME;
}
String[] indices() {
return indices;
}
byte[] next() {
return nextQuery;
}
BitSet mask() {
return mask;
}
List<BucketExtractor> extractors() {
return extractors;
}
int limit() {
return limit;
}
@Override
public void nextPage(Configuration cfg, Client client, NamedWriteableRegistry registry, ActionListener<RowSet> listener) {
SearchSourceBuilder q;
try {
q = deserializeQuery(registry, nextQuery);
} catch (Exception ex) {
listener.onFailure(ex);
return;
}
SearchSourceBuilder query = q;
if (log.isTraceEnabled()) {
log.trace("About to execute composite query {} on {}", StringUtils.toString(query), indices);
}
SearchRequest search = Querier.prepareRequest(client, query, cfg.pageTimeout(), indices);
client.search(search, new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse r) {
try {
// retry
if (shouldRetryDueToEmptyPage(r)) {
CompositeAggregationCursor.updateCompositeAfterKey(r, search.source());
client.search(search, this);
return;
}
boolean hasAfterKey = updateCompositeAfterKey(r, query);
CompositeAggsRowSet rowSet = new CompositeAggsRowSet(extractors, mask, r, limit,
hasAfterKey ? serializeQuery(query) : null, indices);
listener.onResponse(rowSet);
} catch (Exception ex) {
listener.onFailure(ex);
}
}
@Override
public void onFailure(Exception ex) {
listener.onFailure(ex);
}
});
}
static boolean shouldRetryDueToEmptyPage(SearchResponse response) {
CompositeAggregation composite = getComposite(response);
// if there are no buckets but a next page, go fetch it instead of sending an empty response to the client
return composite != null && composite.getBuckets().isEmpty() && composite.afterKey() != null && !composite.afterKey().isEmpty();
}
static CompositeAggregation getComposite(SearchResponse response) {
Aggregation agg = response.getAggregations().get(Aggs.ROOT_GROUP_NAME);
if (agg == null) {
return null;
}
if (agg instanceof CompositeAggregation) {
return (CompositeAggregation) agg;
}
throw new SqlIllegalArgumentException("Unrecognized root group found; {}", agg.getClass());
}
static boolean updateCompositeAfterKey(SearchResponse r, SearchSourceBuilder next) {
CompositeAggregation composite = getComposite(r);
if (composite == null) {
throw new SqlIllegalArgumentException("Invalid server response; no group-by detected");
}
Map<String, Object> afterKey = composite.afterKey();
// a null after-key means done
if (afterKey == null) {
return false;
}
AggregationBuilder aggBuilder = next.aggregations().getAggregatorFactories().iterator().next();
// update after-key with the new value
if (aggBuilder instanceof CompositeAggregationBuilder) {
CompositeAggregationBuilder comp = (CompositeAggregationBuilder) aggBuilder;
comp.aggregateAfter(afterKey);
return true;
} else {
throw new SqlIllegalArgumentException("Invalid client request; expected a group-by but instead got {}", aggBuilder);
}
}
/**
* Deserializes the search source from a byte array.
*/
private static SearchSourceBuilder deserializeQuery(NamedWriteableRegistry registry, byte[] source) throws IOException {
try (NamedWriteableAwareStreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(source), registry)) {
return new SearchSourceBuilder(in);
}
}
/**
* Serializes the search source to a byte array.
*/
static byte[] serializeQuery(SearchSourceBuilder source) throws IOException {
if (source == null) {
return new byte[0];
}
try (BytesStreamOutput out = new BytesStreamOutput()) {
source.writeTo(out);
return BytesReference.toBytes(out.bytes());
}
}
@Override
public void clear(Configuration cfg, Client client, ActionListener<Boolean> listener) {
listener.onResponse(true);
}
@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(indices), Arrays.hashCode(nextQuery), extractors, limit);
}
@Override
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != getClass()) {
return false;
}
CompositeAggregationCursor other = (CompositeAggregationCursor) obj;
return Arrays.equals(indices, other.indices)
&& Arrays.equals(nextQuery, other.nextQuery)
&& Objects.equals(extractors, other.extractors)
&& Objects.equals(limit, other.limit);
}
@Override
public String toString() {
return "cursor for composite on index [" + Arrays.toString(indices) + "]";
}
}