/
DataFrameDataExtractor.java
313 lines (268 loc) · 11.8 KB
/
DataFrameDataExtractor.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* 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.ml.dataframe.extractor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.ClearScrollAction;
import org.elasticsearch.action.search.ClearScrollRequest;
import org.elasticsearch.action.search.SearchAction;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollAction;
import org.elasticsearch.action.search.SearchScrollRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.StoredFieldsContext;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.xpack.core.ClientHelper;
import org.elasticsearch.xpack.core.ml.dataframe.analyses.DataFrameAnalysis;
import org.elasticsearch.xpack.ml.dataframe.DestinationIndex;
import org.elasticsearch.xpack.ml.extractor.ExtractedField;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* An implementation that extracts data from elasticsearch using search and scroll on a client.
* It supports safe and responsive cancellation by continuing the scroll until a new timestamp
* is seen.
* Note that this class is NOT thread-safe.
*/
public class DataFrameDataExtractor {
private static final Logger LOGGER = LogManager.getLogger(DataFrameDataExtractor.class);
private static final TimeValue SCROLL_TIMEOUT = new TimeValue(30, TimeUnit.MINUTES);
private static final String EMPTY_STRING = "";
private final Client client;
private final DataFrameDataExtractorContext context;
private String scrollId;
private boolean isCancelled;
private boolean hasNext;
private boolean searchHasShardFailure;
DataFrameDataExtractor(Client client, DataFrameDataExtractorContext context) {
this.client = Objects.requireNonNull(client);
this.context = Objects.requireNonNull(context);
hasNext = true;
searchHasShardFailure = false;
}
public Map<String, String> getHeaders() {
return Collections.unmodifiableMap(context.headers);
}
public boolean hasNext() {
return hasNext;
}
public boolean isCancelled() {
return isCancelled;
}
public void cancel() {
LOGGER.debug("[{}] Data extractor was cancelled", context.jobId);
isCancelled = true;
}
public Optional<List<Row>> next() throws IOException {
if (!hasNext()) {
throw new NoSuchElementException();
}
Optional<List<Row>> hits = scrollId == null ? Optional.ofNullable(initScroll()) : Optional.ofNullable(continueScroll());
if (!hits.isPresent()) {
hasNext = false;
}
return hits;
}
protected List<Row> initScroll() throws IOException {
LOGGER.debug("[{}] Initializing scroll", context.jobId);
return tryRequestWithSearchResponse(() -> executeSearchRequest(buildSearchRequest()));
}
private List<Row> tryRequestWithSearchResponse(Supplier<SearchResponse> request) throws IOException {
try {
// We've set allow_partial_search_results to false which means if something
// goes wrong the request will throw.
SearchResponse searchResponse = request.get();
LOGGER.debug("[{}] Search response was obtained", context.jobId);
// Request was successful so we can restore the flag to retry if a future failure occurs
searchHasShardFailure = false;
return processSearchResponse(searchResponse);
} catch (Exception e) {
if (searchHasShardFailure) {
throw e;
}
LOGGER.warn(new ParameterizedMessage("[{}] Search resulted to failure; retrying once", context.jobId), e);
markScrollAsErrored();
return initScroll();
}
}
protected SearchResponse executeSearchRequest(SearchRequestBuilder searchRequestBuilder) {
return ClientHelper.executeWithHeaders(context.headers, ClientHelper.ML_ORIGIN, client, searchRequestBuilder::get);
}
private SearchRequestBuilder buildSearchRequest() {
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE)
.setScroll(SCROLL_TIMEOUT)
// This ensures the search throws if there are failures and the scroll context gets cleared automatically
.setAllowPartialSearchResults(false)
.addSort(DestinationIndex.ID_COPY, SortOrder.ASC)
.setIndices(context.indices)
.setSize(context.scrollSize)
.setQuery(context.query);
setFetchSource(searchRequestBuilder);
for (ExtractedField docValueField : context.extractedFields.getDocValueFields()) {
searchRequestBuilder.addDocValueField(docValueField.getSearchField(), docValueField.getDocValueFormat());
}
return searchRequestBuilder;
}
private void setFetchSource(SearchRequestBuilder searchRequestBuilder) {
if (context.includeSource) {
searchRequestBuilder.setFetchSource(true);
} else {
String[] sourceFields = context.extractedFields.getSourceFields();
if (sourceFields.length == 0) {
searchRequestBuilder.setFetchSource(false);
searchRequestBuilder.storedFields(StoredFieldsContext._NONE_);
} else {
searchRequestBuilder.setFetchSource(sourceFields, null);
}
}
}
private List<Row> processSearchResponse(SearchResponse searchResponse) throws IOException {
scrollId = searchResponse.getScrollId();
if (searchResponse.getHits().getHits().length == 0) {
hasNext = false;
clearScroll(scrollId);
return null;
}
SearchHit[] hits = searchResponse.getHits().getHits();
List<Row> rows = new ArrayList<>(hits.length);
for (SearchHit hit : hits) {
if (isCancelled) {
hasNext = false;
clearScroll(scrollId);
break;
}
rows.add(createRow(hit));
}
return rows;
}
private Row createRow(SearchHit hit) {
String[] extractedValues = new String[context.extractedFields.getAllFields().size()];
for (int i = 0; i < extractedValues.length; ++i) {
ExtractedField field = context.extractedFields.getAllFields().get(i);
Object[] values = field.value(hit);
if (values.length == 1 && (values[0] instanceof Number || values[0] instanceof String)) {
extractedValues[i] = Objects.toString(values[0]);
} else {
if (values.length == 0 && context.includeRowsWithMissingValues) {
// if values is empty then it means it's a missing value
extractedValues[i] = EMPTY_STRING;
} else {
// we are here if we have a missing value but the analysis does not support those
// or the value type is not supported (e.g. arrays, etc.)
extractedValues = null;
break;
}
}
}
return new Row(extractedValues, hit);
}
private List<Row> continueScroll() throws IOException {
LOGGER.debug("[{}] Continuing scroll with id [{}]", context.jobId, scrollId);
return tryRequestWithSearchResponse(() -> executeSearchScrollRequest(scrollId));
}
private void markScrollAsErrored() {
// This could be a transient error with the scroll Id.
// Reinitialise the scroll and try again but only once.
scrollId = null;
searchHasShardFailure = true;
}
protected SearchResponse executeSearchScrollRequest(String scrollId) {
return ClientHelper.executeWithHeaders(context.headers, ClientHelper.ML_ORIGIN, client,
() -> new SearchScrollRequestBuilder(client, SearchScrollAction.INSTANCE)
.setScroll(SCROLL_TIMEOUT)
.setScrollId(scrollId)
.get());
}
private void clearScroll(String scrollId) {
if (scrollId != null) {
ClearScrollRequest request = new ClearScrollRequest();
request.addScrollId(scrollId);
ClientHelper.executeWithHeaders(context.headers, ClientHelper.ML_ORIGIN, client,
() -> client.execute(ClearScrollAction.INSTANCE, request).actionGet());
}
}
public List<String> getFieldNames() {
return context.extractedFields.getAllFields().stream().map(ExtractedField::getName).collect(Collectors.toList());
}
public DataSummary collectDataSummary() {
SearchRequestBuilder searchRequestBuilder = buildDataSummarySearchRequestBuilder();
SearchResponse searchResponse = executeSearchRequest(searchRequestBuilder);
long rows = searchResponse.getHits().getTotalHits().value;
LOGGER.debug("[{}] Data summary rows [{}]", context.jobId, rows);
return new DataSummary(rows, context.extractedFields.getAllFields().size());
}
public void collectDataSummaryAsync(ActionListener<DataSummary> dataSummaryActionListener) {
SearchRequestBuilder searchRequestBuilder = buildDataSummarySearchRequestBuilder();
final int numberOfFields = context.extractedFields.getAllFields().size();
ClientHelper.executeWithHeadersAsync(context.headers,
ClientHelper.ML_ORIGIN,
client,
SearchAction.INSTANCE,
searchRequestBuilder.request(),
ActionListener.wrap(
searchResponse -> dataSummaryActionListener.onResponse(
new DataSummary(searchResponse.getHits().getTotalHits().value, numberOfFields)),
dataSummaryActionListener::onFailure
));
}
private SearchRequestBuilder buildDataSummarySearchRequestBuilder() {
return new SearchRequestBuilder(client, SearchAction.INSTANCE)
.setIndices(context.indices)
.setSize(0)
.setQuery(context.query)
.setTrackTotalHits(true);
}
public Set<String> getCategoricalFields(DataFrameAnalysis analysis) {
return ExtractedFieldsDetector.getCategoricalFields(context.extractedFields, analysis);
}
public static class DataSummary {
public final long rows;
public final int cols;
public DataSummary(long rows, int cols) {
this.rows = rows;
this.cols = cols;
}
}
public static class Row {
private SearchHit hit;
@Nullable
private String[] values;
private Row(String[] values, SearchHit hit) {
this.values = values;
this.hit = hit;
}
@Nullable
public String[] getValues() {
return values;
}
public SearchHit getHit() {
return hit;
}
public boolean shouldSkip() {
return values == null;
}
public int getChecksum() {
return Arrays.hashCode(values);
}
}
}