tasks.asciidoc 10 KB

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