Search in sources :

Example 86 with HystrixCommandKey

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

the class RollingCommandMaxConcurrencyStreamTest method testConcurrencyStreamProperlyFiltersOutResponseFromCache.

@Test
public void testConcurrencyStreamProperlyFiltersOutResponseFromCache() throws InterruptedException {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-F");
    stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 40);
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.RESPONSE_FROM_CACHE);
    Command cmd3 = Command.from(groupKey, key, HystrixEventType.RESPONSE_FROM_CACHE);
    Command cmd4 = Command.from(groupKey, key, HystrixEventType.RESPONSE_FROM_CACHE);
    cmd1.observe();
    Thread.sleep(5);
    cmd2.observe();
    cmd3.observe();
    cmd4.observe();
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(1, stream.getLatestRollingMax());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 87 with HystrixCommandKey

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

the class RollingCommandMaxConcurrencyStreamTest method testMultipleCommandsCarryOverMultipleBuckets.

/**
     * BUCKETS
     *     A    |    B    |    C    |    D    |    E    |
     * 1:  [-------------------------------]
     * 2:          [-------------------------------]
     * 3:                      [--]
     * 4:                              [--]
     *
     * Max concurrency should be 3
     */
@Test
public void testMultipleCommandsCarryOverMultipleBuckets() throws InterruptedException {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-D");
    stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).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(3, stream.getLatestRollingMax());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 88 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey 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 89 with HystrixCommandKey

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

the class RollingCommandMaxConcurrencyStreamTest method testEmptyStreamProducesZeros.

@Test
public void testEmptyStreamProducesZeros() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-A");
    stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 500);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(5).subscribe(getSubscriber(latch));
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(0, stream.getLatestRollingMax());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 90 with HystrixCommandKey

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

the class RollingCommandMaxConcurrencyStreamTest method testOneCommandCarriesOverToNextBucket.

/***
     * 3 Commands,
     * Command 1 gets started in Bucket A and not completed until Bucket B
     * Commands 2 and 3 both start and end in Bucket B, and there should be a max-concurrency of 3
     */
@Test
public void testOneCommandCarriesOverToNextBucket() throws InterruptedException {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-C");
    stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(5).subscribe(getSubscriber(latch));
    Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 160);
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
    Command cmd3 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 15);
    cmd1.observe();
    //bucket roll
    Thread.sleep(100);
    cmd2.observe();
    Thread.sleep(1);
    cmd3.observe();
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    assertEquals(3, stream.getLatestRollingMax());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) 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