Tasks

    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 API operation.

    The following request returns information about all of your 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.

    Sample 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. }

    You can also use the following parameters with your query.

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

    1. GET /_tasks?nodes=opensearch-node1

    copy

    Sample Response

    The following request returns detailed information about active search tasks:

    Sample Request

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

    copy

    Sample 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. "total" : {
    32. "cpu_time_in_nanos" : 0,
    33. "memory_in_bytes" : 0
    34. }
    35. }
    36. }
    37. }
    38. }
    39. }
    40. }

    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.

    copy

    The 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:

      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 with specific tasks:

      copy