tasks.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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=nodes]
  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 all found tasks are 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 to distinguish tasks from each other but
  107. is more 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. "cancelled" : false
  135. }
  136. }
  137. }
  138. }
  139. }
  140. --------------------------------------------------
  141. The new `description` field contains human readable text that identifies the
  142. particular request that the task is performing such as identifying the search
  143. request being performed by a search task like the example above. Other kinds of
  144. tasks have different descriptions, like <<docs-reindex,`_reindex`>> which
  145. has the source and the destination, or <<docs-bulk,`_bulk`>> which just has the
  146. number of requests and the destination indices. Many requests will only have an
  147. empty description because more detailed information about the request is not
  148. easily available or particularly helpful in identifying the request.
  149. [IMPORTANT]
  150. ==============================
  151. `_tasks` requests with `detailed` may also return a `status`. This is a report
  152. of the internal status of the task. As such its format varies from task to task.
  153. While we try to keep the `status` for a particular task consistent from version
  154. to version this isn't always possible because we sometimes change the
  155. implementation. In that case we might remove fields from the `status` for a
  156. particular request so any parsing you do of the status might break in minor
  157. releases.
  158. ==============================
  159. ===== Wait for completion
  160. The task API can also be used to wait for completion of a particular task. The
  161. following call will block for 10 seconds or until the task with id
  162. `oTUltX4IQMOUUVeiohTt8A:12345` is completed.
  163. [source,console]
  164. --------------------------------------------------
  165. GET _tasks/oTUltX4IQMOUUVeiohTt8A:12345?wait_for_completion=true&timeout=10s
  166. --------------------------------------------------
  167. // TEST[catch:missing]
  168. You can also wait for all tasks for certain action types to finish. This command
  169. will wait for all `reindex` tasks to finish:
  170. [source,console]
  171. --------------------------------------------------
  172. GET _tasks?actions=*reindex&wait_for_completion=true&timeout=10s
  173. --------------------------------------------------
  174. [[task-cancellation]]
  175. ===== Task Cancellation
  176. If a long-running task supports cancellation, it can be cancelled with the cancel
  177. tasks API. The following example cancels task `oTUltX4IQMOUUVeiohTt8A:12345`:
  178. [source,console]
  179. --------------------------------------------------
  180. POST _tasks/oTUltX4IQMOUUVeiohTt8A:12345/_cancel
  181. --------------------------------------------------
  182. The task cancellation command supports the same task selection parameters as the
  183. list tasks command, so multiple tasks can be cancelled at the same time. For
  184. example, the following command will cancel all reindex tasks running on the
  185. nodes `nodeId1` and `nodeId2`.
  186. [source,console]
  187. --------------------------------------------------
  188. POST _tasks/_cancel?nodes=nodeId1,nodeId2&actions=*reindex
  189. --------------------------------------------------
  190. A task may continue to run for some time after it has been cancelled because it
  191. may not be able to safely stop its current activity straight away, or because
  192. {es} must complete its work on other tasks before it can process the
  193. cancellation. The list tasks API will continue to list these cancelled tasks
  194. until they complete. The `cancelled` flag in the response to the list tasks API
  195. indicates that the cancellation command has been processed and the task will
  196. stop as soon as possible. To troubleshoot why a cancelled task does not
  197. complete promptly, use the list tasks API with the `?detailed` parameter to
  198. identify the other tasks the system is running and also use the
  199. <<cluster-nodes-hot-threads>> API to obtain detailed information about the work
  200. the system is doing instead of completing the cancelled task.
  201. ===== Task Grouping
  202. The task lists returned by task API commands can be grouped either by nodes
  203. (default) or by parent tasks using the `group_by` parameter. The following
  204. command will change the grouping to parent tasks:
  205. [source,console]
  206. --------------------------------------------------
  207. GET _tasks?group_by=parents
  208. --------------------------------------------------
  209. The grouping can be disabled by specifying `none` as a `group_by` parameter:
  210. [source,console]
  211. --------------------------------------------------
  212. GET _tasks?group_by=none
  213. --------------------------------------------------
  214. ===== Identifying running tasks
  215. The `X-Opaque-Id` header, when provided on the HTTP request header, is going to
  216. be returned as a header in the response as well as in the `headers` field for in
  217. the task information. This allows to track certain calls, or associate certain
  218. tasks with the client that started them:
  219. [source,sh]
  220. --------------------------------------------------
  221. curl -i -H "X-Opaque-Id: 123456" "http://localhost:9200/_tasks?group_by=parents"
  222. --------------------------------------------------
  223. //NOTCONSOLE
  224. The API returns the following result:
  225. [source,js]
  226. --------------------------------------------------
  227. HTTP/1.1 200 OK
  228. X-Opaque-Id: 123456 <1>
  229. content-type: application/json; charset=UTF-8
  230. content-length: 831
  231. {
  232. "tasks" : {
  233. "u5lcZHqcQhu-rUoFaqDphA:45" : {
  234. "node" : "u5lcZHqcQhu-rUoFaqDphA",
  235. "id" : 45,
  236. "type" : "transport",
  237. "action" : "cluster:monitor/tasks/lists",
  238. "start_time_in_millis" : 1513823752749,
  239. "running_time_in_nanos" : 293139,
  240. "cancellable" : false,
  241. "headers" : {
  242. "X-Opaque-Id" : "123456" <2>
  243. },
  244. "children" : [
  245. {
  246. "node" : "u5lcZHqcQhu-rUoFaqDphA",
  247. "id" : 46,
  248. "type" : "direct",
  249. "action" : "cluster:monitor/tasks/lists[n]",
  250. "start_time_in_millis" : 1513823752750,
  251. "running_time_in_nanos" : 92133,
  252. "cancellable" : false,
  253. "parent_task_id" : "u5lcZHqcQhu-rUoFaqDphA:45",
  254. "headers" : {
  255. "X-Opaque-Id" : "123456" <3>
  256. }
  257. }
  258. ]
  259. }
  260. }
  261. }
  262. --------------------------------------------------
  263. //NOTCONSOLE
  264. <1> id as a part of the response header
  265. <2> id for the tasks that was initiated by the REST request
  266. <3> the child task of the task initiated by the REST request