Elasticsearch线程池介绍

原文地址:http://blog.csdn.net/opensure/article/details/51491815

每个Elasticsearch节点内部都维护着多个线程池,如index、search、get、bulk等,用户可以修改线程池的类型和大小,线程池默认大小跟CPU逻辑一致,本文基于最新的Elasticsearch2.3.x


一、查看当前线程组状态

[html]  view plain  copy
  1. curl -XGET 'http://localhost:9200/_nodes/stats?pretty'  

[html]  view plain  copy
  1. "thread_pool" : {  
  2.     "bulk" : {  
  3.       "threads" : 32,  
  4.       "queue" : 0,  
  5.       "active" : 0,  
  6.       "rejected" : 0,  
  7.       "largest" : 32,  
  8.       "completed" : 659997  
  9.     },  
  10. "index" : {  
  11.       "threads" : 2,  
  12.       "queue" : 0,  
  13.       "active" : 0,  
  14.       "rejected" : 0,  
  15.       "largest" : 2,  
  16.       "completed" : 2  
  17.     }  

上面截取了部分线程池的配置,其中,最需要关注的是rejected。当某个线程池active==threads时,表示所有线程都在忙,那么后续新的请求就会进入queue中,即queue>0,一旦queue大小超出限制,如bulk的queue默认50,那么elasticsearch进程将拒绝请求(碰到bulk HTTP状态码429),相应的拒绝次数就会累加到rejected中。

解决方法是

1、记录失败的请求并重发

2、减少并发写的进程个数,同时加大每次bulk请求的size


二、核心线程池

generic:通用操作,如node discovery。它的类型默认为cached。
index:此线程池用于索引和删除操作。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为200。
search:此线程池用于搜索和计数请求。它的类型默认为fixed,size默认为(可用处理器的数量* 3) / 2) + 1,队列的size默认为1000。
suggest:此线程池用于建议器请求。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为1000。
get:此线程池用于实时的GET请求。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为1000。
bulk:此线程池用于批量操作。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为50。
percolate:此线程池用于预匹配器操作。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为1000。


三、线程池类型

1、cached

无限制的线程池,为每个请求创建一个线程。这种线程池是为了防止请求被阻塞或者拒绝,其中的每个线程都有一个超时时间(keep_alive),默认5分钟,一旦超时就会回收/终止。elasticsearch的generic线程池就是用该类型。最近发现5.0.0-alpha2版本中去掉了该类型的线程池。

[html]  view plain  copy
  1. threadpool:  
  2.     generic:  
  3.         keep_alive: 2m  

2、fixed

有着固定大小的线程池,大小由size属性指定,默认是5*cores数,允许你指定一个队列(使用queue_size属性指定,默认是-1,即无限制)用来保存请求,直到有一个空闲的线程来执行请求。如果Elasticsearch无法把请求放到队列中(队列满了),该请求将被拒绝。

[html]  view plain  copy
  1. threadpool:  
  2.     index:  
  3.         size: 30  
  4.         queue_size: 1000  

3、scaling

可变大小的pool,大小根据负载在1到size间,同样keep_alive参数指定了闲置线程被回收的时间。

[html]  view plain  copy
  1. threadpool:  
  2.     warmer:  
  3.         size: 8  
  4.         keep_alive: 2m  

四、修改线程池配置

1、elasticsearch.yml

[html]  view plain  copy
  1. threadpool.index.type: fixed  
  2. threadpool.index.size: 100  
  3. threadpool.index.queue_size: 500  

2、Rest API

[html]  view plain  copy
  1. curl -XPUT 'localhost:9200/_cluster/settings' -d '{  
  2.     "transient": {  
  3.         "threadpool.index.type": "fixed",  
  4.         "threadpool.index.size": 100,  
  5.         "threadpool.index.queue_size": 500  
  6.     }  
  7. }'  


五、bulk异常排查

使用es bulk api时报错如下

[html]  view plain  copy
  1. EsRejectedExcutionException[rejected execution(queue capacity 50) on.......]  
这个错误明显是默认大小为50的队列(queue)处理不过来了,解决方法是增大bulk队列的长度

elasticsearch.yml

[html]  view plain  copy
  1. threadpool.bulk.queue_size: 1000  


六、总结

Elasticsearch的线程池其实就是对java自带的进行封装,虽然用户可以更改相关配置,但官方强烈不建议去修改默认值,关于为什么,可以阅读下面第三篇文章。



相关文档

https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-stats.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-threadpool.html

https://www.elastic.co/guide/en/elasticsearch/guide/current/_don_8217_t_touch_these_settings.html#_threadpools



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值