From 57434456eb4f52474c8354111d5ce68dec999019 Mon Sep 17 00:00:00 2001 From: Christine Poerschke Date: Fri, 1 Sep 2023 15:37:39 +0100 Subject: [PATCH] NO JIRA: factor out protected SearchHandler.checkCircuitBreakers method (#1882) (cherry picked from commit 199d51f1a3fe6188b405ef99f8f75ddd54729f6a) --- .../solr/handler/component/SearchHandler.java | 64 ++++++++++++------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java b/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java index c1ad4ae9900..d6fd21835bb 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java @@ -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 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); @@ -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 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