Polish CircuitBreaker interface and update comments

- Only carry context in tryPass/onComplete method (this might be generic in upcoming versions)

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2020-08-18 15:26:52 +08:00
parent ee2772b513
commit 55e038cc33
5 changed files with 14 additions and 15 deletions

View File

@ -50,7 +50,7 @@ public class DegradeSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
return; return;
} }
for (CircuitBreaker cb : circuitBreakers) { for (CircuitBreaker cb : circuitBreakers) {
if (!cb.tryPass(context, r)) { if (!cb.tryPass(context)) {
throw new DegradeException(cb.getRule().getLimitApp(), cb.getRule()); throw new DegradeException(cb.getRule().getLimitApp(), cb.getRule());
} }
} }
@ -72,7 +72,7 @@ public class DegradeSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
if (curEntry.getBlockError() == null) { if (curEntry.getBlockError() == null) {
// passed request // passed request
for (CircuitBreaker circuitBreaker : circuitBreakers) { for (CircuitBreaker circuitBreaker : circuitBreakers) {
circuitBreaker.onRequestComplete(context, r); circuitBreaker.onRequestComplete(context);
} }
} }

View File

@ -65,13 +65,13 @@ public abstract class AbstractCircuitBreaker implements CircuitBreaker {
} }
@Override @Override
public boolean tryPass(Context context, ResourceWrapper r) { public boolean tryPass(Context context) {
// Template implementation. // Template implementation.
if (currentState.get() == State.CLOSED) { if (currentState.get() == State.CLOSED) {
return true; return true;
} }
if (currentState.get() == State.OPEN) { if (currentState.get() == State.OPEN) {
// For half-open state we allow a request for trial. // For half-open state we allow a request for probing.
return retryTimeoutArrived() && fromOpenToHalfOpen(context); return retryTimeoutArrived() && fromOpenToHalfOpen(context);
} }
return false; return false;
@ -106,16 +106,16 @@ public abstract class AbstractCircuitBreaker implements CircuitBreaker {
notifyObservers(State.OPEN, State.HALF_OPEN, null); notifyObservers(State.OPEN, State.HALF_OPEN, null);
Entry entry = context.getCurEntry(); Entry entry = context.getCurEntry();
entry.whenComplete(new BiConsumer<Context, Entry>() { entry.whenComplete(new BiConsumer<Context, Entry>() {
@Override @Override
public void accept(Context context, Entry entry) { public void accept(Context context, Entry entry) {
// Note: This works as a temporary workaround for https://github.com/alibaba/Sentinel/issues/1638
// Without the hook, the circuit breaker won't recover from half-open state in some circumstances
// when the request is actually blocked by upcoming rules (not only degrade rules).
if (entry.getBlockError() != null) { if (entry.getBlockError() != null) {
// Fallback to OPEN due to detecting request is blocked // Fallback to OPEN due to detecting request is blocked
currentState.compareAndSet(State.HALF_OPEN, State.OPEN); currentState.compareAndSet(State.HALF_OPEN, State.OPEN);
notifyObservers(State.HALF_OPEN, State.OPEN, 1.0d); notifyObservers(State.HALF_OPEN, State.OPEN, 1.0d);
return;
} }
} }
}); });
return true; return true;

View File

@ -36,11 +36,10 @@ public interface CircuitBreaker {
/** /**
* Acquires permission of an invocation only if it is available at the time of invoking. * Acquires permission of an invocation only if it is available at the time of invoking.
* *
* @param context * @param context context of current invocation
* @param r
* @return {@code true} if permission was acquired and {@code false} otherwise * @return {@code true} if permission was acquired and {@code false} otherwise
*/ */
boolean tryPass(Context context, ResourceWrapper r); boolean tryPass(Context context);
/** /**
* Get current state of the circuit breaker. * Get current state of the circuit breaker.
@ -50,12 +49,12 @@ public interface CircuitBreaker {
State currentState(); State currentState();
/** /**
* Called when a `passed` invocation finished. * <p>Record a completed request with the context and handle state transformation of the circuit breaker.</p>
* <p>Called when a <strong>passed</strong> invocation finished.</p>
* *
* @param context context of current invocation * @param context context of current invocation
* @param wrapper current resource
*/ */
void onRequestComplete(Context context, ResourceWrapper wrapper); void onRequestComplete(Context context);
/** /**
* Circuit breaker state. * Circuit breaker state.

View File

@ -63,7 +63,7 @@ public class ExceptionCircuitBreaker extends AbstractCircuitBreaker {
} }
@Override @Override
public void onRequestComplete(Context context, ResourceWrapper r) { public void onRequestComplete(Context context) {
Entry entry = context.getCurEntry(); Entry entry = context.getCurEntry();
if (entry == null) { if (entry == null) {
return; return;

View File

@ -61,7 +61,7 @@ public class ResponseTimeCircuitBreaker extends AbstractCircuitBreaker {
} }
@Override @Override
public void onRequestComplete(Context context, ResourceWrapper wrapper) { public void onRequestComplete(Context context) {
SlowRequestCounter counter = slidingCounter.currentWindow().value(); SlowRequestCounter counter = slidingCounter.currentWindow().value();
Entry entry = context.getCurEntry(); Entry entry = context.getCurEntry();
if (entry == null) { if (entry == null) {