tasks.asciidoc 8.6 KB

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