Skip to content

Commit

Permalink
Hystrix.reset for lifecycle management
Browse files Browse the repository at this point in the history
I'm changing the design from the previous commits so it's more abstract and can handle any type of resources needing cleanup, not just threadpools.

ReactiveX/RxJava#45
  • Loading branch information
benjchristensen committed Feb 15, 2013
1 parent 324d03f commit 976f2fb
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
48 changes: 48 additions & 0 deletions hystrix-core/src/main/java/com/netflix/hystrix/Hystrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.netflix.hystrix;

import java.util.concurrent.TimeUnit;

/**
* Lifecycle management of Hystrix.
*/
public class Hystrix {

/**
* Reset state and release resources in use (such as thread-pools).
* <p>
* NOTE: This can result in race conditions if HystrixCommands are concurrently being executed.
* </p>
*/
public static void reset() {
// shutdown thread-pools
HystrixThreadPool.Factory.shutdown();
_reset();
}

/**
* Reset state and release resources in use (such as threadpools) and wait for completion.
* <p>
* NOTE: This can result in race conditions if HystrixCommands are concurrently being executed.
* </p>
*
* @param time time to wait for thread-pools to shutdown
* @param unit {@link TimeUnit} for <pre>time</pre> to wait for thread-pools to shutdown
*/
public static void reset(long time, TimeUnit unit) {
// shutdown thread-pools
HystrixThreadPool.Factory.shutdown(time, unit);
_reset();
}

/**
* Reset logic that doesn't have time/TimeUnit arguments.
*/
private static void _reset() {
// clear metrics
HystrixCommandMetrics.reset();
// clear collapsers
HystrixCollapser.reset();
// clear circuit breakers
HystrixCircuitBreaker.Factory.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public static HystrixCircuitBreaker getInstance(HystrixCommandKey key, HystrixCo
public static HystrixCircuitBreaker getInstance(HystrixCommandKey key) {
return circuitBreakersByCommand.get(key.name());
}

/**
* Clears all circuit breakers. If new requests come in instances will be recreated.
*/
/* package */ static void reset() {
circuitBreakersByCommand.clear();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,18 +346,6 @@ public Future<ResponseType> queue() {
return response;
}

/**
* Reset the global and request-scoped state for the given collapser key.
*
* This is intended to support uses of collapsers in an environment like a REPL
* where the definition of a collapser may change. It is not thread-safe and should
* not be used in a production environment.
*/
public static void resetCollapser(HystrixCollapserKey key) {
globalScopedCollapsers.remove(key.name());
requestScopedCollapsers.remove(key.name());
}

/**
* Static global cache of RequestCollapsers for Scope.GLOBAL
*/
Expand Down Expand Up @@ -994,6 +982,16 @@ protected String getCacheKey() {
return null;
}

/**
* Clears all state. If new requests come in instances will be recreated and metrics started from scratch.
*/
/* package */ static void reset() {
defaultNameCache.clear();
globalScopedCollapsers.clear();
requestScopedCollapsers.clear();
RealCollapserTimer.timer.reset();
}

private static String getDefaultNameFromClass(@SuppressWarnings("rawtypes") Class<? extends HystrixCollapser> cls) {
String fromCache = defaultNameCache.get(cls);
if (fromCache != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public static Collection<HystrixCommandMetrics> getInstances() {
return Collections.unmodifiableCollection(metrics.values());
}

/**
* Clears all state from metrics. If new requests come in instances will be recreated and metrics started from scratch.
*/
/* package */ static void reset() {
metrics.clear();
}

private final HystrixCommandProperties properties;
private final HystrixRollingNumber counter;
private final HystrixRollingPercentile percentileExecution;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public static HystrixTimer getInstance() {
return INSTANCE;
}

/**
* Clears all listeners.
* <p>
* NOTE: This will result in race conditions if {@link #addTimerListener(com.netflix.hystrix.util.HystrixTimer.TimerListener)} is being concurrently called.
* </p>
*/
public static void reset() {
INSTANCE.listenersPerInterval.clear();
INSTANCE.intervals.clear();
}

private TickThread tickThread = new TickThread();
private ConcurrentHashMap<Integer, ConcurrentLinkedQueue<Reference<TimerListener>>> listenersPerInterval = new ConcurrentHashMap<Integer, ConcurrentLinkedQueue<Reference<TimerListener>>>();
private ConcurrentLinkedQueue<TimerInterval> intervals = new ConcurrentLinkedQueue<TimerInterval>();
Expand Down

0 comments on commit 976f2fb

Please sign in to comment.