Class TaskResourceTrackingService

java.lang.Object
org.opensearch.tasks.TaskResourceTrackingService
All Implemented Interfaces:
RunnableTaskExecutionListener

public class TaskResourceTrackingService extends Object implements RunnableTaskExecutionListener
Service that helps track resource usage of tasks running on a node.
  • Field Details

  • Constructor Details

  • Method Details

    • setTaskResourceTrackingEnabled

      public void setTaskResourceTrackingEnabled(boolean taskResourceTrackingEnabled)
    • isTaskResourceTrackingEnabled

      public boolean isTaskResourceTrackingEnabled()
    • isTaskResourceTrackingSupported

      public boolean isTaskResourceTrackingSupported()
    • startTracking

      public ThreadContext.StoredContext startTracking(Task task)
      Executes logic only if task supports resource tracking and resource tracking setting is enabled.

      1. Starts tracking the task in map of resourceAwareTasks. 2. Adds Task Id in thread context to make sure it's available while task is processed across multiple threads.

      Parameters:
      task - for which resources needs to be tracked
      Returns:
      Autocloseable stored context to restore ThreadContext to the state before this method changed it.
    • stopTracking

      public void stopTracking(Task task)
      Stops tracking task registered earlier for tracking.

      It doesn't have feature enabled check to avoid any issues if setting was disable while the task was in progress.

      It's also responsible to stop tracking the current thread's resources against this task if not already done. This happens when the thread executing the request logic itself calls the unregister method. So in this case unregister happens before runnable finishes.

      Parameters:
      task - task which has finished and doesn't need resource tracking.
    • refreshResourceStats

      public void refreshResourceStats(Task... tasks)
      Refreshes the resource stats for the tasks provided by looking into which threads are actively working on these and how much resources these have consumed till now.
      Parameters:
      tasks - for which resource stats needs to be refreshed.
    • taskExecutionStartedOnThread

      public void taskExecutionStartedOnThread(long taskId, long threadId)
      Called when a thread starts working on a task's runnable.
      Specified by:
      taskExecutionStartedOnThread in interface RunnableTaskExecutionListener
      Parameters:
      taskId - of the task for which runnable is starting
      threadId - of the thread which will be executing the runnable and we need to check resource usage for this thread
    • taskExecutionFinishedOnThread

      public void taskExecutionFinishedOnThread(long taskId, long threadId)
      Called when a thread finishes working on a task's runnable.
      Specified by:
      taskExecutionFinishedOnThread in interface RunnableTaskExecutionListener
      Parameters:
      taskId - of the task for which runnable is complete
      threadId - of the thread which executed the runnable and we need to check resource usage for this thread
    • getResourceAwareTasks

      public Map<Long,Task> getResourceAwareTasks()
    • addTaskCompletionListener

      public void addTaskCompletionListener(TaskResourceTrackingService.TaskCompletionListener listener)