Skip to content

Commit cfb415d

Browse files
committed
Fix broken TaskInfo.toString()
Related to elastic#22387
1 parent d86f97c commit cfb415d

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/ListTasksResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ public XContentBuilder toXContentGroupedByNode(XContentBuilder builder, Params p
161161
}
162162
builder.startObject("tasks");
163163
for(TaskInfo task : entry.getValue()) {
164-
builder.field(task.getTaskId().toString());
164+
builder.startObject(task.getTaskId().toString());
165165
task.toXContent(builder, params);
166+
builder.endObject();
166167
}
167168
builder.endObject();
168169
builder.endObject();

core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/TaskGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public List<TaskGroup> getChildTasks() {
8181
@Override
8282
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
8383
builder.startObject();
84-
task.innerToXContent(builder, params);
84+
task.toXContent(builder, params);
8585
if (childTasks.isEmpty() == false) {
8686
builder.startArray("children");
8787
for (TaskGroup taskGroup : childTasks) {

core/src/main/java/org/elasticsearch/tasks/TaskInfo.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,6 @@ public TaskId getParentTaskId() {
163163

164164
@Override
165165
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
166-
builder.startObject();
167-
innerToXContent(builder, params);
168-
return builder.endObject();
169-
}
170-
171-
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
172166
builder.field("node", taskId.getNodeId());
173167
builder.field("id", taskId.getId());
174168
builder.field("type", type);

core/src/main/java/org/elasticsearch/tasks/TaskResult.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
160160

161161
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
162162
builder.field("completed", completed);
163-
builder.field("task", task);
163+
builder.startObject("task");
164+
task.toXContent(builder, params);
165+
builder.endObject();
164166
if (error != null) {
165167
XContentHelper.writeRawField("error", error, builder, params);
166168
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
package org.elasticsearch.action.admin.cluster.node.tasks;
20+
21+
import org.elasticsearch.common.bytes.BytesArray;
22+
import org.elasticsearch.common.xcontent.XContentHelper;
23+
import org.elasticsearch.tasks.TaskId;
24+
import org.elasticsearch.tasks.TaskInfo;
25+
import org.elasticsearch.test.ESTestCase;
26+
27+
import java.nio.charset.StandardCharsets;
28+
import java.util.Map;
29+
30+
public class TaskTests extends ESTestCase {
31+
32+
public void testTaskInfoToString() {
33+
String nodeId = randomAsciiOfLength(10);
34+
long taskId = randomIntBetween(0, 100000);
35+
long startTime = randomNonNegativeLong();
36+
long runningTime = randomNonNegativeLong();
37+
boolean cancellable = randomBoolean();
38+
TaskInfo taskInfo = new TaskInfo(new TaskId(nodeId, taskId), "test_type",
39+
"test_action", "test_description", null, startTime, runningTime, cancellable, TaskId.EMPTY_TASK_ID);
40+
String taskInfoString = taskInfo.toString();
41+
Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(taskInfoString.getBytes(StandardCharsets.UTF_8)), true).v2();
42+
assertEquals(((Number)map.get("id")).longValue(), taskId);
43+
assertEquals(map.get("type"), "test_type");
44+
assertEquals(map.get("action"), "test_action");
45+
assertEquals(map.get("description"), "test_description");
46+
assertEquals(((Number)map.get("start_time_in_millis")).longValue(), startTime);
47+
assertEquals(((Number)map.get("running_time_in_nanos")).longValue(), runningTime);
48+
assertEquals(map.get("cancellable"), cancellable);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)