-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[POC] Query-level resource usages instrumentation with SearchPhaseResult #12449
Closed
ansjcy
wants to merge
1
commit into
opensearch-project:main
from
ansjcy:top-n-queries-with-resource-usages
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for debugging purpoose only