Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HLRC parsing of CancelTasks response #47017

Merged
merged 7 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -80,13 +81,13 @@
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;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -1029,19 +1030,41 @@ Params withWaitForCompletion(Boolean waitForCompletion) {
}

Params withNodes(String[] nodes) {
if (nodes != null && nodes.length > 0) {
return withNodes(Arrays.asList(nodes));
}

Params withNodes(List<String> nodes) {
if (nodes != null && nodes.size() > 0) {
return putParam("nodes", String.join(",", nodes));
}
return this;
}

Params withActions(String[] actions) {
if (actions != null && actions.length > 0) {
return withActions(Arrays.asList(actions));
}

Params withActions(List<String> actions) {
if (actions != null && actions.size() > 0) {
return putParam("actions", String.join(",", 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@

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 {

private TasksRequestConverters() {}

static Request cancelTasks(CancelTasksRequest cancelTasksRequest) {
static Request cancelTasks(CancelTasksRequest req) {
Request request = new Request(HttpPost.METHOD_NAME, "/_tasks/_cancel");
RequestConverters.Params params = new RequestConverters.Params();
params.withTimeout(cancelTasksRequest.getTimeout())
.withTaskId(cancelTasksRequest.getTaskId())
.withNodes(cancelTasksRequest.getNodes())
.withParentTaskId(cancelTasksRequest.getParentTaskId())
.withActions(cancelTasksRequest.getActions());
req.getTimeout().ifPresent(params::withTimeout);
req.getTaskId().ifPresent(params::withTaskId);
req.getParentTaskId().ifPresent(params::withParentTaskId);
params
.withNodes(req.getNodes())
.withActions(req.getActions());
request.addParameters(params.asMap());
return request;
}
Expand Down Expand Up @@ -70,5 +71,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,151 @@
/*
* 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.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class CancelTasksRequest implements Validatable {

private final List<String> nodes = new ArrayList<>();
private final List<String> actions = new ArrayList<>();
private Optional<TimeValue> timeout = Optional.empty();
private Optional<TaskId> parentTaskId = Optional.empty();
private Optional<TaskId> taskId = Optional.empty();

CancelTasksRequest(){}

void setNodes(List<String> nodes) {
this.nodes.addAll(nodes);
}

public List<String> getNodes() {
return nodes;
}

void setTimeout(TimeValue timeout) {
this.timeout = Optional.of(timeout);
}

public Optional<TimeValue> getTimeout() {
return timeout;
}

void setActions(List<String> actions) {
this.actions.addAll(actions);
}

public List<String> getActions() {
return actions;
}

void setParentTaskId(TaskId parentTaskId) {
this.parentTaskId = Optional.of(parentTaskId);
}

public Optional<TaskId> getParentTaskId() {
return parentTaskId;
}

void setTaskId(TaskId taskId) {
this.taskId = Optional.of(taskId);
}

public Optional<TaskId> getTaskId() {
return taskId;
}

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

@Override
public int hashCode() {
return Objects.hash(getNodes(), getActions(), getTimeout(), getParentTaskId(), getTaskId());
}

@Override
public String toString() {
return "CancelTasksRequest{" +
"nodes=" + nodes +
", actions=" + actions +
", timeout=" + timeout +
", parentTaskId=" + parentTaskId +
", taskId=" + taskId +
'}';
}

public static class Builder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets remove this builder since some of the setters on the class itself already return this, and then add return this; to the methods in the Request that do not have them already.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe lets just remove the "return this" from the methods in the class and keep the builder, i think that makes more sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

private Optional<TimeValue> timeout = Optional.empty();
private Optional<TaskId> taskId = Optional.empty();
private Optional<TaskId> parentTaskId = Optional.empty();
private List<String> actionsFilter = new ArrayList<>();
private List<String> nodesFilter = new ArrayList<>();

public Builder withTimeout(TimeValue timeout){
this.timeout = Optional.of(timeout);
return this;
}

public Builder withTaskId(TaskId taskId){
this.taskId = Optional.of(taskId);
return this;
}

public Builder withParentTaskId(TaskId taskId){
this.parentTaskId = Optional.of(taskId);
return this;
}

public Builder withActionsFiltered(List<String> actions){
this.actionsFilter.clear();
this.actionsFilter.addAll(actions);
return this;
}

public Builder withNodesFiltered(List<String> nodes){
this.nodesFilter.clear();
this.nodesFilter.addAll(nodes);
return this;
}

public CancelTasksRequest build() {
CancelTasksRequest request = new CancelTasksRequest();
timeout.ifPresent(request::setTimeout);
taskId.ifPresent(request::setTaskId);
parentTaskId.ifPresent(request::setParentTaskId);
request.setNodes(nodesFilter);
request.setActions(actionsFilter);
return request;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.List;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;

/**
* cancel tasks response that contains
* - task failures
* - node failures
* - tasks
*/
public class CancelTasksResponse extends ListTasksResponse {

CancelTasksResponse(List<NodeData> nodesInfoData,
List<TaskOperationFailure> taskFailures,
List<ElasticsearchException> nodeFailures) {
super(nodesInfoData, taskFailures, nodeFailures);
}

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

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++];
@SuppressWarnings("unchecked")
List<NodeData> nodesInfoData = (List<NodeData>) 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.declareNamedObjects(optionalConstructorArg(), NodeData.PARSER, new ParseField("nodes"));
PARSER = parser;
}

@Override
public boolean equals(Object o) {
return super.equals(o);
}

@Override
public int hashCode() {
return super.hashCode();
}

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