Expose whether a task is cancellable in the _tasks list API

Closes #17369
This commit is contained in:
Igor Motov 2016-03-31 18:41:45 -04:00
parent e76038e076
commit f599ac5d5a
5 changed files with 27 additions and 3 deletions

View File

@ -57,10 +57,12 @@ public class TaskInfo implements Writeable<TaskInfo>, 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<TaskInfo>, 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<TaskInfo>, ToXContent {
}
startTime = in.readLong();
runningTimeNanos = in.readLong();
cancellable = in.readBoolean();
parentTaskId = new TaskId(in);
}
@ -134,6 +138,13 @@ public class TaskInfo implements Writeable<TaskInfo>, 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<TaskInfo>, ToXContent {
}
out.writeLong(startTime);
out.writeLong(runningTimeNanos);
out.writeBoolean(cancellable);
parentTaskId.writeTo(out);
}
@ -178,6 +190,7 @@ public class TaskInfo implements Writeable<TaskInfo>, 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());
}

View File

@ -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);
}
/**

View File

@ -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

View File

@ -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();

View File

@ -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
}
}
}