|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.core.tasks.resourcetracker; |
| 10 | + |
| 11 | +import org.opensearch.common.annotation.PublicApi; |
| 12 | +import org.opensearch.core.ParseField; |
| 13 | +import org.opensearch.core.common.Strings; |
| 14 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 15 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 16 | +import org.opensearch.core.common.io.stream.Writeable; |
| 17 | +import org.opensearch.core.xcontent.ConstructingObjectParser; |
| 18 | +import org.opensearch.core.xcontent.MediaTypeRegistry; |
| 19 | +import org.opensearch.core.xcontent.ToXContentObject; |
| 20 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +import static org.opensearch.core.xcontent.ConstructingObjectParser.constructorArg; |
| 26 | + |
| 27 | +/** |
| 28 | + * Task resource usage information with minimal information about the task |
| 29 | + * <p> |
| 30 | + * Writeable TaskResourceInfo objects are used to represent resource usage |
| 31 | + * information of running tasks, which can be propagated to coordinator node |
| 32 | + * to infer query-level resource usage |
| 33 | + * |
| 34 | + * @opensearch.api |
| 35 | + */ |
| 36 | +@PublicApi(since = "2.15.0") |
| 37 | +public class TaskResourceInfo implements Writeable, ToXContentObject { |
| 38 | + private final String action; |
| 39 | + private final long taskId; |
| 40 | + private final long parentTaskId; |
| 41 | + private final String nodeId; |
| 42 | + private final TaskResourceUsage taskResourceUsage; |
| 43 | + |
| 44 | + private static final ParseField ACTION = new ParseField("action"); |
| 45 | + private static final ParseField TASK_ID = new ParseField("taskId"); |
| 46 | + private static final ParseField PARENT_TASK_ID = new ParseField("parentTaskId"); |
| 47 | + private static final ParseField NODE_ID = new ParseField("nodeId"); |
| 48 | + private static final ParseField TASK_RESOURCE_USAGE = new ParseField("taskResourceUsage"); |
| 49 | + |
| 50 | + public TaskResourceInfo( |
| 51 | + final String action, |
| 52 | + final long taskId, |
| 53 | + final long parentTaskId, |
| 54 | + final String nodeId, |
| 55 | + final TaskResourceUsage taskResourceUsage |
| 56 | + ) { |
| 57 | + this.action = action; |
| 58 | + this.taskId = taskId; |
| 59 | + this.parentTaskId = parentTaskId; |
| 60 | + this.nodeId = nodeId; |
| 61 | + this.taskResourceUsage = taskResourceUsage; |
| 62 | + } |
| 63 | + |
| 64 | + public static final ConstructingObjectParser<TaskResourceInfo, Void> PARSER = new ConstructingObjectParser<>( |
| 65 | + "task_resource_info", |
| 66 | + a -> new Builder().setAction((String) a[0]) |
| 67 | + .setTaskId((Long) a[1]) |
| 68 | + .setParentTaskId((Long) a[2]) |
| 69 | + .setNodeId((String) a[3]) |
| 70 | + .setTaskResourceUsage((TaskResourceUsage) a[4]) |
| 71 | + .build() |
| 72 | + ); |
| 73 | + |
| 74 | + static { |
| 75 | + PARSER.declareString(constructorArg(), ACTION); |
| 76 | + PARSER.declareLong(constructorArg(), TASK_ID); |
| 77 | + PARSER.declareLong(constructorArg(), PARENT_TASK_ID); |
| 78 | + PARSER.declareString(constructorArg(), NODE_ID); |
| 79 | + PARSER.declareObject(constructorArg(), TaskResourceUsage.PARSER, TASK_RESOURCE_USAGE); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 84 | + builder.startObject(); |
| 85 | + builder.field(ACTION.getPreferredName(), this.action); |
| 86 | + builder.field(TASK_ID.getPreferredName(), this.taskId); |
| 87 | + builder.field(PARENT_TASK_ID.getPreferredName(), this.parentTaskId); |
| 88 | + builder.field(NODE_ID.getPreferredName(), this.nodeId); |
| 89 | + builder.startObject(TASK_RESOURCE_USAGE.getPreferredName()); |
| 90 | + this.taskResourceUsage.toXContent(builder, params); |
| 91 | + builder.endObject(); |
| 92 | + builder.endObject(); |
| 93 | + return builder; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Builder for {@link TaskResourceInfo} |
| 98 | + */ |
| 99 | + public static class Builder { |
| 100 | + private TaskResourceUsage taskResourceUsage; |
| 101 | + private String action; |
| 102 | + private long taskId; |
| 103 | + private long parentTaskId; |
| 104 | + private String nodeId; |
| 105 | + |
| 106 | + public Builder setTaskResourceUsage(final TaskResourceUsage taskResourceUsage) { |
| 107 | + this.taskResourceUsage = taskResourceUsage; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public Builder setAction(final String action) { |
| 112 | + this.action = action; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public Builder setTaskId(final long taskId) { |
| 117 | + this.taskId = taskId; |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + public Builder setParentTaskId(final long parentTaskId) { |
| 122 | + this.parentTaskId = parentTaskId; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public Builder setNodeId(final String nodeId) { |
| 127 | + this.nodeId = nodeId; |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + public TaskResourceInfo build() { |
| 132 | + return new TaskResourceInfo(action, taskId, parentTaskId, nodeId, taskResourceUsage); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Read task info from a stream. |
| 138 | + * |
| 139 | + * @param in StreamInput to read |
| 140 | + * @return {@link TaskResourceInfo} |
| 141 | + * @throws IOException IOException |
| 142 | + */ |
| 143 | + public static TaskResourceInfo readFromStream(StreamInput in) throws IOException { |
| 144 | + return new TaskResourceInfo.Builder().setAction(in.readString()) |
| 145 | + .setTaskId(in.readLong()) |
| 146 | + .setParentTaskId(in.readLong()) |
| 147 | + .setNodeId(in.readString()) |
| 148 | + .setTaskResourceUsage(TaskResourceUsage.readFromStream(in)) |
| 149 | + .build(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get TaskResourceUsage |
| 154 | + * |
| 155 | + * @return taskResourceUsage |
| 156 | + */ |
| 157 | + public TaskResourceUsage getTaskResourceUsage() { |
| 158 | + return taskResourceUsage; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Get parent task id |
| 163 | + * |
| 164 | + * @return parent task id |
| 165 | + */ |
| 166 | + public long getParentTaskId() { |
| 167 | + return parentTaskId; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Get task id |
| 172 | + * @return task id |
| 173 | + */ |
| 174 | + public long getTaskId() { |
| 175 | + return taskId; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Get node id |
| 180 | + * @return node id |
| 181 | + */ |
| 182 | + public String getNodeId() { |
| 183 | + return nodeId; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Get task action |
| 188 | + * @return task action |
| 189 | + */ |
| 190 | + public String getAction() { |
| 191 | + return action; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void writeTo(StreamOutput out) throws IOException { |
| 196 | + out.writeString(action); |
| 197 | + out.writeLong(taskId); |
| 198 | + out.writeLong(parentTaskId); |
| 199 | + out.writeString(nodeId); |
| 200 | + taskResourceUsage.writeTo(out); |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public String toString() { |
| 205 | + return Strings.toString(MediaTypeRegistry.JSON, this); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public boolean equals(Object obj) { |
| 210 | + if (obj == null || obj.getClass() != TaskResourceInfo.class) { |
| 211 | + return false; |
| 212 | + } |
| 213 | + TaskResourceInfo other = (TaskResourceInfo) obj; |
| 214 | + return action.equals(other.action) |
| 215 | + && taskId == other.taskId |
| 216 | + && parentTaskId == other.parentTaskId |
| 217 | + && Objects.equals(nodeId, other.nodeId) |
| 218 | + && taskResourceUsage.equals(other.taskResourceUsage); |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public int hashCode() { |
| 223 | + return Objects.hash(action, taskId, parentTaskId, nodeId, taskResourceUsage); |
| 224 | + } |
| 225 | +} |
0 commit comments