Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a Hystrix circuit breaker open?

I would like to programmatically force a circuit breaker to open for a particular group. I thought I might be able to do that by setting the config on a command in a group to force open, and running that command. However, that doesn't seem to work. Is this possible? Should I take a different approach? Here's the test I tried that fails on the 2nd assertEquals call.

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class ForceCircuitBreakerCommandTest {

    @Test
    public void testForceOpen(){

        assertEquals(Boolean.TRUE, new FakeCommand().execute());

        new OpenCircuitBreakerCommand().execute();

        assertEquals(Boolean.FALSE, new FakeCommand().execute());

    }

    private class FakeCommand extends HystrixCommand<Boolean> {

        public FakeCommand(){
            super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestGroup")));
        }

        @Override
        public Boolean run(){return Boolean.TRUE;}

        @Override
        public Boolean getFallback() {return Boolean.FALSE;}
    }

    private class OpenCircuitBreakerCommand extends HystrixCommand<Boolean> {

        public OpenCircuitBreakerCommand(){
            super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestGroup"))
                    .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                                    .withCircuitBreakerForceOpen(true)));
        }

        @Override
        public Boolean run(){return Boolean.TRUE;}

        @Override
        public Boolean getFallback() {return Boolean.FALSE;}
    }
}
like image 353
rzrelyea Avatar asked Mar 20 '15 11:03

rzrelyea


People also ask

When hystrix circuit opens?

When in 5 seconds you will receive at least 20 requests (volume threshold) and the certain percentage of them will fail (error threshold) circuit will be opened for consecutive 5 seconds.

What is the difference between Hystrix and circuit breaker?

The CircuitBreaker can open when too many calls exceed a certain response time threshold, even before the remote system is unresponsive and exceptions are thrown. Hystrix only performs a single execution when in half-open state to determine whether to close a CircuitBreaker.

How do I enable Hystrix?

First, we need to add the Spring Cloud Starter Hystrix dependency in our build configuration file. Now, add the @EnableHystrix annotation into your main Spring Boot application class file. The @EnableHystrix annotation is used to enable the Hystrix functionalities into your Spring Boot application.

How does hystrix circuit breaker work?

It implements the circuit breaker pattern which work on circuit-breaker transitions from CLOSED to OPEN when a circuit meets a specified threshold and error percentage exceeds the threshold error percentage. While it is open, it short-circuits all requests made against that circuit-breaker.


2 Answers

I have set custom properties such as "hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen" using

import com.netflix.config.ConfigurationManager;

ConfigurationManager.getConfigInstance()
    .setProperty("hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen",
    true);

ConfigurationManager is the Archaius instance that is used internally.

like image 147
Senthilkumar Gopal Avatar answered Oct 03 '22 01:10

Senthilkumar Gopal


This is an edit to the test using Senthilkumar Gopal's answer

@Test
public void testForceOpen() {

    assertEquals(Boolean.TRUE, new OpenCircuitBreakerCommand().execute());

    ConfigurationManager.getConfigInstance()
            .setProperty("hystrix.command.OpenCircuitBreakerCommand.circuitBreaker.forceOpen",
                    true);

    assertEquals(Boolean.FALSE, new OpenCircuitBreakerCommand().execute());
}
like image 23
ggmoriyon Avatar answered Oct 03 '22 00:10

ggmoriyon