use of com.netflix.hystrix.HystrixCommandKey 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);
}
use of com.netflix.hystrix.HystrixCommandKey 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();
}
use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.
the class HystrixServoMetricsPublisherCommandTest method testRollingCounters.
@Test
public void testRollingCounters() throws Exception {
//execute 10 commands, then sleep for 2000ms to let these age out
//execute 10 commands again, these should show up in rolling count
HystrixCommandKey key = HystrixCommandKey.Factory.asKey("ServoCOMMAND-B");
HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key);
HystrixCommandProperties properties = new HystrixPropertiesCommandDefault(key, propertiesSetter);
HystrixCommandMetrics metrics = HystrixCommandMetrics.getInstance(key, groupKey, properties);
HystrixServoMetricsPublisherCommand servoPublisher = new HystrixServoMetricsPublisherCommand(key, groupKey, metrics, circuitBreaker, properties);
servoPublisher.initialize();
new SuccessCommand(key).execute();
new SuccessCommand(key).execute();
new SuccessCommand(key).execute();
new TimeoutCommand(key).execute();
new SuccessCommand(key).execute();
new FailureCommand(key).execute();
new SuccessCommand(key).execute();
new SuccessCommand(key).execute();
new SuccessCommand(key).execute();
new SuccessCommand(key).execute();
Thread.sleep(2000);
new SuccessCommand(key).execute();
new SuccessCommand(key).execute();
new SuccessCommand(key).execute();
new TimeoutCommand(key).execute();
new SuccessCommand(key).execute();
new FailureCommand(key).execute();
new TimeoutCommand(key).execute();
new TimeoutCommand(key).execute();
new TimeoutCommand(key).execute();
new TimeoutCommand(key).execute();
//time for 1 bucket roll
Thread.sleep(100);
assertEquals(4L, servoPublisher.getRollingMonitor("success", HystrixEventType.SUCCESS).getValue());
assertEquals(5L, servoPublisher.getRollingMonitor("timeout", HystrixEventType.TIMEOUT).getValue());
assertEquals(1L, servoPublisher.getRollingMonitor("failure", HystrixEventType.FAILURE).getValue());
assertEquals(6L, servoPublisher.getRollingMonitor("falback_success", HystrixEventType.FALLBACK_SUCCESS).getValue());
}
use of com.netflix.hystrix.HystrixCommandKey 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));
}
use of com.netflix.hystrix.HystrixCommandKey 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));
}
Aggregations