Search in sources :

Example 1 with HystrixThreadPoolKey

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

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

the class HystrixConfigurationJsonStream method convertToString.

public static String convertToString(HystrixConfiguration config) throws IOException {
    StringWriter jsonString = new StringWriter();
    JsonGenerator json = jsonFactory.createGenerator(jsonString);
    json.writeStartObject();
    json.writeStringField("type", "HystrixConfig");
    json.writeObjectFieldStart("commands");
    for (Map.Entry<HystrixCommandKey, HystrixCommandConfiguration> entry : config.getCommandConfig().entrySet()) {
        final HystrixCommandKey key = entry.getKey();
        final HystrixCommandConfiguration commandConfig = entry.getValue();
        writeCommandConfigJson(json, key, commandConfig);
    }
    json.writeEndObject();
    json.writeObjectFieldStart("threadpools");
    for (Map.Entry<HystrixThreadPoolKey, HystrixThreadPoolConfiguration> entry : config.getThreadPoolConfig().entrySet()) {
        final HystrixThreadPoolKey threadPoolKey = entry.getKey();
        final HystrixThreadPoolConfiguration threadPoolConfig = entry.getValue();
        writeThreadPoolConfigJson(json, threadPoolKey, threadPoolConfig);
    }
    json.writeEndObject();
    json.writeObjectFieldStart("collapsers");
    for (Map.Entry<HystrixCollapserKey, HystrixCollapserConfiguration> entry : config.getCollapserConfig().entrySet()) {
        final HystrixCollapserKey collapserKey = entry.getKey();
        final HystrixCollapserConfiguration collapserConfig = entry.getValue();
        writeCollapserConfigJson(json, collapserKey, collapserConfig);
    }
    json.writeEndObject();
    json.writeEndObject();
    json.close();
    return jsonString.getBuffer().toString();
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) StringWriter(java.io.StringWriter) HystrixCommandConfiguration(com.netflix.hystrix.config.HystrixCommandConfiguration) HystrixCollapserKey(com.netflix.hystrix.HystrixCollapserKey) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HystrixThreadPoolKey(com.netflix.hystrix.HystrixThreadPoolKey) Map(java.util.Map) HystrixCollapserConfiguration(com.netflix.hystrix.config.HystrixCollapserConfiguration) HystrixThreadPoolConfiguration(com.netflix.hystrix.config.HystrixThreadPoolConfiguration)

Example 3 with HystrixThreadPoolKey

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

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

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

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