開篇
這篇文章主要是用來講解ES線程池(EsExecutors)的實現,然後象徵性的和JDK的Executors實現進行了簡單的對比,看了這篇文章以後要對Executors和ThreadPoolExecutor的使用更有信心纔對。
elasticsearch線程池配置
public class ThreadPool extends AbstractComponent implements Scheduler, Closeable {
final int availableProcessors = EsExecutors.numberOfProcessors(settings);
final int halfProcMaxAt5 = halfNumberOfProcessorsMaxFive(availableProcessors);
final int halfProcMaxAt10 = halfNumberOfProcessorsMaxTen(availableProcessors);
final int genericThreadPoolMax = boundedBy(4 * availableProcessors, 128, 512);
builders.put(Names.GENERIC,
new ScalingExecutorBuilder(Names.GENERIC, 4, genericThreadPoolMax, TimeValue.timeValueSeconds(30)));
builders.put(Names.INDEX,
new FixedExecutorBuilder(settings, Names.INDEX, availableProcessors, 200));
builders.put(Names.BULK,
new FixedExecutorBuilder(settings, Names.BULK, availableProcessors, 200));
builders.put(Names.GET,
new FixedExecutorBuilder(settings, Names.GET, availableProcessors, 1000));
builders.put(Names.SEARCH,
new AutoQueueAdjustingExecutorBuilder(settings,
Names.SEARCH, searchThreadPoolSize(availableProcessors), 1000, 1000, 1000, 2000));
builders.put(Names.MANAGEMENT,
new ScalingExecutorBuilder(Names.MANAGEMENT, 1, 5, TimeValue.timeValueMinutes(5)));
builders.put(Names.LISTENER,
new FixedExecutorBuilder(settings, Names.LISTENER, halfProcMaxAt10, -1));
builders.put(Names.FLUSH,
new ScalingExecutorBuilder(Names.FLUSH, 1, halfProcMaxAt5, TimeValue.timeValueMinutes(5)));
builders.put(Names.REFRESH,
new ScalingExecutorBuilder(Names.REFRESH, 1, halfProcMaxAt10, TimeValue.timeValueMinutes(5)));
builders.put(Names.WARMER,
new ScalingExecutorBuilder(Names.WARMER, 1, halfProcMaxAt5, TimeValue.timeValueMinutes(5)));
builders.put(Names.SNAPSHOT,
new ScalingExecutorBuilder(Names.SNAPSHOT, 1, halfProcMaxAt5, TimeValue.timeValueMinutes(5)));
builders.put(Names.FETCH_SHARD_STARTED,
new ScalingExecutorBuilder(Names.FETCH_SHARD_STARTED, 1, 2 * availableProcessors, TimeValue.timeValueMinutes(5)));
builders.put(Names.FORCE_MERGE,
new FixedExecutorBuilder(settings, Names.FORCE_MERGE, 1, -1));
builders.put(Names.FETCH_SHARD_STORE,
new ScalingExecutorBuilder(Names.FETCH_SHARD_STORE, 1, 2 * availableProcessors, TimeValue.timeValueMinutes(5)));
}
說明:
- elasticsearch線程池根據作用的不同主要分爲兩大類 ScalingExecutor和FixedExecutor。
- ScalingExecutor表示線程池中的線程數是動態可變的。
- FixedExecutor表示線程池中的線程池是不可變的。
elasticsearch線程池分類
elasticsearch線程池的線程按照源碼的實現來看分爲FIXED和SCALING兩大類,FIXED的意思是固定線程的數量(core thread個數 = max thread個數),SCALING的意思是動態調整線程數量(core thread個數 != max thread個數)。
FIXED
說明:大小固定設置的threadpool,它有一個queue來存放pending的請求,其中pool的大小默認是core*5,queue_size默認是-1(即是無限制)。
- LISTENER:用作client的操作,默認大小halfProcMaxAt10,queue_size=-1無限制;
- GET:用作get操作,默認大小availableProcessors,queue_size爲1000;
- INDEX:用作index或delete操作,默認大小availableProcessors,queue_size爲200;
- BULK:用作bulk操作,默認大小爲availableProcessors,queue_size爲200;
- SEARCH:用作count或是search操作,默認大小((availableProcessors * 3) / 2) + 1;queue_size爲1000;
- SUGGEST:用作suggest操作,默認大小availableProcessors,queue_size爲1000;
- PERCOLATE:用作percolate,默認大小爲availableProcessors,queue_size爲1000;
- FORCE_MERGE:用作force_merge操作(2.1之前叫做optimize),默認大小爲1;
SCALING
說明:擁有可變大小的pool,其值可在1和設置值之間。
- GENERIC:通用的操作,比如node的discovery,默認大小genericThreadPoolMax,默認keep alive時間是30sec;
- MANAGEMENT:用作ES的管理,比如集羣的管理;默認大小5,keep alive時間爲5min;
- FLUSH:用作flush操作,默認大小爲halfProcMaxAt5,keep alive時間爲5min;
- REFRESH:用作refresh操作,默認大小爲halfProcMaxAt10,keep alive時間爲5min;
- WARMER:用作index warm-up操作,默認大小爲halfProcMaxAt5,keep alive時間爲5min;
- SNAPSHOT:用作snapshot操作,默認大小爲halfProcMaxAt5,keep alive時間爲5min;
- FETCH_SHARD_STARTED:用作fetch shard開始操作,默認大小availableProcessors * 2,keep alive時間爲5min;
- FETCH_SHARD_STORE:用作fetch shard存儲操作,默認大小availableProcessors * 2,keep alive時間爲5min;
JDK的Executors
public class Executors {
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
}
說明:
- Executors的newFixedThreadPool創建固定線程數量的線程池。
- Executors的newCachedThreadPool創建可動態調整線程數量的線程池。
- Executors創建的是ThreadPoolExecutor對象。
Elasticsearch的EsExecutors
public class EsExecutors {
public static final Setting<Integer> PROCESSORS_SETTING =
Setting.intSetting("processors", Runtime.getRuntime().availableProcessors(), 1, Property.NodeScope);
public static int numberOfProcessors(final Settings settings) {
return PROCESSORS_SETTING.get(settings);
}
public static EsThreadPoolExecutor newScaling(String name, int min, int max, long keepAliveTime,
TimeUnit unit, ThreadFactory threadFactory, ThreadContext contextHolder) {
ExecutorScalingQueue<Runnable> queue = new ExecutorScalingQueue<>();
EsThreadPoolExecutor executor = new EsThreadPoolExecutor(name, min, max, keepAliveTime,
unit, queue, threadFactory, new ForceQueuePolicy(), contextHolder);
queue.executor = executor;
return executor;
}
public static EsThreadPoolExecutor newFixed(String name, int size, int queueCapacity,
ThreadFactory threadFactory, ThreadContext contextHolder) {
BlockingQueue<Runnable> queue;
if (queueCapacity < 0) {
queue = ConcurrentCollections.newBlockingQueue();
} else {
queue = new SizeBlockingQueue<>(ConcurrentCollections.<Runnable>newBlockingQueue(), queueCapacity);
}
return new EsThreadPoolExecutor(name, size, size, 0, TimeUnit.MILLISECONDS, queue,
threadFactory, new EsAbortPolicy(), contextHolder);
}
public static EsThreadPoolExecutor newAutoQueueFixed(String name, int size, int initialQueueCapacity, int minQueueSize,
int maxQueueSize, int frameSize, TimeValue targetedResponseTime,
ThreadFactory threadFactory, ThreadContext contextHolder) {
if (initialQueueCapacity <= 0) {
throw new IllegalArgumentException("initial queue capacity for [" + name + "] executor must be positive, got: " +
initialQueueCapacity);
}
ResizableBlockingQueue<Runnable> queue =
new ResizableBlockingQueue<>(ConcurrentCollections.<Runnable>newBlockingQueue(), initialQueueCapacity);
return new QueueResizingEsThreadPoolExecutor(name, size, size, 0, TimeUnit.MILLISECONDS,
queue, minQueueSize, maxQueueSize, TimedRunnable::new, frameSize, targetedResponseTime, threadFactory,
new EsAbortPolicy(), contextHolder);
}
}
說明:
- newScaling()方法創建可擴展線程數量的線程池,淘汰策略使用ForceQueuePolicy。
- newFixed()方法創建創建固定線程數量的線程池,淘汰策略使用EsAbortPolicy。
- newAutoQueueFixed()方法創建固定線程數量但是Queue隊列數量可以動態調整的線程池,淘汰策略使用EsAbortPolicy。。
- EsExecutors內部創建的是EsThreadPoolExecutor對象。
- EsExecutors的實現借鑑了JDK的Executors接口,給我們提供了自定Executors的思路。
EsExecutors的EsThreadPoolExecutor
public class EsThreadPoolExecutor extends ThreadPoolExecutor {
EsThreadPoolExecutor(String name, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, ThreadContext contextHolder) {
this(name, corePoolSize, maximumPoolSize, keepAliveTime, unit,
workQueue, threadFactory, new EsAbortPolicy(), contextHolder);
}
EsThreadPoolExecutor(String name, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, XRejectedExecutionHandler handler,
ThreadContext contextHolder) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
this.name = name;
this.contextHolder = contextHolder;
}
@Override
public void execute(final Runnable command) {
doExecute(wrapRunnable(command));
}
protected void doExecute(final Runnable command) {
try {
super.execute(command);
} catch (EsRejectedExecutionException ex) {
if (command instanceof AbstractRunnable) {
try {
((AbstractRunnable) command).onRejection(ex);
} finally {
((AbstractRunnable) command).onAfter();
}
} else {
throw ex;
}
}
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
assert assertDefaultContext(r);
}
}
說明:
- EsThreadPoolExecutor繼承自ThreadPoolExecutor對象,構造函數內部初始化ThreadPoolExecutor對象。
- EsThreadPoolExecutor的核心的execute方法內部也是調用了ThreadPoolExecutor的execute方法。
- EsThreadPoolExecutor重新了execute和afterExecute方法。
- EsThreadPoolExecutor給我們提供了重寫ThreadPoolExecutor的思路,值得學習。
EsExecutors的ThreadFactory
public class EsExecutors {
public static String threadName(Settings settings, String ... names) {
String namePrefix =
Arrays
.stream(names)
.filter(name -> name != null)
.collect(Collectors.joining(".", "[", "]"));
return threadName(settings, namePrefix);
}
public static ThreadFactory daemonThreadFactory(String namePrefix) {
return new EsThreadFactory(namePrefix);
}
static class EsThreadFactory implements ThreadFactory {
final ThreadGroup group;
final AtomicInteger threadNumber = new AtomicInteger(1);
final String namePrefix;
EsThreadFactory(String namePrefix) {
this.namePrefix = namePrefix;
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + "[T#" + threadNumber.getAndIncrement() + "]",
0);
t.setDaemon(true);
return t;
}
}
private EsExecutors() {
}
}
說明:
- 1.EsExecutors給我們提供一種創建線程工廠的標準方法,實現ThreadFactory接口重新newThread()方法。
- 2.通過AtomicInteger threadNumber = new AtomicInteger(1)變量生成線程自增的線程id。
- 3.線程池的線程都有具體意義的線程名非常重要有利於排查問題,非常推薦使用。
EsExecutors的AbortPolicy
public interface XRejectedExecutionHandler extends RejectedExecutionHandler {
long rejected();
}
public class EsAbortPolicy implements XRejectedExecutionHandler {
private final CounterMetric rejected = new CounterMetric();
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
if (r instanceof AbstractRunnable) {
if (((AbstractRunnable) r).isForceExecution()) {
BlockingQueue<Runnable> queue = executor.getQueue();
if (!(queue instanceof SizeBlockingQueue)) {
throw new IllegalStateException("forced execution, but expected a size queue");
}
try {
((SizeBlockingQueue) queue).forcePut(r);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("forced execution, but got interrupted", e);
}
return;
}
}
rejected.inc();
throw new EsRejectedExecutionException("rejected execution of " + r + " on " + executor, executor.isShutdown());
}
public long rejected() {
return rejected.count();
}
}
static class ForceQueuePolicy implements XRejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
executor.getQueue().put(r);
} catch (InterruptedException e) {
throw new EsRejectedExecutionException(e);
}
}
@Override
public long rejected() {
return 0;
}
}
說明:
- EsAbortPolicy的過期策略提供了我們自定實現過期策略的案例。