5

I am writing an application and I want to implement circuit breaker pattern. This is the Hystrix Command class I have written:

public class HystrixCommandNextGen extends HystrixCommand<ScriptContext> {

    private ScriptContext scriptctx;
    private ScriptFactory scriptFactory;
    private ScriptContext responseContext = null;

    private Logger logger = LoggerFactory.getLogger(HystrixCommandNextGen.class);

    public HystrixCommandNextGen(ScriptContext scriptctx, ScriptFactory scriptFactory) {
        super(
            Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Thread_Pool"))
            .andCommandKey(HystrixCommandKey.Factory.asKey(scriptctx.getExecutionData(ExecutionParam.SCRIPTNAME)))
        );
        this.scriptctx = scriptctx;
        this.scriptFactory = scriptFactory;

        HystrixCommandProperties.Setter().withCircuitBreakerEnabled(true);
        HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(150);
    }

    @Override
    protected ScriptContext run() throws Exception {
        scriptFactory.execute(scriptctx);
        return scriptctx;
    }

    @Override
    protected ScriptContext getFallback() {
        logger.error("FI is not responding: Error occurred in the execution of " + getClass().getSimpleName());
        return scriptctx;
    }
}

I am not able to understand how to configure the number of threads, threshold time for circuit breaker and number of requests to handle.

1
  • 1
    Unless I misunderstood the code of Hystrix, those two calls you have to HystrixCommandProperties.Setter() are not doing anything, as just creating a new object of type Setter, and not actually setting up any global property
    – arcuri82
    Nov 18, 2016 at 15:21

3 Answers 3

9

Hystrix uses Archaius for configuration management. The Archaius library allows for dynamic reloading of property values at runtime. Documentation on how to configure Archaius is here: https://github.com/Netflix/archaius/wiki/Users-Guide

If you want to configure Hystrix in code, you can use the Archaius ConfigurationManager class like this:

ConfigurationManager.getConfigInstance().setProperty(
  "hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds", 
  500);

Note that the HystrixCommandKey part of the property name string is actually the name of the circuit breaker you set with the .andCommandKey() method of the Setter. So if you set the command key to be "MyCommand", the property key for timeout would actually be "hystrix.command.MyCommand.execution.isolation.thread.timeoutInMilliseconds"

1

The complete list of configurations and the means are available here: https://github.com/Netflix/Hystrix/wiki/Configuration

For your specific questions:

  1. configure the no. of threads use 'hystrix.threadpool.HystrixThreadPoolKey.coreSize'

  2. threshold time for circuit breaker use 'hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds'

  3. no. of requests to handle. This is a little tricky. But the max. number of concurrent threads is the same as no. of requests to handle.

It would be better to read through the Configuration wiki to understand the structure and the usage of each property before attempting to setup.

1

It is best to set command properties before creating the command. Hystrix documentation specifically states this for some of the command properties. For example:

metrics.rollingStats.numBuckets: As of 1.4.12, this property affects the initial metrics creation only, and adjustments made to this property after startup will not take effect.

In other words, do not initialize this command property from inside the constructor as that is too late. I am using 1.4.3 and at least for this version, I found this to hold for all rolling metrics and circuit breaker properties. I have used ConfigurationManager to set these properties before initializing the command:

ConfigurationManager.getConfigInstance().setProperty("hystrix.command.<HystrixCommandKey>.circuitBreaker.requestVolumeThreshold, 30);

Replace with the command key (which in the question being asked is: "Thread_Pool").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.