Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resettable command and thread pool defaults #453

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hystrix-core/src/main/java/com/netflix/hystrix/Hystrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.TimeUnit;

import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -58,6 +59,7 @@ private static void _reset() {
// clear circuit breakers
HystrixCircuitBreaker.Factory.reset();
HystrixPlugins.reset();
HystrixPropertiesFactory.reset();
}

private static ThreadLocal<LinkedList<HystrixCommandKey>> currentCommand = new ThreadLocal<LinkedList<HystrixCommandKey>>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
*/
public class HystrixPropertiesFactory {

/**
* Clears all the defaults in the static property cache. This makes it possible for property defaults to not persist for
* an entire JVM lifetime. May be invoked directly, and also gets invoked by <code>Hystrix.reset()</code>
*/
public static void reset() {
commandProperties.clear();
threadPoolProperties.clear();
}

// String is CommandKey.name() (we can't use CommandKey directly as we can't guarantee it implements hashcode/equals correctly)
private static final ConcurrentHashMap<String, HystrixCommandProperties> commandProperties = new ConcurrentHashMap<String, HystrixCommandProperties>();

Expand Down
27 changes: 27 additions & 0 deletions hystrix-core/src/test/java/com/netflix/hystrix/HystrixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,31 @@ protected Boolean run() {
assertNull(Hystrix.getCurrentThreadExecutingCommand());
}

//see https://github.com/Netflix/Hystrix/issues/280
@Test
public void testResetCommandProperties() {
HystrixCommand<Boolean> cmd1 = new ResettableCommand(100, 10);
assertEquals(100L, (long) cmd1.getProperties().executionIsolationThreadTimeoutInMilliseconds().get());
assertEquals(10L, (long) cmd1.threadPool.getExecutor().getCorePoolSize());

Hystrix.reset();

HystrixCommand<Boolean> cmd2 = new ResettableCommand(700, 40);
assertEquals(700L, (long) cmd2.getProperties().executionIsolationThreadTimeoutInMilliseconds().get());
assertEquals(40L, (long) cmd2.threadPool.getExecutor().getCorePoolSize());

}

private static class ResettableCommand extends HystrixCommand<Boolean> {
ResettableCommand(int timeout, int poolCoreSize) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(timeout))
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(poolCoreSize)));
}

@Override
protected Boolean run() throws Exception {
return true;
}
}
}