From f599ac5d5a28d2df5c8ce1d62b371820abe0b9f7 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Thu, 31 Mar 2016 18:41:45 -0400 Subject: [PATCH] Expose whether a task is cancellable in the _tasks list API Closes #17369 --- .../admin/cluster/node/tasks/list/TaskInfo.java | 15 ++++++++++++++- .../main/java/org/elasticsearch/tasks/Task.java | 2 +- .../cluster/node/tasks/CancellableTasksTests.java | 5 +++++ .../node/tasks/TransportTasksActionTests.java | 4 ++++ docs/reference/cluster/tasks.asciidoc | 4 +++- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/TaskInfo.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/TaskInfo.java index 6b180e2fd7f..37b42f54919 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/TaskInfo.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/TaskInfo.java @@ -57,10 +57,12 @@ public class TaskInfo implements Writeable, ToXContent { private final Task.Status status; + private final boolean cancellable; + private final TaskId parentTaskId; public TaskInfo(DiscoveryNode node, long id, String type, String action, String description, Task.Status status, long startTime, - long runningTimeNanos, TaskId parentTaskId) { + long runningTimeNanos, boolean cancellable, TaskId parentTaskId) { this.node = node; this.taskId = new TaskId(node.getId(), id); this.type = type; @@ -69,6 +71,7 @@ public class TaskInfo implements Writeable, ToXContent { this.status = status; this.startTime = startTime; this.runningTimeNanos = runningTimeNanos; + this.cancellable = cancellable; this.parentTaskId = parentTaskId; } @@ -85,6 +88,7 @@ public class TaskInfo implements Writeable, ToXContent { } startTime = in.readLong(); runningTimeNanos = in.readLong(); + cancellable = in.readBoolean(); parentTaskId = new TaskId(in); } @@ -134,6 +138,13 @@ public class TaskInfo implements Writeable, ToXContent { return runningTimeNanos; } + /** + * Returns true if the task supports cancellation + */ + public boolean isCancellable() { + return cancellable; + } + /** * Returns the parent task id */ @@ -161,6 +172,7 @@ public class TaskInfo implements Writeable, ToXContent { } out.writeLong(startTime); out.writeLong(runningTimeNanos); + out.writeBoolean(cancellable); parentTaskId.writeTo(out); } @@ -178,6 +190,7 @@ public class TaskInfo implements Writeable, ToXContent { } builder.dateValueField("start_time_in_millis", "start_time", startTime); builder.timeValueField("running_time_in_nanos", "running_time", runningTimeNanos, TimeUnit.NANOSECONDS); + builder.field("cancellable", cancellable); if (parentTaskId.isSet()) { builder.field("parent_task_id", parentTaskId.toString()); } diff --git a/core/src/main/java/org/elasticsearch/tasks/Task.java b/core/src/main/java/org/elasticsearch/tasks/Task.java index 2334d10e11b..e3eba1dd1e4 100644 --- a/core/src/main/java/org/elasticsearch/tasks/Task.java +++ b/core/src/main/java/org/elasticsearch/tasks/Task.java @@ -80,7 +80,7 @@ public class Task { status = getStatus(); } return new TaskInfo(node, getId(), getType(), getAction(), description, status, startTime, System.nanoTime() - startTimeNanos, - parentTask); + this instanceof CancellableTask, parentTask); } /** diff --git a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/CancellableTasksTests.java b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/CancellableTasksTests.java index 379cb5942a2..1a3c28acc8f 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/CancellableTasksTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/CancellableTasksTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksReque 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.TaskInfo; import org.elasticsearch.action.support.nodes.BaseNodeRequest; import org.elasticsearch.action.support.nodes.BaseNodesRequest; import org.elasticsearch.action.support.replication.ClusterStateCreationUtils; @@ -267,6 +268,10 @@ public class CancellableTasksTests extends TaskManagerTestCase { // We should have the information about the cancelled task in the cancel operation response assertEquals(1, response.getTasks().size()); assertEquals(mainTask.getId(), response.getTasks().get(0).getId()); + // Verify that all cancelled tasks reported that they support cancellation + for(TaskInfo taskInfo : response.getTasks()) { + assertTrue(taskInfo.isCancellable()); + } } // Make sure that tasks are no longer running diff --git a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TransportTasksActionTests.java b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TransportTasksActionTests.java index 34218a99432..5676747f0d4 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TransportTasksActionTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TransportTasksActionTests.java @@ -565,6 +565,10 @@ public class TransportTasksActionTests extends TaskManagerTestCase { ListTasksResponse listResponse = testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction.execute (listTasksRequest).get(); assertEquals(1, listResponse.getPerNodeTasks().size()); + // Verify that tasks are marked as non-cancellable + for (TaskInfo taskInfo : listResponse.getTasks()) { + assertFalse(taskInfo.isCancellable()); + } // Release all tasks and wait for response checkLatch.countDown(); diff --git a/docs/reference/cluster/tasks.asciidoc b/docs/reference/cluster/tasks.asciidoc index 4ed2a7b899a..4c146c46328 100644 --- a/docs/reference/cluster/tasks.asciidoc +++ b/docs/reference/cluster/tasks.asciidoc @@ -40,6 +40,7 @@ The result will look similar to the following: "action" : "cluster:monitor/tasks/lists[n]", "start_time_in_millis" : 1458585884904, "running_time_in_nanos" : 47402, + "cancellable" : false, "parent_task_id" : "oTUltX4IQMOUUVeiohTt8A:123" }, "oTUltX4IQMOUUVeiohTt8A:123" : { @@ -48,7 +49,8 @@ The result will look similar to the following: "type" : "transport", "action" : "cluster:monitor/tasks/lists", "start_time_in_millis" : 1458585884904, - "running_time_in_nanos" : 236042 + "running_time_in_nanos" : 236042, + "cancellable" : false } } }