1. 程式人生 > 其它 >EsRejectedExecutionException排錯與執行緒池型別

EsRejectedExecutionException排錯與執行緒池型別

1、EsRejectedExecutionException異常示例

java.util.concurrent.ExecutionException: RemoteTransportException[[node-client10][10.93.21.21:9300][indices:data/write/update]]; nested: RemoteTransportException[[node-client
09][10.93.18.35:9300][indices:data/write/update[s]]]; nested: EsRejectedExecutionException[rejected execution of org.elasticsearch.transport.netty.MessageChannelHandler$Reque
stHandler@661fbe1d on EsThreadPoolExecutor[index, queue capacity = 200, org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor@637f6431[Running, pool size = 12, active
threads = 12, queued tasks = 200, completed tasks = 15656095]]];

2、EsRejectedExecutionException異常解釋

EsRejectedExecutionException異常,從字面意思上看是ES拒絕執行請求。這個異常的觸發場景如下。

使用Elasticsearch的時候,在併發查詢量大的情況下,訪問流量超過了叢集中單個Elasticsearch例項的處理能力,Elasticsearch服務端會觸發保護性的機制,拒絕執行新的訪問,並且丟擲EsRejectedExecutionException異常。

這個保護機制與異常觸發是由Elasticsearch API實現中的thread pool與配套的queue決定的。

在示例中,Elasticsearch為index操作分配的執行緒池,pool size=12, queue capacity=200,當12個執行緒處理不過來,並且佇列中緩衝的tasks超過200個,那麼新的task就會被簡單的丟棄掉,並且丟擲EsRejectedExecutionException異常。

官方的詳細解釋連結在這裡:

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

這裡是幾個重要執行緒池的預設配置,其中很多配置都與processors(cpu core)有關。

1)processors

processors指的是cpu的core數,這個核數可以被自動的探知並且線上程池的配置中自動引入,processors不必在elasitcsearch.yml中配置,當然你可以重寫配置。

重寫方法:

 processor: 8

若在一臺機器上部署多個Elasticsearch node,可以將cpu cores平均分配給不同的node,例如2nodes部署在12個core的機器上,可以配置processors = 6。

2)執行緒池與佇列

一個Elasticsearch節點會有多個執行緒池,但重要的是下面四個:  索引(index):主要是索引資料和刪除資料操作(預設是cached型別)  搜尋(search):主要是獲取,統計和搜尋操作(預設是cached型別)  批量操作(bulk):主要是對索引的批量操作(預設是cached型別)  更新(refresh):主要是更新操作(預設是cached型別) 

generic
For generic operations (e.g., background node discovery). Thread pool type is scaling.
index
For index/delete operations. Thread pool type is fixed with a size of # of available processors, queue_size of 200. The maximum size for this pool is 1 + # of available processors.
search
For count/search/suggest operations. Thread pool type is fixed with a size of int((# of available_processors * 3) / 2) + 1, queue_size of 1000.
get
For get operations. Thread pool type is fixed with a size of # of available processors, queue_size of 1000.
bulk
For bulk operations. Thread pool type is fixed with a size of # of available processors, queue_size of 200. The maximum size for this pool is 1 + # of available processors.
snapshot
For snapshot/restore operations. Thread pool type is scaling with a keep-alive of 5m and a max of min(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 of min(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 is scaling with a default max of min(10, (# of available processors)/2).

3)執行緒池型別

fixed

The fixed thread pool holds a fixed size of threads to handle the requests with a queue (optionally bounded) for pending requests that have no threads to service them.

The size parameter controls the number of threads, and defaults to the number of cores times 5.

The queue_size allows to control the size of the queue of pending requests that have no threads to execute them. By default, it is set to -1 which means its unbounded. When a request comes in and the queue is full, it will abort the request.

fixed執行緒池保持固定個數的執行緒來處理請求佇列。 

size引數設定執行緒的個數,預設設定是cpu核心數的5倍。

queue_size可以控制待處理請求佇列的大小。預設是設定為-1,意味著無限制。當一個請求到來但佇列滿了的時候,reject_policy引數可以控制它的行為。預設是abort,會使那個請求失敗。設定成caller會使該請求在io執行緒中執行。 

threadpool:   
    index:   
        type: fixed   
        size: 30   
        queue: 1000   
        reject_policy: caller  

scaling

The scaling thread pool holds a dynamic number of threads. This number is proportional to the workload and varies between the value of the core and max parameters.

The keep_alive parameter determines how long a thread should be kept around in the thread pool without it doing any work.

scaling執行緒池保持著動態數量的執行緒。在core和max數量之間的動態變化,keep_alive配置的時間,維持了執行緒長時間未被呼叫。

thread_pool:
    warmer:
        core: 1
        max: 8
        keep_alive: 2m

blocking

在不同的版本有不同的型別。blocking型別的執行緒池特徵是

blocking執行緒池允許設定一個最小值(min,預設為1)和執行緒池大小(size,預設為cpu核心數的5倍)。它也有一個等待佇列,佇列的大小(queue_size )預設是1000,當這佇列滿了的時候。它會根據定好的等待時間(wait_time,預設是60秒)來呼叫io執行緒,如果沒有執行就會報錯。 

threadpool:   
    index:   
        type: blocking   
        min: 1   
        size: 30   
        wait_time: 30s