Elasticsearch5.2.1源码阅读-项目搭建

获取源码

https://github.com/elastic/elasticsearch 获取源代码,切换到要读取的分支。

导入idea

由于现在elasticsearch使用gradle进行代码管理(吐槽下,为啥从maven改成gradle,超级难用),所以需要提前安装gradle。
gradle安装完成后,将代码导入idea,这个过程需要你指定gradle路径。

项目引入.png

编译Elasticsearch源码

如果执行gradle build.gradle会编译失败,不知道是什么鬼。
直接运行:gradle idea
漫长下载的等待过程。。。。

编译成功后,文件目录

源码目录

Elasticsearch核心包

核心包

启动Elasticsearch

其中bootstrap包是elasticsearch系统启动包,下面的elasticsearch.java是启动入口,直接运行main函数。会发现控制台报错
<pre>
Exception in thread "main" java.lang.IllegalStateException: path.home is not configured
at org.elasticsearch.env.Environment.<init>(Environment.java:115)
at org.elasticsearch.node.internal.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:93)
at org.elasticsearch.bootstrap.Bootstrap.initialEnvironment(Bootstrap.java:257)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:297)
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:123)
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114)
at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54)
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122)
at org.elasticsearch.cli.Command.main(Command.java:88)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Refer to the log for complete error details.
</pre>

这个错误是没有指定配置文件目录,我们知道在安装elasticsearch时候,在${ES_HOME}/config下面有elasticsearch.yml、jvm.options、log4j2.properties配置文件,而我们要指定的就是这些配置文件。elasticsearch源码目录下的distribution/resources目录下就是自带了这些配置文件,可以直接指定:

指定配置文件目录

点击运行继续报错:
<pre>
2017-03-08 11:06:11,447 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:585)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.checkMBeanTrustPermission(DefaultMBeanServerInterceptor.java:1848)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:322)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
at org.apache.logging.log4j.core.jmx.Server.register(Server.java:389)
at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:167)
at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:140)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:541)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:258)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:206)
at org.apache.logging.log4j.core.config.Configurator.initialize(Configurator.java:220)
at org.apache.logging.log4j.core.config.Configurator.initialize(Configurator.java:197)
at org.elasticsearch.common.logging.LogConfigurator.configureStatusLogger(LogConfigurator.java:125)
at org.elasticsearch.common.logging.LogConfigurator.configureWithoutConfig(LogConfigurator.java:67)
at org.elasticsearch.cli.Command.main(Command.java:85)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
</pre>
这个问题在https://github.com/elastic/elasticsearch/issues/21932 看到是权限问题,但是发现对我们这样启动并不起作用。
可以创建一个elasticsearch.policy文件,然后在里面写入:
<pre>
grant {
permission javax.management.MBeanTrustPermission "register";
permission javax.management.MBeanServerPermission "createMBeanServer";
};
</pre>
然后放倒distribution/src/main/resources/config下,然后再通过jvm参数指定:

指定policy文件

点击运行继续报错:

<pre>
[2017-03-08T10:51:22,451][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: jar hell!
class: jdk.packager.services.UserJvmOptionsService
jar1: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/ant-javafx.jar
jar2: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/packager.jar
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:127) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114) ~[main/:?]
at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54) ~[main/:?]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122) ~[main/:?]
at org.elasticsearch.cli.Command.main(Command.java:88) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) ~[main/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) ~[idea_rt.jar:?]
</pre>
这个是由于Elasticsearch中的一个UserJvmOptionsService类和jdk类冲突,现在先解决的一个方法是将ant-javafx.jar去掉,前提是你不使用该JavaFX功能。可以将其重名为ant-javafx.jar.back。这种方式不是最好的,可以寻求更好的解决方式。

继续报错误:
<pre>
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: Unsupported transport.type []
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:127) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114) ~[main/:?]
at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54) ~[main/:?]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122) ~[main/:?]
at org.elasticsearch.cli.Command.main(Command.java:88) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) ~[main/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) ~[idea_rt.jar:?]
Caused by: java.lang.IllegalStateException: Unsupported transport.type []
at org.elasticsearch.common.network.NetworkModule.getTransportSupplier(NetworkModule.java:214) ~[main/:?]
at org.elasticsearch.node.Node.<init>(Node.java:401) ~[main/:?]
at org.elasticsearch.node.Node.<init>(Node.java:232) ~[main/:?]
at org.elasticsearch.bootstrap.Bootstrap$6.<init>(Bootstrap.java:241) ~[main/:?]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:241) ~[main/:?]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:333) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:123) ~[main/:?]
... 11 more
</pre>

这个是由于依赖的transport等jar并没有找到,可以在项目根目录找到models模块,然后将下面目录打包,然后copy到distribution/src/main/models目录下,也可以直接去官网下载zip包,解压后直接copy。
注意:注意copy的版本,如果版本不一致,有可能出现版本冲突。

ok启动成功

<pre>
[2017-03-08T14:23:00,982][INFO ][o.e.n.Node ] [] initializing ...
[2017-03-08T14:23:01,074][INFO ][o.e.e.NodeEnvironment ] [JotwXSM] using [1] data paths, mounts [[/ (/dev/disk1)]], net usable_space [115.5gb], net total_space [177.8gb], spins? [unknown], types [hfs]
[2017-03-08T14:23:01,074][INFO ][o.e.e.NodeEnvironment ] [JotwXSM] heap size [3.5gb], compressed ordinary object pointers [true]
[2017-03-08T14:23:01,076][INFO ][o.e.n.Node ] node name [JotwXSM] derived from node ID [JotwXSM9TR23cYhO1CstiQ]; set [node.name] to override
[2017-03-08T14:23:01,077][INFO ][o.e.n.Node ] version[5.2.1-SNAPSHOT], pid[7688], build[Unknown/Unknown], OS[Mac OS X/10.11.6/x86_64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_91/25.91-b14]
[2017-03-08T14:23:01,077][WARN ][o.e.n.Node ] version [5.2.1-SNAPSHOT] is a pre-release version of Elasticsearch and is not suitable for production
[2017-03-08T14:23:04,574][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [aggs-matrix-stats]
[2017-03-08T14:23:04,574][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [ingest-common]
[2017-03-08T14:23:04,574][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [lang-expression]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [lang-groovy]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [lang-mustache]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [lang-painless]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [percolator]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [reindex]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [transport-netty3]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [transport-netty4]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] no plugins loaded
[2017-03-08T14:23:07,038][INFO ][o.e.n.Node ] initialized
[2017-03-08T14:23:07,038][INFO ][o.e.n.Node ] [JotwXSM] starting ...
[2017-03-08T14:23:07,076][INFO ][i.n.u.i.PlatformDependent] Your platform does not provide complete low-level API for accessing direct buffers reliably. Unless explicitly requested, heap buffer will always be preferred to avoid potential system unstability.
[2017-03-08T14:23:07,213][INFO ][o.e.t.TransportService ] [JotwXSM] publish_address {127.0.0.1:9300}, bound_addresses {[fe80::1]:9300}, {[::1]:9300}, {127.0.0.1:9300}
[2017-03-08T14:23:07,221][WARN ][o.e.b.BootstrapChecks ] [JotwXSM] initial heap size [268435456] not equal to maximum heap size [4294967296]; this can cause resize pauses and prevents mlockall from locking the entire heap
[2017-03-08T14:23:10,287][INFO ][o.e.c.s.ClusterService ] [JotwXSM] new_master {JotwXSM}{JotwXSM9TR23cYhO1CstiQ}{otfcgw_KR1eU81nblu5T9w}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
[2017-03-08T14:23:10,307][INFO ][o.e.h.HttpServer ] [JotwXSM] publish_address {127.0.0.1:9200}, bound_addresses {[fe80::1]:9200}, {[::1]:9200}, {127.0.0.1:9200}
[2017-03-08T14:23:10,308][INFO ][o.e.n.Node ] [JotwXSM] started
[2017-03-08T14:23:10,313][INFO ][o.e.g.GatewayService ] [JotwXSM] recovered [0] indices into cluster_state
</pre>

关注我

欢迎关注我的公众号,会定期推送优质技术文章,让我们一起进步、一起成长!
公众号搜索:data_tc
或直接扫码:🔽


欢迎关注我
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,047评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,807评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,501评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,839评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,951评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,117评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,188评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,929评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,372评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,679评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,837评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,536评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,168评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,886评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,129评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,665评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,739评论 2 351

推荐阅读更多精彩内容