Search in sources :

Example 41 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class RollingThreadPoolMaxConcurrencyStreamTest method testConcurrencyStreamProperlyFiltersOutShortCircuits.

@Test
public void testConcurrencyStreamProperlyFiltersOutShortCircuits() throws InterruptedException {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-H");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-H");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-H");
    stream = RollingThreadPoolMaxConcurrencyStream.getInstance(threadPoolKey, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    //after 3 failures, next command should short-circuit.
    //to prove short-circuited commands don't contribute to concurrency, execute 3 FAILURES in the first bucket sequentially
    //then when circuit is open, execute 20 concurrent commands.  they should all get short-circuited, and max concurrency should be 1
    Command failure1 = Command.from(groupKey, key, HystrixEventType.FAILURE);
    Command failure2 = Command.from(groupKey, key, HystrixEventType.FAILURE);
    Command failure3 = Command.from(groupKey, key, HystrixEventType.FAILURE);
    List<Command> shortCircuited = new ArrayList<Command>();
    for (int i = 0; i < 20; i++) {
        shortCircuited.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 100));
    }
    failure1.execute();
    failure2.execute();
    failure3.execute();
    Thread.sleep(150);
    for (Command cmd : shortCircuited) {
        cmd.observe();
    }
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    for (Command cmd : shortCircuited) {
        assertTrue(cmd.isResponseShortCircuited());
    }
    assertEquals(1, stream.getLatestRollingMax());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) ArrayList(java.util.ArrayList) HystrixThreadPoolKey(com.netflix.hystrix.HystrixThreadPoolKey) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 42 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class RollingThreadPoolMaxConcurrencyStreamTest method testMultipleCommandsCarryOverMultipleBucketsAndThenAgeOut.

/**
     * BUCKETS
     *     A    |    B    |    C    |    D    |    E    |
     * 1:  [-------------------------------]
     * 2:          [-------------------------------]
     * 3:                      [--]
     * 4:                              [--]
     *
     * Max concurrency should be 3, but by waiting for 30 bucket rolls, final max concurrency should be 0
     */
@Test
public void testMultipleCommandsCarryOverMultipleBucketsAndThenAgeOut() throws InterruptedException {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-F");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-F");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-F");
    stream = RollingThreadPoolMaxConcurrencyStream.getInstance(threadPoolKey, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(30).subscribe(getSubscriber(latch));
    Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 300);
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 300);
    Command cmd3 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
    Command cmd4 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
    cmd1.observe();
    //bucket roll
    Thread.sleep(100);
    cmd2.observe();
    Thread.sleep(100);
    cmd3.observe();
    Thread.sleep(100);
    cmd4.observe();
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    assertEquals(0, stream.getLatestRollingMax());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) HystrixThreadPoolKey(com.netflix.hystrix.HystrixThreadPoolKey) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 43 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class RollingCommandEventCounterStreamTest method testEmptyStreamProducesZeros.

@Test
public void testEmptyStreamProducesZeros() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-A");
    stream = RollingCommandEventCounterStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(HystrixEventType.values().length, stream.getLatest().length);
    assertFalse(hasData(stream.getLatest()));
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 44 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class RollingCommandEventCounterStreamTest method testThreadPoolRejected.

@Test
public void testThreadPoolRejected() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-I");
    stream = RollingCommandEventCounterStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    //10 commands will saturate threadpools when called concurrently.
    //submit 2 more requests and they should be THREADPOOL_REJECTED
    //should see 10 SUCCESSes, 2 THREADPOOL_REJECTED and 2 FALLBACK_SUCCESSes
    List<CommandStreamTest.Command> saturators = new ArrayList<CommandStreamTest.Command>();
    for (int i = 0; i < 10; i++) {
        saturators.add(CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 200));
    }
    CommandStreamTest.Command rejected1 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 0);
    CommandStreamTest.Command rejected2 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 0);
    for (final CommandStreamTest.Command saturator : saturators) {
        saturator.observe();
    }
    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.isResponseThreadPoolRejected());
    assertTrue(rejected2.isResponseThreadPoolRejected());
    assertEquals(HystrixEventType.values().length, stream.getLatest().length);
    long[] expected = new long[HystrixEventType.values().length];
    expected[HystrixEventType.SUCCESS.ordinal()] = 10;
    expected[HystrixEventType.THREAD_POOL_REJECTED.ordinal()] = 2;
    expected[HystrixEventType.FALLBACK_SUCCESS.ordinal()] = 2;
    assertArrayEquals(expected, stream.getLatest());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 45 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class RollingCommandEventCounterStreamTest method testSingleFailure.

@Test
public void testSingleFailure() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-C");
    stream = RollingCommandEventCounterStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    CommandStreamTest.Command cmd = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 20);
    cmd.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(HystrixEventType.values().length, stream.getLatest().length);
    long[] expected = new long[HystrixEventType.values().length];
    expected[HystrixEventType.FAILURE.ordinal()] = 1;
    expected[HystrixEventType.FALLBACK_SUCCESS.ordinal()] = 1;
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertArrayEquals(expected, stream.getLatest());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Aggregations

HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)113 Test (org.junit.Test)106 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)99 CountDownLatch (java.util.concurrent.CountDownLatch)99 HystrixThreadPoolKey (com.netflix.hystrix.HystrixThreadPoolKey)42 HystrixCommandGroupKey (com.netflix.hystrix.HystrixCommandGroupKey)38 ArrayList (java.util.ArrayList)25 CachedValuesHistogram (com.netflix.hystrix.metric.CachedValuesHistogram)9 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)8 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)8 HystrixCommandMetrics (com.netflix.hystrix.HystrixCommandMetrics)7 HystrixCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreaker)5 HystrixCommand (com.netflix.hystrix.HystrixCommand)4 Map (java.util.Map)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 HystrixPropertiesCommandDefault (com.netflix.hystrix.strategy.properties.HystrixPropertiesCommandDefault)3 StringWriter (java.io.StringWriter)3 HystrixCollapserKey (com.netflix.hystrix.HystrixCollapserKey)2 HystrixCollapserConfiguration (com.netflix.hystrix.config.HystrixCollapserConfiguration)2 HystrixCommandConfiguration (com.netflix.hystrix.config.HystrixCommandConfiguration)2