@Test public void testSetNeitherCoreNorMaximumSizeWithDivergenceDisallowed() { HystrixThreadPoolProperties properties = new HystrixThreadPoolProperties(TestThreadPoolKey.TEST, HystrixThreadPoolProperties.Setter() .withAllowMaximumSizeToDivergeFromCoreSize(false)) { }; assertEquals(HystrixThreadPoolProperties.default_coreSize, properties.coreSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_maximumSize, properties.maximumSize().get().intValue()); assertEquals(HystrixThreadPoolProperties.default_coreSize, (int) properties.actualMaximumSize()); }
private HystrixThreadPoolConfiguration(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties threadPoolProperties) { this(threadPoolKey, threadPoolProperties.coreSize().get(), threadPoolProperties.maximumSize().get(), threadPoolProperties.actualMaximumSize(), threadPoolProperties.maxQueueSize().get(), threadPoolProperties.queueSizeRejectionThreshold().get(), threadPoolProperties.keepAliveTimeMinutes().get(), threadPoolProperties.getAllowMaximumSizeToDivergeFromCoreSize().get(), threadPoolProperties.metricsRollingStatisticalWindowBuckets().get(), threadPoolProperties.metricsRollingStatisticalWindowInMilliseconds().get()); }
public static RollingThreadPoolEventCounterStream getInstance(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties properties) { final int counterMetricWindow = properties.metricsRollingStatisticalWindowInMilliseconds().get(); final int numCounterBuckets = properties.metricsRollingStatisticalWindowBuckets().get(); final int counterBucketSizeInMs = counterMetricWindow / numCounterBuckets; return getInstance(threadPoolKey, numCounterBuckets, counterBucketSizeInMs); }
/** * Given all of the thread pool configuration, what is the actual maximumSize applied to the thread pool * via {@link ThreadPoolExecutor#setMaximumPoolSize(int)} * * Cases: * 1) allowMaximumSizeToDivergeFromCoreSize == false: maximumSize is set to coreSize * 2) allowMaximumSizeToDivergeFromCoreSize == true, maximumSize >= coreSize: thread pool has different core/max sizes, so return the configured max * 3) allowMaximumSizeToDivergeFromCoreSize == true, maximumSize < coreSize: threadpool incorrectly configured, use coreSize for max size * @return actually configured maximum size of threadpool */ public Integer actualMaximumSize() { final int coreSize = coreSize().get(); final int maximumSize = maximumSize().get(); if (getAllowMaximumSizeToDivergeFromCoreSize().get()) { if (coreSize > maximumSize) { return coreSize; } else { return maximumSize; } } else { return coreSize; } }
/** * Factory method to retrieve the default Setter. * Groovy has a bug (GROOVY-6286) which does not allow method names and inner classes to have the same name * This method fixes Issue #967 and allows Groovy consumers to choose this method and not trigger the bug */ public static Setter defaultSetter() { return Setter(); }
public static TenacityConfiguration getTenacityConfiguration(TenacityPropertyKey key) { final HystrixCommandProperties commandProperties = TenacityCommand.getCommandProperties(key); final HystrixThreadPoolProperties threadPoolProperties = TenacityCommand.getThreadpoolProperties(key); return new TenacityConfiguration( new ThreadPoolConfiguration( threadPoolProperties.coreSize().get(), threadPoolProperties.keepAliveTimeMinutes().get(), threadPoolProperties.maxQueueSize().get(), threadPoolProperties.queueSizeRejectionThreshold().get(), threadPoolProperties.metricsRollingStatisticalWindowInMilliseconds().get(), threadPoolProperties.metricsRollingStatisticalWindowBuckets().get()), new CircuitBreakerConfiguration( commandProperties.circuitBreakerRequestVolumeThreshold().get(), commandProperties.circuitBreakerSleepWindowInMilliseconds().get(), commandProperties.circuitBreakerErrorThresholdPercentage().get(), commandProperties.metricsRollingStatisticalWindowInMilliseconds().get(), commandProperties.metricsRollingStatisticalWindowBuckets().get()), new SemaphoreConfiguration( commandProperties.executionIsolationSemaphoreMaxConcurrentRequests().get(), commandProperties.fallbackIsolationSemaphoreMaxConcurrentRequests().get()), commandProperties.executionTimeoutInMilliseconds().get(), commandProperties.executionIsolationStrategy().get()); } }
public ThreadPoolExecutor getThreadPool(final HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties threadPoolProperties) { final ThreadFactory threadFactory = getThreadFactory(threadPoolKey); final boolean allowMaximumSizeToDivergeFromCoreSize = threadPoolProperties.getAllowMaximumSizeToDivergeFromCoreSize().get(); final int dynamicCoreSize = threadPoolProperties.coreSize().get(); final int keepAliveTime = threadPoolProperties.keepAliveTimeMinutes().get(); final int maxQueueSize = threadPoolProperties.maxQueueSize().get(); final BlockingQueue<Runnable> workQueue = getBlockingQueue(maxQueueSize); if (allowMaximumSizeToDivergeFromCoreSize) { final int dynamicMaximumSize = threadPoolProperties.maximumSize().get(); if (dynamicCoreSize > dynamicMaximumSize) { logger.error("Hystrix ThreadPool configuration at startup for : " + threadPoolKey.name() + " is trying to set coreSize = " + dynamicCoreSize + " and maximumSize = " + dynamicMaximumSize + ". Maximum size will be set to " + dynamicCoreSize + ", the coreSize value, since it must be equal to or greater than the coreSize value"); return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory); } else { return new ThreadPoolExecutor(dynamicCoreSize, dynamicMaximumSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory); } } else { return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory); } }
private void touchConfig() { final int dynamicCoreSize = properties.coreSize().get(); final int configuredMaximumSize = properties.maximumSize().get(); int dynamicMaximumSize = properties.actualMaximumSize(); final boolean allowSizesToDiverge = properties.getAllowMaximumSizeToDivergeFromCoreSize().get(); boolean maxTooLow = false; if (allowSizesToDiverge && configuredMaximumSize < dynamicCoreSize) { //if user sets maximum < core (or defaults get us there), we need to maintain invariant of core <= maximum dynamicMaximumSize = dynamicCoreSize; maxTooLow = true; } // In JDK 6, setCorePoolSize and setMaximumPoolSize will execute a lock operation. Avoid them if the pool size is not changed. if (threadPool.getCorePoolSize() != dynamicCoreSize || (allowSizesToDiverge && threadPool.getMaximumPoolSize() != dynamicMaximumSize)) { if (maxTooLow) { logger.error("Hystrix ThreadPool configuration for : " + metrics.getThreadPoolKey().name() + " is trying to set coreSize = " + dynamicCoreSize + " and maximumSize = " + configuredMaximumSize + ". Maximum size will be set to " + dynamicMaximumSize + ", the coreSize value, since it must be equal to or greater than the coreSize value"); } threadPool.setCorePoolSize(dynamicCoreSize); threadPool.setMaximumPoolSize(dynamicMaximumSize); } threadPool.setKeepAliveTime(properties.keepAliveTimeMinutes().get(), TimeUnit.MINUTES); }
json.writeNumberField("rollingMaxActiveThreads", threadPoolMetrics.getRollingMaxActiveThreads()); json.writeNumberField("propertyValue_queueSizeRejectionThreshold", threadPoolMetrics.getProperties().queueSizeRejectionThreshold().get()); json.writeNumberField("propertyValue_metricsRollingStatisticalWindowInMilliseconds", threadPoolMetrics.getProperties().metricsRollingStatisticalWindowInMilliseconds().get());
@Override public Number getValue() { return properties.maxQueueSize().get(); } });
@Override public Number getValue() { return properties.queueSizeRejectionThreshold().get(); } });
@Override public Number getValue() { return properties.coreSize().get(); } });
@Override public Number getValue() { return properties.keepAliveTimeMinutes().get(); } });
@Override public Number getValue() { return properties.maximumSize().get(); } });
@Override public Number getValue() { return properties.actualMaximumSize(); } });
/** * Creates and sets Hystrix thread pool properties. * * @param properties the collapser properties */ public static HystrixThreadPoolProperties.Setter initializeThreadPoolProperties(List<HystrixProperty> properties) throws IllegalArgumentException { return initializeProperties(HystrixThreadPoolProperties.Setter(), properties, TP_PROP_MAP, "thread pool"); }
public static TenacityConfiguration getTenacityConfiguration(TenacityPropertyKey key) { final HystrixCommandProperties commandProperties = TenacityCommand.getCommandProperties(key); final HystrixThreadPoolProperties threadPoolProperties = TenacityCommand.getThreadpoolProperties(key); return new TenacityConfiguration( new ThreadPoolConfiguration( threadPoolProperties.coreSize().get(), threadPoolProperties.keepAliveTimeMinutes().get(), threadPoolProperties.maxQueueSize().get(), threadPoolProperties.queueSizeRejectionThreshold().get(), threadPoolProperties.metricsRollingStatisticalWindowInMilliseconds().get(), threadPoolProperties.metricsRollingStatisticalWindowBuckets().get()), new CircuitBreakerConfiguration( commandProperties.circuitBreakerRequestVolumeThreshold().get(), commandProperties.circuitBreakerSleepWindowInMilliseconds().get(), commandProperties.circuitBreakerErrorThresholdPercentage().get(), commandProperties.metricsRollingStatisticalWindowInMilliseconds().get(), commandProperties.metricsRollingStatisticalWindowBuckets().get()), new SemaphoreConfiguration( commandProperties.executionIsolationSemaphoreMaxConcurrentRequests().get(), commandProperties.fallbackIsolationSemaphoreMaxConcurrentRequests().get()), commandProperties.executionTimeoutInMilliseconds().get(), commandProperties.executionIsolationStrategy().get()); } }
@Override public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties threadPoolProperties) { boolean allowMaximumSizeToDivergeFromCoreSize = threadPoolProperties.getAllowMaximumSizeToDivergeFromCoreSize().get(); int dynamicCoreSize = threadPoolProperties.coreSize().get(); int dynamicMaximumSize = threadPoolProperties.maximumSize().get(); int keepAliveTime = threadPoolProperties.keepAliveTimeMinutes().get(); int maxQueueSize = threadPoolProperties.maxQueueSize().get(); BlockingQueue<Runnable> workQueue = getBlockingQueue(maxQueueSize); LOGGER.debugf("Get thread pool executor for %s [allowMaximumSizeToDivergeFromCoreSize: %s, core: %s, max: %s]", threadPoolKey.name(), allowMaximumSizeToDivergeFromCoreSize, dynamicCoreSize, dynamicMaximumSize); if (allowMaximumSizeToDivergeFromCoreSize) { return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize > dynamicMaximumSize ? dynamicCoreSize : dynamicMaximumSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory); } else { return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory); } }
private void touchConfig() { final int dynamicCoreSize = properties.coreSize().get(); final int configuredMaximumSize = properties.maximumSize().get(); int dynamicMaximumSize = properties.actualMaximumSize(); final boolean allowSizesToDiverge = properties.getAllowMaximumSizeToDivergeFromCoreSize().get(); boolean maxTooLow = false; if (allowSizesToDiverge && configuredMaximumSize < dynamicCoreSize) { //if user sets maximum < core (or defaults get us there), we need to maintain invariant of core <= maximum dynamicMaximumSize = dynamicCoreSize; maxTooLow = true; } // In JDK 6, setCorePoolSize and setMaximumPoolSize will execute a lock operation. Avoid them if the pool size is not changed. if (threadPool.getCorePoolSize() != dynamicCoreSize || (allowSizesToDiverge && threadPool.getMaximumPoolSize() != dynamicMaximumSize)) { if (maxTooLow) { logger.error("Hystrix ThreadPool configuration for : " + metrics.getThreadPoolKey().name() + " is trying to set coreSize = " + dynamicCoreSize + " and maximumSize = " + configuredMaximumSize + ". Maximum size will be set to " + dynamicMaximumSize + ", the coreSize value, since it must be equal to or greater than the coreSize value"); } threadPool.setCorePoolSize(dynamicCoreSize); threadPool.setMaximumPoolSize(dynamicMaximumSize); } threadPool.setKeepAliveTime(properties.keepAliveTimeMinutes().get(), TimeUnit.MINUTES); }
@Override public String apply(HystrixThreadPoolMetrics threadPoolMetrics) throws Exception { HystrixThreadPoolKey key = threadPoolMetrics.getThreadPoolKey(); StringWriter jsonString = new StringWriter(); JsonGenerator json = jsonFactory.createGenerator(jsonString); json.writeStartObject(); json.writeStringField("type", "HystrixThreadPool"); json.writeStringField("name", key.name()); json.writeNumberField("currentTime", System.currentTimeMillis()); json.writeNumberField("currentActiveCount", threadPoolMetrics.getCurrentActiveCount().intValue()); json.writeNumberField("currentCompletedTaskCount", threadPoolMetrics.getCurrentCompletedTaskCount().longValue()); json.writeNumberField("currentCorePoolSize", threadPoolMetrics.getCurrentCorePoolSize().intValue()); json.writeNumberField("currentLargestPoolSize", threadPoolMetrics.getCurrentLargestPoolSize().intValue()); json.writeNumberField("currentMaximumPoolSize", threadPoolMetrics.getCurrentMaximumPoolSize().intValue()); json.writeNumberField("currentPoolSize", threadPoolMetrics.getCurrentPoolSize().intValue()); json.writeNumberField("currentQueueSize", threadPoolMetrics.getCurrentQueueSize().intValue()); json.writeNumberField("currentTaskCount", threadPoolMetrics.getCurrentTaskCount().longValue()); json.writeNumberField("rollingCountThreadsExecuted", threadPoolMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_EXECUTION)); json.writeNumberField("rollingMaxActiveThreads", threadPoolMetrics.getRollingMaxActiveThreads()); json.writeNumberField("rollingCountCommandRejections", threadPoolMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_POOL_REJECTED)); json.writeNumberField("propertyValue_queueSizeRejectionThreshold", threadPoolMetrics.getProperties().queueSizeRejectionThreshold().get()); json.writeNumberField("propertyValue_metricsRollingStatisticalWindowInMilliseconds", threadPoolMetrics.getProperties().metricsRollingStatisticalWindowInMilliseconds().get()); json.writeNumberField("reportingHosts", 1); // this will get summed across all instances in a cluster json.writeEndObject(); json.close(); return jsonString.getBuffer().toString(); }