From 36b312a5d500c6e06538bafd106a82a8cbc215ea Mon Sep 17 00:00:00 2001
From: Justin Sweeney <justinsweeney@fullstory.com>
Date: Fri, 3 Nov 2023 08:30:47 -0400
Subject: [PATCH] Adding debug mode for circuit breakers (#162)

* Adding debug mode for circuit breakers

* Tidying code
---
 .../util/circuitbreaker/CircuitBreaker.java     | 10 ++++++++++
 .../circuitbreaker/CircuitBreakerRegistry.java  | 17 +++++++++++++----
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/util/circuitbreaker/CircuitBreaker.java b/solr/core/src/java/org/apache/solr/util/circuitbreaker/CircuitBreaker.java
index 78841cceaf7..b521cfb1123 100644
--- a/solr/core/src/java/org/apache/solr/util/circuitbreaker/CircuitBreaker.java
+++ b/solr/core/src/java/org/apache/solr/util/circuitbreaker/CircuitBreaker.java
@@ -49,6 +49,8 @@ public abstract class CircuitBreaker implements NamedListInitializedPlugin, Clos
   private final List<SolrRequestType> SUPPORTED_TYPES =
       List.of(SolrRequestType.QUERY, SolrRequestType.UPDATE);
 
+  private boolean debugMode = false;
+
   @Override
   public void init(NamedList<?> args) {
     SolrPluginUtils.invokeSetters(this, args);
@@ -62,6 +64,10 @@ public CircuitBreaker() {}
   /** Get error message when the circuit breaker triggers */
   public abstract String getErrorMessage();
 
+  public boolean isDebugMode() {
+    return debugMode;
+  }
+
   @Override
   public void close() throws IOException {
     // Nothing to do by default
@@ -110,4 +116,8 @@ public static SolrException.ErrorCode getErrorCode(List<CircuitBreaker> trippedC
       return SolrException.ErrorCode.TOO_MANY_REQUESTS;
     }
   }
+
+  public void setDebugMode(boolean debugMode) {
+    this.debugMode = debugMode;
+  }
 }
diff --git a/solr/core/src/java/org/apache/solr/util/circuitbreaker/CircuitBreakerRegistry.java b/solr/core/src/java/org/apache/solr/util/circuitbreaker/CircuitBreakerRegistry.java
index a7081df96f6..a63432f7aed 100644
--- a/solr/core/src/java/org/apache/solr/util/circuitbreaker/CircuitBreakerRegistry.java
+++ b/solr/core/src/java/org/apache/solr/util/circuitbreaker/CircuitBreakerRegistry.java
@@ -82,11 +82,20 @@ public List<CircuitBreaker> checkTripped(SolrRequestType requestType) {
     for (CircuitBreaker circuitBreaker :
         circuitBreakerMap.getOrDefault(requestType, Collections.emptyList())) {
       if (circuitBreaker.isTripped()) {
-        if (triggeredCircuitBreakers == null) {
-          triggeredCircuitBreakers = new ArrayList<>();
+        if (circuitBreaker.isDebugMode()) {
+          if (log.isInfoEnabled()) {
+            log.info(
+                "Circuit breaker {} has tripped: {}",
+                circuitBreaker.getClass().getSimpleName(),
+                circuitBreaker.getErrorMessage());
+          }
+        } else {
+          if (triggeredCircuitBreakers == null) {
+            triggeredCircuitBreakers = new ArrayList<>();
+          }
+
+          triggeredCircuitBreakers.add(circuitBreaker);
         }
-
-        triggeredCircuitBreakers.add(circuitBreaker);
       }
     }