您的位置:首页 > 其它

elasticsearch源码研究之启动

2015-07-13 16:02 183 查看
elasticsearch主要使用juice(一款据说比spring快100倍的对象注入框架,如需详细了解,可以谷歌以下),Guava,netty等等实现的开源框架,下面介绍下es的启动过程,

在启动的过程中,在类中InternalNode注入了很多的模块,同时在各个模块中绑定了各自对应的服务,例如:设置模块绑定了Settings等接口服务,节点模块NodeModule绑定NodeService等服务,下面是比较重要的几个模块:

1.DiscoveryModule:主要涉及到ZenDiscovery服务以及ElectMasterService等,ElectMasterService服务主要做的事情,通过ping操作发现(主)节点,以及节点的join和leave,通过共同的集群名称,共同形成一个集群,以及进行节点的reroute等操作。

2.ClusterModule:主要涉及InternalClusterService服务和各种元数据的相关服务等,其中InternalClusterService服务比较重要,集群状态改变提交更新任务都是在此进行转发异步执行,而且此处线程池用的还是但线程池,即核心线程只有一个,最大线程数也是一个,如下代码所示:

this.updateTasksExecutor = EsExecutors.newSinglePrioritizing(daemonThreadFactory(settings, UPDATE_THREAD_NAME));


public static PrioritizedEsThreadPoolExecutor newSinglePrioritizing(ThreadFactory threadFactory) {
return new PrioritizedEsThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, threadFactory);
}
其中InternalClusterService还注册了各种事件监听,用来发送集群改变事件,来恢复集群等操作,例如将此服务IndicesClusterStateService加入到监听事件中去,最终调用其clusterChanged方法,去进行分片操作

for (ClusterStateListener listener : preAppliedListeners) {
try {
listener.clusterChanged(clusterChangedEvent);
} catch (Exception ex) {
logger.warn("failed to notify ClusterStateListener", ex);
}
}


IndicesClusterStateService类的主要内容如下:

applyNewOrUpdatedShards(event);
applyInitializingShard(routingTable, nodes, indexMetaData, routingTable.index(shardRouting.index()).shard(shardRouting.id()), shardRouting);


3.RestModule:里面又绑定了RestActionModule,此模板非常重要,里面绑定了各种action,例如:RestClusterHealthAction(api:/_cluster/health),其他的同理,见如下代码:

bind(RestClusterHealthAction.class).asEagerSingleton();


controller.registerHandler(RestRequest.Method.GET, "/_cluster/health", this);
controller.registerHandler(RestRequest.Method.GET, "/_cluster/health/{index}", this);


4.GatewayModule:绑定了GatewayService,执行了集群状态恢复的相关操作

performStateRecovery(asyncRecovery, enforceRecoverAfterTime, reason);


5.TransportModule:绑定了TransportService,处理了各种请求的提交和发送,例如:创建索引的请求,将action名称以及相关信息发送出去等;另外一个非常重要的地方是启动netty客户端和服务端

transport(NettyTransport).start();


@Override
protected void doStart() throws ElasticsearchException {
boolean success = false;
try {
clientBootstrap = createClientBootstrap();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: