Hi I am trying to use Hystrix pattern in my sample program. Using following version com.netflix.hystrix:hystrix-core:1.4.21
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import java.util.GregorianCalendar;
import java.util.Map;
public class ServiceInvoker extends HystrixCommand<String> {
Map<String, String> serviceParams;
public String invokeService(Map<String, String> serviceParams){
System.out.println("Inside invokeService");
//Induce processing delay START
long currentTime = GregorianCalendar.getInstance().getTimeInMillis();
long timeNow = 0;
long bound = 3000;
while(timeNow < (currentTime+bound)){
timeNow = GregorianCalendar.getInstance().getTimeInMillis();
}
//Induce processing delay END
return "Service Invoked";
}
public ServiceInvoker(Map<String, String> params){
super(Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("MYKEY"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withCircuitBreakerSleepWindowInMilliseconds(60000)
.withExecutionTimeoutInMilliseconds(2000)
.withCircuitBreakerErrorThresholdPercentage(5))
);
this.serviceParams=params;
}
@Override
protected String run() throws Exception {
return invokeService(serviceParams);
}
@Override
protected String getFallback() {
System.out.println("Inside FallBack");
return "FALLBACK";
}
public static void main(String args[]) throws InterruptedException {
while(true) {
ServiceInvoker si = new ServiceInvoker(null);
String op = si.execute();
System.out.println("output="+op);
Thread.sleep(100);
}
}
}
When I run the code above I repeatedly get following non-stop.
Inside invokeService
Inside FallBack
output=FALLBACK
I thought that since I have set the withCircuitBreakerErrorThresholdPercentage as 5% and withCircuitBreakerSleepWindowInMilliseconds at 60000 (1Minute), I thought that once it received few errors, it will OPEN the circuit and just return FALLBACK for all times, it will not even attempt to call invokeService and thus will not print "Inside invokeService" for 60 seconds. Can someone please shed some light on this, why is circuit not being opened?
There is also the parameter circuitBreaker.requestVolumeThreshold
with a default of 20 - meaning you need at least 20 requests in total in the statistic windows. The size of the window is configured by metrics.rollingStats.timeInMilliseconds
.
If you don't reach 20 requests in the window the circuit breaker will not trip.
You might want to log information from HystrixCommandMetrics
as well when investigating this scenario.