Class Task

java.lang.Object
org.opensearch.tasks.Task
Direct Known Subclasses:
CancellableTask, PrimaryReplicaSyncer.ResyncTask, ReplicationTask

public class Task extends Object
Current task information
Opensearch.internal:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Report of the internal status of a task.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The request header to mark tasks with specific ids
  • Constructor Summary

    Constructors
    Constructor
    Description
    Task(long id, String type, String action, String description, org.opensearch.core.tasks.TaskId parentTask, long startTime, long startTimeNanos, Map<String,String> headers, ConcurrentHashMap<Long,List<org.opensearch.core.tasks.resourcetracker.ThreadResourceInfo>> resourceStats, List<org.opensearch.core.action.NotifyOnceListener<Task>> resourceTrackingCompletionListeners)
     
    Task(long id, String type, String action, String description, org.opensearch.core.tasks.TaskId parentTask, Map<String,String> headers)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    addResourceTrackingCompletionListener(org.opensearch.core.action.NotifyOnceListener<Task> listener)
    Registers a task resource tracking completion listener on this task if resource tracking is still active.
    int
    Decrements the number of active resource tracking threads.
    Returns task action
    org.opensearch.core.tasks.resourcetracker.TaskResourceUsage
    Returns current average per-execution resource usage of the task.
    Generates task description
    getHeader(String header)
    Returns stored task header associated with the task
    long
    Returns task id
    org.opensearch.core.tasks.resourcetracker.TaskResourceUsage
    Returns current max per-execution resource usage of the task.
    org.opensearch.core.tasks.resourcetracker.TaskResourceUsage
    Returns current min per-execution resource usage of the task.
    org.opensearch.core.tasks.TaskId
    Returns id of the parent task or NO_PARENT_ID if the task doesn't have any parent tasks
    Map<Long,List<org.opensearch.core.tasks.resourcetracker.ThreadResourceInfo>>
    Returns thread level resource consumption of the task
    long
    Returns the task's start time as a wall clock time since epoch (System.currentTimeMillis() style).
    long
    Returns the task's start time in nanoseconds (System.nanoTime() style).
    Build a status for this task or null if this task doesn't have status.
    org.opensearch.core.tasks.resourcetracker.TaskThreadUsage
    Returns the total and active number of thread executions for the task.
    org.opensearch.core.tasks.resourcetracker.TaskResourceUsage
    Returns current total resource usage of the task.
    long
    getTotalResourceUtilization(org.opensearch.core.tasks.resourcetracker.ResourceStats stats)
    Returns total resource consumption for a specific task stat.
    Returns task channel type (netty, transport, direct)
    int
    Increments the number of active resource tracking threads.
    result(String nodeId, Exception error)
     
    result(String nodeId, org.opensearch.core.action.ActionResponse response)
     
    void
    startThreadResourceTracking(long threadId, org.opensearch.core.tasks.resourcetracker.ResourceStatsType statsType, org.opensearch.core.tasks.resourcetracker.ResourceUsageMetric... resourceUsageMetrics)
    Adds thread's starting resource consumption information
    void
    stopThreadResourceTracking(long threadId, org.opensearch.core.tasks.resourcetracker.ResourceStatsType statsType, org.opensearch.core.tasks.resourcetracker.ResourceUsageMetric... resourceUsageMetrics)
    Record the thread's final resource consumption values.
    boolean
    Individual tasks can override this if they want to support task resource tracking.
    final TaskInfo
    taskInfo(String localNodeId, boolean detailed)
    Build a version of the task status you can throw over the wire and back to the user.
    protected final TaskInfo
    taskInfo(String localNodeId, String description, Task.Status status)
    Build a TaskInfo for this task without resource stats.
    protected final TaskInfo
    taskInfo(String localNodeId, String description, Task.Status status, org.opensearch.core.tasks.resourcetracker.TaskResourceStats resourceStats)
    Build a proper TaskInfo for this task.
    void
    updateThreadResourceStats(long threadId, org.opensearch.core.tasks.resourcetracker.ResourceStatsType statsType, org.opensearch.core.tasks.resourcetracker.ResourceUsageMetric... resourceUsageMetrics)
    This method is used to update the resource consumption stats so that the data isn't too stale for long-running task.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • X_OPAQUE_ID

      public static final String X_OPAQUE_ID
      The request header to mark tasks with specific ids
      See Also:
  • Constructor Details

    • Task

      public Task(long id, String type, String action, String description, org.opensearch.core.tasks.TaskId parentTask, Map<String,String> headers)
    • Task

      public Task(long id, String type, String action, String description, org.opensearch.core.tasks.TaskId parentTask, long startTime, long startTimeNanos, Map<String,String> headers, ConcurrentHashMap<Long,List<org.opensearch.core.tasks.resourcetracker.ThreadResourceInfo>> resourceStats, List<org.opensearch.core.action.NotifyOnceListener<Task>> resourceTrackingCompletionListeners)
  • Method Details

    • taskInfo

      public final TaskInfo taskInfo(String localNodeId, boolean detailed)
      Build a version of the task status you can throw over the wire and back to the user.
      Parameters:
      localNodeId - the id of the node this task is running on
      detailed - should the information include detailed, potentially slow to generate data?
    • taskInfo

      protected final TaskInfo taskInfo(String localNodeId, String description, Task.Status status)
      Build a TaskInfo for this task without resource stats.
    • taskInfo

      protected final TaskInfo taskInfo(String localNodeId, String description, Task.Status status, org.opensearch.core.tasks.resourcetracker.TaskResourceStats resourceStats)
      Build a proper TaskInfo for this task.
    • getId

      public long getId()
      Returns task id
    • getType

      public String getType()
      Returns task channel type (netty, transport, direct)
    • getAction

      public String getAction()
      Returns task action
    • getDescription

      public String getDescription()
      Generates task description
    • getStartTime

      public long getStartTime()
      Returns the task's start time as a wall clock time since epoch (System.currentTimeMillis() style).
    • getStartTimeNanos

      public long getStartTimeNanos()
      Returns the task's start time in nanoseconds (System.nanoTime() style).
    • getParentTaskId

      public org.opensearch.core.tasks.TaskId getParentTaskId()
      Returns id of the parent task or NO_PARENT_ID if the task doesn't have any parent tasks
    • getStatus

      public Task.Status getStatus()
      Build a status for this task or null if this task doesn't have status. Since most tasks don't have status this defaults to returning null. While this can never perform IO it might be a costly operation, requiring collating lists of results, etc. So only use it if you need the value.
    • getResourceStats

      public Map<Long,List<org.opensearch.core.tasks.resourcetracker.ThreadResourceInfo>> getResourceStats()
      Returns thread level resource consumption of the task
    • getTotalResourceStats

      public org.opensearch.core.tasks.resourcetracker.TaskResourceUsage getTotalResourceStats()
      Returns current total resource usage of the task. Currently, this method is only called on demand, during get and listing of tasks. In the future, these values can be cached as an optimization.
    • getAverageResourceStats

      public org.opensearch.core.tasks.resourcetracker.TaskResourceUsage getAverageResourceStats()
      Returns current average per-execution resource usage of the task.
    • getMinResourceStats

      public org.opensearch.core.tasks.resourcetracker.TaskResourceUsage getMinResourceStats()
      Returns current min per-execution resource usage of the task.
    • getMaxResourceStats

      public org.opensearch.core.tasks.resourcetracker.TaskResourceUsage getMaxResourceStats()
      Returns current max per-execution resource usage of the task.
    • getTotalResourceUtilization

      public long getTotalResourceUtilization(org.opensearch.core.tasks.resourcetracker.ResourceStats stats)
      Returns total resource consumption for a specific task stat.
    • getThreadUsage

      public org.opensearch.core.tasks.resourcetracker.TaskThreadUsage getThreadUsage()
      Returns the total and active number of thread executions for the task.
    • startThreadResourceTracking

      public void startThreadResourceTracking(long threadId, org.opensearch.core.tasks.resourcetracker.ResourceStatsType statsType, org.opensearch.core.tasks.resourcetracker.ResourceUsageMetric... resourceUsageMetrics)
      Adds thread's starting resource consumption information
      Parameters:
      threadId - ID of the thread
      statsType - stats type
      resourceUsageMetrics - resource consumption metrics of the thread
      Throws:
      IllegalStateException - matching active thread entry was found which is not expected.
    • updateThreadResourceStats

      public void updateThreadResourceStats(long threadId, org.opensearch.core.tasks.resourcetracker.ResourceStatsType statsType, org.opensearch.core.tasks.resourcetracker.ResourceUsageMetric... resourceUsageMetrics)
      This method is used to update the resource consumption stats so that the data isn't too stale for long-running task. If active thread entry is present in the list, the entry is updated. If one is not found, it throws an exception.
      Parameters:
      threadId - ID of the thread
      statsType - stats type
      resourceUsageMetrics - resource consumption metrics of the thread
      Throws:
      IllegalStateException - if no matching active thread entry was found.
    • stopThreadResourceTracking

      public void stopThreadResourceTracking(long threadId, org.opensearch.core.tasks.resourcetracker.ResourceStatsType statsType, org.opensearch.core.tasks.resourcetracker.ResourceUsageMetric... resourceUsageMetrics)
      Record the thread's final resource consumption values. If active thread entry is present in the list, the entry is updated. If one is not found, it throws an exception.
      Parameters:
      threadId - ID of the thread
      statsType - stats type
      resourceUsageMetrics - resource consumption metrics of the thread
      Throws:
      IllegalStateException - if no matching active thread entry was found.
    • supportsResourceTracking

      public boolean supportsResourceTracking()
      Individual tasks can override this if they want to support task resource tracking. We just need to make sure that the ThreadPool on which the task runs on have runnable wrapper similar to OpenSearchExecutors.newAutoQueueFixed(java.lang.String, int, int, int, int, int, org.opensearch.common.unit.TimeValue, java.util.concurrent.ThreadFactory, org.opensearch.common.util.concurrent.ThreadContext)
      Returns:
      true if resource tracking is supported by the task
    • getHeader

      public String getHeader(String header)
      Returns stored task header associated with the task
    • result

      public TaskResult result(String nodeId, Exception error) throws IOException
      Throws:
      IOException
    • result

      public TaskResult result(String nodeId, org.opensearch.core.action.ActionResponse response) throws IOException
      Throws:
      IOException
    • addResourceTrackingCompletionListener

      public boolean addResourceTrackingCompletionListener(org.opensearch.core.action.NotifyOnceListener<Task> listener)
      Registers a task resource tracking completion listener on this task if resource tracking is still active. Returns true on successful subscription, false otherwise.
    • incrementResourceTrackingThreads

      public int incrementResourceTrackingThreads()
      Increments the number of active resource tracking threads.
      Returns:
      the number of active resource tracking threads.
    • decrementResourceTrackingThreads

      public int decrementResourceTrackingThreads()
      Decrements the number of active resource tracking threads. This method is called when threads finish execution, and also when the task is unregistered (to mark the task's own thread as complete). When the active thread count becomes zero, the onTaskResourceTrackingCompleted method is called exactly once on all registered listeners. Since a task is unregistered after the message is processed, it implies that the threads responsible to produce the response must have started prior to it (i.e. startThreadResourceTracking called before unregister). This ensures that the number of active threads doesn't drop to zero pre-maturely. Rarely, some threads may even start execution after the task is unregistered. As resource stats are piggy-backed with the response, any thread usage info captured after the task is unregistered may be irrelevant.
      Returns:
      the number of active resource tracking threads.