Elastic search source code analysis - source code construction

Keywords: Gradle ElasticSearch git JDK

This article introduces how to build Elasticsearch from source code. Building Elasticsearch source code is the basis of learning and studying Elasticsearch source code, which helps to better understand Elasticsearch.

Environmental preparation

Environment / software Edition Remarks
OS Ubuntu 14.04 LTS
Gradle 5.4
Java 9.0.4+11 Oracle Corporation 9.0.4 [OpenJDK 64-Bit Server VM 9.0.4+11]
Elasticsearch 6.2

To build elastic search from source code, you need to pay attention to the following issues:

1. To build elastic search from source code, you need to use Gradle Therefore, you need to confirm whether the gradle is installed. Please refer to Official website installation documents The installation steps are as follows:

mkdir /opt/gradle
unzip -d /opt/gradle ./download/gradle-5.4-bin.zip
ls /opt/gradle/gradle-5.4
#Add environment variable
vi ~/.bashrc
#Add this sentence under the ~ /. bashrc file
export PATH=$PATH:/opt/gradle/gradle-5.4/bin
#Make new environment variables take effect immediately
source ~/.bashrc
#Check whether gradle is installed and configured successfully
gradle -v

2. The version of JDK required by elastic search compilation and Runtime is different. Take V6.2 as an example, the Runtime requires a minimum of JDK8 and the Compile requires a minimum of JDK9. How should different versions of elastic search determine the required JDK Runtime and compiled version? You can find it in the source code of Elasticsearch, as follows:

/*
 * Elasticsearch <= v6.3
*/
//Code file location: buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy
static final JavaVersion minimumRuntimeVersion = JavaVersion.VERSION_1_8
static final JavaVersion minimumCompilerVersion = JavaVersion.VERSION_1_9
/*
 * Elasticsearch >= v6.4
*/
//Code file location: buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy
//The configuration files corresponding to this code are as follows:
//buildSrc/src/main/resources/minimumCompilerVersion
//buildSrc/src/main/resources/minimumRuntimeVersion
JavaVersion minimumRuntimeVersion = JavaVersion.toVersion(
        BuildPlugin.class.getClassLoader().getResourceAsStream("minimumRuntimeVersion").text.trim()
)
JavaVersion minimumCompilerVersion = JavaVersion.toVersion(
        BuildPlugin.class.getClassLoader().getResourceAsStream("minimumCompilerVersion").text.trim()
)

3. It is recommended to change the terminal to bash before the terminal performs the build operation, otherwise there may be some problems.

Start building

The specific steps are as follows:

#Download source code
git clone https://github.com/elastic/elasticsearch.git
#Enter source directory
cd elasticsearch
#Switch to a stable branch
git checkout 6.2
#Building source code
./gradlew assemble

See the following output to show that the build was successful.

BUILD SUCCESSFUL in 10m 15s
505 actionable tasks: 505 executed

test

After the construction is successful, you can start Elasticsearch as follows:

#Execute in the source directory
./gradlew run

After successful startup, the browser opens 127.0.0.1:9200 as follows:

{
  "name" : "node-0",
  "cluster_name" : "distribution_run",
  "cluster_uuid" : "E3qa7TIkTTGNP32WizSyXg",
  "version" : {
    "number" : "6.2.5",
    "build_hash" : "e38fe8a",
    "build_date" : "2019-04-25T01:27:03.655047Z",
    "build_snapshot" : true,
    "lucene_version" : "7.2.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

Welcome to WeChat public's "big data technology and AI", focusing on big data and AI technology sharing.

Posted by Magicman0022 on Tue, 05 Nov 2019 11:51:35 -0800