diff --git a/plugin/src/main/java/org/elasticsearch/xpack/persistent/PersistentTasksInProgress.java b/plugin/src/main/java/org/elasticsearch/xpack/persistent/PersistentTasksInProgress.java index 37c3ff1bafc..f9c669f91a0 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/persistent/PersistentTasksInProgress.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/persistent/PersistentTasksInProgress.java @@ -58,16 +58,18 @@ public final class PersistentTasksInProgress extends AbstractNamedDiffable PERSISTENT_TASKS_IN_PROGRESS_PARSER = new ObjectParser<>(TYPE, - Builder::new); + private static final ObjectParser PERSISTENT_TASKS_IN_PROGRESS_PARSER = new ObjectParser<>(TYPE, Builder::new); - public static final ObjectParser, Void> PERSISTENT_TASK_IN_PROGRESS_PARSER = + private static final ObjectParser, Void> PERSISTENT_TASK_IN_PROGRESS_PARSER = new ObjectParser<>("running_tasks", TaskBuilder::new); public static final ConstructingObjectParser ASSIGNMENT_PARSER = new ConstructingObjectParser<>("assignment", objects -> new Assignment((String) objects[0], (String) objects[1])); - public static final NamedObjectParser, Void> ACTION_PARSER; + private static final NamedObjectParser REQUEST_PARSER = + (XContentParser p, Void c, String name) -> p.namedObject(PersistentActionRequest.class, name, null); + private static final NamedObjectParser STATUS_PARSER = + (XContentParser p, Void c, String name) -> p.namedObject(Status.class, name, null); static { // Tasks parser initialization @@ -75,13 +77,6 @@ public final class PersistentTasksInProgress extends AbstractNamedDiffable, String> parser = new ObjectParser<>("named"); - parser.declareObject(ActionDescriptionBuilder::setRequest, - (p, c) -> p.namedObject(PersistentActionRequest.class, c, null), new ParseField("request")); - parser.declareObject(ActionDescriptionBuilder::setStatus, - (p, c) -> p.namedObject(Status.class, c, null), new ParseField("status")); - ACTION_PARSER = (XContentParser p, Void c, String name) -> parser.parse(p, new ActionDescriptionBuilder<>(name), name); // Assignment parser ASSIGNMENT_PARSER.declareStringOrNull(constructorArg(), new ParseField("executor_node")); @@ -89,47 +84,32 @@ public final class PersistentTasksInProgress extends AbstractNamedDiffable taskBuilder, List> objects) -> { + (TaskBuilder taskBuilder, List objects) -> { if (objects.size() != 1) { - throw new IllegalArgumentException("only one action description per task is allowed"); + throw new IllegalArgumentException("only one action request per task is allowed"); } - ActionDescriptionBuilder builder = objects.get(0); - taskBuilder.setAction(builder.action); - taskBuilder.setRequest(builder.request); - taskBuilder.setStatus(builder.status); - }, ACTION_PARSER, new ParseField("action")); + taskBuilder.setRequest(objects.get(0)); + }, REQUEST_PARSER, new ParseField("request")); + + PERSISTENT_TASK_IN_PROGRESS_PARSER.declareNamedObjects( + (TaskBuilder taskBuilder, List objects) -> { + if (objects.size() != 1) { + throw new IllegalArgumentException("only one status per task is allowed"); + } + taskBuilder.setStatus(objects.get(0)); + }, STATUS_PARSER, new ParseField("status")); + + PERSISTENT_TASK_IN_PROGRESS_PARSER.declareObject(TaskBuilder::setAssignment, ASSIGNMENT_PARSER, new ParseField("assignment")); PERSISTENT_TASK_IN_PROGRESS_PARSER.declareLong(TaskBuilder::setAllocationIdOnLastStatusUpdate, new ParseField("allocation_id_on_last_status_update")); } - /** - * Private builder used in XContent parser - */ - private static class ActionDescriptionBuilder { - private final String action; - private Request request; - private Status status; - - private ActionDescriptionBuilder(String action) { - this.action = action; - } - - private ActionDescriptionBuilder setRequest(Request request) { - this.request = request; - return this; - } - - private ActionDescriptionBuilder setStatus(Status status) { - this.status = status; - return this; - } - } - public Collection> tasks() { return this.tasks.values(); } @@ -404,19 +384,20 @@ public final class PersistentTasksInProgress extends AbstractNamedDiffable