tasks.asciidoc 11 KB

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