Skip to content
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

Rest High Level client: Add List Tasks #29546

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -118,7 +118,7 @@ public Exception getCause() {

@Override
public String toString() {
return Strings.toString(this);
return "[" + nodeId + "][" + taskId + "] failed, reason [" + getReason() + "]";
}

public static TaskOperationFailure fromXContent(XContentParser parser) {
Expand All @@ -139,20 +139,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;

}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TaskOperationFailure that = (TaskOperationFailure) o;
return taskId == that.taskId &&
Objects.equals(nodeId, that.nodeId) &&
// 'reason' is not checked because Exception doesn't have overridden equals()
status == that.status;
}

@Override
public int hashCode() {
return Objects.hash(taskId, nodeId, status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.elasticsearch.action.admin.cluster.node.tasks.list;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.FailedNodeException;
import org.elasticsearch.action.TaskOperationFailure;
import org.elasticsearch.action.support.tasks.BaseTasksResponse;
import org.elasticsearch.cluster.node.DiscoveryNode;
Expand All @@ -42,7 +41,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
Expand Down Expand Up @@ -270,18 +268,4 @@ public static ListTasksResponse fromXContent(XContentParser parser) {
public String toString() {
return Strings.toString(this);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ListTasksResponse that = (ListTasksResponse) o;
return Objects.equals(tasks, that.tasks) &&
Objects.equals(getTaskFailures(), that.getTaskFailures());
}

@Override
public int hashCode() {
return Objects.hash(tasks);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.AbstractStreamableTestCase;
import org.elasticsearch.test.AbstractXContentTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

public class ListTasksResponseTests extends AbstractStreamableTestCase<ListTasksResponse> {
public class ListTasksResponseTests extends AbstractXContentTestCase<ListTasksResponse> {

public void testEmptyToString() {
assertEquals("{\"tasks\":[]}", new ListTasksResponse().toString());
Expand All @@ -57,17 +58,6 @@ public void testNonEmptyToString() {
+ "\"parent_task_id\":\"node1:0\",\"headers\":{\"foo\":\"bar\"}}]}", tasksResponse.toString());
}

@Override
protected ListTasksResponse createBlankInstance() {
return new ListTasksResponse();
}

@Override
protected NamedWriteableRegistry getNamedWriteableRegistry() {
return new NamedWriteableRegistry(Collections.singletonList(
new NamedWriteableRegistry.Entry(Task.Status.class, RawTaskStatus.NAME, RawTaskStatus::new)));
}

@Override
protected ListTasksResponse createTestInstance() {
List<TaskInfo> tasks = new ArrayList<>();
Expand All @@ -82,18 +72,29 @@ protected ListTasksResponse createTestInstance() {
return new ListTasksResponse(tasks, taskFailures, Collections.singletonList(new FailedNodeException("", "message", null)));
}

public void testXContentWithFailures() throws IOException {
ListTasksResponse expected = createTestInstance();
XContentType xContentType = randomFrom(XContentType.values());
BytesReference serialized = XContentHelper.toXContent(expected, xContentType, false);
XContentParser parser = createParser(XContentFactory.xContent(xContentType), serialized);
ListTasksResponse parsed = ListTasksResponse.fromXContent(parser);
@Override
protected ListTasksResponse doParseInstance(XContentParser parser) throws IOException {
return ListTasksResponse.fromXContent(parser);
}

assertThat(parsed, equalTo(expected));
assertThat(parsed.getNodeFailures().size(), equalTo(1));
for (ElasticsearchException failure : parsed.getNodeFailures()) {
@Override
protected boolean supportsUnknownFields() {
return false;
}

@Override
protected void assertEqualInstances(ListTasksResponse expectedInstance, ListTasksResponse newInstance) {
assertNotSame(expectedInstance, newInstance);
assertThat(newInstance.getTasks(), equalTo(expectedInstance.getTasks()));
assertThat(newInstance.getNodeFailures().size(), equalTo(1));
for (ElasticsearchException failure : newInstance.getNodeFailures()) {
assertThat(failure, notNullValue());
assertThat(failure.getMessage(), equalTo("Elasticsearch exception [type=failed_node_exception, reason=message]"));
}
}

@Override
protected boolean assertToXContentEquivalence() {
return false;
}
}
55 changes: 55 additions & 0 deletions server/src/test/java/org/elasticsearch/tasks/TaskInfoTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

Expand Down Expand Up @@ -65,6 +66,60 @@ protected Predicate<String> getRandomFieldsExcludeFilter() {
return field -> "status".equals(field) || "headers".equals(field);
}

@Override
protected TaskInfo mutateInstance(TaskInfo info) throws IOException {
switch (between(0, 9)) {
case 0:
TaskId taskId = new TaskId(info.getTaskId().getNodeId() + randomAlphaOfLength(5), info.getTaskId().getId());
return new TaskInfo(taskId, info.getType(), info.getAction(), info.getDescription(), info.getStatus(),
info.getStartTime(), info.getRunningTimeNanos(), info.isCancellable(), info.getParentTaskId(), info.getHeaders());
case 1:
return new TaskInfo(info.getTaskId(), info.getType() + randomAlphaOfLength(5), info.getAction(), info.getDescription(),
info.getStatus(), info.getStartTime(), info.getRunningTimeNanos(), info.isCancellable(), info.getParentTaskId(),
info.getHeaders());
case 2:
return new TaskInfo(info.getTaskId(), info.getType(), info.getAction() + randomAlphaOfLength(5), info.getDescription(),
info.getStatus(), info.getStartTime(), info.getRunningTimeNanos(), info.isCancellable(), info.getParentTaskId(),
info.getHeaders());
case 3:
return new TaskInfo(info.getTaskId(), info.getType(), info.getAction(), info.getDescription() + randomAlphaOfLength(5),
info.getStatus(), info.getStartTime(), info.getRunningTimeNanos(), info.isCancellable(), info.getParentTaskId(),
info.getHeaders());
case 4:
Task.Status newStatus = randomValueOtherThan(info.getStatus(), TaskInfoTests::randomRawTaskStatus);
return new TaskInfo(info.getTaskId(), info.getType(), info.getAction(), info.getDescription(), newStatus,
info.getStartTime(), info.getRunningTimeNanos(), info.isCancellable(), info.getParentTaskId(), info.getHeaders());
case 5:
return new TaskInfo(info.getTaskId(), info.getType(), info.getAction(), info.getDescription(), info.getStatus(),
info.getStartTime() + between(1, 100), info.getRunningTimeNanos(), info.isCancellable(), info.getParentTaskId(),
info.getHeaders());
case 6:
return new TaskInfo(info.getTaskId(), info.getType(), info.getAction(), info.getDescription(), info.getStatus(),
info.getStartTime(), info.getRunningTimeNanos() + between(1, 100), info.isCancellable(), info.getParentTaskId(),
info.getHeaders());
case 7:
return new TaskInfo(info.getTaskId(), info.getType(), info.getAction(), info.getDescription(), info.getStatus(),
info.getStartTime(), info.getRunningTimeNanos(), info.isCancellable() == false, info.getParentTaskId(),
info.getHeaders());
case 8:
TaskId parentId = new TaskId(info.getParentTaskId().getNodeId() + randomAlphaOfLength(5), info.getParentTaskId().getId());
return new TaskInfo(info.getTaskId(), info.getType(), info.getAction(), info.getDescription(), info.getStatus(),
info.getStartTime(), info.getRunningTimeNanos(), info.isCancellable(), parentId, info.getHeaders());
case 9:
Map<String, String> headers = info.getHeaders();
if (headers == null) {
headers = new HashMap<>(1);
} else {
headers = new HashMap<>(info.getHeaders());
}
headers.put(randomAlphaOfLength(15), randomAlphaOfLength(15));
return new TaskInfo(info.getTaskId(), info.getType(), info.getAction(), info.getDescription(), info.getStatus(),
info.getStartTime(), info.getRunningTimeNanos(), info.isCancellable(), info.getParentTaskId(), headers);
default:
throw new IllegalStateException();
}
}

static TaskInfo randomTaskInfo() {
TaskId taskId = randomTaskId();
String type = randomAlphaOfLength(5);
Expand Down