Search in sources :

Example 81 with HystrixCommandKey

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

the class RollingCommandLatencyDistributionStreamTest method testSingleBucketWithMultipleEventTypes.

/*
     * The following event types should not have their latency measured:
     * THREAD_POOL_REJECTED
     * SEMAPHORE_REJECTED
     * SHORT_CIRCUITED
     * RESPONSE_FROM_CACHE
     *
     * Newly measured (as of 1.5)
     * BAD_REQUEST
     * FAILURE
     * TIMEOUT
     */
@Test
public void testSingleBucketWithMultipleEventTypes() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-C");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
            if (distribution.getTotalCount() < 4 && distribution.getTotalCount() > 0) {
                //buckets before timeout latency registers
                assertBetween(10, 50, (int) distribution.getMean());
            } else if (distribution.getTotalCount() == 4) {
                //now timeout latency of 600ms is there
                assertBetween(150, 250, (int) distribution.getMean());
            }
        }
    });
    Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
    //latency = 600
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.TIMEOUT);
    Command cmd3 = Command.from(groupKey, key, HystrixEventType.FAILURE, 30);
    Command cmd4 = Command.from(groupKey, key, HystrixEventType.BAD_REQUEST, 40);
    cmd1.observe();
    cmd2.observe();
    cmd3.observe();
    cmd4.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    //now timeout latency of 600ms is there
    assertBetween(150, 350, stream.getLatestMean());
    assertBetween(10, 40, stream.getLatestPercentile(0.0));
    assertBetween(600, 800, stream.getLatestPercentile(100.0));
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 82 with HystrixCommandKey

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

the class RollingCommandLatencyDistributionStreamTest method testMultipleBucketsBothGetStoredAndThenAgeOut.

/**
     * The extra takes on the stream should give enough time for all of the measured latencies to age out
     */
@Test
public void testMultipleBucketsBothGetStoredAndThenAgeOut() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-I");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(30).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
            if (distribution.getTotalCount() == 2) {
                assertBetween(55, 90, (int) distribution.getMean());
            }
            if (distribution.getTotalCount() == 5) {
                assertEquals(60, 90, (long) distribution.getMean());
            }
        }
    });
    Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.FAILURE, 100);
    cmd1.observe();
    cmd2.observe();
    try {
        Thread.sleep(500);
    } catch (InterruptedException ie) {
        fail("Interrupted ex");
    }
    Command cmd3 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 60);
    Command cmd4 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 60);
    Command cmd5 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 70);
    cmd3.observe();
    cmd4.observe();
    cmd5.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(0, stream.getLatest().getTotalCount());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 83 with HystrixCommandKey

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

the class RollingCommandLatencyDistributionStreamTest method testThreadPoolRejectedCommandDoesNotGetLatencyTracked.

@Test
public void testThreadPoolRejectedCommandDoesNotGetLatencyTracked() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-E");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    //10 commands with latency should occupy the entire threadpool.  execute those, then wait for bucket to roll
    //next command should be a thread-pool rejection
    List<Command> commands = new ArrayList<Command>();
    for (int i = 0; i < 10; i++) {
        commands.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 200));
    }
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
        //                if (distribution.getTotalCount() > 0) {
        //                    assertBetween(200, 250, (int) distribution.getMean());
        //                }
        }
    });
    for (Command cmd : commands) {
        cmd.observe();
    }
    Command threadPoolRejected = Command.from(groupKey, key, HystrixEventType.SUCCESS);
    try {
        Thread.sleep(40);
        threadPoolRejected.observe();
    } catch (InterruptedException ie) {
        fail(ie.getMessage());
    }
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(10, stream.getLatest().getTotalCount());
    assertBetween(200, 250, stream.getLatestMean());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertTrue(threadPoolRejected.isResponseThreadPoolRejected());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 84 with HystrixCommandKey

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

the class RollingCommandLatencyDistributionStreamTest method testShortCircuitedCommandDoesNotGetLatencyTracked.

@Test
public void testShortCircuitedCommandDoesNotGetLatencyTracked() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-D");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    //3 failures is enough to trigger short-circuit.  execute those, then wait for bucket to roll
    //next command should be a short-circuit
    List<Command> commands = new ArrayList<Command>();
    for (int i = 0; i < 3; i++) {
        commands.add(Command.from(groupKey, key, HystrixEventType.FAILURE, 0));
    }
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
            assertBetween(0, 30, (int) distribution.getMean());
        }
    });
    for (Command cmd : commands) {
        cmd.observe();
    }
    Command shortCircuit = Command.from(groupKey, key, HystrixEventType.SUCCESS);
    try {
        Thread.sleep(200);
        shortCircuit.observe();
    } catch (InterruptedException ie) {
        fail(ie.getMessage());
    }
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(3, stream.getLatest().getTotalCount());
    assertBetween(0, 30, stream.getLatestMean());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertTrue(shortCircuit.isResponseShortCircuited());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 85 with HystrixCommandKey

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

the class RollingCommandMaxConcurrencyStreamTest method testConcurrencyStreamProperlyFiltersOutThreadPoolRejections.

@Test
public void testConcurrencyStreamProperlyFiltersOutThreadPoolRejections() throws InterruptedException {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-I");
    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 should saturate the Hystrix threadpool
    //once these are in-flight, execute 10 more concurrently
    //since these are threadpool-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));
    }
    final List<Command> rejected = new ArrayList<Command>();
    for (int i = 0; i < 10; i++) {
        rejected.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 100));
    }
    for (final Command saturatingCmd : saturators) {
        saturatingCmd.observe();
    }
    Thread.sleep(30);
    for (final Command rejectedCmd : rejected) {
        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) ArrayList(java.util.ArrayList) 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