-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
instrument resource usage before task finishes on data nodes and gath…
…er on coordinator node
- Loading branch information
Showing
30 changed files
with
415 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
libs/core/src/main/java/org/opensearch/core/tasks/resourcetracker/TaskResourceInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.core.tasks.resourcetracker; | ||
|
||
import org.opensearch.common.annotation.PublicApi; | ||
import org.opensearch.core.ParseField; | ||
import org.opensearch.core.common.Strings; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.common.io.stream.Writeable; | ||
import org.opensearch.core.tasks.TaskId; | ||
import org.opensearch.core.xcontent.ConstructingObjectParser; | ||
import org.opensearch.core.xcontent.MediaTypeRegistry; | ||
import org.opensearch.core.xcontent.ToXContentFragment; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import static org.opensearch.core.xcontent.ConstructingObjectParser.constructorArg; | ||
|
||
/** | ||
* Task resource usage information with minimal information about the task | ||
* <p> | ||
* Writeable TaskResourceInfo objects are used to represent resource usage | ||
* information of running tasks, which can be propagated to coordinator node | ||
* to infer query-level resource usage | ||
* | ||
* @opensearch.api | ||
*/ | ||
@PublicApi(since = "2.1.0") | ||
public class TaskResourceInfo implements Writeable, ToXContentFragment { | ||
public TaskResourceUsage taskResourceUsage; | ||
public String action; | ||
public long taskId; | ||
public long parentTaskId; | ||
|
||
public TaskResourceInfo() { | ||
this.action = ""; | ||
this.taskId = -1L; | ||
this.taskResourceUsage = new TaskResourceUsage(0, 0); | ||
} | ||
|
||
public TaskResourceInfo(String action, long taskId, long cpuTimeInNanos, long memoryInBytes) { | ||
this.action = action; | ||
this.taskId = taskId; | ||
this.taskResourceUsage = new TaskResourceUsage(cpuTimeInNanos, memoryInBytes); | ||
} | ||
|
||
/** | ||
* Read from a stream. | ||
*/ | ||
public static TaskResourceInfo readFromStream(StreamInput in) throws IOException { | ||
TaskResourceInfo info = new TaskResourceInfo(); | ||
info.action = in.readString(); | ||
info.taskId = in.readLong(); | ||
info.taskResourceUsage = TaskResourceUsage.readFromStream(in); | ||
info.parentTaskId = in.readLong(); | ||
return info; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(action); | ||
out.writeLong(taskId); | ||
taskResourceUsage.writeTo(out); | ||
out.writeLong(parentTaskId); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
// TODO: change to a constant | ||
builder.field("Action", action); | ||
taskResourceUsage.toXContent(builder, params); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(MediaTypeRegistry.JSON, this, false, true); | ||
} | ||
|
||
// Implements equals and hashcode for testing | ||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null || obj.getClass() != TaskResourceInfo.class) { | ||
return false; | ||
} | ||
TaskResourceInfo other = (TaskResourceInfo) obj; | ||
return action.equals(other.action) && taskId == other.taskId && taskResourceUsage.equals(other.taskResourceUsage); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(action, taskId, taskResourceUsage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
.../src/main/java/org/opensearch/plugin/insights/core/listener/ResourceTrackingListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.insights.core.listener; | ||
|
||
import org.opensearch.core.tasks.resourcetracker.TaskResourceInfo; | ||
import org.opensearch.plugin.insights.core.service.QueryInsightsService; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.tasks.TaskResourceTrackingService; | ||
import org.opensearch.tasks.TaskResourceTrackingService.TaskCompletionListener; | ||
import org.opensearch.tasks.TaskResourceTrackingService.TaskStartListener; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class ResourceTrackingListener implements TaskCompletionListener, TaskStartListener { | ||
|
||
private final TaskResourceTrackingService taskResourceTrackingService; | ||
private final QueryInsightsService queryInsightsService; | ||
|
||
public ResourceTrackingListener ( | ||
QueryInsightsService queryInsightsService, | ||
TaskResourceTrackingService taskResourceTrackingService | ||
) { | ||
this.queryInsightsService = queryInsightsService; | ||
this.taskResourceTrackingService = taskResourceTrackingService; | ||
this.taskResourceTrackingService.addTaskCompletionListener(this); | ||
this.taskResourceTrackingService.addTaskStartListener(this); | ||
} | ||
@Override | ||
public void onTaskCompleted(Task task) { | ||
TaskResourceInfo info = new TaskResourceInfo(); | ||
info.taskResourceUsage = task.getTotalResourceStats(); | ||
info.taskId = task.getId(); | ||
info.action = task.getAction(); | ||
info.parentTaskId = task.getParentTaskId().getId(); | ||
long parentTaskId = task.getParentTaskId().getId(); | ||
if (parentTaskId == -1) { | ||
parentTaskId = task.getId(); | ||
} | ||
|
||
this.queryInsightsService.taskStatusMap.get(parentTaskId).decrementAndGet(); | ||
queryInsightsService.taskRecordsQueue.add(info); | ||
// System.out.println(String.format("id = %s, parent = %s, resource = %s, action = %s, total CPU and MEM: %s, %s", task.getId(), task.getParentTaskId(), task.getResourceStats(), task.getAction(),task.getTotalResourceUtilization(ResourceStats.CPU),task.getTotalResourceUtilization(ResourceStats.MEMORY) )); | ||
} | ||
|
||
@Override | ||
public void onTaskStarts(Task task) { | ||
long parentId = task.getParentTaskId().getId(); | ||
if (parentId == -1) { | ||
parentId = task.getId(); | ||
} | ||
this.queryInsightsService.taskStatusMap.putIfAbsent(parentId, new AtomicInteger(0)); | ||
this.queryInsightsService.taskStatusMap.get(parentId).incrementAndGet(); | ||
} | ||
} |
Oops, something went wrong.