/** * Tests that the circuit-breaker reports itself as "OPEN" if set as forced-open */ @Test public void testCircuitBreakerReportsOpenIfForcedOpen() { HystrixCommand<Boolean> cmd = new HystrixCommand<Boolean>(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP")).andCommandPropertiesDefaults(new HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(true))) { @Override protected Boolean run() throws Exception { return true; } @Override protected Boolean getFallback() { return false; } }; assertFalse(cmd.execute()); //fallback should fire System.out.println("RESULT : " + cmd.getExecutionEvents()); assertTrue(cmd.isCircuitBreakerOpen()); }
/** * Tests that the circuit-breaker reports itself as "CLOSED" if set as forced-closed */ @Test public void testCircuitBreakerReportsClosedIfForcedClosed() { HystrixCommand<Boolean> cmd = new HystrixCommand<Boolean>(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP")).andCommandPropertiesDefaults( new HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(false).withCircuitBreakerForceClosed(true))) { @Override protected Boolean run() throws Exception { return true; } @Override protected Boolean getFallback() { return false; } }; assertTrue(cmd.execute()); System.out.println("RESULT : " + cmd.getExecutionEvents()); assertFalse(cmd.isCircuitBreakerOpen()); }
invocation.getOperationMeta() .getMicroserviceQualifiedName())) .withCircuitBreakerForceOpen(Configuration.INSTANCE.isCircuitBreakerForceOpen(groupname, invocation.getMicroserviceName(), invocation.getOperationMeta()
public static HystrixCommandProperties.Setter createSetter(){ HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter(); setter.withMetricsRollingStatisticalWindowInMilliseconds(10000);// default => statisticalWindow: 10000 = 10 seconds (and default of 10 buckets so each bucket is 1 second) setter.withMetricsRollingStatisticalWindowBuckets(10);// default => statisticalWindowBuckets: 10 = 10 buckets in a 10 second window so each bucket is 1 second setter.withCircuitBreakerRequestVolumeThreshold(20);// default => statisticalWindowVolumeThreshold: 20 requests in 10 seconds must occur before statistics matter setter.withCircuitBreakerSleepWindowInMilliseconds(5000);// default => sleepWindow: 5000 = 5 seconds that we will sleep before trying again after tripping the circuit setter.withCircuitBreakerErrorThresholdPercentage(50);// default => errorThresholdPercentage = 50 = if 50%+ of requests in 10 seconds are failures or latent then we will trip the circuit setter.withCircuitBreakerForceOpen(false);// default => forceCircuitOpen = false (we want to allow traffic) setter.withCircuitBreakerForceClosed(false);// default => ignoreErrors = false setter.withExecutionTimeoutInMilliseconds(1000); // default => executionTimeoutInMilliseconds: 1000 = 1 second setter.withExecutionTimeoutEnabled(true); setter.withExecutionIsolationThreadInterruptOnTimeout(true); setter.withExecutionIsolationThreadInterruptOnFutureCancel(false); setter.withMetricsRollingPercentileEnabled(true); setter.withRequestCacheEnabled(true); setter.withFallbackIsolationSemaphoreMaxConcurrentRequests(10); setter.withFallbackEnabled(true); setter.withExecutionIsolationSemaphoreMaxConcurrentRequests(10); setter.withRequestLogEnabled(true); setter.withCircuitBreakerEnabled(true); setter.withMetricsRollingPercentileWindowInMilliseconds(60000); // default to 1 minute for RollingPercentile setter.withMetricsRollingPercentileWindowBuckets(6); // default to 6 buckets (10 seconds each in 60 second window) setter.withMetricsRollingPercentileBucketSize(100); // default to 100 values max per bucket setter.withMetricsHealthSnapshotIntervalInMilliseconds(500); // default to 500ms as max frequency between allowing snapshots of health (error percentage etc) return setter; }
public static HystrixCommand.Setter getSetter(DataHystrixGroup group, String commandKey, int timeout, boolean circuitBreakerForceOpen) { return HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(group.getGroupKey())) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(timeout) .withCircuitBreakerForceOpen(circuitBreakerForceOpen)) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withCoreSize(group.getThreadPoolCoreSize())); }
/** * Utility method for creating baseline properties for unit tests. */ /* package */static HystrixCommandProperties.Setter getUnitTestPropertiesSetter() { return new HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(1000)// when an execution will be timed out .withExecutionTimeoutEnabled(true) .withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD) // we want thread execution by default in tests .withExecutionIsolationThreadInterruptOnTimeout(true) .withExecutionIsolationThreadInterruptOnFutureCancel(true) .withCircuitBreakerForceOpen(false) // we don't want short-circuiting by default .withCircuitBreakerErrorThresholdPercentage(40) // % of 'marks' that must be failed to trip the circuit .withMetricsRollingStatisticalWindowInMilliseconds(5000)// milliseconds back that will be tracked .withMetricsRollingStatisticalWindowBuckets(5) // buckets .withCircuitBreakerRequestVolumeThreshold(0) // in testing we will not have a threshold unless we're specifically testing that feature .withCircuitBreakerSleepWindowInMilliseconds(5000000) // milliseconds after tripping circuit before allowing retry (by default set VERY long as we want it to effectively never allow a singleTest for most unit tests) .withCircuitBreakerEnabled(true) .withRequestLogEnabled(true) .withExecutionIsolationSemaphoreMaxConcurrentRequests(20) .withFallbackIsolationSemaphoreMaxConcurrentRequests(10) .withFallbackEnabled(true) .withCircuitBreakerForceClosed(false) .withMetricsRollingPercentileEnabled(true) .withRequestCacheEnabled(true) .withMetricsRollingPercentileWindowInMilliseconds(60000) .withMetricsRollingPercentileWindowBuckets(12) .withMetricsRollingPercentileBucketSize(1000) .withMetricsHealthSnapshotIntervalInMilliseconds(100); }
protected ShortCircuitedCommand() { super(HystrixCommand.Setter.withGroupKey( HystrixCommandGroupKey.Factory.asKey("shortCircuitedCommand")) .andCommandPropertiesDefaults(HystrixCommandPropertiesTest .getUnitTestPropertiesSetter() .withCircuitBreakerForceOpen(true))); }