tasks.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. [[tasks]]
  2. === Task management API
  3. ++++
  4. <titleabbrev>Task management</titleabbrev>
  5. ++++
  6. beta::["The task management API is new and should still be considered a beta feature. The API may change in ways that are not backwards compatible.",{es-issue}51628]
  7. Returns information about the tasks currently executing in the cluster.
  8. [[tasks-api-request]]
  9. ==== {api-request-title}
  10. `GET /_tasks/<task_id>`
  11. `GET /_tasks`
  12. [[tasks-api-desc]]
  13. ==== {api-description-title}
  14. The task management API returns information
  15. about tasks currently executing
  16. on one or more nodes in the cluster.
  17. [[tasks-api-path-params]]
  18. ==== {api-path-parms-title}
  19. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=task-id]
  20. [[tasks-api-query-params]]
  21. ==== {api-query-parms-title}
  22. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=actions]
  23. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=detailed]
  24. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=group-by]
  25. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=node-id-query-parm]
  26. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=parent-task-id]
  27. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  28. `wait_for_completion`::
  29. (Optional, Boolean) If `true`, the request blocks until the operation is complete.
  30. Defaults to `false`.
  31. [[tasks-api-response-codes]]
  32. ==== {api-response-codes-title}
  33. // tag::tasks-api-404[]
  34. `404` (Missing resources)::
  35. If `<task_id>` is specified but not found, this code indicates that there
  36. are no resources that match the request.
  37. // end::tasks-api-404[]
  38. [[tasks-api-examples]]
  39. ==== {api-examples-title}
  40. [source,console]
  41. --------------------------------------------------
  42. GET _tasks <1>
  43. GET _tasks?nodes=nodeId1,nodeId2 <2>
  44. GET _tasks?nodes=nodeId1,nodeId2&actions=cluster:* <3>
  45. --------------------------------------------------
  46. // TEST[skip:No tasks to retrieve]
  47. <1> Retrieves all tasks currently running on all nodes in the cluster.
  48. <2> Retrieves all tasks running on nodes `nodeId1` and `nodeId2`. See <<cluster-nodes>> for more info about how to select individual nodes.
  49. <3> Retrieves all cluster-related tasks running on nodes `nodeId1` and `nodeId2`.
  50. The API returns the following result:
  51. [source,console-result]
  52. --------------------------------------------------
  53. {
  54. "nodes" : {
  55. "oTUltX4IQMOUUVeiohTt8A" : {
  56. "name" : "H5dfFeA",
  57. "transport_address" : "127.0.0.1:9300",
  58. "host" : "127.0.0.1",
  59. "ip" : "127.0.0.1:9300",
  60. "tasks" : {
  61. "oTUltX4IQMOUUVeiohTt8A:124" : {
  62. "node" : "oTUltX4IQMOUUVeiohTt8A",
  63. "id" : 124,
  64. "type" : "direct",
  65. "action" : "cluster:monitor/tasks/lists[n]",
  66. "start_time_in_millis" : 1458585884904,
  67. "running_time_in_nanos" : 47402,
  68. "cancellable" : false,
  69. "parent_task_id" : "oTUltX4IQMOUUVeiohTt8A:123"
  70. },
  71. "oTUltX4IQMOUUVeiohTt8A:123" : {
  72. "node" : "oTUltX4IQMOUUVeiohTt8A",
  73. "id" : 123,
  74. "type" : "transport",
  75. "action" : "cluster:monitor/tasks/lists",
  76. "start_time_in_millis" : 1458585884904,
  77. "running_time_in_nanos" : 236042,
  78. "cancellable" : false
  79. }
  80. }
  81. }
  82. }
  83. }
  84. --------------------------------------------------
  85. ===== Retrieve information from a particular task
  86. It is also possible to retrieve information for a particular task. The following
  87. example retrieves information about task `oTUltX4IQMOUUVeiohTt8A:124`:
  88. [source,console]
  89. --------------------------------------------------
  90. GET _tasks/oTUltX4IQMOUUVeiohTt8A:124
  91. --------------------------------------------------
  92. // TEST[catch:missing]
  93. If the task isn't found, the API returns a 404.
  94. To retrieve all children of a particular task:
  95. [source,console]
  96. --------------------------------------------------
  97. GET _tasks?parent_task_id=oTUltX4IQMOUUVeiohTt8A:123
  98. --------------------------------------------------
  99. If the parent isn't found, the API does not return a 404.
  100. ===== Get more information about tasks
  101. You can also use the `detailed` request parameter to get more information about
  102. the running tasks. This is useful for telling one task from another but is more
  103. costly to execute. For example, fetching all searches using the `detailed`
  104. request parameter:
  105. [source,console]
  106. --------------------------------------------------
  107. GET _tasks?actions=*search&detailed
  108. --------------------------------------------------
  109. // TEST[skip:No tasks to retrieve]
  110. The API returns the following result:
  111. [source,console-result]
  112. --------------------------------------------------
  113. {
  114. "nodes" : {
  115. "oTUltX4IQMOUUVeiohTt8A" : {
  116. "name" : "H5dfFeA",
  117. "transport_address" : "127.0.0.1:9300",
  118. "host" : "127.0.0.1",
  119. "ip" : "127.0.0.1:9300",
  120. "tasks" : {
  121. "oTUltX4IQMOUUVeiohTt8A:464" : {
  122. "node" : "oTUltX4IQMOUUVeiohTt8A",
  123. "id" : 464,
  124. "type" : "transport",
  125. "action" : "indices:data/read/search",
  126. "description" : "indices[test], types[test], search_type[QUERY_THEN_FETCH], source[{\"query\":...}]",
  127. "start_time_in_millis" : 1483478610008,
  128. "running_time_in_nanos" : 13991383,
  129. "cancellable" : true
  130. }
  131. }
  132. }
  133. }
  134. }
  135. --------------------------------------------------
  136. The new `description` field contains human readable text that identifies the
  137. particular request that the task is performing such as identifying the search
  138. request being performed by a search task like the example above. Other kinds of
  139. task have different descriptions, like <<docs-reindex,`_reindex`>> which
  140. has the search and the destination, or <<docs-bulk,`_bulk`>> which just has the
  141. number of requests and the destination indices. Many requests will only have an
  142. empty description because more detailed information about the request is not
  143. easily available or particularly helpful in identifying the request.
  144. [IMPORTANT]
  145. ==============================
  146. `_tasks` requests with `detailed` may also return a `status`. This is a report
  147. of the internal status of the task. As such its format varies from task to task.
  148. While we try to keep the `status` for a particular task consistent from version
  149. to version this isn't always possible because we sometimes change the
  150. implementation. In that case we might remove fields from the `status` for a
  151. particular request so any parsing you do of the status might break in minor
  152. releases.
  153. ==============================
  154. ===== Wait for completion
  155. The task API can also be used to wait for completion of a particular task. The
  156. following call will block for 10 seconds or until the task with id
  157. `oTUltX4IQMOUUVeiohTt8A:12345` is completed.
  158. [source,console]
  159. --------------------------------------------------
  160. GET _tasks/oTUltX4IQMOUUVeiohTt8A:12345?wait_for_completion=true&timeout=10s
  161. --------------------------------------------------
  162. // TEST[catch:missing]
  163. You can also wait for all tasks for certain action types to finish. This command
  164. will wait for all `reindex` tasks to finish:
  165. [source,console]
  166. --------------------------------------------------
  167. GET _tasks?actions=*reindex&wait_for_completion=true&timeout=10s
  168. --------------------------------------------------
  169. [[task-cancellation]]
  170. ===== Task Cancellation
  171. If a long-running task supports cancellation, it can be cancelled with the cancel
  172. tasks API. The following example cancels task `oTUltX4IQMOUUVeiohTt8A:12345`:
  173. [source,console]
  174. --------------------------------------------------
  175. POST _tasks/oTUltX4IQMOUUVeiohTt8A:12345/_cancel
  176. --------------------------------------------------
  177. The task cancellation command supports the same task selection parameters as the
  178. list tasks command, so multiple tasks can be cancelled at the same time. For
  179. example, the following command will cancel all reindex tasks running on the
  180. nodes `nodeId1` and `nodeId2`.
  181. `wait_for_completion`::
  182. (Optional, Boolean) If `true`, the request blocks until the cancellation of the
  183. task and its descendant tasks is completed. Otherwise, the request can return soon
  184. after the cancellation is started. Defaults to `false`.
  185. [source,console]
  186. --------------------------------------------------
  187. POST _tasks/_cancel?nodes=nodeId1,nodeId2&actions=*reindex
  188. --------------------------------------------------
  189. ===== Task Grouping
  190. The task lists returned by task API commands can be grouped either by nodes
  191. (default) or by parent tasks using the `group_by` parameter. The following
  192. command will change the grouping to parent tasks:
  193. [source,console]
  194. --------------------------------------------------
  195. GET _tasks?group_by=parents
  196. --------------------------------------------------
  197. The grouping can be disabled by specifying `none` as a `group_by` parameter:
  198. [source,console]
  199. --------------------------------------------------
  200. GET _tasks?group_by=none
  201. --------------------------------------------------
  202. ===== Identifying running tasks
  203. The `X-Opaque-Id` header, when provided on the HTTP request header, is going to
  204. be returned as a header in the response as well as in the `headers` field for in
  205. the task information. This allows to track certain calls, or associate certain
  206. tasks with the client that started them:
  207. [source,sh]
  208. --------------------------------------------------
  209. curl -i -H "X-Opaque-Id: 123456" "http://localhost:9200/_tasks?group_by=parents"
  210. --------------------------------------------------
  211. //NOTCONSOLE
  212. The API returns the following result:
  213. [source,js]
  214. --------------------------------------------------
  215. HTTP/1.1 200 OK
  216. X-Opaque-Id: 123456 <1>
  217. content-type: application/json; charset=UTF-8
  218. content-length: 831
  219. {
  220. "tasks" : {
  221. "u5lcZHqcQhu-rUoFaqDphA:45" : {
  222. "node" : "u5lcZHqcQhu-rUoFaqDphA",
  223. "id" : 45,
  224. "type" : "transport",
  225. "action" : "cluster:monitor/tasks/lists",
  226. "start_time_in_millis" : 1513823752749,
  227. "running_time_in_nanos" : 293139,
  228. "cancellable" : false,
  229. "headers" : {
  230. "X-Opaque-Id" : "123456" <2>
  231. },
  232. "children" : [
  233. {
  234. "node" : "u5lcZHqcQhu-rUoFaqDphA",
  235. "id" : 46,
  236. "type" : "direct",
  237. "action" : "cluster:monitor/tasks/lists[n]",
  238. "start_time_in_millis" : 1513823752750,
  239. "running_time_in_nanos" : 92133,
  240. "cancellable" : false,
  241. "parent_task_id" : "u5lcZHqcQhu-rUoFaqDphA:45",
  242. "headers" : {
  243. "X-Opaque-Id" : "123456" <3>
  244. }
  245. }
  246. ]
  247. }
  248. }
  249. }
  250. --------------------------------------------------
  251. //NOTCONSOLE
  252. <1> id as a part of the response header
  253. <2> id for the tasks that was initiated by the REST request
  254. <3> the child task of the task initiated by the REST request