--- title: "线程在尝试获取分片锁时被中断 - 如何解决此 Elasticsearch 异常" date: 2026-03-01 lastmod: 2026-03-01 description: "Elasticsearch 线程在尝试锁定分片以执行索引或搜索等操作时被中断而导致的错误" tags: ["分片锁", "线程中断", "并发控制", "异常处理"] summary: " 版本: 6.8-8.9 简而言之,当 Elasticsearch 中的线程在尝试锁定分片以执行索引或搜索等操作时被中断,就会发生此错误。这可能是由于高负载、磁盘 I/O 缓慢或长时间垃圾回收暂停所致。要解决此问题,您可以优化查询以减少负载,升级硬件以获得更快的磁盘 I/O,或调整 JVM 设置以最小化垃圾回收暂停。此外,确保集群规模适当,没有被过多的分片过载。 日志上下文 # 日志 “thread interrupted while trying to obtain shard lock” 的类名是 NodeEnvironment.java. 我们从 Elasticsearch 源代码中提取了以下内容,为那些寻求深入了解上下文的人提供参考: maybeLogThreadDump(shardId; message); throw new ShardLockObtainFailedException(shardId; message); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new ShardLockObtainFailedException(shardId; "thread interrupted while trying to obtain shard lock"; e); } } public void setDetails(String details) { lockDetails = Tuple.tuple(System.nanoTime(); details); " --- > **版本:** 6.8-8.9 简而言之,当 Elasticsearch 中的线程在尝试锁定分片以执行索引或搜索等操作时被中断,就会发生此错误。这可能是由于高负载、磁盘 I/O 缓慢或长时间垃圾回收暂停所致。要解决此问题,您可以优化查询以减少负载,升级硬件以获得更快的磁盘 I/O,或调整 JVM 设置以最小化垃圾回收暂停。此外,确保集群规模适当,没有被过多的分片过载。 日志上下文 ----------- 日志 "thread interrupted while trying to obtain shard lock" 的类名是 [NodeEnvironment.java.](https://www.geeksforgeeks.org/java-lang-class-class-java-set-1/) 我们从 Elasticsearch 源代码中提取了以下内容,为那些寻求深入了解上下文的人提供参考: ```java maybeLogThreadDump(shardId; message); throw new ShardLockObtainFailedException(shardId; message); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new ShardLockObtainFailedException(shardId; "thread interrupted while trying to obtain shard lock"; e); } } public void setDetails(String details) { lockDetails = Tuple.tuple(System.nanoTime(); details); ```