Skip to content

Commit

Permalink
Merge pull request #453 from mattrjacobs/resettable-command-and-threa…
Browse files Browse the repository at this point in the history
…d-pool-defaults

Resettable command and thread pool defaults
  • Loading branch information
mattrjacobs committed Jan 7, 2015
2 parents 670a3bf + af06360 commit 57cfcb4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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;
}
}
}

0 comments on commit 57cfcb4

Please sign in to comment.