Skip to content

Commit

Permalink
NO JIRA: factor out protected SearchHandler.checkCircuitBreakers meth…
Browse files Browse the repository at this point in the history
…od (apache#1882)

(cherry picked from commit 199d51f)
  • Loading branch information
cpoerschke authored and Justin Sweeney committed Nov 2, 2023
1 parent 3749004 commit 5743445
Showing 1 changed file with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,44 @@ protected ResponseBuilder newResponseBuilder(
return new ResponseBuilder(req, rsp, components);
}

/**
* Check if circuit breakers are tripped. Override this method in sub classes that do not want to
* check circuit breakers.
*
* @return true if circuit breakers are tripped, false otherwise.
*/
protected boolean checkCircuitBreakers(
SolrQueryRequest req, SolrQueryResponse rsp, ResponseBuilder rb) {
final RTimerTree timer = rb.isDebug() ? req.getRequestTimer() : null;

final CircuitBreakerRegistry circuitBreakerRegistry = req.getCore().getCircuitBreakerRegistry();
if (circuitBreakerRegistry.isEnabled()) {
List<CircuitBreaker> trippedCircuitBreakers;

if (timer != null) {
RTimerTree subt = timer.sub("circuitbreaker");
rb.setTimer(subt);

trippedCircuitBreakers = circuitBreakerRegistry.checkTripped();

rb.getTimer().stop();
} else {
trippedCircuitBreakers = circuitBreakerRegistry.checkTripped();
}

if (trippedCircuitBreakers != null) {
String errorMessage = CircuitBreakerRegistry.toErrorMessage(trippedCircuitBreakers);
rsp.add(STATUS, FAILURE);
rsp.setException(
new SolrException(
SolrException.ErrorCode.SERVICE_UNAVAILABLE,
"Circuit Breakers tripped " + errorMessage));
return true;
}
}
return false;
}

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
boolean isShard = req.getParams().getBool(ShardParams.IS_SHARD, false);
Expand Down Expand Up @@ -387,30 +425,8 @@ public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throw

final RTimerTree timer = rb.isDebug() ? req.getRequestTimer() : null;

final CircuitBreakerRegistry circuitBreakerRegistry = req.getCore().getCircuitBreakerRegistry();
if (circuitBreakerRegistry.isEnabled()) {
List<CircuitBreaker> trippedCircuitBreakers;

if (timer != null) {
RTimerTree subt = timer.sub("circuitbreaker");
rb.setTimer(subt);

trippedCircuitBreakers = circuitBreakerRegistry.checkTripped();

rb.getTimer().stop();
} else {
trippedCircuitBreakers = circuitBreakerRegistry.checkTripped();
}

if (trippedCircuitBreakers != null) {
String errorMessage = CircuitBreakerRegistry.toErrorMessage(trippedCircuitBreakers);
rsp.add(STATUS, FAILURE);
rsp.setException(
new SolrException(
SolrException.ErrorCode.SERVICE_UNAVAILABLE,
"Circuit Breakers tripped " + errorMessage));
return;
}
if (checkCircuitBreakers(req, rsp, rb)) {
return; // Circuit breaker tripped, return immediately
}

// creates a ShardHandler object only if it's needed
Expand Down

0 comments on commit 5743445

Please sign in to comment.