Skip to content

Commit a06f068

Browse files
committed
Move list tasks under Tasks namespace (#30906)
Our API spec define the tasks API as e.g. tasks.list, meaning that they belong to their own namespace. This commit moves them from the cluster namespace to their own namespace. Relates to #29546
1 parent 5b4f3f6 commit a06f068

File tree

10 files changed

+303
-163
lines changed

10 files changed

+303
-163
lines changed

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

-24
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import org.apache.http.Header;
2323
import org.elasticsearch.action.ActionListener;
24-
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
25-
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
2624
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
2725
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse;
2826
import org.elasticsearch.action.ingest.PutPipelineRequest;
@@ -68,28 +66,6 @@ public void putSettingsAsync(ClusterUpdateSettingsRequest clusterUpdateSettingsR
6866
ClusterUpdateSettingsResponse::fromXContent, listener, emptySet(), headers);
6967
}
7068

71-
/**
72-
* Get current tasks using the Task Management API
73-
* <p>
74-
* See
75-
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html"> Task Management API on elastic.co</a>
76-
*/
77-
public ListTasksResponse listTasks(ListTasksRequest request, Header... headers) throws IOException {
78-
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::listTasks, ListTasksResponse::fromXContent,
79-
emptySet(), headers);
80-
}
81-
82-
/**
83-
* Asynchronously get current tasks using the Task Management API
84-
* <p>
85-
* See
86-
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html"> Task Management API on elastic.co</a>
87-
*/
88-
public void listTasksAsync(ListTasksRequest request, ActionListener<ListTasksResponse> listener, Header... headers) {
89-
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::listTasks, ListTasksResponse::fromXContent,
90-
listener, emptySet(), headers);
91-
}
92-
9369
/**
9470
* Add a pipeline or update an existing pipeline in the cluster
9571
* <p>

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

+10
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public class RestHighLevelClient implements Closeable {
192192
private final IndicesClient indicesClient = new IndicesClient(this);
193193
private final ClusterClient clusterClient = new ClusterClient(this);
194194
private final SnapshotClient snapshotClient = new SnapshotClient(this);
195+
private final TasksClient tasksClient = new TasksClient(this);
195196

196197
/**
197198
* Creates a {@link RestHighLevelClient} given the low level {@link RestClientBuilder} that allows to build the
@@ -264,6 +265,15 @@ public final SnapshotClient snapshot() {
264265
return snapshotClient;
265266
}
266267

268+
/**
269+
* Provides a {@link TasksClient} which can be used to access the Tasks API.
270+
*
271+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html">Task Management API on elastic.co</a>
272+
*/
273+
public final TasksClient tasks() {
274+
return tasksClient;
275+
}
276+
267277
/**
268278
* Executes a bulk request using the Bulk API
269279
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
20+
package org.elasticsearch.client;
21+
22+
import org.apache.http.Header;
23+
import org.elasticsearch.action.ActionListener;
24+
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
25+
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
26+
27+
import java.io.IOException;
28+
29+
import static java.util.Collections.emptySet;
30+
31+
/**
32+
* A wrapper for the {@link RestHighLevelClient} that provides methods for accessing the Tasks API.
33+
* <p>
34+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html">Task Management API on elastic.co</a>
35+
*/
36+
public class TasksClient {
37+
private final RestHighLevelClient restHighLevelClient;
38+
39+
TasksClient(RestHighLevelClient restHighLevelClient) {
40+
this.restHighLevelClient = restHighLevelClient;
41+
}
42+
43+
/**
44+
* Get current tasks using the Task Management API
45+
* <p>
46+
* See
47+
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html"> Task Management API on elastic.co</a>
48+
*/
49+
public ListTasksResponse list(ListTasksRequest request, Header... headers) throws IOException {
50+
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::listTasks, ListTasksResponse::fromXContent,
51+
emptySet(), headers);
52+
}
53+
54+
/**
55+
* Asynchronously get current tasks using the Task Management API
56+
* <p>
57+
* See
58+
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html"> Task Management API on elastic.co</a>
59+
*/
60+
public void listAsync(ListTasksRequest request, ActionListener<ListTasksResponse> listener, Header... headers) {
61+
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::listTasks, ListTasksResponse::fromXContent,
62+
listener, emptySet(), headers);
63+
}
64+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java

-31
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
package org.elasticsearch.client;
2121

2222
import org.elasticsearch.ElasticsearchException;
23-
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
24-
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
25-
import org.elasticsearch.action.admin.cluster.node.tasks.list.TaskGroup;
2623
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
2724
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse;
2825
import org.elasticsearch.action.ingest.PutPipelineRequest;
@@ -37,16 +34,13 @@
3734
import org.elasticsearch.indices.recovery.RecoverySettings;
3835
import org.elasticsearch.ingest.Pipeline;
3936
import org.elasticsearch.rest.RestStatus;
40-
import org.elasticsearch.tasks.TaskInfo;
4137

4238
import java.io.IOException;
4339
import java.util.HashMap;
4440
import java.util.Map;
4541

46-
import static java.util.Collections.emptyList;
4742
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
4843
import static org.hamcrest.Matchers.equalTo;
49-
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
5044
import static org.hamcrest.Matchers.notNullValue;
5145
import static org.hamcrest.Matchers.nullValue;
5246

@@ -117,31 +111,6 @@ public void testClusterUpdateSettingNonExistent() {
117111
"Elasticsearch exception [type=illegal_argument_exception, reason=transient setting [" + setting + "], not recognized]"));
118112
}
119113

120-
public void testListTasks() throws IOException {
121-
ListTasksRequest request = new ListTasksRequest();
122-
ListTasksResponse response = execute(request, highLevelClient().cluster()::listTasks, highLevelClient().cluster()::listTasksAsync);
123-
124-
assertThat(response, notNullValue());
125-
assertThat(response.getNodeFailures(), equalTo(emptyList()));
126-
assertThat(response.getTaskFailures(), equalTo(emptyList()));
127-
// It's possible that there are other tasks except 'cluster:monitor/tasks/lists[n]' and 'action":"cluster:monitor/tasks/lists'
128-
assertThat(response.getTasks().size(), greaterThanOrEqualTo(2));
129-
boolean listTasksFound = false;
130-
for (TaskGroup taskGroup : response.getTaskGroups()) {
131-
TaskInfo parent = taskGroup.getTaskInfo();
132-
if ("cluster:monitor/tasks/lists".equals(parent.getAction())) {
133-
assertThat(taskGroup.getChildTasks().size(), equalTo(1));
134-
TaskGroup childGroup = taskGroup.getChildTasks().iterator().next();
135-
assertThat(childGroup.getChildTasks().isEmpty(), equalTo(true));
136-
TaskInfo child = childGroup.getTaskInfo();
137-
assertThat(child.getAction(), equalTo("cluster:monitor/tasks/lists[n]"));
138-
assertThat(child.getParentTaskId(), equalTo(parent.getTaskId()));
139-
listTasksFound = true;
140-
}
141-
}
142-
assertTrue("List tasks were not found", listTasksFound);
143-
}
144-
145114
public void testPutPipeline() throws IOException {
146115
String id = "some_pipeline_id";
147116
XContentType xContentType = randomFrom(XContentType.values());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
20+
package org.elasticsearch.client;
21+
22+
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
23+
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
24+
import org.elasticsearch.action.admin.cluster.node.tasks.list.TaskGroup;
25+
import org.elasticsearch.tasks.TaskInfo;
26+
27+
import java.io.IOException;
28+
29+
import static java.util.Collections.emptyList;
30+
import static org.hamcrest.Matchers.equalTo;
31+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
32+
import static org.hamcrest.Matchers.notNullValue;
33+
34+
public class TasksIT extends ESRestHighLevelClientTestCase {
35+
36+
public void testListTasks() throws IOException {
37+
ListTasksRequest request = new ListTasksRequest();
38+
ListTasksResponse response = execute(request, highLevelClient().tasks()::list, highLevelClient().tasks()::listAsync);
39+
40+
assertThat(response, notNullValue());
41+
assertThat(response.getNodeFailures(), equalTo(emptyList()));
42+
assertThat(response.getTaskFailures(), equalTo(emptyList()));
43+
// It's possible that there are other tasks except 'cluster:monitor/tasks/lists[n]' and 'action":"cluster:monitor/tasks/lists'
44+
assertThat(response.getTasks().size(), greaterThanOrEqualTo(2));
45+
boolean listTasksFound = false;
46+
for (TaskGroup taskGroup : response.getTaskGroups()) {
47+
TaskInfo parent = taskGroup.getTaskInfo();
48+
if ("cluster:monitor/tasks/lists".equals(parent.getAction())) {
49+
assertThat(taskGroup.getChildTasks().size(), equalTo(1));
50+
TaskGroup childGroup = taskGroup.getChildTasks().iterator().next();
51+
assertThat(childGroup.getChildTasks().isEmpty(), equalTo(true));
52+
TaskInfo child = childGroup.getTaskInfo();
53+
assertThat(child.getAction(), equalTo("cluster:monitor/tasks/lists[n]"));
54+
assertThat(child.getParentTaskId(), equalTo(parent.getTaskId()));
55+
listTasksFound = true;
56+
}
57+
}
58+
assertTrue("List tasks were not found", listTasksFound);
59+
}
60+
61+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java

-94
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@
1919

2020
package org.elasticsearch.client.documentation;
2121

22-
import org.elasticsearch.ElasticsearchException;
2322
import org.elasticsearch.action.ActionListener;
2423
import org.elasticsearch.action.LatchedActionListener;
25-
import org.elasticsearch.action.TaskOperationFailure;
26-
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
27-
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
28-
import org.elasticsearch.action.admin.cluster.node.tasks.list.TaskGroup;
2924
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
3025
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse;
3126
import org.elasticsearch.action.ingest.PutPipelineRequest;
@@ -39,21 +34,15 @@
3934
import org.elasticsearch.common.unit.TimeValue;
4035
import org.elasticsearch.common.xcontent.XContentType;
4136
import org.elasticsearch.indices.recovery.RecoverySettings;
42-
import org.elasticsearch.tasks.TaskId;
43-
import org.elasticsearch.tasks.TaskInfo;
4437

4538
import java.io.IOException;
4639
import java.nio.charset.StandardCharsets;
4740
import java.util.HashMap;
48-
import java.util.List;
4941
import java.util.Map;
5042
import java.util.concurrent.CountDownLatch;
5143
import java.util.concurrent.TimeUnit;
5244

53-
import static java.util.Collections.emptyList;
5445
import static org.hamcrest.Matchers.equalTo;
55-
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
56-
import static org.hamcrest.Matchers.notNullValue;
5746

5847
/**
5948
* This class is used to generate the Java Cluster API documentation.
@@ -193,89 +182,6 @@ public void onFailure(Exception e) {
193182
}
194183
}
195184

196-
public void testListTasks() throws IOException {
197-
RestHighLevelClient client = highLevelClient();
198-
{
199-
// tag::list-tasks-request
200-
ListTasksRequest request = new ListTasksRequest();
201-
// end::list-tasks-request
202-
203-
// tag::list-tasks-request-filter
204-
request.setActions("cluster:*"); // <1>
205-
request.setNodes("nodeId1", "nodeId2"); // <2>
206-
request.setParentTaskId(new TaskId("parentTaskId", 42)); // <3>
207-
// end::list-tasks-request-filter
208-
209-
// tag::list-tasks-request-detailed
210-
request.setDetailed(true); // <1>
211-
// end::list-tasks-request-detailed
212-
213-
// tag::list-tasks-request-wait-completion
214-
request.setWaitForCompletion(true); // <1>
215-
request.setTimeout(TimeValue.timeValueSeconds(50)); // <2>
216-
request.setTimeout("50s"); // <3>
217-
// end::list-tasks-request-wait-completion
218-
}
219-
220-
ListTasksRequest request = new ListTasksRequest();
221-
222-
// tag::list-tasks-execute
223-
ListTasksResponse response = client.cluster().listTasks(request);
224-
// end::list-tasks-execute
225-
226-
assertThat(response, notNullValue());
227-
228-
// tag::list-tasks-response-tasks
229-
List<TaskInfo> tasks = response.getTasks(); // <1>
230-
// end::list-tasks-response-tasks
231-
232-
// tag::list-tasks-response-calc
233-
Map<String, List<TaskInfo>> perNodeTasks = response.getPerNodeTasks(); // <1>
234-
List<TaskGroup> groups = response.getTaskGroups(); // <2>
235-
// end::list-tasks-response-calc
236-
237-
// tag::list-tasks-response-failures
238-
List<ElasticsearchException> nodeFailures = response.getNodeFailures(); // <1>
239-
List<TaskOperationFailure> taskFailures = response.getTaskFailures(); // <2>
240-
// end::list-tasks-response-failures
241-
242-
assertThat(response.getNodeFailures(), equalTo(emptyList()));
243-
assertThat(response.getTaskFailures(), equalTo(emptyList()));
244-
assertThat(response.getTasks().size(), greaterThanOrEqualTo(2));
245-
}
246-
247-
public void testListTasksAsync() throws Exception {
248-
RestHighLevelClient client = highLevelClient();
249-
{
250-
ListTasksRequest request = new ListTasksRequest();
251-
252-
// tag::list-tasks-execute-listener
253-
ActionListener<ListTasksResponse> listener =
254-
new ActionListener<ListTasksResponse>() {
255-
@Override
256-
public void onResponse(ListTasksResponse response) {
257-
// <1>
258-
}
259-
260-
@Override
261-
public void onFailure(Exception e) {
262-
// <2>
263-
}
264-
};
265-
// end::list-tasks-execute-listener
266-
267-
// Replace the empty listener by a blocking listener in test
268-
final CountDownLatch latch = new CountDownLatch(1);
269-
listener = new LatchedActionListener<>(listener, latch);
270-
271-
// tag::list-tasks-execute-async
272-
client.cluster().listTasksAsync(request, listener); // <1>
273-
// end::list-tasks-execute-async
274-
275-
assertTrue(latch.await(30L, TimeUnit.SECONDS));
276-
}
277-
}
278-
279185
public void testPutPipeline() throws IOException {
280186
RestHighLevelClient client = highLevelClient();
281187

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import static org.hamcrest.Matchers.equalTo;
4646

4747
/**
48-
* This class is used to generate the Java Cluster API documentation.
48+
* This class is used to generate the Java Snapshot API documentation.
4949
* You need to wrap your code between two tags like:
5050
* // tag::example
5151
* // end::example

0 commit comments

Comments
 (0)