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

适用版本: 7.6-7.15

1. 错误异常的基本描述 #

Cannot delete model [model_id] as it is still referenced by ingest processors 表示在 Elasticsearch 中删除训练模型(Trained Model)时,该模型仍被一个或多个 Ingest Pipeline 中的处理器(如 inference processor)引用,因此删除操作被阻止。这是一个保护性的冲突异常,防止删除正在被使用的模型。

常见现象 #

  • 执行删除模型 API 时返回 409 Conflict 状态码。
  • 在 Kibana 的 Machine Learning 界面中删除模型时提示错误,显示模型仍被引用。
  • 自动化部署脚本在清理模型时失败,报错提示模型被 Ingest Processor 引用。
  • 删除模型后发现某些 Ingest Pipeline 无法正常工作。

典型报错与异常栈 #

典型错误信息如下:

ElasticsearchStatusException: Cannot delete model [my_model] as it is still referenced by ingest processors

底层异常栈通常类似:

ElasticsearchStatusException: Cannot delete model [my_model] as it is still referenced by ingest processors
    at org.elasticsearch.xpack.ml.action.TransportDeleteTrainedModelAction.masterOperation(TransportDeleteTrainedModelAction.java:XX)
    at org.elasticsearch.xpack.ml.action.TransportDeleteTrainedModelAction.doExecute(...)

在 Elasticsearch 日志文件中可能会出现:

[ERROR][o.e.x.m.a.TransportDeleteTrainedModelAction] [node_name] failed to delete trained model [my_model]
ElasticsearchStatusException[Cannot delete model [my_model] as it is still referenced by ingest processors]

2. 为什么会发生这个错误 #

Cannot delete model as it is still referenced by ingest processors 异常由以下几种原因导致:

  • Ingest Pipeline 中的 Inference Processor 引用了该模型:在 Ingest Pipeline 配置中,inference processor 的 model_id 字段指定了该模型。
  • 多个 Pipeline 引用同一模型:可能存在多个 Ingest Pipeline 都引用了同一个模型,需要全部清理。
  • 旧 Pipeline 未清理:之前测试或使用的 Pipeline 没有被正确清理,仍然保留着对模型的引用。
  • 动态创建的 Pipeline:某些自动化流程可能动态创建包含模型引用的 Pipeline,删除模型前未清理这些动态 Pipeline。
  • Pipeline 定义缓存:即使更新了 Pipeline 定义,旧的定义可能仍在某个节点的缓存中。

3. 如何排查和解决这个异常和解决这个异常 #

建议按以下步骤进行排查:

排查步骤 #

  1. 查找哪些 Ingest Pipeline 引用了该模型
# 获取所有 Ingest Pipeline
curl -X GET "localhost:9200/_ingest/pipeline?pretty"

# 搜索引用了特定模型的 Pipeline(使用 jq 过滤)
curl -s "localhost:9200/_ingest/pipeline" | jq 'to_entries[] | select(.value.processors[]?.inference?.model_id == "my_model") | .key'

或者使用 grep 搜索:

curl -s "localhost:9200/_ingest/pipeline" | grep -i "my_model"
  1. 查看具体 Pipeline 的配置
# 查看特定 Pipeline 的完整配置
curl -X GET "localhost:9200/_ingest/pipeline/my_pipeline?pretty"

检查其中的 inference processor 配置:

{
  "description": "My inference pipeline",
  "processors": [
    {
      "inference": {
        "model_id": "my_model",
        "target_field": "ml"
      }
    }
  ]
}
  1. 检查集群状态中的 Ingest Metadata
# 查看集群状态中的 Ingest 元数据
curl -X GET "localhost:9200/_cluster/state/metadata?pretty" | grep -A50 "ingest"
  1. 查看 Elasticsearch 日志中的相关信息
grep -i "referenced by ingest processors" /var/log/elasticsearch/elasticsearch.log

排查时需要注意的问题 #

  • 一个模型可能被多个 Pipeline 引用,需要全部找到并清理。
  • Pipeline 名称可能不直观,需要检查所有 Pipeline 的配置。
  • 如果使用了索引模板自动应用 Pipeline,也需要检查模板配置。

4. 如何解决这个错误 #

常用修复思路 #

  1. 更新或删除引用该模型的 Ingest Pipeline
# 删除引用了该模型的 Pipeline
curl -X DELETE "localhost:9200/_ingest/pipeline/my_pipeline?pretty"

# 或者更新 Pipeline,移除 inference processor 或改用其他模型
curl -X PUT "localhost:9200/_ingest/pipeline/my_pipeline" -H 'Content-Type: application/json' -d '{
  "description": "Updated pipeline without model reference",
  "processors": []
}'
  1. 批量查找并清理引用
#!/bin/bash
MODEL_ID="my_model"
ES_URL="localhost:9200"

# 获取所有引用了该模型的 Pipeline
PIPELINES=$(curl -s "${ES_URL}/_ingest/pipeline" | jq -r --arg model "$MODEL_ID" 'to_entries[] | select(.value.processors[]?.inference?.model_id == $model) | .key')

for PIPELINE in $PIPELINES; do
  echo "Deleting pipeline: $PIPELINE"
  curl -X DELETE "${ES_URL}/_ingest/pipeline/${PIPELINE}"
done
  1. 确认 Ingest Metadata 中不再包含该模型 ID
# 再次检查是否还有引用
curl -s "localhost:9200/_ingest/pipeline" | jq 'to_entries[] | select(.value.processors[]?.inference?.model_id == "my_model")'

如果返回为空,说明引用已清理完成。

  1. 执行模型删除
# 删除模型
curl -X DELETE "localhost:9200/_ml/trained_models/my_model?pretty"

后续注意事项与推荐建议 #

  • 在删除模型前,始终先检查是否有 Ingest Pipeline 引用了该模型。
  • 建立模型使用情况的追踪机制,记录每个模型被哪些 Pipeline 引用。
  • 在更新或删除 Pipeline 时,考虑对模型依赖的影响。
  • 对于生产环境,建议在删除模型前先在测试环境验证。

借助 INFINI 产品提升排障效率 #

  • INFINI Console 可以可视化查看和管理 Elasticsearch 集群中的 Ingest Pipeline 和训练模型。通过 Console 的 Pipeline 管理界面,可以快速搜索哪些 Pipeline 引用了特定模型,并一键编辑或删除这些 Pipeline。Console 还提供模型使用情况的统计信息,帮助识别模型依赖关系。

  • INFINI Gateway 部署在 Elasticsearch 前端时,可以对模型删除和 Pipeline 更新请求进行监控。当模型删除操作因引用冲突而失败时,Gateway 的请求日志可以帮助分析引用关系,识别哪些请求正在使用该模型。Gateway 还可以在检测到模型删除失败时,提供友好的错误提示和修复建议。

5. 小结 #

Cannot delete model as it is still referenced by ingest processors 是一个保护性的冲突异常,防止删除正在被 Ingest Pipeline 使用的模型。解决这个问题的关键是:

  1. 先查找所有引用了该模型的 Ingest Pipeline;
  2. 更新或删除这些 Pipeline,移除对模型的引用;
  3. 确认引用已完全清理后再执行模型删除。

通过 INFINI Console 进行 Pipeline 和模型的可视化管理,以及使用 INFINI Gateway 实现请求监控,可以更高效地管理模型依赖关系。

相关错误 #

参考文档 #

附:日志上下文 #

下面保留当前页面中的源码或日志片段,便于继续结合异常调用栈定位问题:

String id = request.getId();
IngestMetadata currentIngestMetadata = state.metadata().custom(IngestMetadata.TYPE);
SetreferencedModels = getReferencedModelKeys(currentIngestMetadata; ingestService);  if (referencedModels.contains(id)) {
listener.onFailure(new ElasticsearchStatusException("Cannot delete model [{}] as it is still referenced by ingest processors";
RestStatus.CONFLICT;
id));
return;
}