elasticsearch通过guice注入Node组装启动过程

目录
  • elasticsearch启动过程
  • 首先看一下node的初始化
  • 启动各个模块的过程
  • 插件的加载过程

elasticsearch启动过程

elasticsearch的启动过程是根据配置和环境组装需要的模块并启动的过程。这一过程就是通过guice注入各个功能模块并启动这些模块,从而得到一个功能完整的node。正如之前所说elasticsearch的模块化特点,它的各个功能都是独立实现,然后实现通过guice对外提供。

首先简单的说一下guice,它是google的一个轻量级依赖注入框架。它的作者也是依赖注入标准(JSR-330)制定者。虽然它是轻量级框架,但是它的功能及性能却非常好,这也是elasticsearch选用它作为代码主要框架的一个主要原因。它的使用请参考它的用户指导。

首先看一下node的初始化

node接口的唯一实现是InternalNode,它的初始化代码如下所示:

public InternalNode(Settings preparedSettings, boolean loadConfigSettings) throws ElasticsearchException {
        final Settings pSettings = settingsBuilder().put(preparedSettings)
                .put(Client.CLIENT_TYPE_SETTING, CLIENT_TYPE).build();
        Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettings(pSettings, loadConfigSettings);
        tuple = new Tuple<>(TribeService.processSettings(tuple.v1()), tuple.v2());
        logger.info("initializing ...");
       //启动插件service,加载并启动插件
        this.pluginsService = new PluginsService(tuple.v1(), tuple.v2());
        this.settings = pluginsService.updatedSettings();
        // create the environment based on the finalized (processed) view of the settings
        this.environment = new Environment(this.settings());
        CompressorFactory.configure(settings);
       //配置node环境
        final NodeEnvironment nodeEnvironment;
        try {
            nodeEnvironment = new NodeEnvironment(this.settings, this.environment);
        } catch (IOException ex) {
            throw new ElasticsearchIllegalStateException("Failed to created node environment", ex);
        }
        boolean success = false;
        try {
       //加载各个模块
            ModulesBuilder modules = new ModulesBuilder();
            modules.add(new Version.Module(version));
            modules.add(new CacheRecyclerModule(settings));
            modules.add(new PageCacheRecyclerModule(settings));
            modules.add(new CircuitBreakerModule(settings));
            modules.add(new BigArraysModule(settings));
            modules.add(new PluginsModule(settings, pluginsService));
            modules.add(new SettingsModule(settings));
            modules.add(new NodeModule(this));
            modules.add(new NetworkModule());
            modules.add(new ScriptModule(settings));
            modules.add(new EnvironmentModule(environment));
……
}

可以看到node的初始化过程主要包括三部分

第一是启动插件服务(es提供了插件功能来进行扩展功能,这也是它的一个亮点),加载需要的插件

第二是配置node环境

最后就是通过guice加载各个模块。

启动各个模块的过程

插件服务稍微复杂最后再说。elasticsearch各个功能模块实现的非常好,解耦非常完美,很多模块都实现了生命周期接口,只有启动它才能够对外提供服务,它的启动过程也是功能模块初始化的过程。因此,node节点的启动过程也就是这些模块初始化的过程。如下面的代码片段所示:

public Node start() {
        if (!lifecycle.moveToStarted()) {
            return this;
        }// hack around dependency injection problem (for now...)
        injector.getInstance(Discovery.class).setAllocationService(injector.getInstance(AllocationService.class));
        for (Class<? extends LifecycleComponent> plugin : pluginsService.services()) {
            injector.getInstance(plugin).start();
        }
    //通过guice获取各个模块的service接口并启动
        injector.getInstance(MappingUpdatedAction.class).start();
        injector.getInstance(IndicesService.class).start();
        injector.getInstance(IndexingMemoryController.class).start();
        injector.getInstance(IndicesClusterStateService.class).start();
        injector.getInstance(IndicesTTLService.class).start();
        injector.getInstance(RiversManager.class).start();
        injector.getInstance(SnapshotsService.class).start();
        injector.getInstance(TransportService.class).start();
……
}

每个模块service都会实现一个start接口,模块功能初始化过程都是这这个函数中实现。每个模块的具体启动过程后面会有涉及,这里就不做深入分析。

插件的加载过程

每个node在加载各个模块前,会首先加载所需要的插件,如果有些插件加载不成功node会启动失败。这里会加载三类插件:首先是配置插件,配置到节点配置文件中插件,如分词插件等;然后查找classpath中能找到的插件,这些插件一般防止在plugin文件夹中;最后是加载site插件,site插件是不涉及java代码的纯网站式插件,如监控插件head,bigdesk等。任何使用者都可以开发自己需要的插件,只要按着elasticsearch相关版本的插件开发规范来实现即可。

public PluginsService(Settings settings, Environment environment) {
        super(settings);
        this.environment = environment;
        ……
        ImmutableList.Builder<Tuple<PluginInfo, Plugin>> tupleBuilder = ImmutableList.builder();
        // 加载配置插件
        String[] defaultPluginsClasses = settings.getAsArray("plugin.types");
        for (String pluginClass : defaultPluginsClasses) {
            Plugin plugin = loadPlugin(pluginClass, settings);
            PluginInfo pluginInfo = new PluginInfo(plugin.name(), plugin.description(), hasSite(plugin.name()), true, PluginInfo.VERSION_NOT_AVAILABLE);
            if (logger.isTraceEnabled()) {
                logger.trace("plugin loaded from settings [{}]", pluginInfo);
            }
            tupleBuilder.add(new Tuple<>(pluginInfo, plugin));
        }
        // 查找classpatch中的所有插件
        loadPluginsIntoClassLoader();
        if (loadClasspathPlugins) {
            tupleBuilder.addAll(loadPluginsFromClasspath(settings));
        }
        this.plugins = tupleBuilder.build();
        // We need to build a List of jvm and site plugins for checking mandatory plugins
        Map<String, Plugin> jvmPlugins = Maps.newHashMap();
        List<String> sitePlugins = Lists.newArrayList();
        for (Tuple<PluginInfo, Plugin> tuple : this.plugins) {
            jvmPlugins.put(tuple.v2().name(), tuple.v2());
            if (tuple.v1().isSite()) {
                sitePlugins.add(tuple.v1().getName());
            }
        }
        // 加载site插件
        ImmutableList<Tuple<PluginInfo, Plugin>> tuples = loadSitePlugins();
        for (Tuple<PluginInfo, Plugin> tuple : tuples) {
            sitePlugins.add(tuple.v1().getName());
        }
        // 检验代理插件,如果有加载不成功的插件就抛出异常,停止node启动。
        String[] mandatoryPlugins = settings.getAsArray("plugin.mandatory", null);
        if (mandatoryPlugins != null) {
            Set<String> missingPlugins = Sets.newHashSet();
            for (String mandatoryPlugin : mandatoryPlugins) {
                if (!jvmPlugins.containsKey(mandatoryPlugin) && !sitePlugins.contains(mandatoryPlugin) && !missingPlugins.contains(mandatoryPlugin)) {
                    missingPlugins.add(mandatoryPlugin);
                }
            }
            if (!missingPlugins.isEmpty()) {
                throw new ElasticsearchException("Missing mandatory plugins [" + Strings.collectionToDelimitedString(missingPlugins, ", ") + "]");
            }
        }
……
}

以上就是pluginservice的初始化过程,在它的初始化过程中,加载所有能够找的插件。插件的启动在其他功能模块之前,些插件,如分词插件,启动不成功则会对节点的功能造成影响,因此会在这里就中断节点的启动过程让用户知道。这就是节点的启动过程。插件加载成功,各个功能模块加载并启动成功,节点就启动成功。

限于篇幅这里模块相关详情都没有进行深入在接下来模块分析中会一一分析,希望大家以后多多支持我们!

(0)

相关推荐

  • elasticsearch集群cluster主要功能详细分析

    在源码概述中我们分析过,elasticsearch源码从功能上可以分为分布式功能和数据功能,接下来这几篇会就分布式功能展开.这里首先会对cluster作简单概述,然后对cluster所涉及的主要功能详细分析. elasticsearch的集群功能代码在cluster包中,通过ClusterService接口对外暴露. cluster主要包括以下功能: 发现(Discovery),路由(routing),传送功能(transport),集群状态(clusterstates)等. 发现功能功能主要用

  • elasticsearch节点间通信的基础transport启动过程

    目录 前言 transport 启动serverBootStrap 如何连接到node 连接方法的代码 总结 前言 在前一篇中我们分析了cluster的一些元素.接下来的章节会对cluster的运作机制做详细分析.本节先分析一些transport,它是cluster间通信的基础.它有两种实现,一种是基于netty实现nettytransport,主要用于节点间的通信.另一种是localtransport,主要是用于同一个jvm上的节点通信.因为是同一个jvm上的网络模拟,localtranspo

  • elasticsearch节点的transport请求发送处理分析

    目录 transport请求的发送和处理过程 request的发送过程 request的接受过程 request和response是如何被处理 request的处理 response的处理过程 最后总结 transport请求的发送和处理过程 前一篇分析对nettytransport的启动及连接,本篇主要分析transport请求的发送和处理过程. cluster中各个节点之间需要相互发送很多信息,如master检测其它节点是否存在,node节点定期检测master节点是否存储,cluster状

  • elasticsearch集群cluster示例详解

    目录 前言 节点DiscoveryNode 集群阻塞 clusterService接口 总结 前言 上一篇通过clusterservice对cluster做了一个简单的概述, 应该能够给大家一个初步认识.本篇将对cluster的代码组成进行详细分析,力求能够对cluster做一个更清晰的描述.cluster作为多个节点的协同工作机制,它需要节点,节点间通信,各个节点的状态及各个节点上的数据(index)状态.因此这一部分代码包括了上述的几个部分. 节点DiscoveryNode 首先是节点(Di

  • elasticsearch分布式及数据的功能源码分析

    从功能上说,可以分为两部分,分布式功能和数据功能.分布式功能主要是节点集群及集群附属功能如restful借口.集群性能检测功能等,数据功能主要是索引和搜索.代码上这些功能并不是完全独立,而是由相互交叉部分.当然分布式功能是为数据功能服务,数据功能肯定也难以完全独立于分布式功能. 它的源码有以下几个特点: 模块化: 每个功能都以模块化的方式实现,最后以一个借口向外暴露,最终通过guice(google轻量级DI框架)进行管理.整个系统有30多个模块(version1.5). 接口解耦: es代码中

  • elasticsearch通过guice注入Node组装启动过程

    目录 elasticsearch启动过程 首先看一下node的初始化 启动各个模块的过程 插件的加载过程 elasticsearch启动过程 elasticsearch的启动过程是根据配置和环境组装需要的模块并启动的过程.这一过程就是通过guice注入各个功能模块并启动这些模块,从而得到一个功能完整的node.正如之前所说elasticsearch的模块化特点,它的各个功能都是独立实现,然后实现通过guice对外提供. 首先简单的说一下guice,它是google的一个轻量级依赖注入框架.它的作

  • Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析

    在前面一篇文章Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路中,介绍了在Android系统中Binder进程间通信机制中的Server角色是如何获得Service Manager远程接口的,即defaultServiceManager函数的实现.Server获得了Service Manager远程接口之后,就要把自己的Service添加到Service Manager中去,然后把自己启动起来,等待Client的请求.

  • 利用forever和pm2部署node.js项目过程

    本文主要给大家分享的是利用forever和pm2部署node.js项目过程的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: 一.forever forever可以在cmd或ssh连接断开时,让项目一直运行,而且可以在项目崩溃时自动重启 安装 npm install -g forever forever的帮助手册 forever --help 使用forever启动项目 forever start app.js 使用forever停止项目 forever stop app.js 列出所有

  • Spring Boot启动过程完全解析(二)

    上篇给大家介绍了Spring Boot启动过程完全解析(一),大家可以点击参考下 该说refreshContext(context)了,首先是判断context是否是AbstractApplicationContext派生类的实例,之后调用了强转为AbstractApplicationContext类型并调用它的refresh方法.由于AnnotationConfigEmbeddedWebApplicationContext继承自EmbeddedWebApplicationContext,所以会

  • Spring Boot启动过程完全解析(一)

    之前在排查一个线上问题时,不得不仔细跑了很多遍Spring Boot的代码,于是整理一下,我用的是1.4.3.RELEASE. 首先,普通的入口,这没什么好说的,我就随便贴贴代码了: SpringApplication.run(Application.class, args); --> public static ConfigurableApplicationContext run(Object source, String... args) { return run(new Object[]

  • Spring Bean的实例化之属性注入源码剖析过程

    前言 这一章节我们来讨论创建Bean过程中的属性注入,在Spring的IOC容器启动过程中,会把定义的Bean封装成BeanDefinition注册到一个ConcurrentHashMap中,Bean注册完成后,就会对单利的且lazy-init=false 的Bean进行实例化.创建Bean的代码在 AbstractAutowireCapableBeanFactory#doCreateBean 中,当Bean创建成功之后,会调用AbstractAutowireCapableBeanFactory

  • springboot启动过程中常用的回调示例详解

    目录 1.介绍 2.常用的拓展接口 3.实例: 4.其他拓展接口: 1.介绍 springboot提供非常丰富回调接口,利用这些接口可以做非常多的事情,对于一些常用的回调接口进行介绍 2.常用的拓展接口 1.ApplicationContextInitializer2.ApplicationListener3.ApplicationRunner4.CommandLineRunner 3.实例: 1.ApplicationContextInitializer接口是在spring容器初初始化之前调用

  • Spring Boot启动过程(六)之内嵌Tomcat中StandardHost、StandardContext和StandardWrapper的启动教程详解

    StandardEngine[Tomcat].StandardHost[localhost]的启动与StandardEngine不在同一个线程中,它的start: // Start our child containers, if any Container children[] = findChildren(); List<Future<Void>> results = new ArrayList<>(); for (int i = 0; i < childre

  • mysql的启动过程详解

    有一天,两个不懂mysql内核的人想去了解mysql内核代码,两个人不是去调试代码.查找资料,而是在那边思考.因为不了解内核,所以边思考边去验证.   使用的mysql代码是5.1.7,调试环境是windows平台下的vs2003.   Bingxi:"alex,你觉得mysql的启动过程会是什么样的呢?我们以银行为例吧." Alex:"嗯,bingxi.早上银行开门了,会先准备好环境,然后开门迎客,mysql也是这样.Mysql里面会有一个handle_connection

随机推荐