教你编译调试Elasticsearch 6.3.2源码
前言
想深入理解 Elasticsearch,阅读它的源码是很有必要的,一来可以了解它内部的具体实现,有助于调优,二来可以了解优秀开源项目的代码架构,提高我们的代码架构能力等
阅读Elasticsearch源码的第一步是搭建调试环境,然后作者在这个过程中遇到很多麻烦,在网上找不到想要的答案,历经千辛最后一一解决,所以记录下,帮助有需要的童鞋
软件环境
- 操作系统:win7
- Elasticsearch 源码版本: 6.3.2
- JDK版本: 10.0.2
- Gradle版本: 4.7
- Intellij Idea版本: 2018.2
环境准备及工程导入
1.安装JDK
Elasticsearch 6.3.2需要JDK1.9编译,否则后面步骤会报错。
Java SE Downloads 地址:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
作者装的是 JDK 10.0.2
2.下载Elasticsearch源码,并且切换到6.3.2分支
Elasticsearch github源码托管地址:
https://github.com/elastic/elasticsearch.git
git checkout v6.3.2
也可直接下载源码包,地址在 https://github.com/elastic/elasticsearch/releases
3.下载gradle的安装包
查看 elasticsearch\gradle\wrapper\gradle-wrapper.properties
发现如下配置:
distributionUrl=https://services.gradle.org/distributions/gradle-4.5-all.zip
Elasticsearch 6.3.2需要安装gradle-4.5,官方下载地址:
https://services.gradle.org/distributions/gradle-4.5-all.zip
注意:由于国内网速问题,为了加快速度,进行第4步操作
4.拷贝文件
将下载的gradle-4.5-all.zip包放到 elasticsearch\gradle\wrapper
目录下,
确保和 elasticsearch\gradle\wrapper\gradle-wrapper.properties
在同级目录,
然后修改 elasticsearch\gradle\wrapper\gradle-wrapper.properties
配置如下:
distributionUrl=gradle-4.5-all.zip
5.修改源码Maven仓库地址
国内下载国外仓库的jar包速度慢,需要替换Maven地址,设置为本地或者国内可用的Maven仓库。
需要修改下列文件的 maven URL 配置:
- elasticsearch\benchmarks\build.gradle
- elasticsearch\client\benchmark\build.gradle
修改源码中上面build.gradle文件里面的repositories-maven-url
的值,
配置为可用的仓库地址,譬如修改为阿里云maven地址 http://maven.aliyun.com/nexus/content/groups/public/
,修改示例如下:
buildscript {repositories {maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}}dependencies {classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2'}
}
6.修改全局Maven仓库地址
在USER_HOME/.gradle/
下面创建新文件 init.gradle
,输入下面的内容并保存。
allprojects{repositories {def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'all {ArtifactRepository repo ->if (repo instanceof MavenArtifactRepository) {def url = repo.url.toString()if (url.startsWith('https://repo.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."remove repo}}}maven {url REPOSITORY_URL}}
}
其中USER_HOME/.gradle/
是自己的gradle安装目录,示例值:C:\Users\Administrator\.gradle
,
如果没有.gradle
目录,可用自己创建,或者先执行第7步,等gradle安装后再回来修改。
上面脚本把url匹配到的仓库都替换成了阿里云的仓库,
如果有未匹配到的导致编译失败,可用自己仿照着添加匹配条件。
7.gradle编译源码
windows运行cmd,进入DOS命令行,然后切换到elasticsearch源码的根目录,执行如下命令,把elasticsearch编译为 idea 工程:
gradlew idea
编译失败则按照错误信息解决问题,可用使用如下命令帮助定位问题:
gradlew idea -info
gradlew idea -debug
一般是Maven仓库地址不可用导致jar包无法下载,从而编译失败,此时请参考步骤5和6修改相关的仓库地址。
编译成功后打印日志:
BUILD SUCCESSFUL in 1m 23s
8. idea 导入elasticsearch工程
idea 中 File -> New Project From Existing Sources
选择你下载的 Elasticsearch 根目录,然后点 open
,之后 Import project from external model -> Gradle
, 选中 Use auto-import
, 然后就可以了
导入进去后,gradle 又会编译一遍,需要等一会,好了之后如下:
运行,开始 solve error 模式
前面的步骤都挺顺利,接下来遇到的 ERROR & EXCEPTION 让作者耗费了好几天,心力交瘁,好在最终运行成功
在 elasticsearch/server/src/main/org/elasticsearch/bootstrap
下找到Elasticsearch的启动类 Elasticsearch.java
,打开文件,右键 Run Elasticsearch.main()
,运行main方法
1、 报错如下:
ERROR: the system property [es.path.conf] must be set
这是需要配置 es.path.conf 参数,我们先在 elasticsearch 源码目录下新建一个 home 目录,然后在 https://www.elastic.co/downloads/elasticsearch
下载一个同版本号的 Elasticsearch6.3.2 发行版,解压,将 config 目录拷贝到 home 目录中
然后打开 Edit Configurations
,在 VM options
加入如下配置:
-Des.path.conf=D:\elasticsearch-6.3.2\home\config
再次运行 Run Elasticsearch.main()
2、报错如下:
Exception in thread "main" java.lang.IllegalStateException: path.home is not configuredat org.elasticsearch.env.Environment.<init>(Environment.java:103)
...
需要配置 path.home
这个参数,在 VM options 中添加如下配置:
-Des.path.home=D:\elasticsearch-6.3.2
再次RUN
3、报错如下:
2018-08-22 15:07:17,094 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
...Caused by: java.nio.file.NoSuchFileException: D:\elasticsearch-6.3.2\modules\aggs-matrix-stats\plugin-descriptor.properties
...
在 VM options
中把 path.home
的值修改为如下:
-Des.path.home=D:\elasticsearch-6.3.2\home
然后把 ES6.3.2 发行版中的 modules
文件夹复制到 home
目录下,然后再次RUN
4、报错如下:
2018-08-22 15:12:29,876 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
...
在 VM options
中加入
-Dlog4j2.disable.jmx=true
1、2、3、4 的配置最终如下:
再次RUN
5、报错如下:
[2018-08-23T00:53:17,003][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [] fatal error in thread [main], exiting
java.lang.NoClassDefFoundError: org/elasticsearch/plugins/ExtendedPluginsClassLoaderat org.elasticsearch.plugins.PluginsService.loadBundle(PluginsService.java:632) ~[main/:?]at org.elasticsearch.plugins.PluginsService.loadBundles(PluginsService.java:557) ~[main/:?]at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:162) ~[main/:?]at org.elasticsearch.node.Node.<init>(Node.java:311) ~[main/:?]at org.elasticsearch.node.Node.<init>(Node.java:252) ~[main/:?]at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:213) ~[main/:?]at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:213) ~[main/:?]at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:326) ~[main/:?]at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[main/:?]at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:127) ~[main/:?]at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[main/:?]at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[main/:?]at org.elasticsearch.cli.Command.main(Command.java:90) ~[main/:?]at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[main/:?]at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:86) ~[main/:?]
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.plugins.ExtendedPluginsClassLoaderat jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582) ~[?:?]at jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190) ~[?:?]at java.lang.ClassLoader.loadClass(ClassLoader.java:499) ~[?:?]... 15 more
这个问题其实不算真正的问题,但是说起来挺好笑,为了解决这个问题耗费了作者好几天,当最后发现问题所在的时候,哭笑不得 \~_\~ 正是所谓的 踏破铁鞋无觅处,得来全不费工夫
解决方法: 打开 IDEA Edit Configurations
,给 Include dependencies with Provided scope
打上勾即可解决,很简单吧!!
继续RUN,又来一个 Exception
6、报错如下:
[2018-08-23T01:13:38,551][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "createClassLoader")at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:140) ~[main/:?]at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:127) ~[main/:?]at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[main/:?]at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[main/:?]at org.elasticsearch.cli.Command.main(Command.java:90) ~[main/:?]at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[main/:?]at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:86) ~[main/:?]
Caused by: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "createClassLoader")at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472) ~[?:?]at java.security.AccessController.checkPermission(AccessController.java:895) ~[?:?]at java.lang.SecurityManager.checkPermission(SecurityManager.java:335) ~[?:?]at java.lang.SecurityManager.checkCreateClassLoader(SecurityManager.java:397) ~[?:?]
...Exception: java.security.AccessControlException thrown from the UncaughtExceptionHandler in thread "Thread-2"
这个问题也找了挺久,最终才发现解决方法(两种):
第一种: 在 home/config
目录下新建 java.policy
文件,填入下面内容
grant {permission java.lang.RuntimePermission "createClassLoader";
};
然后在 VM options
加入 java.security.policy
的设置,指向该文件即可
-Djava.security.policy=D:\elasticsearch-6.3.2\home\config\java.policy
第二种: 就是在 %JAVA_HOME%/conf/security
目录下(JDK10是这个路径,之前的版本不确定),我的目录是 C:\Program Files\Java\jdk-10.0.2\conf\security
,打开 java.policy
文件,在 grant
中加入下面这句,赋予权限
permission java.lang.RuntimePermission "createClassLoader";
效果如下:
再RUN,这次可终于运行起来了!!!
来看一下效果,浏览器访问 http://localhost:9200/
浏览器访问 http://localhost:9200/_cat/health?v
一切正常,终于可以愉快的 DEBUG 源码啦!!!
另一种源码调试方式:远程调试
如果上面第五个报错之后解决不了无法继续进行,可以选择这种方式:
在 Elasticsearch 源码目录下打开 CMD,输入下面的命令启动一个 debug 实例
gradlew run --debug-jvm
如果启动失败可能需要先执行 gradlew clean
再 gradlew run --debug-jvm
或者 先退出 IDEA
在 IDEA 中打开 Edit Configurations
,添加 remote
配置 host 和 port
点击 debug,浏览器访问 http://localhost:9200/
,即可看到ES返回的信息
随机调试一下, 打开 elasticsearch/server/src/main/org/elasticsearch/rest/action/cat
下的 RestHealthAction
类,在第 54 行出设置一个断点,然后浏览器访问 http://localhost:9200/_cat/health
,可以看到断点已经捕获到该请求了
运行成功,可以开始设置断点进行其他调试
其他可能遇到的问题
1. 错误信息如下
JAVA8_HOME required to run tasks gradle
配置环境变量 JAVA8_HOME
,值为 JDK8 的安装目录
2. 错误信息如下
[2018-08-22T13:07:23,197][INFO ][o.e.t.TransportService ] [EFQliuV] publish_address {10.100.99.118:9300}, bound_addresses {[::]:9300}
[2018-08-22T13:07:23,211][INFO ][o.e.b.BootstrapChecks ] [EFQliuV] bound or publishing to a non-loopback address, enforcing bootstrap checks
ERROR: [1] bootstrap checks failed
[1]: initial heap size [268435456] not equal to maximum heap size [4273995776]; this can cause resize pauses and prevents mlockall from locking the entire heap
[2018-08-22T13:07:23,219][INFO ][o.e.n.Node ] [EFQliuV] stopping ...
2018-08-22 13:07:23,269 Thread-2 ERROR No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
Disconnected from the target VM, address: '127.0.0.1:5272', transport: 'socket'
在 Edit Configurations
的 VM options
加入下面配置
-Xms2g
-Xmx2g
欢迎访问我的个人博客:http://laijianfeng.org
参考文档:
1. Eclipse导入Elasticsearch源码
2. Elasticsearch源码分析—环境准备(一)
3. 渣渣菜鸡的 ElasticSearch 源码解析 —— 环境搭建
4. 教你如何在 IDEA 远程 Debug ElasticSearch
欢迎访问我的个人博客:http://laijianfeng.org
打开微信扫一扫,关注【小旋锋】微信公众号,及时接收博文推送
教你编译调试Elasticsearch 6.3.2源码相关推荐
- java编译使用androidsdk,详解Android源码的编译
本文将为大家介绍的是如何设置Android源码的编译环境,包括Linux下的配置.主要基于Android 1.0环境,希望对大家了解Android开发有所帮助. 本次编译过程主要参考官方文档(http ...
- vs+cmake完美编译RTS游戏,类似魔兽争霸源码
网上的一个RTS游戏,网上的代码比较老,不能直接编译.这个仓库是我整理编译通过的.代码版权归于原作者. 源码下载 vs+cmake完美编译RTS游戏,类似魔兽争霸源码下载-其他文档类资源-CSDN文库 ...
- 【朝花夕拾】Android自定义View之(一)手把手教你看懂View绘制流程——向源码要答案
前言 原文:Android自定义View之(一)手把手教你看懂View绘制流程--向源码要答案 View作为整个app的颜值担当,在Android体系中占有重要的地位.深入理解Android View ...
- 实时编译、动态执行C/C++源码函数
实时编译.动态执行C/C++源码函数 语法格式:fileCLASS *pObj = <file.cpp> 该语法获得源代码file.cpp的函数接口对象指针pObj,通过pObj调用fil ...
- ubuntu 14.04.5 编译Android 4.4.4 r1源码(最新)
本文博客链接:http://blog.csdn.net/qq1084283172/article/details/54426189 吐槽:ubuntu系统真是让人又爱又恨,也有可能是VMware Wo ...
- 如何用电脑反编译微信小程序,获得源码(学习用途)
故事背景:上周末,搞前端的发小(老表)找到我,问我微信小程序能否反编译成功,然后他需要源码,看他的接口签名算法 周末在公司研究了一下午 最后成功了,心路历程走一波,总结一下 一.准备工具: 模拟器: ...
- 汇编 debug调试没有执行对应文件源码指令---》失灵---》正确使用debug第一步
汇编 debug调试时 没有执行 对应文件源码指令 果然不认真听课就是这样 把调试命令 debug test.exe 错误写成 debug test.asm 并一直使用多达两周时长 我真是个人才Σ( ...
- 【Elasticsearch】Elasticsearch 7.6 IDEA 源码环境搭建
1.概述 1.1 软件环境 Intellij idea:2019.3 Gradle gradle-6.3-all.zip JDK 12.0.2 macOS 10.15.4 1.2 注意 elastic ...
- 调试系列1:bugreport源码篇
framework/native/cmds/bugreport/bugreport.cpp framework/native/cmds/dumpstate/dumpstate.cpp framewor ...
最新文章
- 医院信息化建设历程(5)互联互通的数字化医院阶段
- 如何用python写脚本_【按键教程】用python写脚本 另附垫材24与变奏22的实现
- 智源-知乎联合发布大规模用户关系数据集,同步开启10万元竞赛
- y7000p内存是一个16还是8+8_16层蜜瓜蛋糕,每日限量8件,只卖一个夏天!
- matlab中nc文件,教程合集 | MATLAB文件读写(以nc与txt为例)
- java nio底层实现_Java NIO 底层原理
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许...
- MyEclipse8.5默认工作区间修改
- KUBERNETES存储之PERSISTENT VOLUMES简介
- 基于时间片的高优先级调度_CFS调度器(1)-基本原理
- 安卓 多条通知_【安卓+苹果】石头阅读,全网小说、漫画免费看,最好用的追书神器!...
- 浅谈几款软件的创新点
- GSMA在G20峰会召开之前就妇女数字融合提出综合性议程
- oracle加密表空间
- oracle左连接查询去重,左连接去重(objec)
- 【python小技巧】 批量将.png格式图片转换为.jpg格式图片
- mysql计算1000天后的日期_Mysql中常用的日期函数
- 国培南通之行的感悟——(其三)
- 为什么有了路由器还要光猫
- 测试方法介绍-计算模型复杂度(GMac)、模型大小(M)、计算速度(FPS)