elasticsearch源码运行调试的问题

由于官方文档对es原理方面的讲解非常少,所以如果时间允许可以自己下载源码在本地调试。

首先在github上可以下载到各个版本的源码,写作本文时最新的是6.4.2版本:https://github.com/elastic/elasticsearch/tree/v6.4.2

下载的源码是gradle项目,所以需要安装gradle,对版本有要求,我用的是4.10.2版本。

java的版本也有要求,我用的是jdk11jdk9以上应该就可以了。

idea打开需要在源码根目录下执行gradle idea命令。

然后尝试运行server模块下org.elasticsearch.bootstrap.Elasticsearch主类,可能会遇到下列问题:

1. 配置文件路径设置问题

ERROR: the system property [es.path.conf] must be set

解决方法:新建任意目录,ideavm option中添加设置,如-Des.path.conf=F:\middleware\elasticsearch-data\config

这是放配置文件的地方,如果路径下没有配置文件,将使用默认配置,可将elasticsearch.yml文件放在此路径下。

2. home路径设置问题

Exception in thread "main" java.lang.IllegalStateException: path.home is not configured

解决方法:添加es.path.home的路径设置,可随意设置,如-Des.path.home=F:\middleware\elasticsearch-data

这是es运行的home路径。pluginsmoduleslib等都会在此路径下相应的子路径加载。如果不指定,默认的datalogconfig路径也会创建在此home路径下。但其实因为代码的问题config路径必须指定,否则会报错。

3. java security权限设置问题

ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")

解决方法:添加设置,-Dlog4j2.disable.jmx=true
由于本人对java security了解甚少,此处存疑,这是网上找到的办法,不知道是真正解决了问题,还是仅仅抑制了报错。
经试验也可以在server模块找到src\main\resources\org\elasticsearch\bootstrap\security.policy文件,在grant{}中添加上相应的permission,比如这里可以加上

permission javax.management.MBeanTrustPermission "register";

后面遇到其它类似的问题,加上相应的permission即可。

4. 日志配置问题

ERROR: no log4j2.properties found; tried [F:\middleware\elasticsearch-data\config] and its subdirectories

解决方法:在上面配置的es.path.conf路径下添加log4j2.properties文件,此文件可以在distribution模块的src\config\路径下找到,建议下载一个相同版本的es发行版(因为后面也要用到),用发行版里面的配置文件。

5. modules加载问题

org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: modules directory [F:\middleware\elasticsearch-data\modules] not found

解决方法:下载一个相同版本的es发行版,将里面的modules文件夹复制到上面配置的es.path.home路径下。

6. libs模块的类加载问题

java.lang.NoClassDefFoundError: org/elasticsearch/plugins/ExtendedPluginsClassLoader

解决方法:这个类在libs模块,server模块中原来的gradle配置是

compileOnly project(':libs:plugin-classloader')

compileOnly改为compile即可。

至此,应该已经能够正常运行es源码了。

7. transport通信断点调试

es集群节点间通信都是通过transport模块来进行的,想要调试节点间的transport请求,在TransportRequestHandler接口的默认方法messageReceived方法处打个断点,节点间交互时都会进来。

public interface TransportRequestHandler<T extends TransportRequest> {

    /**
     * Override this method if access to the Task parameter is needed
     */
    default void messageReceived(final T request, final TransportChannel channel, Task task) throws Exception {
        messageReceived(request, channel);
    }

    void messageReceived(T request, TransportChannel channel) throws Exception;
}

8. http通信断点调试

集群外部访问集群,比如searchindex等操作通常是http方式的,调试这种可以在RestController类的dispatchRequest方法处打个断点,这样对该节点的http请求都会进到这里来。

    @Override
    public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
        if (request.rawPath().equals("/favicon.ico")) {
            handleFavicon(request, channel);
            return;
        }
        try {
            tryAllHandlers(request, channel, threadContext);
        } catch (Exception e) {
            try {
                channel.sendResponse(new BytesRestResponse(channel, e));
            } catch (Exception inner) {
                inner.addSuppressed(e);
                logger.error(() ->
                    new ParameterizedMessage("failed to send failure response for uri [{}]", request.uri()), inner);
            }
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,726评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,044评论 2 59
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 能让自己快乐 是一种能力 能让别人快乐也是一种能力 我今天在医院里了 icu的病人 让我深深地感受到了生命的力量 ...
    静庄主阅读 294评论 0 0
  • 在每一次和同学闹矛盾的时候,我总是得理不让人,她也总是自私。 我知道在每一次闹矛盾的时候我们都...
    诺and便阅读 196评论 0 0