美文网首页
[15]es线程池的优化

[15]es线程池的优化

作者: 不怕天黑_0819 | 来源:发表于2020-08-10 14:35 被阅读0次

    本文集主要是总结自己在项目中使用ES 的经验教训,包括各种实战和调优。


    查看当前线程状态

    curl -XGET 'http://localhost:9200/_nodes/stats?pretty'  
    

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

    解决方法

    1、记录失败的请求并重发
    2、减少并发写的进程个数,同时加大每次bulk请求的size


    ES核心线程池:

    5.x以后 thread_pool有变化,取消了cached线程池类型。ES针对index、bulk、get、search、refresh等操作使用不同的线程池。根据自己的业务场景,可以对线程池进行调优。

    generic:通用操作,如node discovery。它的类型默认为scaling。5.x以后取消了cached类型

    index:此线程池用于索引和删除操作。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为200,最大线程池大小是可用处理器+1

    search:此线程池用于搜索和计数请求。它的类型默认为fixed,size默认为(可用处理器的数量* 3) / 2) + 1,队列的size默认为1000。

    get:此线程池用于实时的GET请求。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为1000。

    bulk:此线程池用于批量操作。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为200。

    percolate:此线程池用于预匹配器操作。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为1000。

    snaphot:For snapshot/restore operations. Thread pool type is scaling with a keep-alive of 5m and a max ofmin(5, (# of available processors)/2).

    warmer:For segment warm-up operations. Thread pool type is scaling with a keep-alive of 5m and a max ofmin(5, (# of available processors)/2).

    refresh:For refresh operations. Thread pool type is scaling with a keep-alive of 5m and a max of min(10, (# of available processors)/2).

    listener:Mainly for java client executing of action when listener threaded is set to true. Thread pool type isscaling with a default max of min(10, (# of available processors)/2).


    线程池类型:

    1、cached 5.x以后取消了cached类型

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

    2、fixed

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

    threadpool:  
        index:  
            size: 30  
            queue_size: 1000  
    

    3、scaling:

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

    threadpool:  
         warmer:  
             size: 8  
             keep_alive: 2m  
    

    修改线程池配置:

    1.通过elasticsearch.yml文件进行修改

    threadpool.index.type: fixed  
    threadpool.index.size: 100  
    threadpool.index.queue_size: 500  
    

    2.通过Rest API

    curl -XPUT 'localhost:9200/_cluster/settings' -d '{  
    
        "transient": {  
            "threadpool.index.type": "fixed",  
            "threadpool.index.size": 100,  
            "threadpool.index.queue_size": 500  
        }  
    }' 
    

    bulk异常排查

    使用es bulk api时报错如下

    EsRejectedExcutionException[rejected execution(queue capacity 50) on.......]

    这个错误明显是默认大小为50的队列(queue)处理不过来了,解决方法是增大bulk队列的长度但是会导致cpu消耗高或者降低并发量也可以采用重试的机制,捕获EsRejectedExcutionException异常,然后重新执行上一次的操作。

    配置方法:

    elasticsearch.yml 中添加  threadpool.bulk.queue_size: 1000  
    

    类似的配置写法如下:

    elasticserch.yml:
    
    thread_pool.index.type: fixed 
    thread_pool.index.size: 100 
    thread_pool.index.queue_size: 500
    

    通过restAPI设置:

    curl -XPUT 'localhost:9200/_cluster/settings' -d '{
    "transient": {
    "threadpool.index.type": "fixed",
    "threadpool.index.size": 100,
    "threadpool.index.queue_size": 500
    }
    }'
    

    Elasticsearch的线程池其实就是对Java自带的进行封装,虽然用户可以更改相关配置,但官方强烈不建议去修改默认值,在项目的实际使用中,我们也是对es的bulk线程配置做了调整。主要是通过type、queue_size、reject_policy来优化性能


    参考链接:http://rockelixir.iteye.com/blog/1890867

    官网关于thread_pool的链接:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/modules-threadpool.html#processors

    相关文章

      网友评论

          本文标题:[15]es线程池的优化

          本文链接:https://www.haomeiwen.com/subject/vgxorktx.html