|
| 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.tasks.TaskId; |
| 18 | +import org.opensearch.core.xcontent.ConstructingObjectParser; |
| 19 | +import org.opensearch.core.xcontent.MediaTypeRegistry; |
| 20 | +import org.opensearch.core.xcontent.ToXContentFragment; |
| 21 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 22 | +import org.opensearch.core.xcontent.XContentParser; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +import static org.opensearch.core.xcontent.ConstructingObjectParser.constructorArg; |
| 28 | + |
| 29 | +/** |
| 30 | + * Task resource usage information with minimal information about the task |
| 31 | + * <p> |
| 32 | + * Writeable TaskResourceInfo objects are used to represent resource usage |
| 33 | + * information of running tasks, which can be propagated to coordinator node |
| 34 | + * to infer query-level resource usage |
| 35 | + * |
| 36 | + * @opensearch.api |
| 37 | + */ |
| 38 | +@PublicApi(since = "2.1.0") |
| 39 | +public class TaskResourceInfo implements Writeable, ToXContentFragment { |
| 40 | + public TaskResourceUsage taskResourceUsage; |
| 41 | + public String action; |
| 42 | + public long taskId; |
| 43 | + public long parentTaskId; |
| 44 | + |
| 45 | + public TaskResourceInfo() { |
| 46 | + this.action = ""; |
| 47 | + this.taskId = -1L; |
| 48 | + this.taskResourceUsage = new TaskResourceUsage(0, 0); |
| 49 | + } |
| 50 | + |
| 51 | + public TaskResourceInfo(String action, long taskId, long cpuTimeInNanos, long memoryInBytes) { |
| 52 | + this.action = action; |
| 53 | + this.taskId = taskId; |
| 54 | + this.taskResourceUsage = new TaskResourceUsage(cpuTimeInNanos, memoryInBytes); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Read from a stream. |
| 59 | + */ |
| 60 | + public static TaskResourceInfo readFromStream(StreamInput in) throws IOException { |
| 61 | + TaskResourceInfo info = new TaskResourceInfo(); |
| 62 | + info.action = in.readString(); |
| 63 | + info.taskId = in.readLong(); |
| 64 | + info.taskResourceUsage = TaskResourceUsage.readFromStream(in); |
| 65 | + info.parentTaskId = in.readLong(); |
| 66 | + return info; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void writeTo(StreamOutput out) throws IOException { |
| 71 | + out.writeString(action); |
| 72 | + out.writeLong(taskId); |
| 73 | + taskResourceUsage.writeTo(out); |
| 74 | + out.writeLong(parentTaskId); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 79 | + // TODO: change to a constant |
| 80 | + builder.field("Action", action); |
| 81 | + taskResourceUsage.toXContent(builder, params); |
| 82 | + return builder; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String toString() { |
| 87 | + return Strings.toString(MediaTypeRegistry.JSON, this, false, true); |
| 88 | + } |
| 89 | + |
| 90 | + // Implements equals and hashcode for testing |
| 91 | + @Override |
| 92 | + public boolean equals(Object obj) { |
| 93 | + if (obj == null || obj.getClass() != TaskResourceInfo.class) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + TaskResourceInfo other = (TaskResourceInfo) obj; |
| 97 | + return action.equals(other.action) && taskId == other.taskId && taskResourceUsage.equals(other.taskResourceUsage); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public int hashCode() { |
| 102 | + return Objects.hash(action, taskId, taskResourceUsage); |
| 103 | + } |
| 104 | +} |
0 commit comments