Package org.opensearch.tasks
Class Task
java.lang.Object
org.opensearch.tasks.Task
- Direct Known Subclasses:
CancellableTask
,PrimaryReplicaSyncer.ResyncTask
,ReplicationTask
Current task information
- Opensearch.internal:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Report of the internal status of a task. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The request header to mark tasks with specific ids -
Constructor Summary
ConstructorDescriptionTask
(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 TypeMethodDescriptionboolean
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 actionorg.opensearch.core.tasks.resourcetracker.TaskResourceUsage
Returns current average per-execution resource usage of the task.Generates task descriptionReturns stored task header associated with the tasklong
getId()
Returns task idorg.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 tasksReturns thread level resource consumption of the tasklong
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.getType()
Returns task channel type (netty, transport, direct)int
Increments the number of active resource tracking threads.void
startThreadResourceTracking
(long threadId, org.opensearch.core.tasks.resourcetracker.ResourceStatsType statsType, org.opensearch.core.tasks.resourcetracker.ResourceUsageMetric... resourceUsageMetrics) Adds thread's starting resource consumption informationvoid
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
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 aTaskInfo
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 properTaskInfo
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.
-
Field Details
-
X_OPAQUE_ID
The request header to mark tasks with specific ids- See Also:
-
-
Constructor Details
-
Task
-
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
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 ondetailed
- should the information include detailed, potentially slow to generate data?
-
taskInfo
Build aTaskInfo
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 properTaskInfo
for this task. -
getId
public long getId()Returns task id -
getType
Returns task channel type (netty, transport, direct) -
getAction
Returns task action -
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
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 threadstatsType
- stats typeresourceUsageMetrics
- 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 threadstatsType
- stats typeresourceUsageMetrics
- 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 threadstatsType
- stats typeresourceUsageMetrics
- 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 toOpenSearchExecutors.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
Returns stored task header associated with the task -
result
- 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.
-