@Override public Boolean getValue() { return circuitBreaker.isOpen(); } });
assertTrue(cb.allowRequest()); assertFalse(cb.isOpen()); assertTrue(cb.isOpen()); assertTrue(cb.isOpen()); assertFalse(cb.allowRequest()); System.out.println("!!!! 5: Confirmed that no other requests go out during single-test"); assertFalse(cb.allowRequest()); assertFalse(cb.allowRequest()); assertFalse(cb.allowRequest()); assertFalse(cb.allowRequest()); System.out.println("confirmed that 2nd singletest only happened once"); assertFalse(cb.allowRequest()); assertFalse(cb.allowRequest()); assertFalse(cb.allowRequest()); assertTrue(cb.isOpen()); assertFalse(cb.allowRequest()); assertTrue(cb.allowRequest()); assertTrue(cb.allowRequest()); assertTrue(cb.allowRequest());
assertTrue(cb.allowRequest()); assertFalse(cb.isOpen()); assertFalse(cb.allowRequest()); assertTrue(cb.isOpen()); assertTrue(cb.attemptExecution()); assertTrue(cb.isOpen()); assertFalse(cb.attemptExecution());
public static Optional<CircuitBreaker> usingHystrix(TenacityPropertyKey id) { final HystrixCircuitBreaker circuitBreaker = TenacityCommand.getCircuitBreaker(id); if (circuitBreaker == null) { return Optional.empty(); } final HystrixCommandProperties commandProperties = TenacityCommand.getCommandProperties(id); if (commandProperties.circuitBreakerForceOpen().get()) { return Optional.of(CircuitBreaker.forcedOpen(id)); } else if (commandProperties.circuitBreakerForceClosed().get()) { return Optional.of(CircuitBreaker.forcedClosed(id)); } else if (circuitBreaker.allowRequest()) { return Optional.of(CircuitBreaker.closed(id)); } else { return Optional.of(CircuitBreaker.open(id)); } }
@Override public void call() { if (!commandIsScalar()) { long latency = System.currentTimeMillis() - executionResult.getStartTimestamp(); eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey); executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS); eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList()); circuitBreaker.markSuccess(); } } };
if (circuitBreaker.attemptExecution()) { final TryableSemaphore executionSemaphore = getExecutionSemaphore(); final AtomicBoolean semaphoreHasBeenReleased = new AtomicBoolean(false);
public static Optional<CircuitBreaker> usingHystrix(TenacityPropertyKey id) { final HystrixCircuitBreaker circuitBreaker = TenacityCommand.getCircuitBreaker(id); if (circuitBreaker == null) { return Optional.empty(); } final HystrixCommandProperties commandProperties = TenacityCommand.getCommandProperties(id); if (commandProperties.circuitBreakerForceOpen().get()) { return Optional.of(CircuitBreaker.forcedOpen(id)); } else if (commandProperties.circuitBreakerForceClosed().get()) { return Optional.of(CircuitBreaker.forcedClosed(id)); } else if (circuitBreaker.allowRequest()) { return Optional.of(CircuitBreaker.closed(id)); } else { return Optional.of(CircuitBreaker.open(id)); } }
@Override public void call(R r) { if (shouldOutputOnNextEvents()) { executionResult = executionResult.addEvent(HystrixEventType.EMIT); eventNotifier.markEvent(HystrixEventType.EMIT, commandKey); } if (commandIsScalar()) { long latency = System.currentTimeMillis() - executionResult.getStartTimestamp(); eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey); executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS); eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList()); circuitBreaker.markSuccess(); } } };
/** * Whether the 'circuit-breaker' is open meaning that <code>execute()</code> will immediately return * the <code>getFallback()</code> response and not attempt a HystrixCommand execution. * * 4 columns are ForcedOpen | ForcedClosed | CircuitBreaker open due to health ||| Expected Result * * T | T | T ||| OPEN (true) * T | T | F ||| OPEN (true) * T | F | T ||| OPEN (true) * T | F | F ||| OPEN (true) * F | T | T ||| CLOSED (false) * F | T | F ||| CLOSED (false) * F | F | T ||| OPEN (true) * F | F | F ||| CLOSED (false) * * @return boolean */ public boolean isCircuitBreakerOpen() { return properties.circuitBreakerForceOpen().get() || (!properties.circuitBreakerForceClosed().get() && circuitBreaker.isOpen()); }
/** * Test that if all 'marks' are timeouts that it will trip the circuit. */ @Test public void testTripCircuitOnTimeouts() { String key = "cmd-D"; try { HystrixCommand<Boolean> cmd1 = new TimeoutCommand(key); HystrixCircuitBreaker cb = cmd1.circuitBreaker; // this should start as allowing requests assertTrue(cb.allowRequest()); assertFalse(cb.isOpen()); // success with high latency cmd1.execute(); HystrixCommand<Boolean> cmd2 = new TimeoutCommand(key); cmd2.execute(); HystrixCommand<Boolean> cmd3 = new TimeoutCommand(key); cmd3.execute(); HystrixCommand<Boolean> cmd4 = new TimeoutCommand(key); cmd4.execute(); // everything has been a timeout so we should not allow any requests Thread.sleep(100); assertFalse(cb.allowRequest()); assertTrue(cb.isOpen()); } catch (Exception e) { e.printStackTrace(); fail("Error occurred: " + e.getMessage()); } }
if (circuitBreaker.allowRequest()) { final TryableSemaphore executionSemaphore = getExecutionSemaphore(); final AtomicBoolean semaphoreHasBeenReleased = new AtomicBoolean(false);
@Override public void call() { if (!commandIsScalar()) { long latency = System.currentTimeMillis() - executionResult.getStartTimestamp(); eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList()); eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey); executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS); circuitBreaker.markSuccess(); } } };
@Override public Boolean getValue() { return circuitBreaker.isOpen(); } });
assertTrue(cb.allowRequest()); assertFalse(cb.isOpen()); System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); System.out.println("CircuitBreaker state 1 : " + cmd1.getMetrics().getHealthCounts()); assertTrue(cb.isOpen()); assertTrue(cb.isOpen()); assertFalse(cb.allowRequest()); assertTrue(cb.allowRequest()); assertTrue(cb.allowRequest()); assertTrue(cb.allowRequest()); assertFalse(cb.isOpen());
@Override public void call(R r) { if (shouldOutputOnNextEvents()) { executionResult = executionResult.addEvent(HystrixEventType.EMIT); eventNotifier.markEvent(HystrixEventType.EMIT, commandKey); } if (commandIsScalar()) { long latency = System.currentTimeMillis() - executionResult.getStartTimestamp(); eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList()); eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey); executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS); circuitBreaker.markSuccess(); } } };
@Override public Boolean getValue() { return circuitBreaker.isOpen(); } });
assertTrue(cb.allowRequest()); assertFalse(cb.isOpen()); assertTrue(cb.allowRequest()); assertFalse(cb.isOpen());
@Override public Boolean value() { return circuitBreaker.isOpen(); } });
assertFalse(cb.allowRequest()); assertTrue(cb.isOpen()); assertTrue(cb.allowRequest()); assertFalse(cb.isOpen());
@Override public Number call() { return HystrixPrometheusMetricsPublisherCommand.this.booleanToNumber(circuitBreaker.isOpen()); } });