tasks.asciidoc 12 KB

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