Search in sources :

Example 1 with HystrixCommandGroupKey

use of com.netflix.hystrix.HystrixCommandGroupKey in project camel by apache.

the class HystrixProcessorFactory method doCreateProcessor.

@Override
public Processor doCreateProcessor(RouteContext routeContext, HystrixDefinition definition) throws Exception {
    // create the regular and fallback processors
    Processor processor = definition.createChildProcessor(routeContext, true);
    Processor fallback = null;
    if (definition.getOnFallback() != null) {
        fallback = definition.getOnFallback().createProcessor(routeContext);
    }
    final HystrixConfigurationDefinition config = buildHystrixConfiguration(routeContext.getCamelContext(), definition);
    final String id = definition.idOrCreate(routeContext.getCamelContext().getNodeIdFactory());
    // group and thread pool keys to use they can be configured on configRef and config, so look there first, and if none then use default
    String groupKey = config.getGroupKey();
    String threadPoolKey = config.getThreadPoolKey();
    if (groupKey == null) {
        groupKey = HystrixConfigurationDefinition.DEFAULT_GROUP_KEY;
    }
    if (threadPoolKey == null) {
        // by default use the thread pool from the group
        threadPoolKey = groupKey;
    }
    // use the node id as the command key
    HystrixCommandKey hcCommandKey = HystrixCommandKey.Factory.asKey(id);
    HystrixCommandKey hcFallbackCommandKey = HystrixCommandKey.Factory.asKey(id + "-fallback");
    // use the configured group key
    HystrixCommandGroupKey hcGroupKey = HystrixCommandGroupKey.Factory.asKey(groupKey);
    HystrixThreadPoolKey tpKey = HystrixThreadPoolKey.Factory.asKey(threadPoolKey);
    // create setter using the default options
    HystrixCommand.Setter setter = HystrixCommand.Setter.withGroupKey(hcGroupKey).andCommandKey(hcCommandKey).andThreadPoolKey(tpKey);
    HystrixCommandProperties.Setter commandSetter = HystrixCommandProperties.Setter();
    setter.andCommandPropertiesDefaults(commandSetter);
    HystrixThreadPoolProperties.Setter threadPoolSetter = HystrixThreadPoolProperties.Setter();
    setter.andThreadPoolPropertiesDefaults(threadPoolSetter);
    configureHystrix(commandSetter, threadPoolSetter, config);
    // create setter for fallback via network
    HystrixCommand.Setter fallbackSetter = null;
    boolean fallbackViaNetwork = definition.getOnFallback() != null && definition.getOnFallback().isFallbackViaNetwork();
    if (fallbackViaNetwork) {
        // use a different thread pool that is for fallback (should never use the same thread pool as the regular command)
        HystrixThreadPoolKey tpFallbackKey = HystrixThreadPoolKey.Factory.asKey(threadPoolKey + "-fallback");
        fallbackSetter = HystrixCommand.Setter.withGroupKey(hcGroupKey).andCommandKey(hcFallbackCommandKey).andThreadPoolKey(tpFallbackKey);
        HystrixCommandProperties.Setter commandFallbackSetter = HystrixCommandProperties.Setter();
        fallbackSetter.andCommandPropertiesDefaults(commandFallbackSetter);
        HystrixThreadPoolProperties.Setter fallbackThreadPoolSetter = HystrixThreadPoolProperties.Setter();
        fallbackSetter.andThreadPoolPropertiesDefaults(fallbackThreadPoolSetter);
        // at first configure any shared options
        configureHystrix(commandFallbackSetter, fallbackThreadPoolSetter, config);
    }
    return new HystrixProcessor(hcGroupKey, hcCommandKey, hcFallbackCommandKey, setter, fallbackSetter, processor, fallback, fallbackViaNetwork);
}
Also used : Processor(org.apache.camel.Processor) HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) HystrixThreadPoolKey(com.netflix.hystrix.HystrixThreadPoolKey) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) HystrixCommand(com.netflix.hystrix.HystrixCommand) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) HystrixConfigurationDefinition(org.apache.camel.model.HystrixConfigurationDefinition) HystrixThreadPoolProperties(com.netflix.hystrix.HystrixThreadPoolProperties)

Example 2 with HystrixCommandGroupKey

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

the class CumulativeThreadPoolEventCounterStreamTest method testEmptyStreamProducesZeros.

@Test
public void testEmptyStreamProducesZeros() {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-A");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-A");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-A");
    stream = CumulativeThreadPoolEventCounterStream.getInstance(threadPoolKey, 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");
    }
    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) 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 3 with HystrixCommandGroupKey

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

the class CumulativeThreadPoolEventCounterStreamTest method testFallbackMissing.

@Test
public void testFallbackMissing() {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-K");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-K");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-K");
    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, HystrixEventType.FALLBACK_MISSING);
    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)

Example 4 with HystrixCommandGroupKey

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

the class CumulativeThreadPoolEventCounterStreamTest method testShortCircuited.

@Test
public void testShortCircuited() {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-G");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-G");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-G");
    stream = CumulativeThreadPoolEventCounterStream.getInstance(threadPoolKey, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    //3 failures in a row will trip circuit.  let bucket roll once then submit 2 requests.
    //should see 3 FAILUREs and 2 SHORT_CIRCUITs and each should see a FALLBACK_SUCCESS
    CommandStreamTest.Command failure1 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 20);
    CommandStreamTest.Command failure2 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 20);
    CommandStreamTest.Command failure3 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 20);
    CommandStreamTest.Command shortCircuit1 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS);
    CommandStreamTest.Command shortCircuit2 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS);
    failure1.observe();
    failure2.observe();
    failure3.observe();
    try {
        Thread.sleep(100);
    } catch (InterruptedException ie) {
        fail(ie.getMessage());
    }
    shortCircuit1.observe();
    shortCircuit2.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertTrue(shortCircuit1.isResponseShortCircuited());
    assertTrue(shortCircuit2.isResponseShortCircuited());
    //only the FAILUREs should show up in thread pool counters
    assertEquals(2, stream.getLatest().length);
    assertEquals(3, 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)

Example 5 with HystrixCommandGroupKey

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

the class CumulativeThreadPoolEventCounterStreamTest method testRequestFromCache.

@Test
public void testRequestFromCache() {
    HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-F");
    HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-F");
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-F");
    stream = CumulativeThreadPoolEventCounterStream.getInstance(threadPoolKey, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    CommandStreamTest.Command cmd1 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 20);
    CommandStreamTest.Command cmd2 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.RESPONSE_FROM_CACHE);
    CommandStreamTest.Command cmd3 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.RESPONSE_FROM_CACHE);
    cmd1.observe();
    cmd2.observe();
    cmd3.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    //RESPONSE_FROM_CACHE should not show up at all in thread pool counters - just the success
    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

HystrixCommandGroupKey (com.netflix.hystrix.HystrixCommandGroupKey)39 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)38 HystrixThreadPoolKey (com.netflix.hystrix.HystrixThreadPoolKey)38 Test (org.junit.Test)38 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)37 CountDownLatch (java.util.concurrent.CountDownLatch)37 ArrayList (java.util.ArrayList)9 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)3 HystrixCommand (com.netflix.hystrix.HystrixCommand)1 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)1 HystrixThreadPoolProperties (com.netflix.hystrix.HystrixThreadPoolProperties)1 Invocation (io.servicecomb.core.Invocation)1 OperationMeta (io.servicecomb.core.definition.OperationMeta)1 Processor (org.apache.camel.Processor)1 HystrixConfigurationDefinition (org.apache.camel.model.HystrixConfigurationDefinition)1