📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

获取集群中当前正在运行的任务信息。

API 格式 #

GET /_tasks
GET /_tasks/{task_id}

API 作用 #

该 API 用于检索集群中当前正在运行的任务信息:

  • 查看所有节点的运行任务
  • 按节点、父任务或平铺方式分组显示
  • 过滤特定节点、动作或父任务
  • 等待任务完成后再返回结果
  • 检查任务是否可取消

API 参数 #

路径参数 #

参数类型是否必填默认值描述
{task_id}String-任务 ID(格式:node_id:task_number)。指定后仅返回该任务信息

查询参数 #

参数类型是否必填默认值描述
nodesString所有节点逗号分隔的节点 ID 或名称列表。使用 _local 仅返回连接节点的信息
actionsString所有动作逗号分隔的动作掩码列表,用于过滤返回的动作
detailedBooleanfalse是否返回详细任务信息(包含状态信息)
parent_task_idString所有任务按父任务 ID 过滤(格式:node_id:task_number)。设为 -1 或留空返回所有任务
wait_for_completionBooleanfalse是否等待所有找到的任务完成
group_byStringnodes任务分组方式:nodes(按节点)、parents(按父任务)、none(平铺)
timeoutTime30s显式操作超时时间
master_timeoutTime30s等待主节点连接的超时时间

请求示例 #

查询所有任务 #

GET /_tasks

查询指定任务 #

GET /_tasks/node1:123

查询特定节点的任务 #

GET /_tasks?nodes=node1,node2

详细信息模式 #

GET /_tasks?detailed=true

按父任务分组 #

GET /_tasks?group_by=parents

等待任务完成 #

GET /_tasks?wait_for_completion=true&timeout=10m

响应示例 #

默认响应(group_by=nodes) #

{
  "nodes": {
    "node1": {
      "name": "node-1",
      "transport_address": "192.168.1.10:9300",
      "host": "node-1.example.com",
      "ip": "192.168.1.10:9300",
      "roles": ["data", "ingest", "master"],
      "attributes": {
        "attr1": "value1"
      },
      "tasks": {
        "node1:123": {
          "node": "node1",
          "id": 123,
          "type": "transport",
          "action": "indices:data/write/index",
          "description": "index[{index1}],
                         type[_doc], id[AVx7XlYqBYs1B8mWEqJl]",
          "start_time_in_millis": 1738665600000,
          "running_time_in_nanos": 1500000000,
          "running_time": "1.5s",
          "cancellable": true,
          "headers": {}
        },
        "node1:124": {
          "node": "node1",
          "id": 124,
          "type": "transport",
          "action": "indices:data/read/search",
          "description": "search[query]",
          "start_time_in_millis": 1738665601000,
          "running_time_in_nanos": 500000000,
          "running_time": "500ms",
          "cancellable": true,
          "parent_task_id": "node1:100",
          "headers": {}
        }
      }
    },
    "node2": {
      "name": "node-2",
      "transport_address": "192.168.1.11:9300",
      "host": "node-2.example.com",
      "ip": "192.168.1.11:9300",
      "roles": ["data", "master"],
      "attributes": {},
      "tasks": {
        "node2:456": {
          "node": "node2",
          "id": 456,
          "type": "transport",
          "action": "cluster:monitor/health",
          "description": "cluster health",
          "start_time_in_millis": 1738665602000,
          "running_time_in_nanos": 300000000,
          "running_time": "300ms",
          "cancellable": false,
          "headers": {}
        }
      }
    }
  }
}

详细模式响应(detailed=true) #

{
  "nodes": {
    "node1": {
      "name": "node-1",
      "transport_address": "192.168.1.10:9300",
      "host": "node-1.example.com",
      "ip": "192.168.1.10:9300",
      "roles": ["data"],
      "tasks": {
        "node1:123": {
          "node": "node1",
          "id": 123,
          "type": "transport",
          "action": "indices:data/write/index",
          "description": "index[{index1}]",
          "status": "running",
          "start_time_in_millis": 1738665600000,
          "running_time_in_nanos": 1500000000,
          "running_time": "1.5s",
          "cancellable": true,
          "headers": {}
        }
      }
    }
  }
}

平铺模式响应(group_by=none) #

{
  "tasks": [
    {
      "node": "node1",
      "id": 123,
      "type": "transport",
      "action": "indices:data/write/index",
      "description": "index[{index1}]",
      "start_time_in_millis": 1738665600000,
      "running_time_in_nanos": 1500000000,
      "running_time": "1.5s",
      "cancellable": true
    }
  ]
}

响应字段说明 #

节点对象字段 #

字段类型描述
nameString节点名称
transport_addressString传输地址(host:port)
hostString主机名
ipStringIP 地址
rolesString[]节点角色列表
attributesObject节点属性
tasksObject该节点上的任务对象

任务对象字段 #

字段类型描述
nodeString任务运行的节点 ID
idInteger数字任务 ID
typeString任务类型(通常为 “transport”)
actionString正在执行的动作名称
descriptionString可读的任务描述
statusString当前任务状态(仅当 detailed=true)
start_time_in_millisLong任务开始时间(毫秒时间戳)
running_time_in_nanosLong任务运行时长(纳秒)
running_timeString可读的运行时长
cancellableBoolean任务是否可取消
parent_task_idString父任务 ID(如果存在)
headersObject任务特定的头信息

常见动作类型 #

动作描述
indices:data/write/index索引文档
indices:data/read/search搜索操作
cluster:monitor/health集群健康检查
cluster:update_settings更新集群设置
indices:admin/create创建索引
indices:admin/delete删除索引

使用场景 #

  1. 任务监控:查看集群中正在运行的任务
  2. 性能分析:识别长时间运行的任务
  3. 故障排查:检查任务状态和依赖关系
  4. 资源管理:了解节点负载和任务分布

注意事项 #

  1. 任务 ID 格式:任务 ID 格式为 node_id:task_number
  2. 可取消性:并非所有任务都支持取消操作
  3. 性能影响:detailed=true 可能会增加响应时间
  4. 分组方式:group_by 参数影响响应结构
  5. 超时设置:长时间运行的任务可能需要增加超时时间

相关操作 #

  • POST /_tasks/{task_id}/_cancel:取消任务
  • GET /_cat/tasks:以表格格式查看任务
  • GET /_tasks/{task_id}:查询特定任务

实现文件 #

  • REST 处理器RestListTasksAction.java
  • Transport ActionTransportListTasksAction.java
  • 请求类ListTasksRequest.java
  • 响应类ListTasksResponse.java
  • 任务信息TaskInfo.java