tasks.asciidoc 9.2 KB

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