问题现象:

	java.util.concurrent.CancellationException: Request execution cancelled
	at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.execute(CloseableHttpAsyncClientBase.java:114)
	at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:138)
	at org.opensearch.client.RestClient.performRequest(RestClient.java:329)
	at org.opensearch.client.RestClient.performRequest(RestClient.java:321)
	at org.opensearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1918)
	at org.opensearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1884)
	at org.opensearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1852)
	at org.opensearch.client.RestHighLevelClient.index(RestHighLevelClient.java:969)
	at com.kone.sfm.service.impl.logs.LogRepository.saveSystemOperationLog(LogRepository.java:116)
	at com.kone.sfm.service.impl.logs.LogServiceImpl.lambda$createSystemOperationLog$1(LogServiceImpl.java:90)
	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1583)

火焰图:
image-20251112100801223

OpenSearch Version:2.13
JDK 21
SpringBoot 21

经过排查发现OpenSearch 性能正常,就是不断报这个错误。之后咨询AWS OpenSearch专家,确认了这是一个opensearch-project/opensearch-java SDK的Bug.

Github 地址:https://github.com/opensearch-project/opensearch-java/issues/545

stackoverflow地址:https://stackoverflow.com/questions/71611560/got-cancellationexception-request-execution-cancelled-always-when-throwing-an

GitHub Issue #545 核心内容回顾

标题

[BUG] java.lang.RuntimeException: Request execution cancelled in Java Opensearch rest and high level rest clients

现象

  • 在高并发或压力测试下,OpenSearch 客户端偶尔抛出:

    java.lang.RuntimeException: Request execution cancelled
    Caused by: java.util.concurrent.CancellationException: Request execution cancelled
    
  • 一旦发生,后续所有请求都失败,必须重启应用才能恢复

环境

  • AWS OpenSearch 2.13(托管)
  • Java 21(Corretto)
  • opensearch-rest-clientopensearch-rest-high-level-client 版本 2.15.0
  • 运行在 EKS(Kubernetes)

根本原因(社区确认)

Apache HttpClient 的异步客户端(CloseableHttpAsyncClient)在线程池耗尽或 I/O reactor 异常关闭后,会进入不可恢复的“取消”状态,导致所有新请求立即失败。

这与你的火焰图中 66μs 就失败 的现象完全吻合 —— 请求根本没有真正发出,而是被客户端内部状态直接拒绝。

二、Stack Overflow 链接补充说明

该问题也出现在 Elasticsearch/HttpClient 社区中:

当底层 HttpAsyncClient 被关闭(例如因异常、资源泄漏、线程中断等),其状态变为 CANCELED,之后所有调用 execute() 都会立即抛出 CancellationException

常见触发场景:

  • 应用使用了自定义 HttpClient 配置但未正确管理生命周期
  • 线程池满载或 reactor 崩溃
  • JVM 关闭钩子提前关闭了客户端
  • 多个线程共享同一个 RestClient 实例,但其中一个线程异常中断了它

三、为什么“重启才能恢复”?

因为 RestHighLevelClient 内部封装的 RestClient 持有一个 单例的 CloseableHttpAsyncClient
一旦这个 HTTP 客户端因任何原因被关闭(close() 被调用)或进入错误状态,它不会自动重建

所以:

  • 第一次失败可能是偶然(如网络抖动 + 线程中断)
  • 但客户端状态已损坏 → 后续所有请求都立即失败
  • 只有重启应用(重建客户端)才能恢复
Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐