Skip to content

Commit

Permalink
Fixing HLRC parsing of CancelTasks response (elastic#45414)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesinity committed Sep 24, 2019
1 parent 02b6ce9 commit 8affb56
Show file tree
Hide file tree
Showing 17 changed files with 1,291 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.client.security.RefreshPolicy;
import org.elasticsearch.client.tasks.TaskId;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Priority;
Expand Down Expand Up @@ -81,7 +82,6 @@
import org.elasticsearch.script.mustache.MultiSearchTemplateRequest;
import org.elasticsearch.script.mustache.SearchTemplateRequest;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.tasks.TaskId;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -1051,6 +1051,20 @@ Params withActions(String[] actions) {
return this;
}

Params withTaskId(org.elasticsearch.tasks.TaskId taskId) {
if (taskId != null && taskId.isSet()) {
return putParam("task_id", taskId.toString());
}
return this;
}

Params withParentTaskId(org.elasticsearch.tasks.TaskId parentTaskId) {
if (parentTaskId != null && parentTaskId.isSet()) {
return putParam("parent_task_id", parentTaskId.toString());
}
return this;
}

Params withTaskId(TaskId taskId) {
if (taskId != null && taskId.isSet()) {
return putParam("task_id", taskId.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
package org.elasticsearch.client;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest;
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse;
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
import org.elasticsearch.client.tasks.CancelTasksRequest;
import org.elasticsearch.client.tasks.CancelTasksResponse;
import org.elasticsearch.client.tasks.GetTaskRequest;
import org.elasticsearch.client.tasks.GetTaskResponse;

Expand Down Expand Up @@ -102,17 +102,7 @@ public Cancellable getAsync(GetTaskRequest request, RequestOptions options,
GetTaskResponse::fromXContent, listener);
}

/**
* Cancel one or more cluster tasks using the Task Management API.
*
* See
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html"> Task Management API on elastic.co</a>
* @param cancelTasksRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response
*
*/

public CancelTasksResponse cancel(CancelTasksRequest cancelTasksRequest, RequestOptions options ) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(
cancelTasksRequest,
Expand All @@ -135,6 +125,7 @@ public CancelTasksResponse cancel(CancelTasksRequest cancelTasksRequest, Request
*/
public Cancellable cancelAsync(CancelTasksRequest cancelTasksRequest, RequestOptions options,
ActionListener<CancelTasksResponse> listener) {

return restHighLevelClient.performRequestAsyncAndParseEntity(
cancelTasksRequest,
TasksRequestConverters::cancelTasks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest;
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
import org.elasticsearch.client.RequestConverters.EndpointBuilder;
import org.elasticsearch.client.tasks.CancelTasksRequest;
import org.elasticsearch.client.tasks.GetTaskRequest;

final class TasksRequestConverters {
Expand Down Expand Up @@ -70,5 +70,5 @@ static Request getTask(GetTaskRequest getTaskRequest) {
request.addParameters(params.asMap());
return request;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.tasks;

import org.elasticsearch.client.Validatable;
import org.elasticsearch.common.unit.TimeValue;

import java.util.Arrays;
import java.util.Objects;

public class CancelTasksRequest implements Validatable {

public static final String[] EMPTY_ARRAY = new String[0];
public static final String[] ALL_ACTIONS = EMPTY_ARRAY;
public static final String[] ALL_NODES = EMPTY_ARRAY;
private String[] nodes = ALL_NODES;
private TimeValue timeout;
private String[] actions = ALL_ACTIONS;
private TaskId parentTaskId = TaskId.EMPTY_TASK_ID;
private TaskId taskId = TaskId.EMPTY_TASK_ID;
private String reason = "";

public final CancelTasksRequest setNodes(String... nodes) {
this.nodes = nodes;
return this;
}

public String[] getNodes() {
return nodes;
}

public CancelTasksRequest setTimeout(TimeValue timeout) {
this.timeout = timeout;
return this;
}

public final CancelTasksRequest setTimeout(String timeout) {
this.timeout = TimeValue.parseTimeValue(timeout, null, getClass().getSimpleName() + ".timeout");
return this;
}

public TimeValue getTimeout() {
return timeout;
}

public CancelTasksRequest setActions(String... actions) {
this.actions = actions;
return this;
}

public String[] getActions() {
return actions;
}

public CancelTasksRequest setParentTaskId(TaskId parentTaskId) {
this.parentTaskId = parentTaskId;
return this;
}

public TaskId getParentTaskId() {
return parentTaskId;
}

public CancelTasksRequest setTaskId(TaskId taskId) {
this.taskId = taskId;
return this;
}

public TaskId getTaskId() {
return taskId;
}


public String getReason() {
return reason;
}

public void setReason(String reason) {
this.reason = reason;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CancelTasksRequest)) return false;
CancelTasksRequest that = (CancelTasksRequest) o;
return Arrays.equals(getNodes(), that.getNodes()) &&
Objects.equals(getTimeout(), that.getTimeout()) &&
Arrays.equals(getActions(), that.getActions()) &&
Objects.equals(getParentTaskId(), that.getParentTaskId()) &&
Objects.equals(getTaskId(), that.getTaskId()) &&
Objects.equals(getReason(), that.getReason());
}

@Override
public int hashCode() {
int result = Objects.hash(getTimeout(), getParentTaskId(), getTaskId(), getReason());
result = 31 * result + Arrays.hashCode(getNodes());
result = 31 * result + Arrays.hashCode(getActions());
return result;
}

@Override
public String toString() {
return "CancelTasksRequest{" +
"nodes=" + Arrays.toString(nodes) +
", timeout=" + timeout +
", actions=" + Arrays.toString(actions) +
", parentTaskId=" + parentTaskId +
", taskId=" + taskId +
", reason='" + reason + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.tasks;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;

public class CancelTasksResponse {

private final NodesInfoData nodesInfoData;
private final List<TaskOperationFailure> taskFailures = new ArrayList<>();
private final List<ElasticsearchException> nodeFailures = new ArrayList<>();
private final List<TaskInfo> tasks = new ArrayList<>();
private final List<TaskGroup> taskGroups = new ArrayList<>();

public CancelTasksResponse(NodesInfoData nodesInfoData,
List<TaskOperationFailure> taskFailures,
List<ElasticsearchException> nodeFailures) {
this.nodesInfoData = nodesInfoData;
if (taskFailures!= null){
this.taskFailures.addAll(taskFailures);
}
if (nodeFailures!=null) {
this.nodeFailures.addAll(nodeFailures);
}
this.tasks.addAll(nodesInfoData
.getNodesInfoData()
.stream()
.flatMap(nodeData -> nodeData.getTasks().stream())
.collect(toList())
);

this.taskGroups.addAll(buildTaskGroups());
}

private List<TaskGroup> buildTaskGroups() {
Map<TaskId, TaskGroup.Builder> taskGroups = new HashMap<>();
List<TaskGroup.Builder> topLevelTasks = new ArrayList<>();
// First populate all tasks
for (TaskInfo taskInfo : this.tasks) {
taskGroups.put(taskInfo.getTaskId(), TaskGroup.builder(taskInfo));
}

// Now go through all task group builders and add children to their parents
for (TaskGroup.Builder taskGroup : taskGroups.values()) {
TaskId parentTaskId = taskGroup.getTaskInfo().getParentTaskId();
if (parentTaskId.isSet()) {
TaskGroup.Builder parentTask = taskGroups.get(parentTaskId);
if (parentTask != null) {
// we found parent in the list of tasks - add it to the parent list
parentTask.addGroup(taskGroup);
} else {
// we got zombie or the parent was filtered out - add it to the top task list
topLevelTasks.add(taskGroup);
}
} else {
// top level task - add it to the top task list
topLevelTasks.add(taskGroup);
}
}
return Collections.unmodifiableList(topLevelTasks.stream().map(TaskGroup.Builder::build).collect(Collectors.toList()));
}

public NodesInfoData getNodesInfoData() {
return nodesInfoData;
}

public List<TaskInfo> getTasks() {
return tasks;
}

public Map<String, List<TaskInfo>> getPerNodeTasks() {
return getTasks()
.stream()
.collect(groupingBy(TaskInfo::getNodeId));
}

public List<TaskOperationFailure> getTaskFailures() {
return taskFailures;
}

public List<ElasticsearchException> getNodeFailures() {
return nodeFailures;
}

public List<TaskGroup> getTaskGroups() {
return taskGroups;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CancelTasksResponse)) return false;
CancelTasksResponse response = (CancelTasksResponse) o;
return getNodesInfoData().equals(response.getNodesInfoData()) &&
Objects.equals(getTaskFailures(), response.getTaskFailures()) &&
Objects.equals(getNodeFailures(), response.getNodeFailures()) &&
Objects.equals(getTasks(), response.getTasks()) &&
Objects.equals(getTaskGroups(), response.getTaskGroups());
}

@Override
public int hashCode() {
return Objects.hash(getNodesInfoData(), getTaskFailures(), getNodeFailures(), getTasks(), getTaskGroups());
}

public static CancelTasksResponse fromXContent(final XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}

@Override
public String toString() {
return "CancelTasksResponse{" +
"nodesInfoData=" + nodesInfoData +
", taskFailures=" + taskFailures +
", nodeFailures=" + nodeFailures +
", tasks=" + tasks +
", taskGroups=" + taskGroups +
'}';
}

private static ConstructingObjectParser<CancelTasksResponse, Void> PARSER;

static {
ConstructingObjectParser<CancelTasksResponse, Void> parser = new ConstructingObjectParser<>("cancel_tasks_response", true,
constructingObjects -> {
int i = 0;
@SuppressWarnings("unchecked")
List<TaskOperationFailure> tasksFailures = (List<TaskOperationFailure>) constructingObjects[i++];
@SuppressWarnings("unchecked")
List<ElasticsearchException> nodeFailures = (List<ElasticsearchException>) constructingObjects[i++];
NodesInfoData nodesInfoData = (NodesInfoData) constructingObjects[i];
return new CancelTasksResponse(nodesInfoData, tasksFailures, nodeFailures);
});

parser.declareObjectArray(optionalConstructorArg(), (p, c) ->
TaskOperationFailure.fromXContent(p), new ParseField("task_failures"));
parser.declareObjectArray(optionalConstructorArg(), (p, c) ->
ElasticsearchException.fromXContent(p), new ParseField("node_failures"));
parser.declareObject(optionalConstructorArg(), NodesInfoData.PARSER, new ParseField("nodes"));
PARSER = parser;
}
}
Loading

0 comments on commit 8affb56

Please sign in to comment.