1. 程式人生 > >Elasticsearch 編譯除錯總結

Elasticsearch 編譯除錯總結

前言

想深入理解 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 又會編譯一遍,需要等一會,好了之後如下:

IDEA匯入Elasticsearch6.3.2之後

執行,開始 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 加入如下配置:

Edit Configurations

-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 configured
    at 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 的配置最終如下:

image

再次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/ExtendedPluginsClassLoader
    at 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.ExtendedPluginsClassLoader
    at 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 打上勾即可解決,很簡單吧!!

image

繼續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";

效果如下:

java.policy

createClassLoader

再RUN,這次可終於執行起來了!!!

來看一下效果,瀏覽器訪問 http://localhost:9200/

image1

瀏覽器訪問 http://localhost:9200/_cat/health?v

image

一切正常,終於可以愉快的 DEBUG 原始碼啦!!!

另一種原始碼除錯方式:遠端除錯

如果上面第五個報錯之後解決不了無法繼續進行,可以選擇這種方式:

在 Elasticsearch 原始碼目錄下開啟 CMD,輸入下面的命令啟動一個 debug 例項

gradlew run --debug-jvm

如果啟動失敗可能需要先執行 gradlew clean 再 gradlew run --debug-jvm 或者 先退出 IDEA

image

在 IDEA 中開啟 Edit Configurations,新增 remote

image

配置 host 和 port

image

點選 debug,瀏覽器訪問 http://localhost:9200/,即可看到ES返回的資訊

隨機除錯一下, 開啟 elasticsearch/server/src/main/org/elasticsearch/rest/action/cat下的 RestHealthAction 類,在第 54 行出設定一個斷點,然後瀏覽器訪問 http://localhost:9200/_cat/health,可以看到斷點已經捕獲到該請求了

image

執行成功,可以開始設定斷點進行其他除錯

其他可能遇到的問題

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

相關推薦

Elasticsearch 編譯除錯總結

前言 想深入理解 Elasticsearch,閱讀它的原始碼是很有必要的,一來可以瞭解它內部的具體實現,有助於調優,二來可以瞭解優秀開源專案的程式碼架構,提高我們的程式碼架構能力等 閱讀Elasticsearch原始碼的第一步是搭建除錯環境,然後作者在這個過程中遇到很多麻

教你編譯除錯Elasticsearch 6.3.2原始碼

前言 想深入理解 Elasticsearch,閱讀它的原始碼是很有必要的,一來可以瞭解它內部的具體實現,有助於調優,二來可以瞭解優秀開源專案的程式碼架構,提高我們的程式碼架構能力等 閱讀Elasticsearch原始碼的第一步是搭建除錯環境,然後作者在這個過

STM32學習的總結編譯除錯錯誤

 由於學習STM32也不久,經常遇到一些編譯除錯的錯誤,但是老是找不到原因,多虧身邊大俠的幫忙才得以解決。因此,將之前遇到的一些錯誤進行總結,方便以後自己檢視,也希望能滿足某些有需要的同行的需求。如果大家也有相關的經驗,希望能寫出來供大家分享。在此先謝謝啦!      一、

交叉編譯知識點總結

搜索 參考 install pri 庫文件 sta rar nsis blog 參考鏈接: http://www.cppblog.com/runsisi/archive/2012/10/08/193027.html gcc 搜索路徑: 1、 gcc -print-

elasticsearch簡單JavaAPI總結

elasticsearch基於員工信息的CRUD操作/** * 員工增刪改查的應用程序 * * @author Administrator * */ public class EmployeeCRUDApp { @SuppressWarnings({ "unchecked"

編譯原理總結

研究 yacc 語義檢查 裏的 思維 概念 and 靜態 後來 Decaf編譯器(mind語言) PL/0編譯器 構造工具:lex yacc 單遍編譯器 類p-code虛擬機 學編譯是學的相關的思想和研究,科研和教學還是有不同目的的 抽象語法樹AST 三地址碼TA

hadoop原始碼編譯方法,以及編譯問題總結

一、編譯所需的一些軟體 1.jdk1.7 2.maven 3.3.9 3.findbugs 3.0.1 4.protocolBuffer 2.5.0 5.cmake 2.6 6.zlib-devel 7.openssl-devel 8.gcc-c++ 9.autoco

mac vscode c++自動編譯除錯執行

##VScode拓展包 ##新建cpp檔案 ##配置編譯檔案tasks.json 快捷鍵“⇧⌘B” ###配置內容如下 其中注意label,與之後的自動除錯執行launch.json的preLaunchTask有關 { // See https://go.micr

編譯問題總結記錄

今天專案中遇到一個編譯問題: Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal

編譯ROCKSDB總結

Rocksdb是挺好的一個東西,就是取得一個可用的庫太麻煩。之前我是用的rocksdbsharp裡面他有編譯好windows 和 linux的庫 兼 容性還挺好,ubuntu win10 直接跑沒毛病,可惜他是去年build的了,我要用的checkpoint 這麼重要的功能,他這個版本沒有。於是乎只好自行編

spark-2.2.0-bin-2.6.0-cdh5.12.1.tgz 編譯方法總結

菜雞一隻,如果有說錯的地方,還請見諒和指出批評!! 事情是這樣的,想在自己本地部署一個hadoop2.6.0-cdh5.12.1,hive1.1.0-cdh5.12.1,spark-2.2.0-bin-2.6.0-cdh5.12.1的環境,前兩個還是很好找到哪裡下載和部署的! c

PIC微控制器除錯總結

使用程式設計環境 :MPLAB X IDE V3.40 微控制器 :p33FJ64GS606 模擬器 :pickit3 問題       :斷點無法解析為有效的程式儲存器地址,及模擬無法          

android 編譯錯誤總結--使用ButterKnife

1.Android Studio使用ButterKnife各種錯誤解決Plugin with id 'android-apt' not found.   ButterKnife可以幫助我們查詢控制元件,新增事件繫結等等,可以減少很多程式碼,但是在使用過程中對新手來說,往往會碰到很多

強 用VC++6 0編譯除錯彙編程式

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

ffmpeg庫在VS2017中的編譯除錯

最近開始學習ffmpeg,其中參考了ffmpeg視訊教程連結:https://pan.baidu.com/s/1Z5M1erSc75TaSyAWjeN18g 密碼:cttq,以及ffmpeg開發庫在vs環境編譯執行的部落格文章:http://blog.yundiantech.

Linux編譯除錯相關

一、gcc編譯器選項                  告警處理選項待補充 二、gdb常用命令              

GDB除錯總結和例項

1)啟動gdb gdb hello 或者 gdb file hello 2)顯示程式 list 或者 l 3)打斷點 break(b) main() //函式的入口處 break(b) 11 //行號 break(b) hello.c:14 //

VSCode Windows下VSCode編譯除錯c/c++更新launch和tasks 2018.03.27

新的launch.json // Available variables which can be used inside of strings. // ${workspaceRoot}: the root folder of the team // ${f

電池驅動除錯總結,電池服務+電量計驅動+除錯方法

1.概述: Android電池服務,用來監聽核心上報的電池事件,並將最新的電池資料上報給系統,系統收到新資料後會去更新電池顯示狀態、剩餘電量等資訊。如果收到過溫報警和低電報警,系統會自動觸發關機流程,保護電池和機器不受到危害。 Android電池服務的啟動和執行流程: And

win7 編譯除錯x265

首先裝Tortoisehg和cmake,然後通過命令hg clone http://hg.videolan.org/x265克隆程式碼。 進入x265/build資料夾下選擇自己VS版本對應的,我是VS2015,要選vc12-x86. cmake時要選擇對應的版本: 這裡一定要選2