private ExecutorHolder rebuild( String name, ExecutorHolder previousExecutorHolder, @Nullable Settings settings, Settings defaultSettings) { if (Names.SAME.equals(name)) { // Don't allow to change the "same" thread executor return previousExecutorHolder; } if (settings == null) { settings = ImmutableSettings.Builder.EMPTY_SETTINGS; } Info previousInfo = previousExecutorHolder != null ? previousExecutorHolder.info : null; String type = settings.get( "type", previousInfo != null ? previousInfo.getType() : defaultSettings.get("type")); ThreadFactory threadFactory = EsExecutors.daemonThreadFactory(this.settings, name); if ("same".equals(type)) { if (previousExecutorHolder != null) { logger.debug("updating thread_pool [{}], type [{}]", name, type); } else { logger.debug("creating thread_pool [{}], type [{}]", name, type); } return new ExecutorHolder(MoreExecutors.sameThreadExecutor(), new Info(name, type)); } else if ("cached".equals(type)) { TimeValue defaultKeepAlive = defaultSettings.getAsTime("keep_alive", timeValueMinutes(5)); if (previousExecutorHolder != null) { if ("cached".equals(previousInfo.getType())) { TimeValue updatedKeepAlive = settings.getAsTime("keep_alive", previousInfo.getKeepAlive()); if (!previousInfo.getKeepAlive().equals(updatedKeepAlive)) { logger.debug( "updating thread_pool [{}], type [{}], keep_alive [{}]", name, type, updatedKeepAlive); ((EsThreadPoolExecutor) previousExecutorHolder.executor) .setKeepAliveTime(updatedKeepAlive.millis(), TimeUnit.MILLISECONDS); return new ExecutorHolder( previousExecutorHolder.executor, new Info(name, type, -1, -1, updatedKeepAlive, null)); } return previousExecutorHolder; } if (previousInfo.getKeepAlive() != null) { defaultKeepAlive = previousInfo.getKeepAlive(); } } TimeValue keepAlive = settings.getAsTime("keep_alive", defaultKeepAlive); if (previousExecutorHolder != null) { logger.debug( "updating thread_pool [{}], type [{}], keep_alive [{}]", name, type, keepAlive); } else { logger.debug( "creating thread_pool [{}], type [{}], keep_alive [{}]", name, type, keepAlive); } Executor executor = EsExecutors.newCached(keepAlive.millis(), TimeUnit.MILLISECONDS, threadFactory); return new ExecutorHolder(executor, new Info(name, type, -1, -1, keepAlive, null)); } else if ("fixed".equals(type)) { int defaultSize = defaultSettings.getAsInt("size", EsExecutors.boundedNumberOfProcessors(settings)); SizeValue defaultQueueSize = defaultSettings.getAsSize("queue", defaultSettings.getAsSize("queue_size", null)); if (previousExecutorHolder != null) { if ("fixed".equals(previousInfo.getType())) { SizeValue updatedQueueSize = settings.getAsSize( "capacity", settings.getAsSize( "queue", settings.getAsSize("queue_size", previousInfo.getQueueSize()))); if (Objects.equal(previousInfo.getQueueSize(), updatedQueueSize)) { int updatedSize = settings.getAsInt("size", previousInfo.getMax()); if (previousInfo.getMax() != updatedSize) { logger.debug( "updating thread_pool [{}], type [{}], size [{}], queue_size [{}]", name, type, updatedSize, updatedQueueSize); ((EsThreadPoolExecutor) previousExecutorHolder.executor).setCorePoolSize(updatedSize); ((EsThreadPoolExecutor) previousExecutorHolder.executor) .setMaximumPoolSize(updatedSize); return new ExecutorHolder( previousExecutorHolder.executor, new Info(name, type, updatedSize, updatedSize, null, updatedQueueSize)); } return previousExecutorHolder; } } if (previousInfo.getMax() >= 0) { defaultSize = previousInfo.getMax(); } defaultQueueSize = previousInfo.getQueueSize(); } int size = settings.getAsInt("size", defaultSize); SizeValue queueSize = settings.getAsSize( "capacity", settings.getAsSize("queue", settings.getAsSize("queue_size", defaultQueueSize))); logger.debug( "creating thread_pool [{}], type [{}], size [{}], queue_size [{}]", name, type, size, queueSize); Executor executor = EsExecutors.newFixed( size, queueSize == null ? -1 : (int) queueSize.singles(), threadFactory); return new ExecutorHolder(executor, new Info(name, type, size, size, null, queueSize)); } else if ("scaling".equals(type)) { TimeValue defaultKeepAlive = defaultSettings.getAsTime("keep_alive", timeValueMinutes(5)); int defaultMin = defaultSettings.getAsInt("min", 1); int defaultSize = defaultSettings.getAsInt("size", EsExecutors.boundedNumberOfProcessors(settings)); if (previousExecutorHolder != null) { if ("scaling".equals(previousInfo.getType())) { TimeValue updatedKeepAlive = settings.getAsTime("keep_alive", previousInfo.getKeepAlive()); int updatedMin = settings.getAsInt("min", previousInfo.getMin()); int updatedSize = settings.getAsInt("max", settings.getAsInt("size", previousInfo.getMax())); if (!previousInfo.getKeepAlive().equals(updatedKeepAlive) || previousInfo.getMin() != updatedMin || previousInfo.getMax() != updatedSize) { logger.debug( "updating thread_pool [{}], type [{}], keep_alive [{}]", name, type, updatedKeepAlive); if (!previousInfo.getKeepAlive().equals(updatedKeepAlive)) { ((EsThreadPoolExecutor) previousExecutorHolder.executor) .setKeepAliveTime(updatedKeepAlive.millis(), TimeUnit.MILLISECONDS); } if (previousInfo.getMin() != updatedMin) { ((EsThreadPoolExecutor) previousExecutorHolder.executor).setCorePoolSize(updatedMin); } if (previousInfo.getMax() != updatedSize) { ((EsThreadPoolExecutor) previousExecutorHolder.executor) .setMaximumPoolSize(updatedSize); } return new ExecutorHolder( previousExecutorHolder.executor, new Info(name, type, updatedMin, updatedSize, updatedKeepAlive, null)); } return previousExecutorHolder; } if (previousInfo.getKeepAlive() != null) { defaultKeepAlive = previousInfo.getKeepAlive(); } if (previousInfo.getMin() >= 0) { defaultMin = previousInfo.getMin(); } if (previousInfo.getMax() >= 0) { defaultSize = previousInfo.getMax(); } } TimeValue keepAlive = settings.getAsTime("keep_alive", defaultKeepAlive); int min = settings.getAsInt("min", defaultMin); int size = settings.getAsInt("max", settings.getAsInt("size", defaultSize)); if (previousExecutorHolder != null) { logger.debug( "updating thread_pool [{}], type [{}], min [{}], size [{}], keep_alive [{}]", name, type, min, size, keepAlive); } else { logger.debug( "creating thread_pool [{}], type [{}], min [{}], size [{}], keep_alive [{}]", name, type, min, size, keepAlive); } Executor executor = EsExecutors.newScaling( min, size, keepAlive.millis(), TimeUnit.MILLISECONDS, threadFactory); return new ExecutorHolder(executor, new Info(name, type, min, size, keepAlive, null)); } throw new ElasticSearchIllegalArgumentException( "No type found [" + type + "], for [" + name + "]"); }
public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) { if (!Names.SAME.equals(name)) { command = new ThreadedRunnable(command, executor(name)); } return scheduler.schedule(command, delay.millis(), TimeUnit.MILLISECONDS); }