This article is the first of the elasticSearch source analysis series of documents, this brief introduction of elasticSearch source code in the local compilation environment

Use the following tools: IntelliJ Idea, JDK1.8, Gradle3.5, ElasticSearch-6.0.0-rc2 distribution

Download to prepare

Download elasticSearch from Gihut. When you import the IDE, you see that many dependencies are missing because you haven’t yet compiled the project using Gradle.

First check elasticsearch source code of CURRENT version, in the class org. Elasticsearch. Version# can see, the CURRENT view to the CURRENT master version 7.0.0-1 release, and there is no this version website releases, the version of the document pages also just set up, So switch the code in the IDE to 6.0.0-rc2, as this is the version of the more recent release that can be downloaded from the official website, as shown below:

After the switch is complete, go to the official website to download the 6.0.0-Rc2 release, which will be used later. The diagram below:

The default compiler for ElasticSearch changed from Maven to Gradle after 5.x. You need to configure your Gradle environment on your local computer and switch between domestic sources if necessary. Compile ElasticSearch using Gradle3.5 and JDK1.8. It is recommended that the Gradle version not be too high. The diagram below:

Begin to compile

If you run Gradle idea in the IDE now (if the IDE is Eclipse, then it is gradle idea), you will be prompted with “Gradle idea must be executed before import”, as shown below:

CMD switch to the core directory and run the Gradle idea command (because the IDE is IDEA, use the Gradle Eclipse command if the IDE is Eclipse). The diagram below:

Run the gradle idea command in the root directory of ElasticSearch. If it builds successfully, it will display the following figure:

After compiling successfully, start the IDE, idea will take a long time to configure Gradle, wait for the configuration is finished, check the project as shown in the following picture, basically you can see the project overview:

Start the path

Elasticsearch’s startup file is ElasticSearch Mentioned in the file path. The conf, search the field found in the distribution package RPM/SRC/main/packaging/init. D/a, this is the place where elasticsearch initialization, path. Some parameters such as the conf also is set here.

According to the startup scripts to find the main method of entrance org. Elasticsearch. The bootstrap. Elasticsearch, this method inherited from EnvironmentAwareCommand class, this class is mainly some of the parameters specified and to boot. EnvironmentAwareCommand in turn inherits from the Command class, as shown below:

The main method of Elasticsearch calls the main method of Command. This method creates the shutdown hook, configures logging, and then executes the mainWithoutErrorHandling method. This method finally calls the Execute method of EnvironmentAwareCommand. The diagram below:

The EnvironmentAwareCommand environment constructor class constructs elasticSearch parameters such as path.data, path.home, and path.logs using the execute method, and then constructs the run configuration using the createEnv method of the class, as shown in figure 1:

Running the main method prompts an error:

Look at the source code to see that es.path.conf is passed as a parameter, as shown below:

Parameter configuration

You need to unpack the elasticSearch distribution you just downloaded. In the Run/Debug Configurations configuration window, configure the VM Options parameter to:

- Des. Path. The conf = D: \ Seymour \ elasticsearch \ elasticsearch - 6.0.0 - rc2 - Des. Path. Home = D: \ Seymour \ elasticsearch \ elasticsearch 6.0.0 - rc2 - Dlog4j2. Disable the JMX = trueCopy the code

D:\idea\workspace\elasticsearch\distribution\ SRC \main\resources is the path to download the unzipped distribution.

Set parameters are passed as parameters in the createEnv method of EnvironmentAwareCommand.

With the above three parameters configured, the local startup of ElasticSearch will read the distribution’s parameters, plug-ins, and modules and run successfully.

Problems encountered during operation

You may encounter problems with Java security policy

Access denied (” javax.mail. Management. MBeanTrustPermission “” register”) solution is to create a new Java policy file elasticsearch. The policy

Content is:

grant{ permission javax.management.MBeanTruxtPermission "register"; permission javax.management.MBeanServerPermission "createMBeanServer"; };Copy the code

Add JVM parameters:

-Djava.security.policy=/User/seymour/workspace/elasticsearch.policy
Copy the code
It is possible to encounter the SNAPSHOT version of the JAR package in the Module

In the org. Elasticsearch. The bootstrap. Security class readPolicy () method, is the snapshot version of code to detect the running, But the elasticsearch-rest-client-6.0.0-rc2.jar in the release we downloaded and unzipped is the release, Change the name of the jar to snapshot version (Elasticsearch-REST-client-6.0.0-rc2-snapshot) and it will work as shown below:

Run successfully

After a successful run, you can use 127.0.0.1:9200 to test, and after a successful test, you can debug the code.