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

Bypass fallback/circuit breaker for BadRequestExceptions from ExecutionHook #456

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
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@ public Observable<R> call(Throwable t) {
} catch (Exception hookException) {
logger.warn("Error calling ExecutionHook.endRunFailure", hookException);
}
/*
* Treat HystrixBadRequestException from ExecutionHook like a plain HystrixBadRequestException.
*/
if (e instanceof HystrixBadRequestException){
return Observable.error(e);
}

logger.debug("Error executing HystrixCommand.run(). Proceeding to fallback logic ...", e);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4347,6 +4347,25 @@ public void call(Throwable t1) {
assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}

@Test
public void testExceptionConvertedToBadRequestExceptionInExecutionHookBypassesCircuitBreaker(){
TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
try {
new ExceptionToBadRequestByExecutionHookCommand(circuitBreaker, ExecutionIsolationStrategy.THREAD).execute();
fail("we expect to receive a " + HystrixBadRequestException.class.getSimpleName());
} catch (HystrixBadRequestException e) {
// success
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
fail("We expect a " + HystrixBadRequestException.class.getSimpleName() + " but got a " + e.getClass().getSimpleName());
}

assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));
assertEquals(0, circuitBreaker.metrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));
}

/* ******************************************************************************** */
/* ******************************************************************************** */
/* private HystrixCommand class implementations for unit testing */
Expand Down Expand Up @@ -4435,6 +4454,11 @@ TestCommandBuilder setExecutionSemaphore(TryableSemaphore executionSemaphore) {
return this;
}

TestCommandBuilder setExecutionHook(TestExecutionHook executionHook) {
this.executionHook = executionHook;
return this;
}

}

}
Expand Down Expand Up @@ -5217,6 +5241,37 @@ protected String getCacheKey() {

}

private static class BusinessException extends Exception {
public BusinessException(String msg) {
super(msg);
}
}

private static class ExceptionToBadRequestByExecutionHookCommand extends TestHystrixCommand<Boolean> {
public ExceptionToBadRequestByExecutionHookCommand(TestCircuitBreaker circuitBreaker, ExecutionIsolationStrategy isolationType) {
super(testPropsBuilder()
.setCircuitBreaker(circuitBreaker)
.setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter().withExecutionIsolationStrategy(isolationType))
.setExecutionHook(new TestExecutionHook(){
@Override
public <T> Exception onRunError(HystrixInvokable<T> commandInstance, Exception e) {
super.onRunError(commandInstance, e);
return new HystrixBadRequestException("autoconverted exception", e);
}
}));
}

@Override
protected Boolean run() throws BusinessException {
throw new BusinessException("invalid input by the user");
}

@Override
protected String getCacheKey() {
return "nein";
}
}

private static class CommandWithErrorThrown extends TestHystrixCommand<Boolean> {

public CommandWithErrorThrown(TestCircuitBreaker circuitBreaker) {
Expand Down Expand Up @@ -5404,4 +5459,5 @@ public <T> void onThreadComplete(HystrixInvokable<T> commandInstance) {
}

}

}