-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
high level REST api: cancel task #30745
Changes from 4 commits
c7e221f
1df3544
332f4dd
9495732
9869569
5654c21
2c709ed
3ed1829
62b161a
314e1ff
58e9539
c51eb3c
9e748ee
a511490
96e3fa2
4260549
f646fa4
31f08ef
0c05fe7
783d00a
4e34cfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
|
||
import org.apache.http.Header; | ||
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.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
|
@@ -87,4 +89,37 @@ public void listTasksAsync(ListTasksRequest request, ActionListener<ListTasksRes | |
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::listTasks, ListTasksResponse::fromXContent, | ||
listener, emptySet(), headers); | ||
} | ||
|
||
/** | ||
* Cancel one or more cluster tasks using the Task Management API | ||
* <p> | ||
* See | ||
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html"> Task Management API on elastic.co</a> | ||
* </p> | ||
*/ | ||
public CancelTasksResponse cancelTasks(CancelTasksRequest cancelTasksRequest, Header... headers) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized that these API should belong to the tasks namespace rather than cluster, according to our API spec, see #30906 . |
||
return restHighLevelClient.performRequestAndParseEntity( | ||
cancelTasksRequest, | ||
RequestConverters::cancelTasks, | ||
parser -> CancelTasksResponse.fromXContent(parser), | ||
emptySet(), | ||
headers); | ||
} | ||
|
||
/** | ||
* Asynchronously cancel one or more cluster tasks using the Task Management API | ||
* <p> | ||
* See | ||
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html"> Task Management API on elastic.co</a> | ||
* </p> | ||
*/ | ||
public void cancelTasksAsync(CancelTasksRequest cancelTasksRequest, ActionListener<CancelTasksResponse> listener, Header... headers) { | ||
restHighLevelClient.performRequestAsyncAndParseEntity( | ||
cancelTasksRequest, | ||
RequestConverters::cancelTasks, | ||
parser -> CancelTasksResponse.fromXContent(parser), | ||
listener, | ||
emptySet(), | ||
headers); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import org.apache.http.entity.ContentType; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.action.DocWriteRequest; | ||
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest; | ||
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest; | ||
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest; | ||
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest; | ||
|
@@ -101,6 +102,17 @@ private RequestConverters() { | |
// Contains only status utility methods | ||
} | ||
|
||
static Request cancelTasks(CancelTasksRequest cancelTasksRequest) { | ||
Request request = new Request(HttpPost.METHOD_NAME, "/_tasks/_cancel"); | ||
Params params = new Params(request); | ||
params.withTimeout( | ||
cancelTasksRequest.getTimeout()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can you adjust the indentation? |
||
.withTaskId(cancelTasksRequest.getTaskId()) | ||
.withNodes(cancelTasksRequest.getNodes()) | ||
.withActions(cancelTasksRequest.getActions()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the REST action seems to also support parent_task_id, the parameter is also declared in the REST spec so it should be here too? |
||
return request; | ||
} | ||
|
||
static Request delete(DeleteRequest deleteRequest) { | ||
String endpoint = endpoint(deleteRequest.index(), deleteRequest.type(), deleteRequest.id()); | ||
Request request = new Request(HttpDelete.METHOD_NAME, endpoint); | ||
|
@@ -978,6 +990,13 @@ Params withActions(String[] actions) { | |
return this; | ||
} | ||
|
||
Params withTaskId(TaskId taskId) { | ||
if (taskId != null && taskId.isSet()) { | ||
return putParam("task_id", taskId.toString()); | ||
} | ||
return this; | ||
} | ||
|
||
Params withParentTaskId(TaskId parentTaskId) { | ||
if (parentTaskId != null && parentTaskId.isSet()) { | ||
return putParam("parent_task_id", parentTaskId.toString()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
package org.elasticsearch.client; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
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.action.admin.cluster.node.tasks.list.TaskGroup; | ||
|
@@ -32,6 +34,7 @@ | |
import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
import org.elasticsearch.indices.recovery.RecoverySettings; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.tasks.TaskId; | ||
import org.elasticsearch.tasks.TaskInfo; | ||
|
||
import java.io.IOException; | ||
|
@@ -136,4 +139,30 @@ public void testListTasks() throws IOException { | |
} | ||
assertTrue("List tasks were not found", listTasksFound); | ||
} | ||
|
||
public void testCancelTasks() throws IOException { | ||
ListTasksRequest listRequest = new ListTasksRequest(); | ||
ListTasksResponse listResponse = execute( | ||
listRequest, | ||
highLevelClient().cluster()::listTasks, | ||
highLevelClient().cluster()::listTasksAsync | ||
); | ||
|
||
// TODO[PCS] submit a task that is cancellable and assert it's cancelled | ||
// this case is covered in TasksIT.testTasksCancellation | ||
TaskInfo firstTask = listResponse.getTasks().get(0); | ||
String node = listResponse.getPerNodeTasks().keySet().iterator().next(); | ||
|
||
CancelTasksRequest request = new CancelTasksRequest(); | ||
request.setTaskId(new TaskId(node, firstTask.getId())); | ||
request.setReason("testreason"); | ||
CancelTasksResponse response = execute( | ||
request, | ||
highLevelClient().cluster()::cancelTasks, | ||
highLevelClient().cluster()::cancelTasksAsync | ||
); | ||
// Since the task may or may not have been cancelled, assert that we received a response only | ||
// The actual testing of task cancellation is covered by TasksIT.testTasksCancellation | ||
assertThat(response, notNullValue()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd certainly be nice to have more here but you can't really be sure that you are going to actually cancel the task or it is going to finish. Since we already have an integration test for the act of cancelling such a task I'd probably be satisfied with something fairly basic here and good unit testing on the parsing side of the responses. We know the cancel works because we have I do think it is worth leaving a comment about how this might cancel a task and it might not, depending on how quickly the task executes. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the changes should not be here, these tests have been moved to IngestClientIT upstream |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ | |
import org.apache.http.util.EntityUtils; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.DocWriteRequest; | ||
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.repositories.get.GetRepositoriesRequest; | ||
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest; | ||
|
@@ -1465,6 +1467,20 @@ public void testIndexPutSettings() throws IOException { | |
assertEquals(expectedParams, request.getParameters()); | ||
} | ||
|
||
public void testCancelTasks() { | ||
CancelTasksRequest request = new CancelTasksRequest(); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
TaskId taskId = new TaskId(randomAlphaOfLength(5), randomNonNegativeLong()); | ||
request.setTaskId(taskId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we also test the other supported parameters? |
||
expectedParams.put("task_id", taskId.toString()); | ||
Request httpRequest = RequestConverters.cancelTasks(request); | ||
assertThat(httpRequest, notNullValue()); | ||
assertThat(httpRequest.getMethod(), equalTo(HttpPost.METHOD_NAME)); | ||
assertThat(httpRequest.getEntity(), nullValue()); | ||
assertThat(httpRequest.getEndpoint(), equalTo("/_tasks/_cancel")); | ||
assertThat(httpRequest.getParameters(), equalTo(expectedParams)); | ||
} | ||
|
||
public void testListTasks() { | ||
{ | ||
ListTasksRequest request = new ListTasksRequest(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
[[java-rest-high-cluster-cancel-tasks]] | ||
=== Cancel Tasks API | ||
|
||
The Cancel Tasks API allows cancellation of a currently running task. | ||
|
||
==== Cancel Tasks Request | ||
|
||
A `CancelTasksRequest`: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/ClusterClientDocumentationIT.java[cancel-tasks-request] | ||
-------------------------------------------------- | ||
There are no required parameters. The task cancellation command supports the same | ||
task selection parameters as the list tasks command. | ||
|
||
==== Parameters | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/ClusterClientDocumentationIT.java[list-tasks-request-filter] | ||
-------------------------------------------------- | ||
<1> Cancel a task | ||
<2> Cancel only cluster-related tasks | ||
<3> Cancel all tasks running on nodes nodeId1 and nodeId2 | ||
|
||
==== Synchronous Execution | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/ClusterClientDocumentationIT.java[list-tasks-execute] | ||
-------------------------------------------------- | ||
|
||
==== Asynchronous Execution | ||
|
||
The asynchronous execution requires `CancelTasksRequest` instance and an | ||
`ActionListener` instance to be passed to the asynchronous method: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/ClusterClientDocumentationIT.java[cancel-tasks-execute-async] | ||
-------------------------------------------------- | ||
<1> The `CancelTasksRequest` to execute and the `ActionListener` to use | ||
when the execution completes | ||
|
||
The asynchronous method does not block and returns immediately. Once it is | ||
completed the `ActionListener` is called back using the `onResponse` method | ||
if the execution successfully completed or using the `onFailure` method if | ||
it failed. | ||
|
||
A typical listener for `CancelTasksResponse` looks like: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/ClusterClientDocumentationIT.java[cancel-tasks-execute-listener] | ||
-------------------------------------------------- | ||
<1> Called when the execution is successfully completed. The response is | ||
provided as an argument | ||
<2> Called in case of a failure. The raised exception is provided as an argument | ||
|
||
==== Cancel Tasks Response | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/ClusterClientDocumentationIT.java[list-tasks-response-tasks] | ||
-------------------------------------------------- | ||
<1> List of cancelled tasks | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/ClusterClientDocumentationIT.java[list-tasks-response-calc] | ||
-------------------------------------------------- | ||
<1> List of cancelled tasks grouped by a node | ||
<2> List of cancelled tasks grouped by a parent task | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/ClusterClientDocumentationIT.java[list-tasks-response-failures] | ||
-------------------------------------------------- | ||
<1> List of node failures | ||
<2> List of task cancellation failures | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove empty line?