Skip to content

Commit

Permalink
Updated time unit to milliseconds and fixed the indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
amishra-u committed Apr 20, 2023
1 parent c9a8860 commit a102961
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ enum State {
State state();

/** Called after an execution failed. */
void recordFailure(Throwable t);
void recordFailure(Exception e);

/** Called after an execution succeeded. */
void recordSuccess();
Expand Down Expand Up @@ -130,7 +130,7 @@ public State state() {
}

@Override
public void recordFailure(Throwable t) {}
public void recordFailure(Exception e) {}

@Override
public void recordSuccess() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.devtools.build.lib.remote.Retrier;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.remote.Retrier;
import com.google.devtools.build.lib.remote.common.CacheNotFoundException;
import com.google.devtools.build.lib.remote.options.RemoteOptions;

Expand All @@ -11,24 +10,24 @@
*/
public class CircuitBreakerFactory {

public static final ImmutableSet<Class<? extends Exception>> DEFAULT_IGNORED_ERRORS =
ImmutableSet.of(CacheNotFoundException.class);
public static final ImmutableSet<Class<? extends Exception>> DEFAULT_IGNORED_ERRORS =
ImmutableSet.of(CacheNotFoundException.class);

private CircuitBreakerFactory() {
}
private CircuitBreakerFactory() {
}

/**
* Creates the instance of the CircuitBreaker as per the strategy defined in CircuitBreakerConfig.
* In case of undefined strategy defaults to {@link Retrier.ALLOW_ALL_CALLS} implementation.
*
* @param remoteOptions The configuration for the CircuitBreaker implementation.
* @return an instance of CircuitBreaker.
*/
public static Retrier.CircuitBreaker createCircuitBreaker(final RemoteOptions remoteOptions) {
if (remoteOptions.circuitBreakerStrategy == RemoteOptions.CircuitBreakerStrategy.FAILURE) {
return new FailureCircuitBreaker(remoteOptions.remoteFailureThreshold,
(int) remoteOptions.remoteFailureWindowSize.toSeconds());
}
return Retrier.ALLOW_ALL_CALLS;
/**
* Creates the instance of the {@link Retrier.CircuitBreaker} as per the strategy defined in {@link RemoteOptions}.
* In case of undefined strategy defaults to {@link Retrier.ALLOW_ALL_CALLS} implementation.
*
* @param remoteOptions The configuration for the CircuitBreaker implementation.
* @return an instance of CircuitBreaker.
*/
public static Retrier.CircuitBreaker createCircuitBreaker(final RemoteOptions remoteOptions) {
if (remoteOptions.circuitBreakerStrategy == RemoteOptions.CircuitBreakerStrategy.FAILURE) {
return new FailureCircuitBreaker(remoteOptions.remoteFailureThreshold,
(int) remoteOptions.remoteFailureWindowSize.toMillis());
}
return Retrier.ALLOW_ALL_CALLS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,52 @@
*/
public class FailureCircuitBreaker implements Retrier.CircuitBreaker {

private State state;
private final AtomicInteger failures;
private final int failureThreshold;
private final int slidingWindowSize;
private final ScheduledExecutorService scheduledExecutor;
private final ImmutableSet<Class<? extends Exception>> ignoredErrors;
private State state;
private final AtomicInteger failures;
private final int failureThreshold;
private final int slidingWindowSize;
private final ScheduledExecutorService scheduledExecutor;
private final ImmutableSet<Class<? extends Exception>> ignoredErrors;

/**
* Creates a {@link FailureCircuitBreaker}.
*
* @param failureThreshold is used to set the number of failures required to trip the circuit breaker in given
* time window.
* @param slidingWindowSize the size of the sliding window in seconds to calculate the number of failures.
*/
public FailureCircuitBreaker(final int failureThreshold, final int slidingWindowSize) {
this.failureThreshold = failureThreshold;
this.failures = new AtomicInteger(0);
this.slidingWindowSize = slidingWindowSize;
this.state = State.ACCEPT_CALLS;
this.scheduledExecutor = slidingWindowSize > 0 ? Executors.newSingleThreadScheduledExecutor() : null;
this.ignoredErrors = CircuitBreakerFactory.DEFAULT_IGNORED_ERRORS;
}

@Override
public State state() {
return this.state;
}
/**
* Creates a {@link FailureCircuitBreaker}.
*
* @param failureThreshold is used to set the number of failures required to trip the circuit breaker in given
* time window.
* @param slidingWindowSize the size of the sliding window in milliseconds to calculate the number of failures.
*/
public FailureCircuitBreaker(final int failureThreshold, final int slidingWindowSize) {
this.failureThreshold = failureThreshold;
this.failures = new AtomicInteger(0);
this.slidingWindowSize = slidingWindowSize;
this.state = State.ACCEPT_CALLS;
this.scheduledExecutor = slidingWindowSize > 0 ? Executors.newSingleThreadScheduledExecutor() : null;
this.ignoredErrors = CircuitBreakerFactory.DEFAULT_IGNORED_ERRORS;
}

@Override
public void recordFailure(Throwable t) {
if (ignoredErrors.contains(t.getClass())) {
return;
}
@Override
public State state() {
return this.state;
}

int failureCount = failures.incrementAndGet();
if (slidingWindowSize > 0) {
scheduledExecutor.schedule(failures::decrementAndGet, slidingWindowSize, TimeUnit.SECONDS);
}
// Since the state can only be changed to the open state, synchronization is not required.
if (failureCount > this.failureThreshold) {
this.state = State.REJECT_CALLS;
}
@Override
public void recordFailure(Exception e) {
if (ignoredErrors.contains(e.getClass())) {
return;
}

@Override
public void recordSuccess() {
// do nothing, implement if we need to set threshold on failure rate instead of count.
int failureCount = failures.incrementAndGet();
if (slidingWindowSize > 0) {
scheduledExecutor.schedule(failures::decrementAndGet, slidingWindowSize, TimeUnit.MILLISECONDS);
}
// Since the state can only be changed to the open state, synchronization is not required.
if (failureCount > this.failureThreshold) {
this.state = State.REJECT_CALLS;
}
}

@Override
public void recordSuccess() {
// do nothing, implement if we need to set threshold on failure rate instead of count.
}
}
Loading

0 comments on commit a102961

Please sign in to comment.