从0到1之搭建Elasticsearch(7.10.2)源码debug环境
1
背景
2
软硬件环境
3
软件安装
jdk
https://www.oracle.com/java/technologies/downloads/,如图3-1
图3-1 jdk
vim /etc/profile
alias ll='ls -Alh'
export JAVA_8_HOME=`/usr/libexec/java_home -v 1.8`
export JAVA_11_HOME=`/usr/libexec/java_home -v 11`
export JAVA_17_HOME=`/usr/libexec/java_home -v 17`
export JAVA_HOME=$JAVA_8_HOME
#alias命令动态切换JDK版本
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk11="export JAVA_HOME=$JAVA_11_HOME"
alias jdk17="export JAVA_HOME=$JAVA_17_HOME"
maven
安装maven,maven的作用主要是帮助管理java项目的jar依赖。 在终端执行如下命令
//下载
wget https://dlcdn.apache.org/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz
//解压
tar -zxf apache-maven-3.8.5-bin.tar.gz
cd apache-maven-3.8.5/conf
<!--将其从注释中复制出来,取消掉其注释,目录修改为自己本机自定义的一个目录即可-->
<localRepository>/path/to/local/repo</localRepository>
<mirrors>
<!--为了加快jar的下载速度,可以配置一些国内的镜像仓库-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
gradle在es6版本之后,可以不用提前安装,Elasticsearch的源码包自带。
4
源码下载与运行
//https clone 地址
https://github.com/elastic/elasticsearch.git
//ssh clone地址
[email protected]:elastic/elasticsearch.git
yangsongbai@yangsongbai conf % cd ~/.ssh
yangsongbai@yangsongbai .ssh % ll
total 40
-rw-r--r-- 1 yangsongbai staff 260B 3 25 17:35 config
-rw------- 1 yangsongbai staff 1.6K 3 25 17:36 known_hosts
-rw------- 1 yangsongbai staff 1.1K 3 25 17:36 known_hosts.old
-rw------- 1 yangsongbai staff 3.3K 3 17 23:20 yangsongbai_rsa
-rw-r--r-- 1 yangsongbai staff 750B 3 17 23:20 yangsongbai_rsa.pub
yangsongbai@yangsongbai .ssh % cat config
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/yangsongbai_rsa
//https clone 地址
git clone https://github.com/elastic/elasticsearch.git
//或者 ssh clone地址
git clone [email protected]:elastic/elasticsearch.git
5
本地运行代码
导入IDEA
//切换到指定tag上
git checkout v7.10.2
//基于v7.10.2的tag创建自己的分支
git checkout -b 7.10.2-reader v7.10.2
//切换到自己的分支
git checkout 7.10.2-reader
图 5-1
图5-2
本地debug
mkdir ~/eshome
yangsongbai@yangsongbai eshome % ll
total 0
drwxr-xr-x 12 yangsongbai staff 384B 3 27 10:41 config
drwxr-xr-x@ 53 yangsongbai staff 1.7K 3 24 13:42 modules
drwxr-xr-x@ 2 yangsongbai staff 64B 3 24 13:42 plugins
以上三个目录可以去官方网站下载对应发行版的es或者在自己clone下来的elasticsearch项目下执行./gradlew assemble命令(注意:自己构建打包的安装包,在distribution下的archives和packages目录下),然后解压将如上的三个目录复制到eshome下。
为了获取jdk的一些权限,需要在config目录下创建elasticsearch.policy文件,文件内容如下
grant{
permission javax.management.MBeanTruxtPermission "register";
permission javax.management.MBeanServerPermission "createMBeanServer";
permission java.lang.RuntimePermission "createClassLoader";
permission java.net.SocketPermission "*:*","connect,resolve";
permission java.lang.RuntimePermission "getClassLoader";
permission java.lang.RuntimePermission "setContextClassLoader";
} ;
cluster.name: es_local_on_mac
gateway.write_dangling_indices_info: false
#network.host: 0.0.0.0
discovery.seed_hosts: ["127.0.0.1:9300"]
cluster.initial_master_nodes: ["master-1"]
node.data: true
node.master: true
node.name: master-1
http.port: 9200
transport.tcp.port: 9300
如图5-3所示,其中图中1处 表示使用的jdk,图中2处 表示运行的es模块elasticsearch.server.main,图中3处表示VM options,图中4处表示主类,5 表示将依赖scope为provided的jar加入进classPath(有的idea,直接是个复选框,有的idea是在途中6处选中)
其中比较重点的是图中3处VM options,其详细配置如下(使用时将注释去掉)
//es运行时的家目录
-Des.path.home=/Users/yangsongbai1/apps/eshome
//配置文件的目录
-Des.path.conf=/Users/yangsongbai1/apps/eshome/config
//这个是jvm.options文件的默认配置,不加会报access denied等错,
//idea启动的时候不会去去加载jvm.options
-Dlog4j.disable.jmx=true
//权限问题(比如呀运行时创建类加载器等权限问题),需要配置
-Djava.security.policy=/Users/yangsongbai1/apps/eshome/config/elasticsearch.policy
//配置运行es节点堆内存
-Xms1g
-Xmx1g
远程debug
除了在本地debug外,我们还可以通过远程debug的方式去学习源代码。 假如我们在远端服务器或者本地部署了三个节点的集群,那么我们可以分别在每个节点的vm.options追加如下配置,方便我们进行远程debug
远程debug
#node1
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=2220
#node2
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=2221
#node3
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=2222
图5-4
本地集群
在本地搭建一个集群,其中两个节点为官网的发行版,另外一个节点为本地用源码启动的节点,并且加入集群。
到官方网站下载elasticsearch-7.10.2-darwin-x86_64.tar.gz;具体操作命令如下:
//解压文件
tar -zxf elasticsearch-7.10.2-darwin-x86_64.tar.gz
//创建master节点目录
mkdir master
//创建data1节点目录
mkdir data1
//将发行版解压内容,复制到master
cp -rf elasticsearch-7.10.2/* master/
//将发行版解压内容,复制到data1
cp -rf elasticsearch-7.10.2/* master/
完成以上命令后内容如下
yangsongbai@yangsongbai originate % ll
total 595568
drwxr-xr-x 13 yangsongbai staff 416B 3 26 10:33 data1
drwxr-xr-x@ 12 yangsongbai staff 384B 1 13 2021 elasticsearch-7.10.2
-rw-r--r--@ 1 yangsongbai staff 291M 3 22 12:27 elasticsearch-7.10.2-darwin-x86_64.tar.gz
drwxr-xr-x 13 yangsongbai staff 416B 3 26 10:31 master
cluster.name: es_orig_local_on_mac
gateway.write_dangling_indices_info: false
discovery.seed_hosts: ["127.0.0.1:9300"]
cluster.initial_master_nodes: ["master-1"]
node.data: false
node.master: true
node.name: master-1
http.port: 9200
transport.tcp.port: 9300
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=2220
cluster.name: es_orig_local_on_mac
gateway.write_dangling_indices_info: false
discovery.seed_hosts: ["127.0.0.1:9300"]
cluster.initial_master_nodes: ["master-1"]
node.data: true
node.master: false
node.name: data-1
http.port: 9201
transport.tcp.port: 9301
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=2221
./master/bin/elasticsearch &
./data1/bin/elasticsearch &
cluster.name: es_orig_local_on_mac
gateway.write_dangling_indices_info: false
discovery.seed_hosts: ["127.0.0.1:9300"]
cluster.initial_master_nodes: ["master-1"]
node.data: true
node.master: false
node.name: data-2
http.port: 9202
transport.tcp.port: 9302
测试 restfulApi
测试rest-api-spec的命令如下
./gradlew :rest-api-spec:yamlRestTest
图5-5 restApi测试
./gradlew ':rest-api-spec:yamlRestTest' \
--tests "org.elasticsearch.test.rest.ClientYamlTestSuiteIT" \
-Dtests.method="test {p0=cat.segments/10_basic/Help}"
yangsongbai@yangsonbai elasticsearch-7.10.2 % ./gradlew help --task :rest-api-spec:yamlRestTest
=======================================
Elasticsearch Build Hamster says Hello!
Gradle Version : 6.6.1
OS Info : Mac OS X 12.1 (x86_64)
JDK Version : 11 (Oracle JDK)
JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk-11.0.14.jdk/Contents/Home
Random Testing Seed : 3A61F74191DEDB20
In FIPS 140 mode : false
=======================================
> Task :help
Detailed task information for :rest-api-spec:yamlRestTest
Path
:rest-api-spec:yamlRestTest
Type
RestIntegTestTask (org.elasticsearch.gradle.test.RestIntegTestTask)
Options
--debug-jvm Enable debugging for the test process. The process is started suspended and listening on port 5005.
--fail-fast Stops test execution after the first failed test.
--tests Sets test class or method name to be included, '*' is supported.
Description
Runs the REST tests against an external cluster
Group
verification
https://github.com/elastic/elasticsearch/blob/v7.10.2/TESTING.asciidoc
Elasticsearch