Hystrix CircuitBreakerSleepWindowInMilliseconds无法按预期工作

时间:2018-04-06 11:42:33

标签: java junit hystrix circuit-breaker

我正在测试Hystrix CircuitBreaker的实现。这就是命令类的样子:

public class CommandOne extends HystrixCommand<String>
{
    private MyExternalService service;    
    public static int runCount = 0;

    public CommandGetPunterUnpayoutExternalBets(MyExternalServoce service)
    {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("AAA"))
                .andThreadPoolPropertiesDefaults(
                        HystrixThreadPoolProperties.Setter().
                         .withMetricsRollingStatisticalWindowInMilliseconds(10000))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerEnabled(true)
                        .withCircuitBreakerErrorThresholdPercentage(20)
                        .withCircuitBreakerRequestVolumeThreshold(10)
                        .withExecutionTimeoutInMilliseconds(30)
                        .withCircuitBreakerSleepWindowInMilliseconds(100000)));

        this.service = service;
    }


    @Override
    protected String run()
    {
        run++;
        return service.callMethod();
    }


    @Override
    protected String getFallback()
    {
        return "default;
    }
}

命令的调用如下:

public class AnotherClass
{
    private MyExternalServoce service; 

    public String callCmd()
    {
        CommandOne command = new CommandOne(service);
        return command.execute();
    }
}

在测试中,我执行后续步骤:

@Test
    public void test()
{
    AnotherClass anotherClass = new AnotherClass();

    // stubbing exception on my service
    when(service.callMethod()).thenThrow(new RuntimeException());
    for (int i = 0; i < 1000; i++)
        {
             anotherClass.callCmd();
        }
    System.out.println("Run method was called times = " + CommandOne.runCount);
}

我对命令配置的期望是:MyExternalService.callMethod()应​​该调用10次(RequestVolumeThreshold),之后不要调用100000 ms(长时间)。在我的测试用例中,我期望CommandOne.runCount = 10。 但实际上我从150到200次调用了MyExternalService.callMethod()(CommandOne.runCount =(150-200)。为什么会这样?我做错了什么?

1 个答案:

答案 0 :(得分:2)

根据Hystrix docs,健康快照将每500毫秒拍摄一次(默认情况下)。这意味着在前500ms内hystrix所发生的一切都不会影响断路器状态。在您的示例中,您获得了runCount的随机值,因为每次您的计算机每500毫秒执行一次随机请求值,并且只有在该时间间隔后电路状态才会更新并关闭。

请看一下简单的例子:

 public class CommandOne extends HystrixCommand<String> {

    private String content;
    public static int runCount = 0;


    public CommandOne(String s) {
        super(Setter.withGroupKey
                (HystrixCommandGroupKey.Factory.asKey("SnapshotIntervalTest"))
                .andCommandPropertiesDefaults(
                        HystrixCommandProperties.Setter()
                                .withCircuitBreakerSleepWindowInMilliseconds(500000)
                                .withCircuitBreakerRequestVolumeThreshold(9)
                                .withMetricsHealthSnapshotIntervalInMilliseconds(50)
                                .withMetricsRollingStatisticalWindowInMilliseconds(100000)
                )
        );
        this.content = s;
    }

    @Override
    public String run() throws Exception {
        Thread.sleep(100);
        runCount++;
        if ("".equals(content)) {
            throw new Exception();
        }
        return content;
    }

    @Override
    protected String getFallback() {
        return "FAILURE-" + content;
    }

}

    @Test
    void test() {

        for (int i = 0; i < 100; i++) {
            CommandOne commandOne = new CommandOne();
            commandOne.execute();
        }
        Assertions.assertEquals(10, CommandOne.runCount);
    }

在这个例子中,我添加了:

  • withMetricsHealthSnapshotIntervalInMilliseconds(50)允许hystrix每50ms拍摄一次快照。
  • Thread.sleep(100);使请求速度变慢,如果没有它,它们将会快50毫秒,我们将面临初始问题。

尽管进行了所有这些修改,我还是看到了一些随机故障。在此之后我得出结论,像这样测试hystrix不是一个好主意。而不是它我们可以使用:

1)Fallback/Success flow behavior by manually setting open/close circuit state

2)Configuration tests