ES failed to notify ClusterStateListener java.lang.IllegalStateException: environment is not locked

简介:

ES出现异常:

failed to notify ClusterStateListener
java.lang.IllegalStateException: environment is not locked

定位代码:

 

下载ES 5.5源码,在main/java/org/elasticsearch/env/NodeEnvironment.java 里可以看到:

复制代码
    private void assertEnvIsLocked() {
        if (!closed.get() && locks != null) {
            for (Lock lock : locks) {
                try {
                    lock.ensureValid();
                } catch (IOException e) {
                    logger.warn("lock assertion failed", e);
                    throw new IllegalStateException("environment is not locked", e);
                }
            }
        }
    }
复制代码
assertEnvIsLocked调用地方,可以看到在检测ES node数据路径、index路径使用:
复制代码
    /**
     * Returns an array of all of the nodes data locations.
     * @throws IllegalStateException if the node is not configured to store local locations
     */
    public Path[] nodeDataPaths() {
        assertEnvIsLocked();
        Path[] paths = new Path[nodePaths.length];
        for(int i=0;i<paths.length;i++) {
            paths[i] = nodePaths[i].path;
        }
        return paths;
    }

    /**
     * Returns an array of all of the {@link NodePath}s.
     */
    public NodePath[] nodePaths() {
        assertEnvIsLocked();
        if (nodePaths == null || locks == null) {
            throw new IllegalStateException("node is not configured to store local location");
        }
        return nodePaths;
    }

    public int getNodeLockId() {
        assertEnvIsLocked();
        if (nodePaths == null || locks == null) {
            throw new IllegalStateException("node is not configured to store local location");
        }
        return nodeLockId;
    }

    /**
     * Returns all index paths.
     */
    public Path[] indexPaths(Index index) {
        assertEnvIsLocked();
        Path[] indexPaths = new Path[nodePaths.length];
        for (int i = 0; i < nodePaths.length; i++) {
            indexPaths[i] = nodePaths[i].resolve(index);
        }
        return indexPaths;
    }
复制代码

 

而locks变量的赋值在:
复制代码
 public NodeEnvironment(Settings settings, Environment environment) throws IOException {

        if (!DiscoveryNode.nodeRequiresLocalStorage(settings)) {
            nodePaths = null;
            sharedDataPath = null;
            locks = null;
            nodeLockId = -1;
            nodeMetaData = new NodeMetaData(generateNodeId(settings));
            logger = Loggers.getLogger(getClass(), Node.addNodeNameIfNeeded(settings, this.nodeMetaData.nodeId()));
            return;
        }
        final NodePath[] nodePaths = new NodePath[environment.dataWithClusterFiles().length];
        final Lock[] locks = new Lock[nodePaths.length];
        boolean success = false;
。。。
复制代码

查了下Lock这个类:

import org.apache.lucene.store.Lock

 

作用:

  • org.apache.lucene.store.Lock
  • An interprocess mutex lock.

    Typical use might look like:

     new Lock.With(directory.makeLock("my.lock")) { public Object doBody() { ... code to execute while locked ... } }.run();

    一些加锁、解锁例子 https://www.programcreek.com/java-api-examples/index.php?api=org.apache.lucene.store.Lock
    lucene锁的作用,写保护:在Lucene中,打开一个IndexWrite之后,就会自动在索引目录中生成write.lock文件,这个文件中并不会有内容,不管是在索引打开期间还是在索引关闭之后,其大小都为0KB,并且在IndexWriter关闭之后,并不会删除该文件。如果同时打开多个IndexWriter的话,后打开的IndexWriter就会抛出LockObtainFailedException异常。这是个很重要的保护机制,因为若针对同一索引打开两个writer的话,会导致索引损坏。所以Lucene中的锁主要针对并发写的情况,在写的过程中并不会影响到并发读操作。

    1. lucene并发规则

    a,任意数量的只读属性IndexReader类都可以同时打开一个索引。

    b,对于一个索引来说,一次只能打开一个IndexWriter对象。lucene采用锁来提供保障。

    c,IndexReader可以在indexwriter正在修改索引时打开。该对象只有在IndexWriter提交修改或自己重新打开后才能获知索引的修改情况。

    d,任意多个线程可以共享同一个indexreader或indexwriter。

     

    2. lucene锁机制

    为了实现单一的writer,lucene采用了基于文件的锁,如果锁文件(默认writer.lock)存在于你的索引所在目录内,说明此时正在打开一个writer。此时若企图对同一个索引文件创建其他的writer的话,将产生一个LockObtainFailedException异常。


    而由assertEnvIsLocked看,抛出的异常应该是锁出现了问题,文件损坏或者目录损坏、或者文件系统损坏导致。











本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/7909194.html,如需转载请自行联系原作者

相关文章
|
1月前
|
Java 开发工具 流计算
flink最新master代码编译出现Java Runtime Environment 问题
在尝试编译Flink源码时遇到Java运行时环境致命错误:EXCEPTION_ACCESS_VIOLATION。问题出现在JVM.dll+0x88212。使用的是Java 11.0.28和Java HotSpot(TM) 64-Bit Server VM。系统为Windows客户端,没有生成核心dump文件。错误日志保存在hs_err_pid39364.log和replay_pid39364.log。要解决这个问题,建议检查JDK版本兼容性,更新JDK或参照错误报告文件提交Bug至http://bugreport.java.com/bugreport/crash.jsp。
|
3月前
|
Java
IDEA Error:java: Compilation failed: internal java compiler error 解决办法
IDEA Error:java: Compilation failed: internal java compiler error 解决办法
|
7月前
|
Java
解决java.lang.IllegalStateException: Failed to load ApplicationContext错误~
解决java.lang.IllegalStateException: Failed to load ApplicationContext错误~
|
11天前
|
Java 微服务
IDEA报错There is insufficient memory for the Java Runtime Environment to continue.
IDEA报错There is insufficient memory for the Java Runtime Environment to continue.
|
18天前
|
Java Linux 应用服务中间件
Linux启动tomcat报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
Linux启动tomcat报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
14 0
|
2月前
|
Java Maven Spring
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
115 0
|
2月前
|
JSON 前端开发 数据格式
`java.time.LocalDateTime` from String \“2020-11-19\“: Failed to deserialize java.time.LocalDateTime
`java.time.LocalDateTime` from String \“2020-11-19\“: Failed to deserialize java.time.LocalDateTime
24 1
|
7月前
|
Java 应用服务中间件
线上临时文件夹报错Failed to parse multipart servlet request; nested exception is java.lang.RuntimeException: java.nio.file.NoSuchFileException
Failed to parse multipart servlet request; nested exception is java.lang.RuntimeException: java.nio.file.NoSuchFileException、tmp、配置文件指定目录
131 0
|
7月前
|
Java Apache Maven
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
1339 0
|
7月前
|
XML Java 数据库连接
Mybatis的报错问题:1 字节的 UTF-8 序列的字节 1 无效,​java.lang.IllegalStateException: Failed to load ApplicationCon
Mybatis的报错问题:1 字节的 UTF-8 序列的字节 1 无效,​java.lang.IllegalStateException: Failed to load ApplicationCon
73 0