Skip to content

Commit 52003a2

Browse files
jesinityhub-cap
authored andcommitted
Fix HLRC parsing of CancelTasks response (#47017)
Adds support for proper cancel tasks parsing. Closes #45414
1 parent 17e9cd5 commit 52003a2

19 files changed

+1638
-52
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.elasticsearch.client.core.TermVectorsRequest;
5454
import org.elasticsearch.client.indices.AnalyzeRequest;
5555
import org.elasticsearch.client.security.RefreshPolicy;
56+
import org.elasticsearch.client.tasks.TaskId;
5657
import org.elasticsearch.cluster.health.ClusterHealthStatus;
5758
import org.elasticsearch.common.Nullable;
5859
import org.elasticsearch.common.Priority;
@@ -80,13 +81,13 @@
8081
import org.elasticsearch.script.mustache.MultiSearchTemplateRequest;
8182
import org.elasticsearch.script.mustache.SearchTemplateRequest;
8283
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
83-
import org.elasticsearch.tasks.TaskId;
8484

8585
import java.io.ByteArrayOutputStream;
8686
import java.io.IOException;
8787
import java.net.URI;
8888
import java.net.URISyntaxException;
8989
import java.nio.charset.Charset;
90+
import java.util.Arrays;
9091
import java.util.HashMap;
9192
import java.util.List;
9293
import java.util.Locale;
@@ -1029,19 +1030,41 @@ Params withWaitForCompletion(Boolean waitForCompletion) {
10291030
}
10301031

10311032
Params withNodes(String[] nodes) {
1032-
if (nodes != null && nodes.length > 0) {
1033+
return withNodes(Arrays.asList(nodes));
1034+
}
1035+
1036+
Params withNodes(List<String> nodes) {
1037+
if (nodes != null && nodes.size() > 0) {
10331038
return putParam("nodes", String.join(",", nodes));
10341039
}
10351040
return this;
10361041
}
10371042

10381043
Params withActions(String[] actions) {
1039-
if (actions != null && actions.length > 0) {
1044+
return withActions(Arrays.asList(actions));
1045+
}
1046+
1047+
Params withActions(List<String> actions) {
1048+
if (actions != null && actions.size() > 0) {
10401049
return putParam("actions", String.join(",", actions));
10411050
}
10421051
return this;
10431052
}
10441053

1054+
Params withTaskId(org.elasticsearch.tasks.TaskId taskId) {
1055+
if (taskId != null && taskId.isSet()) {
1056+
return putParam("task_id", taskId.toString());
1057+
}
1058+
return this;
1059+
}
1060+
1061+
Params withParentTaskId(org.elasticsearch.tasks.TaskId parentTaskId) {
1062+
if (parentTaskId != null && parentTaskId.isSet()) {
1063+
return putParam("parent_task_id", parentTaskId.toString());
1064+
}
1065+
return this;
1066+
}
1067+
10451068
Params withTaskId(TaskId taskId) {
10461069
if (taskId != null && taskId.isSet()) {
10471070
return putParam("task_id", taskId.toString());

client/rest-high-level/src/main/java/org/elasticsearch/client/TasksClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
package org.elasticsearch.client;
2121

2222
import org.elasticsearch.action.ActionListener;
23-
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest;
24-
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse;
2523
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
2624
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
25+
import org.elasticsearch.client.tasks.CancelTasksRequest;
26+
import org.elasticsearch.client.tasks.CancelTasksResponse;
2727
import org.elasticsearch.client.tasks.GetTaskRequest;
2828
import org.elasticsearch.client.tasks.GetTaskResponse;
2929

client/rest-high-level/src/main/java/org/elasticsearch/client/TasksRequestConverters.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,24 @@
2121

2222
import org.apache.http.client.methods.HttpGet;
2323
import org.apache.http.client.methods.HttpPost;
24-
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest;
2524
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
2625
import org.elasticsearch.client.RequestConverters.EndpointBuilder;
26+
import org.elasticsearch.client.tasks.CancelTasksRequest;
2727
import org.elasticsearch.client.tasks.GetTaskRequest;
2828

2929
final class TasksRequestConverters {
3030

3131
private TasksRequestConverters() {}
3232

33-
static Request cancelTasks(CancelTasksRequest cancelTasksRequest) {
33+
static Request cancelTasks(CancelTasksRequest req) {
3434
Request request = new Request(HttpPost.METHOD_NAME, "/_tasks/_cancel");
3535
RequestConverters.Params params = new RequestConverters.Params();
36-
params.withTimeout(cancelTasksRequest.getTimeout())
37-
.withTaskId(cancelTasksRequest.getTaskId())
38-
.withNodes(cancelTasksRequest.getNodes())
39-
.withParentTaskId(cancelTasksRequest.getParentTaskId())
40-
.withActions(cancelTasksRequest.getActions());
36+
req.getTimeout().ifPresent(params::withTimeout);
37+
req.getTaskId().ifPresent(params::withTaskId);
38+
req.getParentTaskId().ifPresent(params::withParentTaskId);
39+
params
40+
.withNodes(req.getNodes())
41+
.withActions(req.getActions());
4142
request.addParameters(params.asMap());
4243
return request;
4344
}
@@ -70,5 +71,5 @@ static Request getTask(GetTaskRequest getTaskRequest) {
7071
request.addParameters(params.asMap());
7172
return request;
7273
}
73-
74+
7475
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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 org.elasticsearch.client.Validatable;
22+
import org.elasticsearch.common.unit.TimeValue;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.Objects;
27+
import java.util.Optional;
28+
29+
public class CancelTasksRequest implements Validatable {
30+
31+
private final List<String> nodes = new ArrayList<>();
32+
private final List<String> actions = new ArrayList<>();
33+
private Optional<TimeValue> timeout = Optional.empty();
34+
private Optional<TaskId> parentTaskId = Optional.empty();
35+
private Optional<TaskId> taskId = Optional.empty();
36+
37+
CancelTasksRequest(){}
38+
39+
void setNodes(List<String> nodes) {
40+
this.nodes.addAll(nodes);
41+
}
42+
43+
public List<String> getNodes() {
44+
return nodes;
45+
}
46+
47+
void setTimeout(TimeValue timeout) {
48+
this.timeout = Optional.of(timeout);
49+
}
50+
51+
public Optional<TimeValue> getTimeout() {
52+
return timeout;
53+
}
54+
55+
void setActions(List<String> actions) {
56+
this.actions.addAll(actions);
57+
}
58+
59+
public List<String> getActions() {
60+
return actions;
61+
}
62+
63+
void setParentTaskId(TaskId parentTaskId) {
64+
this.parentTaskId = Optional.of(parentTaskId);
65+
}
66+
67+
public Optional<TaskId> getParentTaskId() {
68+
return parentTaskId;
69+
}
70+
71+
void setTaskId(TaskId taskId) {
72+
this.taskId = Optional.of(taskId);
73+
}
74+
75+
public Optional<TaskId> getTaskId() {
76+
return taskId;
77+
}
78+
79+
@Override
80+
public boolean equals(Object o) {
81+
if (this == o) return true;
82+
if (!(o instanceof CancelTasksRequest)) return false;
83+
CancelTasksRequest that = (CancelTasksRequest) o;
84+
return Objects.equals(getNodes(), that.getNodes()) &&
85+
Objects.equals(getActions(), that.getActions()) &&
86+
Objects.equals(getTimeout(), that.getTimeout()) &&
87+
Objects.equals(getParentTaskId(), that.getParentTaskId()) &&
88+
Objects.equals(getTaskId(), that.getTaskId()) ;
89+
}
90+
91+
@Override
92+
public int hashCode() {
93+
return Objects.hash(getNodes(), getActions(), getTimeout(), getParentTaskId(), getTaskId());
94+
}
95+
96+
@Override
97+
public String toString() {
98+
return "CancelTasksRequest{" +
99+
"nodes=" + nodes +
100+
", actions=" + actions +
101+
", timeout=" + timeout +
102+
", parentTaskId=" + parentTaskId +
103+
", taskId=" + taskId +
104+
'}';
105+
}
106+
107+
public static class Builder {
108+
private Optional<TimeValue> timeout = Optional.empty();
109+
private Optional<TaskId> taskId = Optional.empty();
110+
private Optional<TaskId> parentTaskId = Optional.empty();
111+
private List<String> actionsFilter = new ArrayList<>();
112+
private List<String> nodesFilter = new ArrayList<>();
113+
114+
public Builder withTimeout(TimeValue timeout){
115+
this.timeout = Optional.of(timeout);
116+
return this;
117+
}
118+
119+
public Builder withTaskId(TaskId taskId){
120+
this.taskId = Optional.of(taskId);
121+
return this;
122+
}
123+
124+
public Builder withParentTaskId(TaskId taskId){
125+
this.parentTaskId = Optional.of(taskId);
126+
return this;
127+
}
128+
129+
public Builder withActionsFiltered(List<String> actions){
130+
this.actionsFilter.clear();
131+
this.actionsFilter.addAll(actions);
132+
return this;
133+
}
134+
135+
public Builder withNodesFiltered(List<String> nodes){
136+
this.nodesFilter.clear();
137+
this.nodesFilter.addAll(nodes);
138+
return this;
139+
}
140+
141+
public CancelTasksRequest build() {
142+
CancelTasksRequest request = new CancelTasksRequest();
143+
timeout.ifPresent(request::setTimeout);
144+
taskId.ifPresent(request::setTaskId);
145+
parentTaskId.ifPresent(request::setParentTaskId);
146+
request.setNodes(nodesFilter);
147+
request.setActions(actionsFilter);
148+
return request;
149+
}
150+
}
151+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 org.elasticsearch.common.ParseField;
22+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
25+
import java.io.IOException;
26+
import java.util.List;
27+
28+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
29+
30+
/**
31+
* cancel tasks response that contains
32+
* - task failures
33+
* - node failures
34+
* - tasks
35+
*/
36+
public class CancelTasksResponse extends ListTasksResponse {
37+
38+
CancelTasksResponse(List<NodeData> nodesInfoData,
39+
List<TaskOperationFailure> taskFailures,
40+
List<ElasticsearchException> nodeFailures) {
41+
super(nodesInfoData, taskFailures, nodeFailures);
42+
}
43+
44+
public static CancelTasksResponse fromXContent(final XContentParser parser) throws IOException {
45+
return PARSER.parse(parser, null);
46+
}
47+
48+
private static ConstructingObjectParser<CancelTasksResponse, Void> PARSER;
49+
50+
static {
51+
ConstructingObjectParser<CancelTasksResponse, Void> parser = new ConstructingObjectParser<>("cancel_tasks_response", true,
52+
constructingObjects -> {
53+
int i = 0;
54+
@SuppressWarnings("unchecked")
55+
List<TaskOperationFailure> tasksFailures = (List<TaskOperationFailure>) constructingObjects[i++];
56+
@SuppressWarnings("unchecked")
57+
List<ElasticsearchException> nodeFailures = (List<ElasticsearchException>) constructingObjects[i++];
58+
@SuppressWarnings("unchecked")
59+
List<NodeData> nodesInfoData = (List<NodeData>) constructingObjects[i];
60+
return new CancelTasksResponse(nodesInfoData, tasksFailures, nodeFailures);
61+
});
62+
63+
parser.declareObjectArray(optionalConstructorArg(), (p, c) ->
64+
TaskOperationFailure.fromXContent(p), new ParseField("task_failures"));
65+
parser.declareObjectArray(optionalConstructorArg(), (p, c) ->
66+
ElasticsearchException.fromXContent(p), new ParseField("node_failures"));
67+
parser.declareNamedObjects(optionalConstructorArg(), NodeData.PARSER, new ParseField("nodes"));
68+
PARSER = parser;
69+
}
70+
71+
@Override
72+
public boolean equals(Object o) {
73+
return super.equals(o);
74+
}
75+
76+
@Override
77+
public int hashCode() {
78+
return super.hashCode();
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return "CancelTasksResponse{" +
84+
"taskFailures=" + taskFailures +
85+
", nodeFailures=" + nodeFailures +
86+
", nodesInfoData=" + nodesInfoData +
87+
", tasks=" + tasks +
88+
", taskGroups=" + taskGroups +
89+
'}';
90+
}
91+
}

0 commit comments

Comments
 (0)