From e20fe47cbb12e62864966e6d50cf44ae4b6063f8 Mon Sep 17 00:00:00 2001 From: Matt Jacobs Date: Mon, 19 Jan 2015 13:26:26 -0800 Subject: [PATCH] Creating a synthetic exception in the semaphore execution and short-circuited case that gets passed to onError() hook --- .../java/com/netflix/hystrix/AbstractCommand.java | 11 ++--------- .../java/com/netflix/hystrix/HystrixCommandTest.java | 12 ++++++------ .../hystrix/HystrixObservableCommandTest.java | 12 ++++++------ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java b/hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java index 849525dfa..9f0600962 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java @@ -404,7 +404,7 @@ public void call() { metrics.markSemaphoreRejection(); logger.debug("HystrixCommand Execution Rejection by Semaphore."); // debug only since we're throwing the exception and someone higher will do something with it // retrieve a fallback or throw an exception if no fallback available - getFallbackOrThrowException(HystrixEventType.SEMAPHORE_REJECTED, FailureType.REJECTED_SEMAPHORE_EXECUTION, "could not acquire a semaphore for execution"). + getFallbackOrThrowException(HystrixEventType.SEMAPHORE_REJECTED, FailureType.REJECTED_SEMAPHORE_EXECUTION, "could not acquire a semaphore for execution", new RuntimeException("could not acquire a semaphore for execution")). map(new Func1() { @Override @@ -420,7 +420,7 @@ public R call(R t1) { metrics.markShortCircuited(); // short-circuit and go directly to fallback (or throw an exception if no fallback implemented) try { - getFallbackOrThrowException(HystrixEventType.SHORT_CIRCUITED, FailureType.SHORTCIRCUIT, "short-circuited") + getFallbackOrThrowException(HystrixEventType.SHORT_CIRCUITED, FailureType.SHORTCIRCUIT, "short-circuited", new RuntimeException("Hystrix circuit short-circuited and is OPEN")) .map(new Func1() { @Override @@ -772,13 +772,6 @@ public void call() { } } - /** - * @throws HystrixRuntimeException - */ - private Observable getFallbackOrThrowException(HystrixEventType eventType, FailureType failureType, String message) { - return getFallbackOrThrowException(eventType, failureType, message, null); - } - /** * @throws HystrixRuntimeException */ diff --git a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixCommandTest.java b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixCommandTest.java index aa4e628f7..9863430f9 100644 --- a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixCommandTest.java +++ b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixCommandTest.java @@ -4426,7 +4426,7 @@ public TestHystrixCommand call() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.SHORTCIRCUIT, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4496,7 +4496,7 @@ public TestHystrixCommand call() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.SHORTCIRCUIT, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4751,7 +4751,7 @@ public void run() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, null=ex in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.REJECTED_SEMAPHORE_EXECUTION, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4879,7 +4879,7 @@ public void run() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.REJECTED_SEMAPHORE_EXECUTION, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4915,7 +4915,7 @@ public TestHystrixCommand call() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.SHORTCIRCUIT, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4987,7 +4987,7 @@ public TestHystrixCommand call() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.SHORTCIRCUIT, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); diff --git a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixObservableCommandTest.java b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixObservableCommandTest.java index d916e500b..cb5689e32 100644 --- a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixObservableCommandTest.java +++ b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixObservableCommandTest.java @@ -4012,7 +4012,7 @@ public TestHystrixCommand call() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.SHORTCIRCUIT, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4082,7 +4082,7 @@ public TestHystrixCommand call() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.SHORTCIRCUIT, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4337,7 +4337,7 @@ public void run() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, null=ex in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.REJECTED_SEMAPHORE_EXECUTION, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4465,7 +4465,7 @@ public void run() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.REJECTED_SEMAPHORE_EXECUTION, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4501,7 +4501,7 @@ public TestHystrixCommand call() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.SHORTCIRCUIT, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse); @@ -4573,7 +4573,7 @@ public TestHystrixCommand call() { public void call(TestHystrixCommand command) { assertEquals(1, command.builder.executionHook.startExecute.get()); assertNull(command.builder.executionHook.endExecuteSuccessResponse); - assertNull(command.builder.executionHook.endExecuteFailureException); //by design, ex=null in this case + assertEquals(RuntimeException.class, command.builder.executionHook.endExecuteFailureException.getClass()); assertEquals(FailureType.SHORTCIRCUIT, command.builder.executionHook.endExecuteFailureType); assertEquals(0, command.builder.executionHook.startRun.get()); assertNull(command.builder.executionHook.runSuccessResponse);