-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
DownsampleShardPersistentTaskExecutor.java
321 lines (287 loc) · 13.9 KB
/
DownsampleShardPersistentTaskExecutor.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
314
315
316
317
318
319
320
321
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.downsample;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.TransportAction;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.mapper.TimeSeriesIdFieldMapper;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.shard.ShardNotFoundException;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.persistent.AllocatedPersistentTask;
import org.elasticsearch.persistent.PersistentTaskState;
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
import org.elasticsearch.persistent.PersistentTasksExecutor;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.downsample.DownsampleShardIndexerStatus;
import org.elasticsearch.xpack.core.downsample.DownsampleShardPersistentTaskState;
import org.elasticsearch.xpack.core.downsample.DownsampleShardTask;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
public class DownsampleShardPersistentTaskExecutor extends PersistentTasksExecutor<DownsampleShardTaskParams> {
private static final Logger LOGGER = LogManager.getLogger(DownsampleShardPersistentTaskExecutor.class);
private final Client client;
public DownsampleShardPersistentTaskExecutor(final Client client, final String taskName, final Executor executor) {
super(taskName, executor);
this.client = Objects.requireNonNull(client);
}
@Override
protected void nodeOperation(
final AllocatedPersistentTask task,
final DownsampleShardTaskParams params,
final PersistentTaskState state
) {
// NOTE: query the downsampling target index so that we can start the downsampling task from the latest indexed tsid.
final SearchRequest searchRequest = new SearchRequest(params.downsampleIndex());
searchRequest.source().sort(TimeSeriesIdFieldMapper.NAME, SortOrder.DESC).size(1);
searchRequest.preference("_shards:" + params.shardId().id());
client.search(searchRequest, ActionListener.wrap(searchResponse -> {
delegate(task, params, extractTsId(searchResponse.getHits().getHits()));
}, e -> delegate(task, params, null)));
}
private static BytesRef extractTsId(SearchHit[] lastDownsampleTsidHits) {
if (lastDownsampleTsidHits.length == 0) {
return null;
} else {
var searchHit = Arrays.stream(lastDownsampleTsidHits).findFirst().get();
var field = searchHit.field("_tsid");
return field != null ? field.getValue() : null;
}
}
@Override
protected AllocatedPersistentTask createTask(
long id,
final String type,
final String action,
final TaskId parentTaskId,
final PersistentTasksCustomMetadata.PersistentTask<DownsampleShardTaskParams> taskInProgress,
final Map<String, String> headers
) {
final DownsampleShardTaskParams params = taskInProgress.getParams();
return new DownsampleShardTask(
id,
type,
action,
parentTaskId,
params.downsampleIndex(),
params.indexStartTimeMillis(),
params.indexEndTimeMillis(),
params.downsampleConfig(),
headers,
params.shardId()
);
}
@Override
public void validate(DownsampleShardTaskParams params, ClusterState clusterState) {
// This is just a pre-check, but doesn't prevent from avoiding from aborting the task when source index disappeared
// after initial creation of the persistent task.
var indexShardRouting = clusterState.routingTable().shardRoutingTable(params.shardId().getIndexName(), params.shardId().id());
if (indexShardRouting == null) {
throw new ShardNotFoundException(params.shardId());
}
}
@Override
public PersistentTasksCustomMetadata.Assignment getAssignment(
final DownsampleShardTaskParams params,
final Collection<DiscoveryNode> candidateNodes,
final ClusterState clusterState
) {
// NOTE: downsampling works by running a task per each shard of the source index.
// Here we make sure we assign the task to the actual node holding the shard identified by
// the downsampling task shard id.
final ShardId shardId = params.shardId();
// If during re-assignment the source index was deleted, then we need to break out.
// Returning NO_NODE_FOUND just keeps the persistent task until the source index appears again (which would never happen)
// So let's return a node and then in the node operation we would just fail and stop this persistent task
var indexShardRouting = findShardRoutingTable(shardId, clusterState);
if (indexShardRouting == null) {
var node = selectLeastLoadedNode(clusterState, candidateNodes, DiscoveryNode::canContainData);
return new PersistentTasksCustomMetadata.Assignment(node.getId(), "a node to fail and stop this persistent task");
}
final ShardRouting shardRouting = indexShardRouting.primaryShard();
if (shardRouting.started() == false) {
return NO_NODE_FOUND;
}
return candidateNodes.stream()
.filter(candidateNode -> candidateNode.getId().equals(shardRouting.currentNodeId()))
.findAny()
.map(
node -> new PersistentTasksCustomMetadata.Assignment(
node.getId(),
"downsampling using node holding shard [" + shardId + "]"
)
)
.orElse(NO_NODE_FOUND);
}
@Override
public Executor getExecutor() {
// The delegate action forks to the a downsample thread:
return EsExecutors.DIRECT_EXECUTOR_SERVICE;
}
private void delegate(final AllocatedPersistentTask task, final DownsampleShardTaskParams params, final BytesRef lastDownsampleTsid) {
DownsampleShardTask downsampleShardTask = (DownsampleShardTask) task;
client.execute(
DelegatingAction.INSTANCE,
new DelegatingAction.Request(downsampleShardTask, lastDownsampleTsid, params),
ActionListener.wrap(empty -> {}, e -> {
LOGGER.error("error while delegating", e);
markAsFailed(downsampleShardTask, e);
})
);
}
private static IndexShardRoutingTable findShardRoutingTable(ShardId shardId, ClusterState clusterState) {
var indexRoutingTable = clusterState.routingTable().index(shardId.getIndexName());
if (indexRoutingTable != null) {
return indexRoutingTable.shard(shardId.getId());
}
return null;
}
static void realNodeOperation(
Client client,
IndicesService indicesService,
DownsampleShardTask task,
DownsampleShardTaskParams params,
BytesRef lastDownsampledTsid
) {
client.threadPool().executor(Downsample.DOWNSAMPLE_TASK_THREAD_POOL_NAME).execute(new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
markAsFailed(task, e);
}
@Override
protected void doRun() throws Exception {
final var initialState = new DownsampleShardPersistentTaskState(
DownsampleShardIndexerStatus.INITIALIZED,
lastDownsampledTsid
);
try {
final var downsampleShardIndexer = new DownsampleShardIndexer(
task,
client,
indicesService.indexServiceSafe(params.shardId().getIndex()),
params.shardId(),
params.downsampleIndex(),
params.downsampleConfig(),
params.metrics(),
params.labels(),
params.dimensions(),
initialState
);
downsampleShardIndexer.execute();
task.markAsCompleted();
} catch (final DownsampleShardIndexerException e) {
if (e.isRetriable()) {
LOGGER.warn("Downsampling task [" + task.getPersistentTaskId() + " retriable failure [" + e.getMessage() + "]");
task.markAsLocallyAborted(e.getMessage());
} else {
LOGGER.error(
"Downsampling task [" + task.getPersistentTaskId() + " non retriable failure [" + e.getMessage() + "]"
);
markAsFailed(task, e);
}
} catch (IndexNotFoundException e) {
LOGGER.error("Downsampling task [" + task.getPersistentTaskId() + " failing because source index not assigned");
markAsFailed(task, e);
} catch (final Exception e) {
LOGGER.error("Downsampling task [" + task.getPersistentTaskId() + " non-retriable failure [" + e.getMessage() + "]");
markAsFailed(task, e);
}
}
});
}
private static void markAsFailed(DownsampleShardTask task, Exception e) {
task.setDownsampleShardIndexerStatus(DownsampleShardIndexerStatus.FAILED);
task.updatePersistentTaskState(
new DownsampleShardPersistentTaskState(DownsampleShardIndexerStatus.FAILED, null),
ActionListener.running(() -> task.markAsFailed(e))
);
}
// This is needed for FLS/DLS to work correctly. The _indices_permissions in the thread local aren't set if an searcher is acquired
// directly from this persistent task executor. By delegating to this action (with a request that implements IndicesRequest) the
// security thread local will be setup correctly so that we avoid this error:
// org.elasticsearch.ElasticsearchSecurityException: no indices permissions found
public static class DelegatingAction extends ActionType<ActionResponse.Empty> {
public static final DelegatingAction INSTANCE = new DelegatingAction();
public static final String NAME = "indices:data/read/downsample_delegate";
private DelegatingAction() {
super(NAME);
}
public static class Request extends ActionRequest implements IndicesRequest.RemoteClusterShardRequest {
private final DownsampleShardTask task;
private final BytesRef lastDownsampleTsid;
private final DownsampleShardTaskParams params;
public Request(DownsampleShardTask task, BytesRef lastDownsampleTsid, DownsampleShardTaskParams params) {
this.task = task;
this.lastDownsampleTsid = lastDownsampleTsid;
this.params = params;
}
@Override
public ActionRequestValidationException validate() {
return null;
}
@Override
public String[] indices() {
return new String[] { params.shardId().getIndexName() };
}
@Override
public IndicesOptions indicesOptions() {
return IndicesOptions.STRICT_EXPAND_OPEN;
}
@Override
public void writeTo(StreamOutput out) {
throw new IllegalStateException("request should stay local");
}
@Override
public Collection<ShardId> shards() {
return Collections.singletonList(task.shardId());
}
}
public static class TA extends TransportAction<Request, ActionResponse.Empty> {
private final Client client;
private final IndicesService indicesService;
@Inject
public TA(TransportService transportService, ActionFilters actionFilters, Client client, IndicesService indicesService) {
super(NAME, actionFilters, transportService.getTaskManager());
this.client = client;
this.indicesService = indicesService;
}
@Override
protected void doExecute(Task t, Request request, ActionListener<ActionResponse.Empty> listener) {
realNodeOperation(client, indicesService, request.task, request.params, request.lastDownsampleTsid);
listener.onResponse(ActionResponse.Empty.INSTANCE);
}
}
}
}