cpoerschke commented on code in PR #1725:
URL: https://github.com/apache/solr/pull/1725#discussion_r1308670451


##########
solr/core/src/test/org/apache/solr/util/BaseTestCircuitBreaker.java:
##########
@@ -0,0 +1,314 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.util;
+
+import static org.hamcrest.CoreMatchers.containsString;
+
+import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.util.ExecutorUtil;
+import org.apache.solr.common.util.SolrNamedThreadFactory;
+import org.apache.solr.util.circuitbreaker.CPUCircuitBreaker;
+import org.apache.solr.util.circuitbreaker.CircuitBreaker;
+import org.apache.solr.util.circuitbreaker.MemoryCircuitBreaker;
+import org.hamcrest.MatcherAssert;
+import org.junit.After;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class BaseTestCircuitBreaker extends SolrTestCaseJ4 {
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  protected static void indexDocs() {
+    for (int i = 0; i < 20; i++) {
+      assertU(adoc("name", "john smith", "id", "1"));
+      assertU(adoc("name", "johathon smith", "id", "2"));
+      assertU(adoc("name", "john percival smith", "id", "3"));
+      assertU(adoc("id", "1", "title", "this is a title.", "inStock_b1", 
"true"));
+      assertU(adoc("id", "2", "title", "this is another title.", "inStock_b1", 
"true"));
+      assertU(adoc("id", "3", "title", "Mary had a little lamb.", 
"inStock_b1", "false"));
+
+      // commit inside the loop to get multiple segments to make search as 
realistic as possible
+      assertU(commit());
+    }
+  }
+
+  @Override
+  public void tearDown() throws Exception {
+    super.tearDown();
+  }
+
+  @After
+  public void after() {
+    h.getCore().getCircuitBreakerRegistry().deregisterAll();
+  }
+
+  public void testCBAlwaysTrips() {
+    removeAllExistingCircuitBreakers();
+
+    CircuitBreaker circuitBreaker = new MockCircuitBreaker(true);
+
+    h.getCore().getCircuitBreakerRegistry().register(circuitBreaker);
+
+    expectThrows(
+        SolrException.class,
+        () -> {
+          h.query(req("name:\"john smith\""));
+        });
+  }
+
+  public void testCBFakeMemoryPressure() {
+    removeAllExistingCircuitBreakers();
+
+    CircuitBreaker circuitBreaker = new FakeMemoryPressureCircuitBreaker();
+    MemoryCircuitBreaker memoryCircuitBreaker = (MemoryCircuitBreaker) 
circuitBreaker;
+
+    memoryCircuitBreaker.setThreshold(75);
+
+    h.getCore().getCircuitBreakerRegistry().register(circuitBreaker);
+
+    expectThrows(
+        SolrException.class,
+        () -> {
+          h.query(req("name:\"john smith\""));
+        });
+  }
+
+  public void testBuildingMemoryPressure() {
+    ExecutorService executor =
+        ExecutorUtil.newMDCAwareCachedThreadPool(new 
SolrNamedThreadFactory("TestCircuitBreaker"));
+
+    AtomicInteger failureCount = new AtomicInteger();
+
+    try {
+      removeAllExistingCircuitBreakers();
+
+      CircuitBreaker circuitBreaker = new 
BuildingUpMemoryPressureCircuitBreaker();
+      MemoryCircuitBreaker memoryCircuitBreaker = (MemoryCircuitBreaker) 
circuitBreaker;
+
+      memoryCircuitBreaker.setThreshold(75);
+
+      h.getCore().getCircuitBreakerRegistry().register(circuitBreaker);
+
+      List<Future<?>> futures = new ArrayList<>();
+
+      for (int i = 0; i < 5; i++) {
+        Future<?> future =
+            executor.submit(
+                () -> {
+                  try {
+                    h.query(req("name:\"john smith\""));
+                  } catch (SolrException e) {
+                    MatcherAssert.assertThat(
+                        e.getMessage(), containsString("Circuit Breakers 
tripped"));
+                    failureCount.incrementAndGet();
+                  } catch (Exception e) {
+                    throw new RuntimeException(e.getMessage());
+                  }
+                });
+
+        futures.add(future);
+      }
+
+      for (Future<?> future : futures) {
+        try {
+          future.get();
+        } catch (Exception e) {
+          throw new RuntimeException(e.getMessage());
+        }
+      }
+    } finally {
+      ExecutorUtil.shutdownAndAwaitTermination(executor);
+      assertEquals("Number of failed queries is not correct", 1, 
failureCount.get());
+    }
+  }
+
+  public void testFakeCPUCircuitBreaker() {
+    removeAllExistingCircuitBreakers();
+
+    CircuitBreaker circuitBreaker = new FakeCPUCircuitBreaker();
+    CPUCircuitBreaker cpuCircuitBreaker = (CPUCircuitBreaker) circuitBreaker;
+
+    cpuCircuitBreaker.setThreshold(75);
+
+    h.getCore().getCircuitBreakerRegistry().register(circuitBreaker);
+
+    AtomicInteger failureCount = new AtomicInteger();
+
+    ExecutorService executor =
+        ExecutorUtil.newMDCAwareCachedThreadPool(new 
SolrNamedThreadFactory("TestCircuitBreaker"));
+    try {
+      List<Future<?>> futures = new ArrayList<>();
+
+      for (int i = 0; i < 5; i++) {
+        Future<?> future =
+            executor.submit(
+                () -> {
+                  try {
+                    h.query(req("name:\"john smith\""));
+                  } catch (SolrException e) {
+                    MatcherAssert.assertThat(
+                        e.getMessage(), containsString("Circuit Breakers 
tripped"));
+                    failureCount.incrementAndGet();
+                  } catch (Exception e) {
+                    throw new RuntimeException(e.getMessage());
+                  }
+                });
+
+        futures.add(future);
+      }
+
+      for (Future<?> future : futures) {
+        try {
+          future.get();
+        } catch (Exception e) {
+          throw new RuntimeException(e.getMessage());
+        }
+      }
+    } finally {
+      ExecutorUtil.shutdownAndAwaitTermination(executor);
+      assertEquals("Number of failed queries is not correct", 5, 
failureCount.get());
+    }
+  }
+
+  public void testResponseWithCBTiming() {
+    removeAllExistingCircuitBreakers();
+
+    assertQ(
+        req("q", "*:*", CommonParams.DEBUG_QUERY, "true"),
+        "//str[@name='rawquerystring']='*:*'",
+        "//str[@name='querystring']='*:*'",
+        "//str[@name='parsedquery']='MatchAllDocsQuery(*:*)'",
+        "//str[@name='parsedquery_toString']='*:*'",
+        "count(//lst[@name='explain']/*)=3",
+        "//lst[@name='explain']/str[@name='1']",
+        "//lst[@name='explain']/str[@name='2']",
+        "//lst[@name='explain']/str[@name='3']",
+        "//str[@name='QParser']",
+        "count(//lst[@name='timing']/*)=3",
+        "//lst[@name='timing']/double[@name='time']",
+        "count(//lst[@name='prepare']/*)>0",
+        "//lst[@name='prepare']/double[@name='time']",
+        "count(//lst[@name='process']/*)>0",
+        "//lst[@name='process']/double[@name='time']");
+
+    CircuitBreaker circuitBreaker = new MockCircuitBreaker(false);
+    h.getCore().getCircuitBreakerRegistry().register(circuitBreaker);
+
+    assertQ(
+        req("q", "*:*", CommonParams.DEBUG_QUERY, "true"),
+        "//str[@name='rawquerystring']='*:*'",
+        "//str[@name='querystring']='*:*'",
+        "//str[@name='parsedquery']='MatchAllDocsQuery(*:*)'",
+        "//str[@name='parsedquery_toString']='*:*'",
+        "count(//lst[@name='explain']/*)=3",
+        "//lst[@name='explain']/str[@name='1']",
+        "//lst[@name='explain']/str[@name='2']",
+        "//lst[@name='explain']/str[@name='3']",
+        "//str[@name='QParser']",
+        "count(//lst[@name='timing']/*)=4",
+        "//lst[@name='timing']/double[@name='time']",
+        "count(//lst[@name='circuitbreaker']/*)>0",
+        "//lst[@name='circuitbreaker']/double[@name='time']",
+        "count(//lst[@name='prepare']/*)>0",
+        "//lst[@name='prepare']/double[@name='time']",
+        "count(//lst[@name='process']/*)>0",
+        "//lst[@name='process']/double[@name='time']");
+  }
+
+  private void removeAllExistingCircuitBreakers() {
+    List<CircuitBreaker> registeredCircuitBreakers =
+        h.getCore().getCircuitBreakerRegistry().getRegisteredCircuitBreakers();
+
+    registeredCircuitBreakers.clear();

Review Comment:
   No problem! #1870 for the follow-up bits. Would you mind including it in the 
back port?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to