Skip to content

Add start time and duration to tasks #16829

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

Merged
merged 1 commit into from
Feb 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.tasks.TaskId;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
* Information about a currently running task.
Expand All @@ -50,17 +51,24 @@ public class TaskInfo implements Writeable<TaskInfo>, ToXContent {

private final String description;

private final long startTime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these should be TimeValues?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, the next one a TimeValue, this one a DateTime or something? Its not required, at all and I just thought of it but a TimeValue would make runningTimeNanos easier to read I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a transport format, which is easier to work with. We do convert this long to a TimeValue in XContentBuilder#timeValueField() if a user requests human readable output. It also happens to dates in XContentBuilder#dateValueField(). So, I don't think storing it here in milliseconds and nanoseconds will affect readability.


private final long runningTimeNanos;

private final Task.Status status;

private final TaskId parentTaskId;

public TaskInfo(DiscoveryNode node, long id, String type, String action, String description, Task.Status status, TaskId parentTaskId) {
public TaskInfo(DiscoveryNode node, long id, String type, String action, String description, Task.Status status, long startTime,
long runningTimeNanos, TaskId parentTaskId) {
this.node = node;
this.taskId = new TaskId(node.getId(), id);
this.type = type;
this.action = action;
this.description = description;
this.status = status;
this.startTime = startTime;
this.runningTimeNanos = runningTimeNanos;
this.parentTaskId = parentTaskId;
}

Expand All @@ -75,6 +83,8 @@ public TaskInfo(StreamInput in) throws IOException {
} else {
status = null;
}
startTime = in.readLong();
runningTimeNanos = in.readLong();
parentTaskId = new TaskId(in);
}

Expand Down Expand Up @@ -110,6 +120,23 @@ public Task.Status getStatus() {
return status;
}

/**
* Returns the task start time
*/
public long getStartTime() {
return startTime;
}

/**
* Returns the task running time
*/
public long getRunningTimeNanos() {
return runningTimeNanos;
}

/**
* Returns the parent task id
*/
public TaskId getParentTaskId() {
return parentTaskId;
}
Expand All @@ -132,6 +159,8 @@ public void writeTo(StreamOutput out) throws IOException {
} else {
out.writeBoolean(false);
}
out.writeLong(startTime);
out.writeLong(runningTimeNanos);
parentTaskId.writeTo(out);
}

Expand All @@ -147,6 +176,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (description != null) {
builder.field("description", description);
}
builder.dateValueField("start_time_in_millis", "start_time", startTime);
builder.timeValueField("running_time_in_nanos", "running_time", runningTimeNanos, TimeUnit.NANOSECONDS);
if (parentTaskId.isSet() == false) {
builder.field("parent_task_id", parentTaskId.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.concurrent.TimeUnit;

/**
*
Expand Down Expand Up @@ -961,6 +961,23 @@ public XContentBuilder dateValueField(XContentBuilderString rawFieldName, XConte
return this;
}

public XContentBuilder timeValueField(String rawFieldName, String readableFieldName, long rawTime, TimeUnit timeUnit) throws
IOException {
if (humanReadable) {
field(readableFieldName, new TimeValue(rawTime, timeUnit).toString());
}
field(rawFieldName, rawTime);
return this;
}

public XContentBuilder dateValueField(String rawFieldName, String readableFieldName, long rawTimestamp) throws IOException {
if (humanReadable) {
field(readableFieldName, defaultDatePrinter.print(rawTimestamp));
}
field(rawFieldName, rawTimestamp);
return this;
}

public XContentBuilder byteSizeField(XContentBuilderString rawFieldName, XContentBuilderString readableFieldName, ByteSizeValue byteSizeValue) throws IOException {
if (humanReadable) {
field(readableFieldName, byteSizeValue.toString());
Expand Down
20 changes: 19 additions & 1 deletion core/src/main/java/org/elasticsearch/tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,26 @@ public class Task {

private final TaskId parentTask;

private final long startTime;

private final long startTimeNanos;

public Task(long id, String type, String action, String description) {
this(id, type, action, description, TaskId.EMPTY_TASK_ID);
}

public Task(long id, String type, String action, String description, TaskId parentTask) {
this(id, type, action, description, parentTask, System.currentTimeMillis(), System.nanoTime());
}

public Task(long id, String type, String action, String description, TaskId parentTask, long startTime, long startTimeNanos) {
this.id = id;
this.type = type;
this.action = action;
this.description = description;
this.parentTask = parentTask;
this.startTime = startTime;
this.startTimeNanos = startTimeNanos;
}

/**
Expand All @@ -69,7 +79,8 @@ public TaskInfo taskInfo(DiscoveryNode node, boolean detailed) {
description = getDescription();
status = getStatus();
}
return new TaskInfo(node, getId(), getType(), getAction(), description, status, parentTask);
return new TaskInfo(node, getId(), getType(), getAction(), description, status, startTime, System.nanoTime() - startTimeNanos,
parentTask);
}

/**
Expand Down Expand Up @@ -100,6 +111,13 @@ public String getDescription() {
return description;
}

/**
* Returns the task start time
*/
public long getStartTime() {
return startTime;
}

/**
* Returns id of the parent task or NO_PARENT_ID if the task doesn't have any parent tasks
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static org.elasticsearch.action.support.PlainActionFuture.newFuture;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.not;

public class TransportTasksActionTests extends TaskManagerTestCase {
Expand Down Expand Up @@ -461,10 +462,12 @@ public void testTaskManagementOptOut() throws Exception {
}

public void testTasksDescriptions() throws Exception {
long minimalStartTime = System.currentTimeMillis();
setupTestNodes(Settings.EMPTY);
connectNodes(testNodes);
CountDownLatch checkLatch = new CountDownLatch(1);
ActionFuture<NodesResponse> future = startBlockingTestNodesAction(checkLatch);
long maximumStartTimeNanos = System.nanoTime();

// Check task counts using transport with filtering
TestNode testNode = testNodes[randomIntBetween(0, testNodes.length - 1)];
Expand All @@ -478,12 +481,15 @@ public void testTasksDescriptions() throws Exception {
}

// Check task counts using transport with detailed description
long minimalDurationNanos = System.nanoTime() - maximumStartTimeNanos;
listTasksRequest.detailed(true); // same request only with detailed description
response = testNode.transportListTasksAction.execute(listTasksRequest).get();
assertEquals(testNodes.length, response.getPerNodeTasks().size());
for (Map.Entry<DiscoveryNode, List<TaskInfo>> entry : response.getPerNodeTasks().entrySet()) {
assertEquals(1, entry.getValue().size());
assertEquals("CancellableNodeRequest[Test Request, true]", entry.getValue().get(0).getDescription());
assertThat(entry.getValue().get(0).getStartTime(), greaterThanOrEqualTo(minimalStartTime));
assertThat(entry.getValue().get(0).getRunningTimeNanos(), greaterThanOrEqualTo(minimalDurationNanos));
}

// Release all tasks and wait for response
Expand Down