Runtime changes to allowMaximumSizeToDivergeFromCoreSize are ignored
I am configuring my application using Archaius and a local configuration file (set by -Darchaius.configurationSource.additionalUrls), and the hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize property seems to be ignored by Hystrix - making all my threadpools have a fixed size = coreSize.
I have not yet created a test case to support the theory, but by browsing HystrixThreadPoolProperties.java I believe this happens because allowMaximumSizeToDivergeFromCoreSize is set as a boolean instead of a HystrixProperty<Boolean>, so it is only read once once during the threadpool initialization phase and any further changes are ignored.
Here's a simple way to reproduce the issue:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.slf4j.LoggerFactory;
public class BugReproducer {
public static void main(String[] args) {
((Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(Level.INFO);
System.setProperty("archaius.configurationSource.additionalUrls", "file:///tmp/reproduce-the-bug.properties");
HystrixCommand.Setter setter = HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("reproduce-the-bug"));
int numThreads = 5;
final ExecutorService executor = Executors.newFixedThreadPool(numThreads);
final CircuitBreaker circuitBreaker = new CircuitBreaker(setter);
int i = 0;
while (i < numThreads) {
executor.submit(new CommandRunnable(circuitBreaker, "bug " + i));
i++;
}
}
}
class CommandRunnable implements Runnable {
private static final Logger logger = (Logger) LoggerFactory.getLogger(CommandRunnable.class);
private final CircuitBreaker circuitBreaker;
private final String text;
CommandRunnable(CircuitBreaker circuitBreaker, String text) {
this.circuitBreaker = circuitBreaker;
this.text = text;
}
@Override
public void run() {
try {
logger.info(circuitBreaker.returnText(text));
} catch (Exception e) {
logger.error(e.getCause().getMessage());
}
}
}
class CircuitBreaker {
private final HystrixCommand.Setter setter;
CircuitBreaker(HystrixCommand.Setter setter) {
this.setter = setter;
}
String returnText(String text) {
return new HystrixCommand<String>(setter) {
@Override
protected String run() throws Exception {
return text;
}
}.execute();
}
}
Along with /tmp/reproduce-the-bug.properties:
hystrix.command.default.fallback.enabled=false
hystrix.threadpool.default.coreSize=2
hystrix.threadpool.default.maximumSize=5
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true
Running it will output:
2016-12-06 11:12:42,185 [pool-1-thread-1] INFO com.netflix.config.sources.URLConfigurationSource - URLs to be used as dynamic configuration source: [file:/tmp/reproduce-the-bug.properties]
2016-12-06 11:12:42,200 [pool-1-thread-4] INFO com.netflix.config.DynamicPropertyFactory - DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@1e1871c4
2016-12-06 11:12:42,389 [pool-1-thread-2] ERROR CommandRunnable - Task java.util.concurrent.FutureTask@37329359 rejected from java.util.concurrent.ThreadPoolExecutor@554fe138[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
2016-12-06 11:12:42,389 [pool-1-thread-1] ERROR CommandRunnable - Task java.util.concurrent.FutureTask@5a74f816 rejected from java.util.concurrent.ThreadPoolExecutor@554fe138[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
2016-12-06 11:12:42,389 [pool-1-thread-3] ERROR CommandRunnable - Task java.util.concurrent.FutureTask@90efd2d rejected from java.util.concurrent.ThreadPoolExecutor@554fe138[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
2016-12-06 11:12:42,394 [pool-1-thread-4] INFO CommandRunnable - bug 3
2016-12-06 11:12:42,395 [pool-1-thread-5] INFO CommandRunnable - bug 4
If I set the same property using the Setter builder:
HystrixCommand.Setter setter = HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("reproduce-the-bug"))
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.defaultSetter().withAllowMaximumSizeToDivergeFromCoreSize(true))
Will output as expected:
2016-12-06 11:15:12,702 [pool-1-thread-1] INFO com.netflix.config.sources.URLConfigurationSource - URLs to be used as dynamic configuration source: [file:/tmp/reproduce-the-bug.properties]
2016-12-06 11:15:12,713 [pool-1-thread-4] INFO com.netflix.config.DynamicPropertyFactory - DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@675b624f
2016-12-06 11:15:12,902 [pool-1-thread-2] INFO CommandRunnable - bug 1
2016-12-06 11:15:12,902 [pool-1-thread-4] INFO CommandRunnable - bug 3
2016-12-06 11:15:12,902 [pool-1-thread-1] INFO CommandRunnable - bug 0
2016-12-06 11:15:12,902 [pool-1-thread-5] INFO CommandRunnable - bug 4
2016-12-06 11:15:12,902 [pool-1-thread-3] INFO CommandRunnable - bug 2
Just to make sure I'm clear, you're not attempting to change this value at runtime, you're just using the system property to attempt to set it?
That's correct, I'm just trying to initialize a circuit breaker using the properties defined in an Archaius-enabled configuration file (no runtime changes).
Having said that, would it make a difference? There's nothing on the documentation stating that changing that property at runtime is not allowed.
Created #1435, and tested it successfully against my provided test case.
My initial implementation (which looks to have not considered the System-property case) was imagining only setting this value at startup, since it has such a large impact. Not sure that there's a great reason to disallow dynamic-update for this property and not others, so I'll review the change. Thanks for the contribution!
You're welcome! While implementing #1435 I realized that there are actually two different issues at play here:
allowMaximumSizeToDivergeFromCoreSizecan't be changed at runtimethreadpool.default.allowMaximumSizeToDivergeFromCoreSizeis never actually read
I believe my pull request fixes both cases. Let me know if you need any further change or clarification.