Elasticsearch节点关闭
2019-06-12 本文已影响0人
Ombres
节点关闭的时机
当Elasticsearch启动时,在Bootstrap.setup()
过程中有这么一段,这里注册了节点关闭执行的钩子。
if (addShutdownHook) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
IOUtils.close(node, spawner);
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configurator.shutdown(context);
} catch (IOException ex) {
throw new ElasticsearchException("failed to stop node", ex);
}
}
});
}
当收到 SIGTERM
或 SIGINT
信号时就会执行节点关闭流程
节点关闭进行的操作
Node.class
实现了Closeable
接口,当IOUtil.close(node, spawner)
调用时就会调用node.close()
在node.close()
中,会对guice
中管理的Service进行关闭。
Elasticsearch的Service都会实现start
和stop
方法,方便在节点启动和关闭时进行调用。start
和stop
方法实际会调用各个Service自己实现的doStart()
和doStop()
接口来进行启动或者关闭。
由于各个Service之间可能存在依赖关系,因此在关闭的时候也应该按照顺序进行关闭,这些服务关闭的顺序与启动的时候相反
injector.getInstance(ResourceWatcherService.class).stop();
injector.getInstance(HttpServerTransport.class).stop();
injector.getInstance(SnapshotsService.class).stop();
injector.getInstance(SnapshotShardsService.class).stop();
// stop any changes happening as a result of cluster state changes
injector.getInstance(IndicesClusterStateService.class).stop();
// close discovery early to not react to pings anymore.
// This can confuse other nodes and delay things - mostly if we're the master and we're running tests.
injector.getInstance(Discovery.class).stop();
// we close indices first, so operations won't be allowed on it
injector.getInstance(RoutingService.class).stop();
injector.getInstance(ClusterService.class).stop();
injector.getInstance(NodeConnectionsService.class).stop();
nodeService.getMonitorService().stop();
injector.getInstance(GatewayService.class).stop();
injector.getInstance(SearchService.class).stop();
injector.getInstance(TransportService.class).stop();
pluginLifecycleComponents.forEach(LifecycleComponent::stop);
// we should stop this last since it waits for resources to get released
// if we had scroll searchers etc or recovery going on we wait for to finish.
injector.getInstance(IndicesService.class).stop();