Skip to content

Commit

Permalink
Merge pull request #149 from benjchristensen/issue-144
Browse files Browse the repository at this point in the history
Allow getFallback to query failure states
  • Loading branch information
benjchristensen committed Jul 5, 2013
2 parents 6153597 + 2cb2d9d commit 09b6d17
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions hystrix-core/src/main/java/com/netflix/hystrix/HystrixCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -1121,17 +1121,19 @@ private R getFallbackOrThrowException(HystrixEventType eventType, FailureType fa
if (properties.fallbackEnabled().get()) {
/* fallback behavior is permitted so attempt */
try {
// record the executionResult
// do this before executing fallback so it can be queried from within getFallback (see See https://github.com/Netflix/Hystrix/pull/144)
executionResult = executionResult.addEvents(eventType);

// retrieve the fallback
R fallback = getFallbackWithProtection();
// mark fallback on counter
metrics.markFallbackSuccess();
// record the executionResult
executionResult = executionResult.addEvents(eventType, HystrixEventType.FALLBACK_SUCCESS);
executionResult = executionResult.addEvents(HystrixEventType.FALLBACK_SUCCESS);
return executionHook.onComplete(this, fallback);
} catch (UnsupportedOperationException fe) {
logger.debug("No fallback for HystrixCommand. ", fe); // debug only since we're throwing the exception and someone higher will do something with it
// record the executionResult
executionResult = executionResult.addEvents(eventType);

/* executionHook for all errors */
try {
Expand All @@ -1145,7 +1147,7 @@ private R getFallbackOrThrowException(HystrixEventType eventType, FailureType fa
logger.error("Error retrieving fallback for HystrixCommand. ", fe);
metrics.markFallbackFailure();
// record the executionResult
executionResult = executionResult.addEvents(eventType, HystrixEventType.FALLBACK_FAILURE);
executionResult = executionResult.addEvents(HystrixEventType.FALLBACK_FAILURE);

/* executionHook for all errors */
try {
Expand Down Expand Up @@ -1230,6 +1232,7 @@ private ExecutionResult(List<HystrixEventType> events, int executionTime, Except
* @return
*/
public ExecutionResult addEvents(HystrixEventType... events) {
// TODO are there performance reasons for this be changed to a persistent or concurrent data structure so we can append without copying?
ArrayList<HystrixEventType> newEvents = new ArrayList<HystrixEventType>();
newEvents.addAll(this.events);
for (HystrixEventType e : events) {
Expand Down Expand Up @@ -5394,6 +5397,37 @@ public void testExecutionFailureWithFallbackImplementedButDisabled() {
assertEquals(2, HystrixRequestLog.getCurrentRequest().getExecutedCommands().size());
}

@Test
public void testExecutionTimeoutValue() {
HystrixCommand.Setter properties = HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestKey"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionIsolationThreadTimeoutInMilliseconds(50));

HystrixCommand<String> command = new HystrixCommand<String>(properties) {
@Override
protected String run() throws Exception {
Thread.sleep(3000);
// should never reach here
return "hello";
}

@Override
protected String getFallback() {
if (isResponseTimedOut()) {
return "timed-out";
} else {
return "abc";
}
}
};

String value = command.execute();
assertTrue(command.isResponseTimedOut());
assertEquals("expected fallback value", "timed-out", value);

}

/* ******************************************************************************** */
/* ******************************************************************************** */
/* private HystrixCommand class implementations for unit testing */
Expand Down

0 comments on commit 09b6d17

Please sign in to comment.