tasks.asciidoc 10 KB

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