tasks.asciidoc 11 KB

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