Search in sources :

Example 21 with HystrixThreadPoolKey

use of com.netflix.hystrix.HystrixThreadPoolKey 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 22 with HystrixThreadPoolKey

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

the class RollingThreadPoolMaxConcurrencyStreamTest method testConcurrencyStreamProperlyFiltersOutSemaphoreRejections.

@Test
public void testConcurrencyStreamProperlyFiltersOutSemaphoreRejections() throws InterruptedException {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-I");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-I");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-I");
    stream = RollingThreadPoolMaxConcurrencyStream.getInstance(threadPoolKey, 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());
    for (Command rejectedCmd : rejected) {
        assertTrue(rejectedCmd.isResponseSemaphoreRejected() || rejectedCmd.isResponseShortCircuited());
    }
    //should be 0 since all are executed in a semaphore
    assertEquals(0, 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) 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 23 with HystrixThreadPoolKey

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

the class RollingThreadPoolMaxConcurrencyStreamTest method testStartsAndEndsInSameBucketProduceValue.

@Test
public void testStartsAndEndsInSameBucketProduceValue() throws InterruptedException {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-B");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-B");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-B");
    stream = RollingThreadPoolMaxConcurrencyStream.getInstance(threadPoolKey, 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, 50);
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 40);
    cmd1.observe();
    Thread.sleep(1);
    cmd2.observe();
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    assertEquals(2, 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 24 with HystrixThreadPoolKey

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

the class HystrixUtilizationJsonStream method convertToJson.

protected static String convertToJson(HystrixUtilization utilization) throws IOException {
    StringWriter jsonString = new StringWriter();
    JsonGenerator json = jsonFactory.createGenerator(jsonString);
    json.writeStartObject();
    json.writeStringField("type", "HystrixUtilization");
    json.writeObjectFieldStart("commands");
    for (Map.Entry<HystrixCommandKey, HystrixCommandUtilization> entry : utilization.getCommandUtilizationMap().entrySet()) {
        final HystrixCommandKey key = entry.getKey();
        final HystrixCommandUtilization commandUtilization = entry.getValue();
        writeCommandUtilizationJson(json, key, commandUtilization);
    }
    json.writeEndObject();
    json.writeObjectFieldStart("threadpools");
    for (Map.Entry<HystrixThreadPoolKey, HystrixThreadPoolUtilization> entry : utilization.getThreadPoolUtilizationMap().entrySet()) {
        final HystrixThreadPoolKey threadPoolKey = entry.getKey();
        final HystrixThreadPoolUtilization threadPoolUtilization = entry.getValue();
        writeThreadPoolUtilizationJson(json, threadPoolKey, threadPoolUtilization);
    }
    json.writeEndObject();
    json.writeEndObject();
    json.close();
    return jsonString.getBuffer().toString();
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) StringWriter(java.io.StringWriter) HystrixThreadPoolUtilization(com.netflix.hystrix.metric.sample.HystrixThreadPoolUtilization) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HystrixThreadPoolKey(com.netflix.hystrix.HystrixThreadPoolKey) Map(java.util.Map) HystrixCommandUtilization(com.netflix.hystrix.metric.sample.HystrixCommandUtilization)

Example 25 with HystrixThreadPoolKey

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

the class CumulativeThreadPoolEventCounterStreamTest method testSingleFailure.

@Test
public void testSingleFailure() {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-C");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-C");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-C");
    stream = CumulativeThreadPoolEventCounterStream.getInstance(threadPoolKey, 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(2, stream.getLatest().length);
    assertEquals(1, 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) 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)

Aggregations

HystrixThreadPoolKey (com.netflix.hystrix.HystrixThreadPoolKey)44 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)42 HystrixCommandGroupKey (com.netflix.hystrix.HystrixCommandGroupKey)38 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)37 CountDownLatch (java.util.concurrent.CountDownLatch)37 Test (org.junit.Test)37 ArrayList (java.util.ArrayList)9 Map (java.util.Map)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)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 HystrixThreadPoolConfiguration (com.netflix.hystrix.config.HystrixThreadPoolConfiguration)2 HystrixCommandUtilization (com.netflix.hystrix.metric.sample.HystrixCommandUtilization)2 HystrixThreadPoolUtilization (com.netflix.hystrix.metric.sample.HystrixThreadPoolUtilization)2 IOException (java.io.IOException)2 HystrixCommand (com.netflix.hystrix.HystrixCommand)1 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)1