Search in sources :

Example 16 with HystrixContextRunnable

use of com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable in project Hystrix by Netflix.

the class CumulativeThreadPoolEventCounterStreamTest method testSemaphoreRejected.

@Test
public void testSemaphoreRejected() {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-H");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-H");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-H");
    stream = CumulativeThreadPoolEventCounterStream.getInstance(threadPoolKey, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    //10 commands will saturate semaphore when called from different threads.
    //submit 2 more requests and they should be SEMAPHORE_REJECTED
    //should see 10 SUCCESSes, 2 SEMAPHORE_REJECTED and 2 FALLBACK_SUCCESSes
    List<Command> saturators = new ArrayList<Command>();
    for (int i = 0; i < 10; i++) {
        saturators.add(CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 300, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE));
    }
    CommandStreamTest.Command rejected1 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 0, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE);
    CommandStreamTest.Command rejected2 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 0, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE);
    for (final CommandStreamTest.Command saturator : saturators) {
        new Thread(new HystrixContextRunnable(new Runnable() {

            @Override
            public void run() {
                saturator.observe();
            }
        })).start();
    }
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
        fail(ie.getMessage());
    }
    rejected1.observe();
    rejected2.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertTrue(rejected1.isResponseSemaphoreRejected());
    assertTrue(rejected2.isResponseSemaphoreRejected());
    //none of these got executed on a thread-pool, so thread pool metrics should be 0
    assertEquals(2, stream.getLatest().length);
    assertEquals(0, stream.getLatestCount(HystrixEventType.ThreadPool.EXECUTED));
    assertEquals(0, stream.getLatestCount(HystrixEventType.ThreadPool.REJECTED));
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) ArrayList(java.util.ArrayList) HystrixThreadPoolKey(com.netflix.hystrix.HystrixThreadPoolKey) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 17 with HystrixContextRunnable

use of com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable in project Hystrix by Netflix.

the class RollingCommandMaxConcurrencyStreamTest method testConcurrencyStreamProperlyFiltersOutSemaphoreRejections.

@Test
public void testConcurrencyStreamProperlyFiltersOutSemaphoreRejections() throws InterruptedException {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-H");
    stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    //10 commands executed concurrently on different caller threads should saturate semaphore
    //once these are in-flight, execute 10 more concurrently on new caller threads.
    //since these are semaphore-rejected, the max concurrency should be 10
    List<Command> saturators = new ArrayList<Command>();
    for (int i = 0; i < 10; i++) {
        saturators.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 400, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE));
    }
    final List<Command> rejected = new ArrayList<Command>();
    for (int i = 0; i < 10; i++) {
        rejected.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 100, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE));
    }
    for (final Command saturatingCmd : saturators) {
        threadPool.submit(new HystrixContextRunnable(new Runnable() {

            @Override
            public void run() {
                saturatingCmd.observe();
            }
        }));
    }
    Thread.sleep(30);
    for (final Command rejectedCmd : rejected) {
        threadPool.submit(new HystrixContextRunnable(new Runnable() {

            @Override
            public void run() {
                rejectedCmd.observe();
            }
        }));
    }
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(10, stream.getLatestRollingMax());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 18 with HystrixContextRunnable

use of com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable in project Hystrix by Netflix.

the class RollingThreadPoolEventCounterStreamTest method testSemaphoreRejected.

@Test
public void testSemaphoreRejected() {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-H");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-H");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingCounter-H");
    stream = RollingThreadPoolEventCounterStream.getInstance(threadPoolKey, 10, 500);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(5).subscribe(getSubscriber(latch));
    //10 commands will saturate semaphore when called from different threads.
    //submit 2 more requests and they should be SEMAPHORE_REJECTED
    //should see 10 SUCCESSes, 2 SEMAPHORE_REJECTED and 2 FALLBACK_SUCCESSes
    List<Command> saturators = new ArrayList<Command>();
    for (int i = 0; i < 10; i++) {
        saturators.add(CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 500, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE));
    }
    CommandStreamTest.Command rejected1 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 0, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE);
    CommandStreamTest.Command rejected2 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 0, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE);
    for (final CommandStreamTest.Command saturator : saturators) {
        new Thread(new HystrixContextRunnable(new Runnable() {

            @Override
            public void run() {
                saturator.observe();
            }
        })).start();
    }
    try {
        Thread.sleep(100);
    } catch (InterruptedException ie) {
        fail(ie.getMessage());
    }
    rejected1.observe();
    rejected2.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertTrue(rejected1.isResponseSemaphoreRejected());
    assertTrue(rejected2.isResponseSemaphoreRejected());
    //none of these got executed on a thread-pool, so thread pool metrics should be 0
    assertEquals(2, stream.getLatest().length);
    assertEquals(0, stream.getLatestCount(HystrixEventType.ThreadPool.EXECUTED));
    assertEquals(0, stream.getLatestCount(HystrixEventType.ThreadPool.REJECTED));
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) ArrayList(java.util.ArrayList) HystrixThreadPoolKey(com.netflix.hystrix.HystrixThreadPoolKey) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Aggregations

HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)18 Test (org.junit.Test)18 CountDownLatch (java.util.concurrent.CountDownLatch)11 ArrayList (java.util.ArrayList)10 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)8 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)8 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)8 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)8 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)8 IOException (java.io.IOException)8 ExecutionException (java.util.concurrent.ExecutionException)8 TimeoutException (java.util.concurrent.TimeoutException)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)5 CancellationException (java.util.concurrent.CancellationException)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)5 TryableSemaphoreActual (com.netflix.hystrix.AbstractCommand.TryableSemaphoreActual)4 HystrixCommandGroupKey (com.netflix.hystrix.HystrixCommandGroupKey)3 HystrixThreadPoolKey (com.netflix.hystrix.HystrixThreadPoolKey)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3