Task Management API

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.0/tasks.html

译文链接 : http://www.apache.wiki/display/Elasticsearch/Task+Management+API

贡献者 : 那伊抹微笑

警告 :

该 Task Management API 是一个新功能,并且仍然处于实验阶段,该 API 可能会改变方式并且不向后兼容。

Current Tasks Information(当前任务信息)

task management API 可以获取关于集群中一个或多个节点正在执行中的任务的信息。

  1. curl -XGET 'localhost:9200/_tasks ?pretty'
  2. curl -XGET 'localhost:9200/_tasks?nodes=nodeId1,nodeId2 &pretty'
  3. curl -XGET 'localhost:9200/_tasks?nodes=nodeId1,nodeId2&actions=cluster:* &pretty'
编号 描述
Task Management API - 图1 Retrieves all tasks currently running on all nodes in the cluster.
Task Management API - 图2 Retrieves all tasks running on nodes nodeId1 and nodeId2. See the section called “Node specificationedit” for more info about how to select individual nodes.
Task Management API - 图3 Retrieves all cluster-related tasks running on nodes nodeId1 and nodeId2.

返回结果看起来如下 :

  1. {
  2. "nodes" : {
  3. "oTUltX4IQMOUUVeiohTt8A" : {
  4. "name" : "H5dfFeA",
  5. "transport_address" : "127.0.0.1:9300",
  6. "host" : "127.0.0.1",
  7. "ip" : "127.0.0.1:9300",
  8. "tasks" : {
  9. "oTUltX4IQMOUUVeiohTt8A:124" : {
  10. "node" : "oTUltX4IQMOUUVeiohTt8A",
  11. "id" : 124,
  12. "type" : "direct",
  13. "action" : "cluster:monitor/tasks/lists[n]",
  14. "start_time_in_millis" : 1458585884904,
  15. "running_time_in_nanos" : 47402,
  16. "cancellable" : false,
  17. "parent_task_id" : "oTUltX4IQMOUUVeiohTt8A:123"
  18. },
  19. "oTUltX4IQMOUUVeiohTt8A:123" : {
  20. "node" : "oTUltX4IQMOUUVeiohTt8A",
  21. "id" : 123,
  22. "type" : "transport",
  23. "action" : "cluster:monitor/tasks/lists",
  24. "start_time_in_millis" : 1458585884904,
  25. "running_time_in_nanos" : 236042,
  26. "cancellable" : false
  27. }
  28. }
  29. }
  30. }
  31. }

也可以获取指定任务的信息 :

  1. curl -XGET 'localhost:9200/_tasks/task_id:1 ?pretty'

| Task Management API - 图4 | This will return a 404 if the task isn’t found. |

或者获取任务所有子任务的信息 :

  1. curl -XGET 'localhost:9200/_tasks?parent_task_id=parentTaskId:1 &pretty'

如果父任务不存在也不会返回 404 错误。

该任务 API 也可用于等待一个指定的任务完成。在 idoTUltX4IQMOUUVeiohTt8A 的任务完成之前,下面的调用将锁住 10 秒。

  1. curl -XGET 'localhost:9200/_tasks/oTUltX4IQMOUUVeiohTt8A:12345?wait_for_completion=true&timeout=10s&pretty'

你可以等待所有任务中的某个动作类型直到完成。这个命令将等待所有的 reindex 任务直到完成 :

  1. curl -XGET 'localhost:9200/_tasks?actions=*reindex&wait_for_completion=true&timeout=10s&pretty'

可以使用 _cat 列出任务命令列表,它接受与标准的 list task 命令相同的参数。

  1. curl -XGET 'localhost:9200/_cat/tasks?pretty'

Task Cancellation(任务取消)

如果一个长期运行的任务支持取消,可以通过下列的命令来取消它 :

  1. curl -XPOST 'localhost:9200/_tasks/task_id:1/_cancel?pretty'

该任务取消命令支持和 list tasks 命令相同的任务选择参数,所以多个任务可以在同时取消。例如,下面的命令将取消所有运行在 nodeId1nodeId2 上的 reindex 任务。

  1. curl -XPOST 'localhost:9200/_tasks/_cancel?nodes=nodeId1,nodeId2&actions=*reindex&pretty'

Task Grouping(任务组)

通过使用 task API 命令使用 group_by 参数可以通过 nodes(默认)或者父任务来分组以返回任务列表。下面的命令将改变分组为父任务 :

  1. curl -XGET 'localhost:9200/_tasks?group_by=parents&pretty'