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.

The following request returns information about all of your tasks:

  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.

Example response

  1. {
  2. "nodes": {
  3. "Mgqdm0r9SEGClWxp_RbnaQ": {
  4. "name": "opensearch-node1",
  5. "transport_address": "172.18.0.3:9300",
  6. "host": "172.18.0.3",
  7. "ip": "172.18.0.3:9300",
  8. "roles": [
  9. "data",
  10. "ingest",
  11. "master",
  12. "remote_cluster_client"
  13. ],
  14. "tasks": {
  15. "Mgqdm0r9SEGClWxp_RbnaQ:17416": {
  16. "node": "Mgqdm0r9SEGClWxp_RbnaQ",
  17. "id": 17416,
  18. "type": "transport",
  19. "action": "cluster:monitor/tasks/lists",
  20. "start_time_in_millis": 1613599752458,
  21. "running_time_in_nanos": 994000,
  22. "cancellable": false,
  23. "headers": {}
  24. }
  25. },
  26. "Mgqdm0r9SEGClWxp_RbnaQ:17413": {
  27. "node": "Mgqdm0r9SEGClWxp_RbnaQ",
  28. "id": 17413,
  29. "type": "transport",
  30. "action": "indices:data/write/bulk",
  31. "start_time_in_millis": 1613599752286,
  32. "running_time_in_nanos": 172846500,
  33. "cancellable": false,
  34. "parent_task_id": "Mgqdm0r9SEGClWxp_RbnaQ:17366",
  35. "headers": {}
  36. },
  37. "Mgqdm0r9SEGClWxp_RbnaQ:17366": {
  38. "node": "Mgqdm0r9SEGClWxp_RbnaQ",
  39. "id": 17366,
  40. "type": "transport",
  41. "action": "indices:data/write/reindex",
  42. "start_time_in_millis": 1613599750929,
  43. "running_time_in_nanos": 1529733100,
  44. "cancellable": true,
  45. "headers": {}
  46. }
  47. }
  48. }
  49. }
  50. }

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)

For example, this request returns tasks currently running on a node named opensearch-node1:

Example request

  1. GET /_tasks?nodes=opensearch-node1

copy

Example response

  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 following request returns detailed information about active search tasks:

Example request

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

copy

Example response

  1. {
  2. "nodes" : {
  3. "CRqNwnEeRXOjeTSYYktw-A" : {
  4. "name" : "runTask-0",
  5. "transport_address" : "127.0.0.1:9300",
  6. "host" : "127.0.0.1",
  7. "ip" : "127.0.0.1:9300",
  8. "roles" : [
  9. "cluster_manager",
  10. "data",
  11. "ingest",
  12. "remote_cluster_client"
  13. ],
  14. "attributes" : {
  15. "testattr" : "test",
  16. "shard_indexing_pressure_enabled" : "true"
  17. },
  18. "tasks" : {
  19. "CRqNwnEeRXOjeTSYYktw-A:677" : {
  20. "node" : "CRqNwnEeRXOjeTSYYktw-A",
  21. "id" : 677,
  22. "type" : "transport",
  23. "action" : "indices:data/read/search",
  24. "description" : "indices[], search_type[QUERY_THEN_FETCH], source[{\"query\":{\"query_string\":<QUERY_STRING>}}]",
  25. "start_time_in_millis" : 1660106254525,
  26. "running_time_in_nanos" : 1354236,
  27. "cancellable" : true,
  28. "cancelled" : false,
  29. "headers" : { },
  30. "resource_stats" : {
  31. "average" : {
  32. "cpu_time_in_nanos" : 0,
  33. "memory_in_bytes" : 0
  34. },
  35. "total" : {
  36. "cpu_time_in_nanos" : 0,
  37. "memory_in_bytes" : 0
  38. },
  39. "min" : {
  40. "cpu_time_in_nanos" : 0,
  41. "memory_in_bytes" : 0
  42. },
  43. "max" : {
  44. "cpu_time_in_nanos" : 0,
  45. "memory_in_bytes" : 0
  46. },
  47. "thread_info" : {
  48. "thread_executions" : 0,
  49. "active_threads" : 0
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }

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:admin' --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:admin' --insecure

copy