This article for ElasticSearch source analysis of the second article series, because the technology is not fine, and the description of the bad or wrong place also please point out ^ _ ^!!

Source code main module

Zip, deb, RPM, tar) for ElasticSearch Execute the publishToMavenLocal Task under the corresponding distribution module. If successful, the corresponding distribution package will be generated under build/ lookup. The wrapped package will run on the production server. As shown in the figure below:

Core: The core package for ElasticSearch

BuildSrc: Build code for ElasticSearch

Elasticsearch (elasticSearch) client (ElasticSearch) client (ElasticSearch)

Modules: Code that is required for ElasticSearch in addition to the core.

Plugins: Code that is required for ElasticSearch. The structure is shown below:

Start the entrance

You can see the startup script for ElasticSearch in SRC /main/resources/bin in the Distribution module mentioned above. As shown in the figure below:

The script first loads the JVM configuration file jvm.options (in the config folder where we downloaded the decompressed distribution)

ES_JVM_OPTIONS=”$ES_PATH_CONF”/jvm.options

We then load the VM parameters that we configured in Run/Debug Configurations.

- 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

Finally start org. Elasticsearch. The bootstrap. Elasticsearch the main method of the main class.

The main method first adds the closing hook,

Then configure the log output,

Then check the three environment parameters of ElasticSearch:

    putSystemPropertyIfSettingIsMissing(settings, "path.data", "es.path.data");
    putSystemPropertyIfSettingIsMissing(settings, "path.home", "es.path.home");
    putSystemPropertyIfSettingIsMissing(settings, "path.logs", "es.path.logs");
Copy the code

All the inspection done, code conversion to * org. Elasticsearch. The bootstrap. The bootstrap * class init () method, and it is the bootstrap class finished elasticsearch started

Bootstrap.init(! daemonize, pidFile, quiet, initialEnv);Copy the code

The Bootstrap class

Let’s look at some of the important methods in Bootstrap:

init
init(final boolean foreground,final Path pidFile,final boolean quiet,final Environment initialEnv) throws BootstrapException, NodeValidationException, UserException
Copy the code

Needless to say, this method does some pre-startup initialization

Parameters,

  • Foreground: Indicates whether ElasticSearch is started as a background daemon.
  • PidFile: Obtained after args is parsed by the Parser, which actually parses the default command line parameters (verbose, E,silent, version, help, quiet, daemonize, pidFile).
  • Quiet: same as above
  • InitialEnv: Environment parameter object instantiated by Environment. It holds parameters such as repoFile, configFile, pluginsFile, binFile, and libFile.

The main work

  • First, a Bootstrap object is instantiated
  • Configure the log output
  • Creating a PID file will persist a file on disk that records the applied PID
  • The foreground and quiet parameters are used to control log output
  • Call the setup and start methods of Bootstrap
setup
setup(boolean addShutdownHook, Environment environment)throws BootstrapException 
Copy the code

The main work

  • Generate a local plug-in controller through the environment

  • Initialize the local resource

  • Initialize the probe before the security manager is installed

  • Adding closing hooks

  • Check for JAR duplication

  • Configure the log output before the security manager is installed

  • Install the security manager

  • Instantiate Node using the environment parameter

start
start() throws NodeValidationException 
Copy the code

The main work

  • Start an instantiated Node
  • Start the keepAliveThread thread, which was instantiated during Bootstrap initialization. This thread creates a CountDownLatch with a count of 1. The purpose of this thread is to add close hooks once the latch is started.

Add a closed hook to the JVM. When the JVM is shut down, it will execute all the hooks that have been added in the system through the addShutdownHook method. When the system finishes executing these hooks, the JVM will shut down. So these hooks can be used to clean up memory, destroy objects, and so on when the JVM is closed.

As you can see, the focus of startup is in the setup method. After startup, it’s up to the Node.

The Node class

Node is instantiated using NodeBuilder, using Google’s Injector framework Guice Injector for injection and instance acquisition. Modularized ElasticSearch components are modularized using the above methods. ModulesBuilder is used to build elasticSearch modules:

The start of a Node is the start of each component in Node. Again, each component is started by calling a different start method, as shown below

^ _ ^!! ^ _ ^!!