Search in sources :

Example 11 with HystrixBadRequestException

use of com.netflix.hystrix.exception.HystrixBadRequestException in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testBadRequestExceptionObserve.

private void testBadRequestExceptionObserve(ExecutionIsolationStrategy isolationStrategy, boolean asyncException) {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    KnownHystrixBadRequestFailureTestCommand command1 = new KnownHystrixBadRequestFailureTestCommand(circuitBreaker, isolationStrategy, asyncException);
    try {
        command1.observe().toBlocking().single();
        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());
    }
    assertCommandExecutionEvents(command1, HystrixEventType.BAD_REQUEST);
    assertSaneHystrixRequestLog(1);
    assertNotNull(command1.getExecutionException());
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) TimeoutException(java.util.concurrent.TimeoutException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 with HystrixBadRequestException

use of com.netflix.hystrix.exception.HystrixBadRequestException in project Hystrix by Netflix.

the class AbstractCommand method executeCommandAndObserve.

/**
 * This decorates "Hystrix" functionality around the run() Observable.
 *
 * @return R
 */
private Observable<R> executeCommandAndObserve(final AbstractCommand<R> _cmd) {
    final HystrixRequestContext currentRequestContext = HystrixRequestContext.getContextForCurrentThread();
    final Action1<R> markEmits = new Action1<R>() {

        @Override
        public void call(R r) {
            if (shouldOutputOnNextEvents()) {
                executionResult = executionResult.addEvent(HystrixEventType.EMIT);
                eventNotifier.markEvent(HystrixEventType.EMIT, commandKey);
            }
            if (commandIsScalar()) {
                long latency = System.currentTimeMillis() - executionResult.getStartTimestamp();
                eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey);
                executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS);
                eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList());
                circuitBreaker.markSuccess();
            }
        }
    };
    final Action0 markOnCompleted = new Action0() {

        @Override
        public void call() {
            if (!commandIsScalar()) {
                long latency = System.currentTimeMillis() - executionResult.getStartTimestamp();
                eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey);
                executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS);
                eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList());
                circuitBreaker.markSuccess();
            }
        }
    };
    final Func1<Throwable, Observable<R>> handleFallback = new Func1<Throwable, Observable<R>>() {

        @Override
        public Observable<R> call(Throwable t) {
            circuitBreaker.markNonSuccess();
            Exception e = getExceptionFromThrowable(t);
            executionResult = executionResult.setExecutionException(e);
            if (e instanceof RejectedExecutionException) {
                return handleThreadPoolRejectionViaFallback(e);
            } else if (t instanceof HystrixTimeoutException) {
                return handleTimeoutViaFallback();
            } else if (t instanceof HystrixBadRequestException) {
                return handleBadRequestByEmittingError(e);
            } else {
                /*
                     * Treat HystrixBadRequestException from ExecutionHook like a plain HystrixBadRequestException.
                     */
                if (e instanceof HystrixBadRequestException) {
                    eventNotifier.markEvent(HystrixEventType.BAD_REQUEST, commandKey);
                    return Observable.error(e);
                }
                return handleFailureViaFallback(e);
            }
        }
    };
    final Action1<Notification<? super R>> setRequestContext = new Action1<Notification<? super R>>() {

        @Override
        public void call(Notification<? super R> rNotification) {
            setRequestContextIfNeeded(currentRequestContext);
        }
    };
    Observable<R> execution;
    if (properties.executionTimeoutEnabled().get()) {
        execution = executeCommandWithSpecifiedIsolation(_cmd).lift(new HystrixObservableTimeoutOperator<R>(_cmd));
    } else {
        execution = executeCommandWithSpecifiedIsolation(_cmd);
    }
    return execution.doOnNext(markEmits).doOnCompleted(markOnCompleted).onErrorResumeNext(handleFallback).doOnEach(setRequestContext);
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) HystrixTimeoutException(com.netflix.hystrix.exception.HystrixTimeoutException) Observable(rx.Observable) TimeoutException(java.util.concurrent.TimeoutException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) HystrixTimeoutException(com.netflix.hystrix.exception.HystrixTimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Notification(rx.Notification) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) Func1(rx.functions.Func1)

Example 13 with HystrixBadRequestException

use of com.netflix.hystrix.exception.HystrixBadRequestException in project Hystrix by Netflix.

the class HystrixCommandTest method testBadRequestExceptionViaQueueInThread.

/**
 * Test that a BadRequestException can be thrown and not count towards errors and bypasses fallback.
 */
@Test
public void testBadRequestExceptionViaQueueInThread() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    BadRequestCommand command1 = null;
    try {
        command1 = new BadRequestCommand(circuitBreaker, ExecutionIsolationStrategy.THREAD);
        command1.queue().get();
        fail("we expect to receive a " + HystrixBadRequestException.class.getSimpleName());
    } catch (ExecutionException e) {
        e.printStackTrace();
        if (e.getCause() instanceof HystrixBadRequestException) {
        // success
        } else {
            fail("We expect a " + HystrixBadRequestException.class.getSimpleName() + " but got a " + e.getClass().getSimpleName());
        }
    }
    assertCommandExecutionEvents(command1, HystrixEventType.BAD_REQUEST);
    assertNotNull(command1.getExecutionException());
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 14 with HystrixBadRequestException

use of com.netflix.hystrix.exception.HystrixBadRequestException in project Hystrix by Netflix.

the class HystrixCommandTest method testBadRequestExceptionViaQueueInThreadOnResponseFromCache.

/**
 * Test that BadRequestException behavior works the same on a cached response.
 */
@Test
public void testBadRequestExceptionViaQueueInThreadOnResponseFromCache() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    // execute once to cache the value
    BadRequestCommand command1 = null;
    try {
        command1 = new BadRequestCommand(circuitBreaker, ExecutionIsolationStrategy.THREAD);
        command1.execute();
    } catch (Throwable e) {
    // ignore
    }
    BadRequestCommand command2 = null;
    try {
        command2 = new BadRequestCommand(circuitBreaker, ExecutionIsolationStrategy.THREAD);
        command2.queue().get();
        fail("we expect to receive a " + HystrixBadRequestException.class.getSimpleName());
    } catch (ExecutionException e) {
        e.printStackTrace();
        if (e.getCause() instanceof HystrixBadRequestException) {
        // success
        } else {
            fail("We expect a " + HystrixBadRequestException.class.getSimpleName() + " but got a " + e.getClass().getSimpleName());
        }
    }
    assertCommandExecutionEvents(command1, HystrixEventType.BAD_REQUEST);
    assertCommandExecutionEvents(command2, HystrixEventType.BAD_REQUEST, HystrixEventType.RESPONSE_FROM_CACHE);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(2);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 15 with HystrixBadRequestException

use of com.netflix.hystrix.exception.HystrixBadRequestException in project Hystrix by Netflix.

the class HystrixCommandTest method testBadRequestExceptionViaExecuteInSemaphore.

/**
 * Test that a BadRequestException can be thrown and not count towards errors and bypasses fallback.
 */
@Test
public void testBadRequestExceptionViaExecuteInSemaphore() {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    BadRequestCommand command1 = new BadRequestCommand(circuitBreaker, ExecutionIsolationStrategy.SEMAPHORE);
    try {
        command1.execute();
        fail("we expect to receive a " + HystrixBadRequestException.class.getSimpleName());
    } catch (HystrixBadRequestException e) {
        // success
        e.printStackTrace();
    }
    assertCommandExecutionEvents(command1, HystrixEventType.BAD_REQUEST);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) Test(org.junit.Test)

Aggregations

HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)16 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)8 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 ExecutionException (java.util.concurrent.ExecutionException)6 TimeoutException (java.util.concurrent.TimeoutException)6 Test (org.junit.Test)6 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)5 IOException (java.io.IOException)4 CommandActionExecutionException (com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException)2 HystrixTimeoutException (com.netflix.hystrix.exception.HystrixTimeoutException)2 ByteBuf (io.netty.buffer.ByteBuf)2 HashMap (java.util.HashMap)2 Observable (rx.Observable)2 Action0 (rx.functions.Action0)2 Func1 (rx.functions.Func1)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 MockResponse (com.google.mockwebserver.MockResponse)1 MockWebServer (com.google.mockwebserver.MockWebServer)1 HystrixInvokable (com.netflix.hystrix.HystrixInvokable)1 HystrixCollapser (com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)1