|
16 | 16 | import org.elasticsearch.action.FailedNodeException;
|
17 | 17 | import org.elasticsearch.action.TaskOperationFailure;
|
18 | 18 | import org.elasticsearch.action.support.ActionFilters;
|
| 19 | +import org.elasticsearch.action.support.GroupedActionListener; |
19 | 20 | import org.elasticsearch.action.support.tasks.TransportTasksAction;
|
20 | 21 | import org.elasticsearch.client.Client;
|
21 | 22 | import org.elasticsearch.cluster.ClusterState;
|
@@ -158,6 +159,7 @@ static Tuple<Set<String>, Set<String>> findTasksWithoutConfig(ClusterState state
|
158 | 159 | protected void doExecute(Task task, Request request, ActionListener<Response> listener) {
|
159 | 160 | final ClusterState state = clusterService.state();
|
160 | 161 | final DiscoveryNodes nodes = state.nodes();
|
| 162 | + |
161 | 163 | if (nodes.isLocalNodeElectedMaster() == false) {
|
162 | 164 | // Delegates stop transform to elected master node so it becomes the coordinating node.
|
163 | 165 | if (nodes.getMasterNode() == null) {
|
@@ -185,15 +187,29 @@ protected void doExecute(Task task, Request request, ActionListener<Response> li
|
185 | 187 | ActionListener.wrap(hitsAndIds -> {
|
186 | 188 | validateTaskState(state, hitsAndIds.v2(), request.isForce());
|
187 | 189 | request.setExpandedIds(new HashSet<>(hitsAndIds.v2()));
|
188 |
| - request.setNodes(TransformNodes.transformTaskNodes(hitsAndIds.v2(), state)); |
189 |
| - super.doExecute(task, request, finalListener); |
| 190 | + final TransformNodeAssignments transformNodeAssignments = TransformNodes.transformTaskNodes(hitsAndIds.v2(), state); |
| 191 | + |
| 192 | + final ActionListener<Response> doExecuteListener; |
| 193 | + if (transformNodeAssignments.getWaitingForAssignment().size() > 0) { |
| 194 | + doExecuteListener = cancelTransformTasksWithNoAssignment(finalListener, transformNodeAssignments); |
| 195 | + } else { |
| 196 | + doExecuteListener = finalListener; |
| 197 | + } |
| 198 | + |
| 199 | + if (transformNodeAssignments.getExecutorNodes().size() > 0) { |
| 200 | + request.setNodes(transformNodeAssignments.getExecutorNodes().toArray(new String[0])); |
| 201 | + super.doExecute(task, request, doExecuteListener); |
| 202 | + } else { |
| 203 | + doExecuteListener.onResponse(new Response(true)); |
| 204 | + } |
190 | 205 | }, e -> {
|
191 | 206 | if (e instanceof ResourceNotFoundException) {
|
192 | 207 | Tuple<Set<String>, Set<String>> runningTasksAndNodes = findTasksWithoutConfig(state, request.getId());
|
193 | 208 | if (runningTasksAndNodes.v1().isEmpty()) {
|
194 | 209 | listener.onFailure(e);
|
195 | 210 | // found transforms without a config
|
196 | 211 | } else if (request.isForce()) {
|
| 212 | + // TODO: handle tasks waiting for assignment |
197 | 213 | request.setExpandedIds(runningTasksAndNodes.v1());
|
198 | 214 | request.setNodes(runningTasksAndNodes.v2().toArray(new String[0]));
|
199 | 215 | super.doExecute(task, request, finalListener);
|
@@ -457,4 +473,31 @@ private void waitForTransformStopped(
|
457 | 473 | listener.onFailure(e);
|
458 | 474 | }));
|
459 | 475 | }
|
| 476 | + |
| 477 | + private ActionListener<Response> cancelTransformTasksWithNoAssignment( |
| 478 | + final ActionListener<Response> finalListener, |
| 479 | + final TransformNodeAssignments transformNodeAssignments |
| 480 | + ) { |
| 481 | + final ActionListener<Response> doExecuteListener = ActionListener.wrap(response -> { |
| 482 | + GroupedActionListener<PersistentTask<?>> groupedListener = new GroupedActionListener<>( |
| 483 | + ActionListener.wrap(r -> { finalListener.onResponse(response); }, finalListener::onFailure), |
| 484 | + transformNodeAssignments.getWaitingForAssignment().size() |
| 485 | + ); |
| 486 | + |
| 487 | + for (String unassignedTaskId : transformNodeAssignments.getWaitingForAssignment()) { |
| 488 | + persistentTasksService.sendRemoveRequest(unassignedTaskId, groupedListener); |
| 489 | + } |
| 490 | + |
| 491 | + }, e -> { |
| 492 | + GroupedActionListener<PersistentTask<?>> groupedListener = new GroupedActionListener<>( |
| 493 | + ActionListener.wrap(r -> { finalListener.onFailure(e); }, finalListener::onFailure), |
| 494 | + transformNodeAssignments.getWaitingForAssignment().size() |
| 495 | + ); |
| 496 | + |
| 497 | + for (String unassignedTaskId : transformNodeAssignments.getWaitingForAssignment()) { |
| 498 | + persistentTasksService.sendRemoveRequest(unassignedTaskId, groupedListener); |
| 499 | + } |
| 500 | + }); |
| 501 | + return doExecuteListener; |
| 502 | + } |
460 | 503 | }
|
0 commit comments