本篇为elasticsearch源码分析系列文章的第十篇,本篇延续上一篇ElasticSearch的Plugin引出的内容,进行各类Plugin中线程池的分析。服务器
上篇讲到了ElasticSearch中插件的基本概念,以及Node实例化中涉及到的PluginService初始化编码,本篇将会继续研究Node实例化的过程当中PluginsService发挥的做用,也就是经过PluginsService中的参数构建线程池框架。框架
当Node完成了PluginsService的构造后,紧接会经过getExecutorBuilders
方法取得线程池的Executor构造器列表,代码以下:elasticsearch
List<ExecutorBuilder<?>> executorBuilders = pluginsService.getExecutorBuilders(settings)
复制代码
此时PluginsService对象中已经有了须要加载的全部plugin了,包含modules
路径和plugins
路径中的全部组件,这里统称为plugin。以下图所示总共是包含了13个已加载的Plugin,分别是modules路径中的默认必须加载的12个和Plugins路径中的自定义安装的1个(ICU分词器)。以下图所示源码分析
Node实例化过程当中,经过代码:fetch
List<ExecutorBuilder<?>> executorBuilders = pluginsService.getExecutorBuilders(settings);
复制代码
查找到自定义的线程池Executor构建器。再得到自定义线程池构建器集合后,开始构建线程池(ThreadPool)。优化
ThreadPool threadPool = new ThreadPool(settings, executorBuilders.toArray(new ExecutorBuilder[0]));
复制代码
首先经过代码得到处理器CPU的数量,ui
Runtime.getRuntime().availableProcessors()
复制代码
固然这个值是能够被Setting中设置的变量processors来覆盖的。这个变量在代码中被标记为availableProcessors。而后建立变量编码
这两个变量在后面建立各类线程池构造器中反复用到。spa
在肯定了可以使用的处理器数量后,就能肯定线程池的最小值(genericThreadPoolMax),ElasticSearch中是肯定为:可用CPU处理器数量的4倍,且固定范围为最小128,最大为512。插件
因而可知若是用通常服务器的话,线程池上限最终会被肯定为128,能够说仍是比较高的设定了。
接下来开始构造执行不一样操做时线程池Executor,ElasticSearch中把各个操做的Executor构造为Map,Map<String, ExecutorBuilder>
,下面是各个Executor对象的解释:
普通操做的Executor:构建一个可伸缩的Executor构建器,value为ScalingExecutorBuilder对象。接收参数和对应操做以下:
thread_pool.generic.core
的设为这个值。thread_pool.generic.keep_alive
索引操做的Executor:构建一个固定的Executor构建器。key为index
,value为FixedExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.index.size
的值为该参数中cpu的数量thread_pool.index.size
的值为size的值,本机跑的结果是4。thread_pool.index.queue_size
的值为200,注意这个值固定为200。批处理操做的Executor:构建一个固定的Executor构建器。key为bulk
,value为FixedExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.bulk.size
的值为该参数中cpu的数量thread_pool.bulk.size
的值为size的值,本机跑的结果是4。thread_pool.bulk.queue_size
的值为200,注意这个值固定为200。get操做的Executor:构建一个固定的Executor构建器。key为get
,value为FixedExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.get.size
的值为该参数中cpu的数量thread_pool.get.size
的值为size的值,本机跑的结果是4。thread_pool.get.queue_size
的值为1000,注意这个值固定为1000。查询操做的Executor:构建一个根据利特尔法则自动扩展长度的Executor构建器,这个构建器的逻辑与其余构建器不一样,也显得比较复杂,也说明了对于查询操做,ElasticSearch作了特殊的优化。key为search
,value为AutoQueueAdjustingExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.search.size
的值为该参数中cpu的数量thread_pool.search.size
的值为size的值,本机跑的结果是7。thread_pool.search.queue_size
的值为200thread_pool.search.min_queue_size
的值为1000thread_pool.search.max_queue_size
的值为1000thread_pool.search.auto_queue_frame_size
的值为200,注意这个值固定为200。thread_pool.search.target_response_time
针对search操做的相应被设置为1S,管理操做的Executor:构建一个可伸缩的Executor构建器。key为management
,value为ScalingExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.management.size
的值为该参数中cpu的数量thread_pool.management.size
的值为size的值,本机跑的结果是1。thread_pool.management.queue_size
的值为200,注意这个值固定为200。thread_pool.management.keep_alive
。监听操做的Executor:构建一个固定的Executor构建器。key为listener
,value为FixedExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.listener.size
的值为该参数中cpu的数量thread_pool.listener.size
的值为size的值,本机跑的结果是2。thread_pool.listener.queue_size
的值为**-1**,意思就没有阻塞队列。flush操做的Executor:构建一个可伸缩的Executor构建器。key为flush
,value为ScalingExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.flush.size
的值为该参数中cpu的数量thread_pool.flush.size
的值为size的值,本机跑的结果是4。thread_pool.management.keep_alive
。refresh操做的Executor:构建一个可伸缩的Executor构建器。key为refresh
,value为ScalingExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.refresh.size
的值为该参数中cpu的数量thread_pool.refresh.size
的值为size的值,本机跑的结果是4。thread_pool.management.keep_alive
。warmer操做的Executor:构建一个可伸缩的Executor构建器。key为warmer
,value为ScalingExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.warmer.size
的值为该参数中cpu的数量thread_pool.warmer.size
的值为size的值,本机跑的结果是4。thread_pool.management.keep_alive
。snapshot操做的Executor:构建一个可伸缩的Executor构建器。key为snapshot
,value为ScalingExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.snapshot.size
的值为该参数中cpu的数量thread_pool.snapshot.size
的值为size的值,本机跑的结果是4。thread_pool.management.keep_alive
。碎片处理操做的Executor:构建一个可伸缩的Executor构建器。key为fetch_shard_started
,value为ScalingExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.fetch_shard_started.size
的值为该参数中cpu的数量thread_pool.fetch_shard_started.size
的值为size的值,本机跑的结果是4。thread_pool.fetch_shard_started.queue_size
的值为200,注意这个值固定为200。强制merge操做的Executor:构建一个可伸缩的Executor构建器。key为force_merge
,value为ScalingExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.force_merge.size
的值为该参数中cpu的数量thread_pool.force_merge.size
的值为size的值,本机跑的结果是4。thread_pool.force_merge.queue_size
的值为200,注意这个值固定为200。获取碎片操做的Executor:构建一个可伸缩的Executor构建器。key为fetch_shard_store
,value为ScalingExecutorBuilder对象,接收参数和对应操做以下:
thread_pool.fetch_shard_store.size
的值为该参数中cpu的数量thread_pool.fetch_shard_store.size
的值为size的值,本机跑的结果是4。thread_pool.fetch_shard_store.queue_size
的值为200,注意这个值固定为200。至此就完成了org.elasticsearch.threadpool.ThreadPool对象的建立。
获得ThreadPool的对象后,经过线程池进行了NodeClient的构建。
client = new NodeClient(settings, threadPool);
复制代码
和ResourceWatcherService对象的构建,
final ResourceWatcherService resourceWatcherService = new ResourceWatcherService(settings, threadPool);
复制代码
后面还有不少的组件都用到了线程池,好比:
能够看出都是ElasticSearch的核心组件,这些组件的功能和原理,我都会在之后的文章中讲解,而像ElasticSearch这种存储搜索系统来讲IO操做确定很是频繁,而线程池是专门致力于解决系统的IO问题,它在这些服务组件中的做用也显得愈发重要。
查询操做中提到的利特尔法则是一种描述稳定系统中,三个变量之间关系的法则。
其中L表示平均请求数量,λ表示请求的频率,W表示响应请求的平均时间。举例来讲,若是每秒请求数为10次,每一个请求处理时间为1秒,那么在任什么时候刻都有10个请求正在被处理。回到咱们的话题,就是须要使用10个线程来进行处理。若是单个请求的处理时间翻倍,那么处理的线程数也要翻倍,变成20个。
理解了处理时间对于请求处理效率的影响以后,咱们会发现,一般理论上限可能不是线程池大小的最佳值。线程池上限还须要参考任务处理时间。
假设JVM能够并行处理1000个任务,若是每一个请求处理时间不超过30秒,那么在最坏状况下,每秒最多只能处理33.3个请求。然而,若是每一个请求只须要500毫秒,那么应用程序每秒能够处理2000个请求。