tasks.asciidoc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. The task API can also be used to wait for completion of a particular task. The
  117. following call will block for 10 seconds or until the task with id
  118. `oTUltX4IQMOUUVeiohTt8A:12345` is completed.
  119. [source,js]
  120. --------------------------------------------------
  121. GET _tasks/oTUltX4IQMOUUVeiohTt8A:12345?wait_for_completion=true&timeout=10s
  122. --------------------------------------------------
  123. // CONSOLE
  124. // TEST[catch:missing]
  125. You can also wait for all tasks for certain action types to finish. This
  126. command will wait for all `reindex` tasks to finish:
  127. [source,js]
  128. --------------------------------------------------
  129. GET _tasks?actions=*reindex&wait_for_completion=true&timeout=10s
  130. --------------------------------------------------
  131. // CONSOLE
  132. Tasks can be also listed using _cat version of the list tasks command, which
  133. accepts the same arguments as the standard list tasks command.
  134. [source,js]
  135. --------------------------------------------------
  136. GET _cat/tasks
  137. GET _cat/tasks?detailed
  138. --------------------------------------------------
  139. // CONSOLE
  140. [float]
  141. [[task-cancellation]]
  142. === Task Cancellation
  143. If a long-running task supports cancellation, it can be cancelled with the cancel
  144. tasks API. The following example cancels task `oTUltX4IQMOUUVeiohTt8A:12345`:
  145. [source,js]
  146. --------------------------------------------------
  147. POST _tasks/oTUltX4IQMOUUVeiohTt8A:12345/_cancel
  148. --------------------------------------------------
  149. // CONSOLE
  150. The task cancellation command supports the same task selection parameters as the list tasks command, so multiple tasks
  151. can be cancelled at the same time. For example, the following command will cancel all reindex tasks running on the
  152. nodes `nodeId1` and `nodeId2`.
  153. [source,js]
  154. --------------------------------------------------
  155. POST _tasks/_cancel?nodes=nodeId1,nodeId2&actions=*reindex
  156. --------------------------------------------------
  157. // CONSOLE
  158. [float]
  159. === Task Grouping
  160. The task lists returned by task API commands can be grouped either by nodes (default) or by parent tasks using the `group_by` parameter.
  161. The following command will change the grouping to parent tasks:
  162. [source,js]
  163. --------------------------------------------------
  164. GET _tasks?group_by=parents
  165. --------------------------------------------------
  166. // CONSOLE
  167. The grouping can be disabled by specifying `none` as a `group_by` parameter:
  168. [source,js]
  169. --------------------------------------------------
  170. GET _tasks?group_by=none
  171. --------------------------------------------------
  172. // CONSOLE
  173. [float]
  174. === Identifying running tasks
  175. 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
  176. in the `headers` field for in the task information. This allows to track certain calls, or associate certain tasks with
  177. a the client that started them:
  178. [source,sh]
  179. --------------------------------------------------
  180. curl -i -H "X-Opaque-Id: 123456" "http://localhost:9200/_tasks?group_by=parents"
  181. --------------------------------------------------
  182. //NOTCONSOLE
  183. The result will look similar to the following:
  184. [source,js]
  185. --------------------------------------------------
  186. HTTP/1.1 200 OK
  187. X-Opaque-Id: 123456 <1>
  188. content-type: application/json; charset=UTF-8
  189. content-length: 831
  190. {
  191. "tasks" : {
  192. "u5lcZHqcQhu-rUoFaqDphA:45" : {
  193. "node" : "u5lcZHqcQhu-rUoFaqDphA",
  194. "id" : 45,
  195. "type" : "transport",
  196. "action" : "cluster:monitor/tasks/lists",
  197. "start_time_in_millis" : 1513823752749,
  198. "running_time_in_nanos" : 293139,
  199. "cancellable" : false,
  200. "headers" : {
  201. "X-Opaque-Id" : "123456" <2>
  202. },
  203. "children" : [
  204. {
  205. "node" : "u5lcZHqcQhu-rUoFaqDphA",
  206. "id" : 46,
  207. "type" : "direct",
  208. "action" : "cluster:monitor/tasks/lists[n]",
  209. "start_time_in_millis" : 1513823752750,
  210. "running_time_in_nanos" : 92133,
  211. "cancellable" : false,
  212. "parent_task_id" : "u5lcZHqcQhu-rUoFaqDphA:45",
  213. "headers" : {
  214. "X-Opaque-Id" : "123456" <3>
  215. }
  216. }
  217. ]
  218. }
  219. }
  220. }
  221. --------------------------------------------------
  222. //NOTCONSOLE
  223. <1> id as a part of the response header
  224. <2> id for the tasks that was initiated by the REST request
  225. <3> the child task of the task initiated by the REST request