diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/CancelTasksResponse.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/CancelTasksResponse.java index 9bfeaecd78b..34daee1d14d 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/CancelTasksResponse.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/CancelTasksResponse.java @@ -22,6 +22,7 @@ package org.elasticsearch.action.admin.cluster.node.tasks.cancel; import org.elasticsearch.action.FailedNodeException; import org.elasticsearch.action.TaskOperationFailure; import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse; +import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.tasks.TaskInfo; import java.util.List; @@ -34,9 +35,13 @@ public class CancelTasksResponse extends ListTasksResponse { public CancelTasksResponse() { } + public CancelTasksResponse(DiscoveryNodes discoveryNodes) { + super(discoveryNodes); + } + public CancelTasksResponse(List tasks, List taskFailures, List - nodeFailures) { - super(tasks, taskFailures, nodeFailures); + nodeFailures, DiscoveryNodes discoveryNodes) { + super(tasks, taskFailures, nodeFailures, discoveryNodes); } } diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/TransportCancelTasksAction.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/TransportCancelTasksAction.java index 6d5936db67a..195cdd86f5c 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/TransportCancelTasksAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/TransportCancelTasksAction.java @@ -66,7 +66,8 @@ public class TransportCancelTasksAction extends TransportTasksAction new CancelTasksResponse(clusterService.state().nodes()), + ThreadPool.Names.MANAGEMENT); transportService.registerRequestHandler(BAN_PARENT_ACTION_NAME, BanParentTaskRequest::new, ThreadPool.Names.SAME, new BanParentRequestHandler()); } @@ -74,7 +75,7 @@ public class TransportCancelTasksAction extends TransportTasksAction tasks, List taskOperationFailures, List failedNodeExceptions) { - return new CancelTasksResponse(tasks, taskOperationFailures, failedNodeExceptions); + return new CancelTasksResponse(tasks, taskOperationFailures, failedNodeExceptions, clusterService.state().nodes()); } @Override diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/ListTasksResponse.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/ListTasksResponse.java index e9d6cf813f9..9fdc7ae6a30 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/ListTasksResponse.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/ListTasksResponse.java @@ -56,12 +56,18 @@ public class ListTasksResponse extends BaseTasksResponse implements ToXContent { private DiscoveryNodes discoveryNodes; public ListTasksResponse() { + this(null, null, null, null); + } + + public ListTasksResponse(DiscoveryNodes discoveryNodes) { + this(null, null, null, discoveryNodes); } public ListTasksResponse(List tasks, List taskFailures, - List nodeFailures) { + List nodeFailures, DiscoveryNodes discoveryNodes) { super(taskFailures, nodeFailures); this.tasks = tasks == null ? Collections.emptyList() : Collections.unmodifiableList(new ArrayList<>(tasks)); + this.discoveryNodes = discoveryNodes; } @Override @@ -125,15 +131,6 @@ public class ListTasksResponse extends BaseTasksResponse implements ToXContent { return tasks; } - /** - * Set a reference to the {@linkplain DiscoveryNodes}. Used for calling {@link #toXContent(XContentBuilder, ToXContent.Params)} with - * {@code group_by=nodes}. - */ - public void setDiscoveryNodes(DiscoveryNodes discoveryNodes) { - //WTF is this? Why isn't this set by default; - this.discoveryNodes = discoveryNodes; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { if (getTaskFailures() != null && getTaskFailures().size() > 0) { @@ -157,9 +154,6 @@ public class ListTasksResponse extends BaseTasksResponse implements ToXContent { } String groupBy = params.param("group_by", "nodes"); if ("nodes".equals(groupBy)) { - if (discoveryNodes == null) { - throw new IllegalStateException("discoveryNodes must be set before calling toXContent with group_by=nodes"); - } builder.startObject("nodes"); for (Map.Entry> entry : getPerNodeTasks().entrySet()) { DiscoveryNode node = discoveryNodes.get(entry.getKey()); @@ -201,27 +195,12 @@ public class ListTasksResponse extends BaseTasksResponse implements ToXContent { group.toXContent(builder, params); } builder.endObject(); - } else { - builder.startObject("tasks"); - if (tasks != null) { - for(TaskInfo task : tasks) { - builder.field(task.getTaskId().toString()); - task.toXContent(builder, params); - } - } - builder.endObject(); } return builder; } @Override public String toString() { - try { - XContentBuilder builder = JsonXContent.contentBuilder(); - toXContent(builder, new MapParams(Collections.singletonMap("group_by", "none"))); - return builder.string(); - } catch (IOException e) { - return "Error building toString out of XContent: " + ExceptionsHelper.stackTrace(e); - } + return Strings.toString(this); } } diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/TransportListTasksAction.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/TransportListTasksAction.java index 26158800346..bbc5a5a9528 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/TransportListTasksAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/TransportListTasksAction.java @@ -35,8 +35,10 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; import java.io.IOException; +import java.util.Collections; import java.util.List; import java.util.function.Consumer; +import java.util.function.Supplier; import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; @@ -57,13 +59,14 @@ public class TransportListTasksAction extends TransportTasksAction new ListTasksResponse(clusterService.state().nodes()), + ThreadPool.Names.MANAGEMENT); } @Override protected ListTasksResponse newResponse(ListTasksRequest request, List tasks, List taskOperationFailures, List failedNodeExceptions) { - return new ListTasksResponse(tasks, taskOperationFailures, failedNodeExceptions); + return new ListTasksResponse(tasks, taskOperationFailures, failedNodeExceptions, clusterService.state().nodes()); } @Override diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestListTasksAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestListTasksAction.java index 4d9f9bd6fa7..ff28d6331bb 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestListTasksAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestListTasksAction.java @@ -81,7 +81,6 @@ public class RestListTasksAction extends BaseRestHandler { return new ActionListener() { @Override public void onResponse(T response) { - response.setDiscoveryNodes(clusterService.state().nodes()); channelListener.onResponse(response); } 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 2c78786ab04..17ca7e7268b 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 @@ -736,12 +736,6 @@ public class TransportTasksActionTests extends TaskManagerTestCase { ListTasksResponse response = testNodes[0].transportListTasksAction.execute(listTasksRequest).get(); assertEquals(testNodes.length + 1, response.getTasks().size()); - // First group by node - DiscoveryNodes.Builder discoNodes = DiscoveryNodes.builder(); - for (TestNode testNode : this.testNodes) { - discoNodes.put(testNode.discoveryNode); - } - response.setDiscoveryNodes(discoNodes.build()); Map byNodes = serialize(response, new ToXContent.MapParams(Collections.singletonMap("group_by", "nodes"))); byNodes = (Map) byNodes.get("nodes"); // One element on the top level diff --git a/core/src/test/java/org/elasticsearch/tasks/ListTasksResponseTests.java b/core/src/test/java/org/elasticsearch/tasks/ListTasksResponseTests.java index 8a473e4c4bb..433f3bdd16a 100644 --- a/core/src/test/java/org/elasticsearch/tasks/ListTasksResponseTests.java +++ b/core/src/test/java/org/elasticsearch/tasks/ListTasksResponseTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.tasks; import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse; +import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matchers; @@ -27,17 +28,19 @@ import java.util.Collections; public class ListTasksResponseTests extends ESTestCase { - public void testToStringWithNoTask() { + public void testToStringNoTask() { ListTasksResponse tasksResponse = new ListTasksResponse(); String string = tasksResponse.toString(); - assertThat(string, Matchers.containsString("tasks")); + assertThat(string, Matchers.containsString("nodes")); } public void testToString() { TaskInfo info = new TaskInfo( new TaskId("node1", 1), "dummy-type", "dummy-action", "dummy-description", null, 0, 1, true, new TaskId("node1", 0)); - ListTasksResponse tasksResponse = new ListTasksResponse(Collections.singletonList(info), Collections.emptyList(), Collections.emptyList()); + DiscoveryNodes nodes = DiscoveryNodes.builder().build(); + ListTasksResponse tasksResponse = new ListTasksResponse(Collections.singletonList(info), Collections.emptyList(), + Collections.emptyList(), nodes); String string = tasksResponse.toString(); assertThat(string, Matchers.containsString("\"type\":\"dummy-type\"")); diff --git a/modules/reindex/src/main/java/org/elasticsearch/index/reindex/TransportRethrottleAction.java b/modules/reindex/src/main/java/org/elasticsearch/index/reindex/TransportRethrottleAction.java index 0ef55fd58ff..408204c801e 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/index/reindex/TransportRethrottleAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/index/reindex/TransportRethrottleAction.java @@ -41,7 +41,8 @@ public class TransportRethrottleAction extends TransportTasksAction new ListTasksResponse(clusterService.state().nodes()), + ThreadPool.Names.MANAGEMENT); } @Override @@ -59,7 +60,7 @@ public class TransportRethrottleAction extends TransportTasksAction tasks, List taskOperationFailures, List failedNodeExceptions) { - return new ListTasksResponse(tasks, taskOperationFailures, failedNodeExceptions); + return new ListTasksResponse(tasks, taskOperationFailures, failedNodeExceptions, clusterService.state().nodes()); } @Override