Skip to content

Commit

Permalink
Query-level resource usages tracking
Browse files Browse the repository at this point in the history
Signed-off-by: Chenyang Ji <cyji@amazon.com>
  • Loading branch information
ansjcy committed Apr 12, 2024
1 parent 5d939b9 commit 32f9a75
Show file tree
Hide file tree
Showing 43 changed files with 574 additions and 67 deletions.
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,151 @@
/*
* 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.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;

import java.io.IOException;
import java.util.Objects;

/**
* 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.15.0")
public class TaskResourceInfo implements Writeable {
private TaskResourceUsage taskResourceUsage;
private String action;
private long taskId;
private long parentTaskId;

public TaskResourceInfo() {
this.action = "";
this.taskId = -1L;
this.taskResourceUsage = new TaskResourceUsage(0, 0);
}

public TaskResourceInfo(String action, long taskId, long parentTaskId, TaskResourceUsage taskResourceUsage) {
this.action = action;
this.taskId = taskId;
this.parentTaskId = parentTaskId;
this.taskResourceUsage = taskResourceUsage;
}

/**
* Read task info from a stream.
*
* @param in StreamInput to read
* @return {@link TaskResourceInfo}
* @throws IOException IOException
*/
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;
}

/**
* Get taskResourceUsage
*
* @return taskResourceUsage
*/
public TaskResourceUsage getTaskResourceUsage() {
return taskResourceUsage;
}

/**
* Set taskResourceUsage
* @param taskResourceUsage the updated taskResourceUsage
*/
public void setTaskResourceUsage(TaskResourceUsage taskResourceUsage) {
this.taskResourceUsage = taskResourceUsage;
}

/**
* Get parent task id
*
* @return parent task id
*/
public long getParentTaskId() {
return parentTaskId;
}

/**
* Set parent task id
* @param parentTaskId parent task id
*/
public void setParentTaskId(long parentTaskId) {
this.parentTaskId = parentTaskId;
}

/**
* Get task id
* @return task id
*/
public long getTaskId() {
return taskId;
}

/**
* Set task id
* @param taskId task id
*/
public void setTaskId(long taskId) {
this.taskId = taskId;
}

/**
* Get task action
* @return task action
*/
public String getAction() {
return action;
}

/**
* Set task action
* @param action task action
*/
public void setAction(String action) {
this.action = action;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(action);
out.writeLong(taskId);
taskResourceUsage.writeTo(out);
out.writeLong(parentTaskId);
}

@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 @@ -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 @@ -67,6 +67,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 @@ -146,7 +147,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 @@ -44,6 +44,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.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;

Expand Down Expand Up @@ -87,7 +88,8 @@ public Collection<Object> createComponents(
NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier
Supplier<RepositoriesService> repositoriesServiceSupplier,
TaskResourceTrackingService taskResourceTrackingService
) {
correlationRuleIndices = new CorrelationRuleIndices(client, clusterService);
return List.of(correlationRuleIndices);
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,11 @@ public boolean isEnabled() {
public void onPhaseStart(SearchPhaseContext context) {}

@Override
public void onPhaseEnd(SearchPhaseContext context, SearchRequestContext searchRequestContext) {}
public void onPhaseEnd(SearchPhaseContext context, SearchRequestContext searchRequestContext) {
// Save task-level resource usages for calculating query-level resource usages later
List<TaskResourceInfo> usages = context.getCurrentPhase().getPhaseResourceUsageFromResults();
this.queryInsightsService.taskRecordsQueue.addAll(usages);
}

@Override
public void onPhaseFailure(SearchPhaseContext context, Throwable cause) {}
Expand All @@ -123,6 +129,11 @@ public void onRequestStart(SearchRequestContext searchRequestContext) {}

@Override
public void onRequestEnd(final SearchPhaseContext context, final SearchRequestContext searchRequestContext) {
long parentTaskId = context.getTask().getParentTaskId().getId();
// If the current task is a parent task
if (parentTaskId == -1) {
parentTaskId = context.getTask().getId();
}
final SearchRequest request = context.getRequest();
try {
Map<MetricType, Number> measurements = new HashMap<>();
Expand All @@ -138,6 +149,7 @@ public void onRequestEnd(final SearchPhaseContext context, final SearchRequestCo
attributes.put(Attribute.TOTAL_SHARDS, context.getNumShards());
attributes.put(Attribute.INDICES, request.indices());
attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap());
attributes.put(Attribute.TASK_ID, parentTaskId);
SearchQueryRecord record = new SearchQueryRecord(request.getOrCreateAbsoluteStartMillis(), measurements, attributes);
queryInsightsService.addRecord(record);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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;

/**
* Listener for task level resource usage
*/
public class ResourceTrackingListener implements TaskCompletionListener, TaskStartListener {
private final QueryInsightsService queryInsightsService;

/**
* Constructor of ResourceTrackingListener
* @param queryInsightsService queryInsightsService
* @param taskResourceTrackingService taskResourceTrackingService
*/
public ResourceTrackingListener(QueryInsightsService queryInsightsService, TaskResourceTrackingService taskResourceTrackingService) {
this.queryInsightsService = queryInsightsService;
taskResourceTrackingService.addTaskCompletionListener(this);
taskResourceTrackingService.addTaskStartListener(this);
}

@Override
public void onTaskCompleted(Task task) {
TaskResourceInfo info = new TaskResourceInfo(
task.getAction(),
task.getId(),
task.getParentTaskId().getId(),
task.getTotalResourceStats()
);
long parentTaskId = task.getParentTaskId().getId();
if (parentTaskId == -1) {
parentTaskId = task.getId();
}
this.queryInsightsService.taskStatusMap.get(parentTaskId).decrementAndGet();
queryInsightsService.taskRecordsQueue.add(info);
}

@Override
public void onTaskStarts(Task task) {
long parentTaskId = task.getParentTaskId().getId();
if (parentTaskId == -1) {
parentTaskId = task.getId();
}
this.queryInsightsService.taskStatusMap.putIfAbsent(parentTaskId, new AtomicInteger(0));
this.queryInsightsService.taskStatusMap.get(parentTaskId).incrementAndGet();
}
}
Loading

0 comments on commit 32f9a75

Please sign in to comment.