# TaskCancellationException: 任务[id:12345]因超时被取消，等待完成中

- **ID:** `elasticsearch/task-cancellation-exception`
- **领域:** elasticsearch
- **类别:** runtime_error
- **错误码:** `TASK_CANCELLED`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

长时间运行的任务（如重新索引、快照）超过了配置的超时时间，被集群强制取消。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Elasticsearch 7.17 | active | — | — |
| Elasticsearch 8.5 | active | — | — |
| Elasticsearch 8.10 | active | — | — |

## 解决方案

1. ```
   通过API增加特定任务的超时时间，例如：`POST _tasks/cancel?actions=cluster:admin/reindex&timeout=2h`
   ```
2. ```
   通过减少批量大小优化任务：对于重新索引，设置`{"source": {"size": 500}, "dest": {"index": "new_index"}}`
   ```
3. ```
   如果任务涉及大量聚合，增加`search.max_buckets`和`search.max_buckets_per_cluster`
   ```

## 无效尝试

- **Increasing task timeout in elasticsearch.yml (e.g., `task.timeout: 60m`) without analyzing actual task duration** — This may mask underlying performance issues (e.g., slow disk, insufficient memory) and cause cascading failures. (70% 失败率)
- **Restarting the cluster to clear all tasks** — Restarting drops all ongoing tasks, but the error will reoccur if the root cause (e.g., slow shard recovery) is not addressed. (80% 失败率)
- **Setting `task.timeout: 0` to disable timeout** — This can lead to indefinite task hangs and resource exhaustion, as the cluster will never cancel stuck tasks. (90% 失败率)
