Skip to content
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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public long getTotalValue() {
return endValue.get() - startValue;
}

public long getStartValue() {
return startValue;
}

@Override
public String toString() {
return String.valueOf(getTotalValue());
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static TaskResourceUsage fromXContent(XContentParser parser) {

@Override
public String toString() {
return Strings.toString(MediaTypeRegistry.JSON, this, true, true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for debugging purpoose only

return Strings.toString(MediaTypeRegistry.JSON, this, false, true);
}

// Implements equals and hashcode for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.script.ScriptContext;
import org.opensearch.script.ScriptService;
import org.opensearch.tasks.TaskResourceTrackingService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;

Expand Down Expand Up @@ -187,7 +188,8 @@ public Collection<Object> createComponents(
NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver expressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier
Supplier<RepositoriesService> repositoriesServiceSupplier,
TaskResourceTrackingService taskResourceTrackingService
) {
this.scriptService.set(scriptService);
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.opensearch.script.ScriptEngine;
import org.opensearch.script.ScriptService;
import org.opensearch.search.aggregations.pipeline.MovingFunctionScript;
import org.opensearch.tasks.TaskResourceTrackingService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;

Expand Down Expand Up @@ -140,7 +141,8 @@ public Collection<Object> createComponents(
NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver expressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier
Supplier<RepositoriesService> repositoriesServiceSupplier,
TaskResourceTrackingService taskResourceTrackingService
) {
// this is a hack to bind the painless script engine in guice (all components are added to guice), so that
// the painless context api. this is a temporary measure until transport actions do no require guice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.opensearch.rest.RestHandler;
import org.opensearch.script.ScriptService;
import org.opensearch.tasks.Task;
import org.opensearch.tasks.TaskResourceTrackingService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;

Expand Down Expand Up @@ -122,7 +123,8 @@ public Collection<Object> createComponents(
NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver expressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier
Supplier<RepositoriesService> repositoriesServiceSupplier,
TaskResourceTrackingService taskResourceTrackingService
) {
return Collections.singletonList(new ReindexSslConfig(environment.settings(), environment, resourceWatcherService));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.opensearch.plugins.Plugin;
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.script.ScriptService;
import org.opensearch.tasks.TaskResourceTrackingService;
import org.opensearch.threadpool.Scheduler;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;
Expand Down Expand Up @@ -100,7 +101,8 @@ public Collection<Object> createComponents(
final NodeEnvironment nodeEnvironment,
final NamedWriteableRegistry namedWriteableRegistry,
final IndexNameExpressionResolver expressionResolver,
final Supplier<RepositoriesService> repositoriesServiceSupplier
final Supplier<RepositoriesService> repositoriesServiceSupplier,
TaskResourceTrackingService taskResourceTrackingService
) {
if (enabled == false) {
extender.set(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.plugin.insights.core.listener.QueryInsightsListener;
import org.opensearch.plugin.insights.core.listener.ResourceTrackingListener;
import org.opensearch.plugin.insights.core.service.QueryInsightsService;
import org.opensearch.plugin.insights.rules.action.top_queries.TopQueriesAction;
import org.opensearch.plugin.insights.rules.resthandler.top_queries.RestTopQueriesAction;
Expand All @@ -37,6 +38,7 @@
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;
import org.opensearch.script.ScriptService;
import org.opensearch.tasks.TaskResourceTrackingService;
import org.opensearch.threadpool.ExecutorBuilder;
import org.opensearch.threadpool.ScalingExecutorBuilder;
import org.opensearch.threadpool.ThreadPool;
Expand Down Expand Up @@ -67,11 +69,16 @@ public Collection<Object> createComponents(
final NodeEnvironment nodeEnvironment,
final NamedWriteableRegistry namedWriteableRegistry,
final IndexNameExpressionResolver indexNameExpressionResolver,
final Supplier<RepositoriesService> repositoriesServiceSupplier
final Supplier<RepositoriesService> repositoriesServiceSupplier,
final TaskResourceTrackingService taskResourceTrackingService
) {
// create top n queries service
final QueryInsightsService queryInsightsService = new QueryInsightsService(threadPool);
return List.of(queryInsightsService, new QueryInsightsListener(clusterService, queryInsightsService));
return List.of(
queryInsightsService,
new QueryInsightsListener(clusterService, queryInsightsService),
new ResourceTrackingListener(queryInsightsService, taskResourceTrackingService)
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.opensearch.action.search.SearchRequestOperationsListener;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.core.tasks.resourcetracker.TaskResourceInfo;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.plugin.insights.core.service.QueryInsightsService;
import org.opensearch.plugin.insights.rules.model.Attribute;
Expand All @@ -24,6 +25,7 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -113,7 +115,10 @@ public boolean isEnabled() {
public void onPhaseStart(SearchPhaseContext context) {}

@Override
public void onPhaseEnd(SearchPhaseContext context, SearchRequestContext searchRequestContext) {}
public void onPhaseEnd(SearchPhaseContext context, SearchRequestContext searchRequestContext) {
List<TaskResourceInfo> usages = context.getCurrentPhase().getPhaseResourceUsageFromResults();
this.queryInsightsService.taskRecordsQueue.addAll(usages);
}

@Override
public void onPhaseFailure(SearchPhaseContext context) {}
Expand All @@ -123,6 +128,10 @@ public void onRequestStart(SearchRequestContext searchRequestContext) {}

@Override
public void onRequestEnd(final SearchPhaseContext context, final SearchRequestContext searchRequestContext) {
long parentId = context.getTask().getParentTaskId().getId();
if (parentId == -1) {
parentId = context.getTask().getId();
}
final SearchRequest request = context.getRequest();
try {
Map<MetricType, Number> measurements = new HashMap<>();
Expand All @@ -139,6 +148,7 @@ public void onRequestEnd(final SearchPhaseContext context, final SearchRequestCo
attributes.put(Attribute.INDICES, request.indices());
attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap());
SearchQueryRecord record = new SearchQueryRecord(request.getOrCreateAbsoluteStartMillis(), measurements, attributes);
record.taskId = parentId;
queryInsightsService.addRecord(record);
} catch (Exception e) {
log.error(String.format(Locale.ROOT, "fail to ingest query insight data, error: %s", e));
Expand Down
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();
}
}
Loading
Loading