Skip to content

Commit 08cefa2

Browse files
authored
Merge pull request #1447 from mattrjacobs/allow-dynamic-update-to-allow-maximum-size-diverge-from-core-size
Allow dynamic update to allow maximum size diverge from core size
2 parents 0c8f733 + a37ff8c commit 08cefa2

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed

hystrix-core/src/main/java/com/netflix/hystrix/HystrixThreadPool.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,12 @@ public HystrixThreadPoolDefault(HystrixThreadPoolKey threadPoolKey, HystrixThrea
172172
this.properties = HystrixPropertiesFactory.getThreadPoolProperties(threadPoolKey, propertiesDefaults);
173173
HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
174174
this.queueSize = properties.maxQueueSize().get();
175-
this.queue = concurrencyStrategy.getBlockingQueue(queueSize);
176175

177-
if (properties.getAllowMaximumSizeToDivergeFromCoreSize().get()) {
178-
this.metrics = HystrixThreadPoolMetrics.getInstance(threadPoolKey,
179-
concurrencyStrategy.getThreadPool(threadPoolKey, properties.coreSize(), properties.maximumSize(), properties.keepAliveTimeMinutes(), TimeUnit.MINUTES, queue),
180-
properties);
181-
this.threadPool = this.metrics.getThreadPool();
182-
} else {
183-
this.metrics = HystrixThreadPoolMetrics.getInstance(threadPoolKey,
184-
concurrencyStrategy.getThreadPool(threadPoolKey, properties.coreSize(), properties.coreSize(), properties.keepAliveTimeMinutes(), TimeUnit.MINUTES, queue),
185-
properties);
186-
this.threadPool = this.metrics.getThreadPool();
187-
}
176+
this.metrics = HystrixThreadPoolMetrics.getInstance(threadPoolKey,
177+
concurrencyStrategy.getThreadPool(threadPoolKey, properties),
178+
properties);
179+
this.threadPool = this.metrics.getThreadPool();
180+
this.queue = this.threadPool.getQueue();
188181

189182
/* strategy: HystrixMetricsPublisherThreadPool */
190183
HystrixMetricsPublisherFactory.createOrRetrievePublisherForThreadPool(threadPoolKey, this.metrics, this.properties);

hystrix-core/src/main/java/com/netflix/hystrix/config/HystrixThreadPoolConfiguration.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@ public HystrixThreadPoolConfiguration(HystrixThreadPoolKey threadPoolKey, int co
3535
int keepAliveTimeInMinutes, boolean allowMaximumSizeToDivergeFromCoreSize, int rollingCounterNumberOfBuckets,
3636
int rollingCounterBucketSizeInMilliseconds) {
3737
this.threadPoolKey = threadPoolKey;
38+
this.allowMaximumSizeToDivergeFromCoreSize = allowMaximumSizeToDivergeFromCoreSize;
3839
this.coreSize = coreSize;
39-
this.maximumSize = maximumSize;
40+
if (allowMaximumSizeToDivergeFromCoreSize) {
41+
if (coreSize > maximumSize) {
42+
this.maximumSize = coreSize;
43+
} else {
44+
this.maximumSize = maximumSize;
45+
}
46+
} else {
47+
this.maximumSize = coreSize;
48+
}
4049
this.maxQueueSize = maxQueueSize;
4150
this.queueRejectionThreshold = queueRejectionThreshold;
4251
this.keepAliveTimeInMinutes = keepAliveTimeInMinutes;
43-
this.allowMaximumSizeToDivergeFromCoreSize = allowMaximumSizeToDivergeFromCoreSize;
4452
this.rollingCounterNumberOfBuckets = rollingCounterNumberOfBuckets;
4553
this.rollingCounterBucketSizeInMilliseconds = rollingCounterBucketSizeInMilliseconds;
4654
}

hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixConcurrencyStrategy.java

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.netflix.hystrix.HystrixCommand;
1919
import com.netflix.hystrix.HystrixThreadPool;
2020
import com.netflix.hystrix.HystrixThreadPoolKey;
21+
import com.netflix.hystrix.HystrixThreadPoolProperties;
2122
import com.netflix.hystrix.strategy.HystrixPlugins;
2223
import com.netflix.hystrix.strategy.properties.HystrixProperty;
2324
import com.netflix.hystrix.util.PlatformSpecific;
@@ -75,9 +76,49 @@ public abstract class HystrixConcurrencyStrategy {
7576
* @return instance of {@link ThreadPoolExecutor}
7677
*/
7778
public ThreadPoolExecutor getThreadPool(final HystrixThreadPoolKey threadPoolKey, HystrixProperty<Integer> corePoolSize, HystrixProperty<Integer> maximumPoolSize, HystrixProperty<Integer> keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
79+
final ThreadFactory threadFactory = getThreadFactory(threadPoolKey);
80+
81+
final int dynamicCoreSize = corePoolSize.get();
82+
final int dynamicMaximumSize = maximumPoolSize.get();
83+
84+
if (dynamicCoreSize > dynamicMaximumSize) {
85+
logger.error("Hystrix ThreadPool configuration at startup for : " + threadPoolKey.name() + " is trying to set coreSize = " +
86+
dynamicCoreSize + " and maximumSize = " + dynamicMaximumSize + ". Maximum size will be set to " +
87+
dynamicCoreSize + ", the coreSize value, since it must be equal to or greater than the coreSize value");
88+
return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime.get(), unit, workQueue, threadFactory);
89+
} else {
90+
return new ThreadPoolExecutor(dynamicCoreSize, dynamicMaximumSize, keepAliveTime.get(), unit, workQueue, threadFactory);
91+
}
92+
}
93+
94+
public ThreadPoolExecutor getThreadPool(final HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties threadPoolProperties) {
95+
final ThreadFactory threadFactory = getThreadFactory(threadPoolKey);
96+
97+
final boolean allowMaximumSizeToDivergeFromCoreSize = threadPoolProperties.getAllowMaximumSizeToDivergeFromCoreSize().get();
98+
final int dynamicCoreSize = threadPoolProperties.coreSize().get();
99+
final int keepAliveTime = threadPoolProperties.keepAliveTimeMinutes().get();
100+
final int maxQueueSize = threadPoolProperties.maxQueueSize().get();
101+
final BlockingQueue<Runnable> workQueue = getBlockingQueue(maxQueueSize);
102+
103+
if (allowMaximumSizeToDivergeFromCoreSize) {
104+
final int dynamicMaximumSize = threadPoolProperties.maximumSize().get();
105+
if (dynamicCoreSize > dynamicMaximumSize) {
106+
logger.error("Hystrix ThreadPool configuration at startup for : " + threadPoolKey.name() + " is trying to set coreSize = " +
107+
dynamicCoreSize + " and maximumSize = " + dynamicMaximumSize + ". Maximum size will be set to " +
108+
dynamicCoreSize + ", the coreSize value, since it must be equal to or greater than the coreSize value");
109+
return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory);
110+
} else {
111+
return new ThreadPoolExecutor(dynamicCoreSize, dynamicMaximumSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory);
112+
}
113+
} else {
114+
return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory);
115+
}
116+
}
117+
118+
private static ThreadFactory getThreadFactory(final HystrixThreadPoolKey threadPoolKey) {
78119
ThreadFactory threadFactory = null;
79120
if (!PlatformSpecific.isAppEngineStandardEnvironment()) {
80-
threadFactory = new ThreadFactory() {
121+
return new ThreadFactory() {
81122
protected final AtomicInteger threadNumber = new AtomicInteger(0);
82123

83124
@Override
@@ -89,19 +130,7 @@ public Thread newThread(Runnable r) {
89130

90131
};
91132
} else {
92-
threadFactory = PlatformSpecific.getAppEngineThreadFactory();
93-
}
94-
95-
final int dynamicCoreSize = corePoolSize.get();
96-
final int dynamicMaximumSize = maximumPoolSize.get();
97-
98-
if (dynamicCoreSize > dynamicMaximumSize) {
99-
logger.error("Hystrix ThreadPool configuration at startup for : " + threadPoolKey.name() + " is trying to set coreSize = " +
100-
dynamicCoreSize + " and maximumSize = " + dynamicMaximumSize + ". Maximum size will be set to " +
101-
dynamicCoreSize + ", the coreSize value, since it must be equal to or greater than the coreSize value");
102-
return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime.get(), unit, workQueue, threadFactory);
103-
} else {
104-
return new ThreadPoolExecutor(dynamicCoreSize, dynamicMaximumSize, keepAliveTime.get(), unit, workQueue, threadFactory);
133+
return PlatformSpecific.getAppEngineThreadFactory();
105134
}
106135
}
107136

0 commit comments

Comments
 (0)