|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.tasks; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +import org.elasticsearch.common.ParseField; |
| 31 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 32 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 33 | + |
| 34 | +import static java.util.stream.Collectors.groupingBy; |
| 35 | +import static java.util.stream.Collectors.toList; |
| 36 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; |
| 37 | + |
| 38 | +public class CancelTasksResponse { |
| 39 | + |
| 40 | + private final NodesInfoData nodesInfoData; |
| 41 | + private final List<TaskOperationFailure> taskFailures = new ArrayList<>(); |
| 42 | + private final List<ElasticsearchException> nodeFailures = new ArrayList<>(); |
| 43 | + private final List<TaskInfo> tasks = new ArrayList<>(); |
| 44 | + private final List<TaskGroup> taskGroups = new ArrayList<>(); |
| 45 | + |
| 46 | + public CancelTasksResponse(NodesInfoData nodesInfoData, |
| 47 | + List<TaskOperationFailure> taskFailures, |
| 48 | + List<ElasticsearchException> nodeFailures) { |
| 49 | + this.nodesInfoData = nodesInfoData; |
| 50 | + if (taskFailures!= null){ |
| 51 | + this.taskFailures.addAll(taskFailures); |
| 52 | + } |
| 53 | + if (nodeFailures!=null) { |
| 54 | + this.nodeFailures.addAll(nodeFailures); |
| 55 | + } |
| 56 | + this.tasks.addAll(nodesInfoData |
| 57 | + .getNodesInfoData() |
| 58 | + .stream() |
| 59 | + .flatMap(nodeData -> nodeData.getTasks().stream()) |
| 60 | + .collect(toList()) |
| 61 | + ); |
| 62 | + |
| 63 | + this.taskGroups.addAll(buildTaskGroups()); |
| 64 | + } |
| 65 | + |
| 66 | + private List<TaskGroup> buildTaskGroups() { |
| 67 | + Map<TaskId, TaskGroup.Builder> taskGroups = new HashMap<>(); |
| 68 | + List<TaskGroup.Builder> topLevelTasks = new ArrayList<>(); |
| 69 | + // First populate all tasks |
| 70 | + for (TaskInfo taskInfo : this.tasks) { |
| 71 | + taskGroups.put(taskInfo.getTaskId(), TaskGroup.builder(taskInfo)); |
| 72 | + } |
| 73 | + |
| 74 | + // Now go through all task group builders and add children to their parents |
| 75 | + for (TaskGroup.Builder taskGroup : taskGroups.values()) { |
| 76 | + TaskId parentTaskId = taskGroup.getTaskInfo().getParentTaskId(); |
| 77 | + if (parentTaskId.isSet()) { |
| 78 | + TaskGroup.Builder parentTask = taskGroups.get(parentTaskId); |
| 79 | + if (parentTask != null) { |
| 80 | + // we found parent in the list of tasks - add it to the parent list |
| 81 | + parentTask.addGroup(taskGroup); |
| 82 | + } else { |
| 83 | + // we got zombie or the parent was filtered out - add it to the top task list |
| 84 | + topLevelTasks.add(taskGroup); |
| 85 | + } |
| 86 | + } else { |
| 87 | + // top level task - add it to the top task list |
| 88 | + topLevelTasks.add(taskGroup); |
| 89 | + } |
| 90 | + } |
| 91 | + return Collections.unmodifiableList(topLevelTasks.stream().map(TaskGroup.Builder::build).collect(Collectors.toList())); |
| 92 | + } |
| 93 | + |
| 94 | + public NodesInfoData getNodesInfoData() { |
| 95 | + return nodesInfoData; |
| 96 | + } |
| 97 | + |
| 98 | + public List<TaskInfo> getTasks() { |
| 99 | + return tasks; |
| 100 | + } |
| 101 | + |
| 102 | + public Map<String, List<TaskInfo>> getPerNodeTasks() { |
| 103 | + return getTasks() |
| 104 | + .stream() |
| 105 | + .collect(groupingBy(TaskInfo::getNodeId)); |
| 106 | + } |
| 107 | + |
| 108 | + public List<TaskOperationFailure> getTaskFailures() { |
| 109 | + return taskFailures; |
| 110 | + } |
| 111 | + |
| 112 | + public List<ElasticsearchException> getNodeFailures() { |
| 113 | + return nodeFailures; |
| 114 | + } |
| 115 | + |
| 116 | + public List<TaskGroup> getTaskGroups() { |
| 117 | + return taskGroups; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public boolean equals(Object o) { |
| 122 | + if (this == o) return true; |
| 123 | + if (!(o instanceof CancelTasksResponse)) return false; |
| 124 | + CancelTasksResponse response = (CancelTasksResponse) o; |
| 125 | + return getNodesInfoData().equals(response.getNodesInfoData()) && |
| 126 | + Objects.equals(getTaskFailures(), response.getTaskFailures()) && |
| 127 | + Objects.equals(getNodeFailures(), response.getNodeFailures()) && |
| 128 | + Objects.equals(getTasks(), response.getTasks()) && |
| 129 | + Objects.equals(getTaskGroups(), response.getTaskGroups()); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public int hashCode() { |
| 134 | + return Objects.hash(getNodesInfoData(), getTaskFailures(), getNodeFailures(), getTasks(), getTaskGroups()); |
| 135 | + } |
| 136 | + |
| 137 | + public static CancelTasksResponse fromXContent(final XContentParser parser) throws IOException { |
| 138 | + return PARSER.parse(parser, null); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public String toString() { |
| 143 | + return "CancelTasksResponse{" + |
| 144 | + "nodesInfoData=" + nodesInfoData + |
| 145 | + ", taskFailures=" + taskFailures + |
| 146 | + ", nodeFailures=" + nodeFailures + |
| 147 | + ", tasks=" + tasks + |
| 148 | + ", taskGroups=" + taskGroups + |
| 149 | + '}'; |
| 150 | + } |
| 151 | + |
| 152 | + private static ConstructingObjectParser<CancelTasksResponse, Void> PARSER; |
| 153 | + |
| 154 | + static { |
| 155 | + ConstructingObjectParser<CancelTasksResponse, Void> parser = new ConstructingObjectParser<>("cancel_tasks_response", true, |
| 156 | + constructingObjects -> { |
| 157 | + int i = 0; |
| 158 | + @SuppressWarnings("unchecked") |
| 159 | + List<TaskOperationFailure> tasksFailures = (List<TaskOperationFailure>) constructingObjects[i++]; |
| 160 | + @SuppressWarnings("unchecked") |
| 161 | + List<ElasticsearchException> nodeFailures = (List<ElasticsearchException>) constructingObjects[i++]; |
| 162 | + NodesInfoData nodesInfoData = (NodesInfoData) constructingObjects[i]; |
| 163 | + return new CancelTasksResponse(nodesInfoData, tasksFailures, nodeFailures); |
| 164 | + }); |
| 165 | + |
| 166 | + parser.declareObjectArray(optionalConstructorArg(), (p, c) -> |
| 167 | + TaskOperationFailure.fromXContent(p), new ParseField("task_failures")); |
| 168 | + parser.declareObjectArray(optionalConstructorArg(), (p, c) -> |
| 169 | + ElasticsearchException.fromXContent(p), new ParseField("node_failures")); |
| 170 | + parser.declareObject(optionalConstructorArg(), NodesInfoData.PARSER, new ParseField("nodes")); |
| 171 | + PARSER = parser; |
| 172 | + } |
| 173 | +} |
0 commit comments