atris commented on a change in pull request #1626:
URL: https://github.com/apache/lucene-solr/pull/1626#discussion_r447185389



##########
File path: 
solr/core/src/java/org/apache/solr/util/circuitbreaker/MemoryCircuitBreaker.java
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.circuitbreaker;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryMXBean;
+
+import org.apache.solr.core.SolrCore;
+
+/**
+ * Tracks the current JVM heap usage and triggers if it exceeds the defined 
percentage of the maximum
+ * heap size allocated to the JVM. This circuit breaker is a part of the 
default CircuitBreakerManager
+ * so is checked for every request -- hence it is realtime. Once the memory 
usage goes below the threshold,
+ * it will start allowing queries again.
+ *
+ * The memory threshold is defined as a percentage of the maximum memory 
allocated -- see memoryCircuitBreakerThreshold
+ * in solrconfig.xml
+ */
+
+public class MemoryCircuitBreaker extends CircuitBreaker {
+  private static final MemoryMXBean MEMORY_MX_BEAN = 
ManagementFactory.getMemoryMXBean();
+
+  private final long currentMaxHeap = 
MEMORY_MX_BEAN.getHeapMemoryUsage().getMax();
+
+  // Assumption -- the value of these parameters will be set correctly before 
invoking printDebugInfo()
+  private ThreadLocal<Long> seenMemory = new ThreadLocal<>();
+  private ThreadLocal<Long> allowedMemory = new ThreadLocal<>();
+
+  public MemoryCircuitBreaker(SolrCore solrCore) {
+    super(solrCore);
+
+    if (currentMaxHeap <= 0) {
+      throw new IllegalArgumentException("Invalid JVM state for the max heap 
usage");
+    }
+  }
+
+  // TODO: An optimization can be to trip the circuit breaker for a duration 
of time
+  // after the circuit breaker condition is matched. This will optimize for 
per call
+  // overhead of calculating the condition parameters but can result in false 
positives.
+  @Override
+  public boolean isCircuitBreakerGauntletTripped() {
+    if (!isCircuitBreakerEnabled()) {
+      return false;
+    }
+
+    allowedMemory.set(getCurrentMemoryThreshold());
+
+    seenMemory.set(calculateLiveMemoryUsage());
+
+    return (seenMemory.get() >= allowedMemory.get());
+  }
+
+  @Override
+  public String printDebugInfo() {
+    return "seenMemory=" + seenMemory.get() + " allowedMemory=" + 
allowedMemory.get();
+  }
+
+  private long getCurrentMemoryThreshold() {
+    int thresholdValueInPercentage = 
solrCore.getSolrConfig().memoryCircuitBreakerThreshold;
+    double thresholdInFraction = thresholdValueInPercentage / (double) 100;
+    long actualLimit = (long) (currentMaxHeap * thresholdInFraction);
+
+    if (actualLimit <= 0) {
+      throw new IllegalStateException("Memory limit cannot be less than or 
equal to zero");
+    }
+
+    return actualLimit;
+  }
+
+  /**
+   * Calculate the live memory usage for the system. This method has package 
visibility
+   * to allow using for testing
+   * @return Memory usage in bytes
+   */
+  protected long calculateLiveMemoryUsage() {
+    // NOTE: MemoryUsageGaugeSet provides memory usage statistics but we do 
not use them
+    // here since MemoryUsageGaugeSet provides combination of heap and non 
heap usage and

Review comment:
       Better wording, thanks!




----------------------------------------------------------------
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.

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



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

Reply via email to