Tasks

Introduced 1.0

A task is any operation you run in a cluster. For example, searching your data collection of books for a title or author name is a task. When you run OpenSearch, a task is automatically created to monitor your cluster’s health and performance. For more information about all of the tasks currently executing in your cluster, you can use the tasks API operation.

Path and HTTP methods

  1. GET _tasks

copy

By including a task ID, you can get information specific to a particular task. Note that a task ID consists of a node’s identifying string and the task’s numerical ID. For example, if your node’s identifying string is nodestring and the task’s numerical ID is 1234, then your task ID is nodestring:1234. You can find this information by running the tasks operation:

  1. GET _tasks/<task_id>

copy

Note that if a task finishes running, it won’t be returned as part of your request. For an example of a task that takes a little longer to finish, you can run the _reindex API operation on a larger document, and then run tasks.

Query parameters

You can also use the following parameters with your query.

ParameterData typeDescription
nodesListA comma-separated list of node IDs or names to limit the returned information. Use _local to return information from the node you’re connecting to, specify the node name to get information from specific nodes, or keep the parameter empty to get information from all nodes.
actionsListA comma-separated list of actions that should be returned. Keep empty to return all.
detailedBooleanReturns detailed task information. (Default: false)
parent_task_idStringReturns tasks with a specified parent task ID (node_id:task_number). Keep empty or set to -1 to return all.
wait_for_completionBooleanWaits for the matching tasks to complete. (Default: false)
group_byEnumGroups tasks by parent/child relationships or nodes. (Default: nodes)
timeoutTimeAn explicit operation timeout. (Default: 30 seconds)
cluster_manager_timeoutTimeThe time to wait for a connection to the primary node. (Default: 30 seconds)

Example requests

Return information about running tasks

The following request returns tasks currently running on a node named opensearch-node1:

  1. GET /_tasks?nodes=opensearch-node1

copy

Return information about active search tasks

The following request returns detailed information about active search tasks:

  1. curl -XGET "localhost:9200/_tasks?actions=*search&detailed

copy

Example response

The following example response shows information about running tasks:

  1. {
  2. "nodes": {
  3. "Mgqdm0r9SEGClWxp_RbnaQ": {
  4. "name": "opensearch-node1",
  5. "transport_address": "sample_address",
  6. "host": "sample_host",
  7. "ip": "sample_ip",
  8. "roles": [
  9. "data",
  10. "ingest",
  11. "master",
  12. "remote_cluster_client"
  13. ],
  14. "tasks": {
  15. "Mgqdm0r9SEGClWxp_RbnaQ:24578": {
  16. "node": "Mgqdm0r9SEGClWxp_RbnaQ",
  17. "id": 24578,
  18. "type": "transport",
  19. "action": "cluster:monitor/tasks/lists",
  20. "start_time_in_millis": 1611612517044,
  21. "running_time_in_nanos": 638700,
  22. "cancellable": false,
  23. "headers": {}
  24. },
  25. "Mgqdm0r9SEGClWxp_RbnaQ:24579": {
  26. "node": "Mgqdm0r9SEGClWxp_RbnaQ",
  27. "id": 24579,
  28. "type": "direct",
  29. "action": "cluster:monitor/tasks/lists[n]",
  30. "start_time_in_millis": 1611612517044,
  31. "running_time_in_nanos": 222200,
  32. "cancellable": false,
  33. "parent_task_id": "Mgqdm0r9SEGClWxp_RbnaQ:24578",
  34. "headers": {}
  35. }
  36. }
  37. }
  38. }
  39. }

The resource_stats object

The resource_stats object is only updated for tasks that support resource tracking. These stats are computed based on scheduled thread executions, including both threads that have finished working on the task and threads currently working on the task. Because the same thread may be scheduled to work on the same task multiple times, each instance of a given thread being scheduled to work on a given task is considered to be a single thread execution.

The following table lists all response fields in the resource_stats object.

Response fieldDescription
averageThe average resource usage across all scheduled thread executions.
totalThe sum of resource usages across all scheduled thread executions.
minThe minimum resource usage across all scheduled thread executions.
maxThe maximum resource usage across all scheduled thread executions.
thread_infoThread-count-related stats.
thread_info.active_threadsThe number of threads currently working on the task.
thread_info.thread_executionsThe number of threads that have been scheduled to work on the task.

Task canceling

After getting a list of tasks, you can cancel all cancelable tasks with the following request:

  1. POST _tasks/_cancel

copy

Note that not all tasks are cancelable. To see if a task is cancelable, refer to the cancellable field in the response to your tasks API request.

You can also cancel a task by including a specific task ID.

  1. POST _tasks/<task_id>/_cancel

copy

The cancel operation supports the same parameters as the tasks operation. The following example shows how to cancel all cancelable tasks on multiple nodes.

  1. POST _tasks/_cancel?nodes=opensearch-node1,opensearch-node2

copy

Attaching headers to tasks

To associate requests with tasks for better tracking, you can provide a X-Opaque-Id:<ID_number> header as part of the HTTPS request reader of your curl command. The API will attach the specified header in the returned result.

Usage:

  1. curl -i -H "X-Opaque-Id: 111111" "https://localhost:9200/_tasks" -u 'admin:<custom-admin-password>' --insecure

copy

The _tasks operation returns the following result.

  1. HTTP/1.1 200 OK
  2. X-Opaque-Id: 111111
  3. content-type: application/json; charset=UTF-8
  4. content-length: 768
  5. {
  6. "nodes": {
  7. "Mgqdm0r9SEGClWxp_RbnaQ": {
  8. "name": "opensearch-node1",
  9. "transport_address": "172.18.0.4:9300",
  10. "host": "172.18.0.4",
  11. "ip": "172.18.0.4:9300",
  12. "roles": [
  13. "data",
  14. "ingest",
  15. "master",
  16. "remote_cluster_client"
  17. ],
  18. "tasks": {
  19. "Mgqdm0r9SEGClWxp_RbnaQ:30072": {
  20. "node": "Mgqdm0r9SEGClWxp_RbnaQ",
  21. "id": 30072,
  22. "type": "direct",
  23. "action": "cluster:monitor/tasks/lists[n]",
  24. "start_time_in_millis": 1613166701725,
  25. "running_time_in_nanos": 245400,
  26. "cancellable": false,
  27. "parent_task_id": "Mgqdm0r9SEGClWxp_RbnaQ:30071",
  28. "headers": {
  29. "X-Opaque-Id": "111111"
  30. }
  31. },
  32. "Mgqdm0r9SEGClWxp_RbnaQ:30071": {
  33. "node": "Mgqdm0r9SEGClWxp_RbnaQ",
  34. "id": 30071,
  35. "type": "transport",
  36. "action": "cluster:monitor/tasks/lists",
  37. "start_time_in_millis": 1613166701725,
  38. "running_time_in_nanos": 658200,
  39. "cancellable": false,
  40. "headers": {
  41. "X-Opaque-Id": "111111"
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }

This operation supports the same parameters as the tasks operation. The following example shows how you can associate X-Opaque-Id with specific tasks:

  1. curl -i -H "X-Opaque-Id: 123456" "https://localhost:9200/_tasks?nodes=opensearch-node1" -u 'admin:<custom-admin-password>' --insecure

copy