sigram commented on code in PR #2708:
URL: https://github.com/apache/solr/pull/2708#discussion_r1777165905


##########
solr/core/src/java/org/apache/solr/search/MemAllowedLimit.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.search;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.lang.invoke.MethodHandles;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadMXBean;
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicLong;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.request.SolrQueryRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MemAllowedLimit implements QueryLimit {
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  private static final double MEBI = 1024.0 * 1024.0;
+  private static final ThreadMXBean threadBean = 
ManagementFactory.getThreadMXBean();
+  private static Method GET_BYTES_METHOD;
+  private static boolean supported;
+
+  static {
+    boolean testSupported;
+    try {
+      Class<?> sunThreadBeanClz = 
Class.forName("com.sun.management.ThreadMXBean");
+      if (sunThreadBeanClz.isAssignableFrom(threadBean.getClass())) {
+        Method m = 
sunThreadBeanClz.getMethod("isThreadAllocatedMemorySupported");
+        Boolean supported = (Boolean) m.invoke(threadBean);
+        if (supported) {
+          m = sunThreadBeanClz.getMethod("setThreadAllocatedMemoryEnabled", 
boolean.class);
+          m.invoke(threadBean, Boolean.TRUE);
+          testSupported = true;
+          GET_BYTES_METHOD = 
sunThreadBeanClz.getMethod("getCurrentThreadAllocatedBytes");
+        } else {
+          testSupported = false;
+        }
+      } else {
+        testSupported = false;
+      }
+    } catch (Exception e) {
+      testSupported = false;
+    }
+    supported = testSupported;
+  }
+
+  private static final ThreadLocal<AtomicLong> threadLocalMem =
+      ThreadLocal.withInitial(() -> new AtomicLong(-1L));
+
+  private long limitBytes;
+  private AtomicLong accumulatedMem = new AtomicLong();
+  private long exitedAt = 0;
+
+  public MemAllowedLimit(SolrQueryRequest req) {
+    if (!supported) {
+      throw new IllegalArgumentException(
+          "Per-thread memory allocation monitoring not available in this 
JVM.");
+    }
+    float reqMemLimit = req.getParams().getFloat(CommonParams.MEM_ALLOWED, 
-1.0f);
+    if (reqMemLimit <= 0.0f) {
+      throw new IllegalArgumentException(
+          "Check for limit with hasMemLimit(req) before creating a 
MemAllowedLimit!");
+    }
+    limitBytes = Math.round(reqMemLimit * MEBI);
+  }
+
+  @VisibleForTesting
+  MemAllowedLimit(float memLimit) {
+    limitBytes = Math.round(memLimit * MEBI);
+  }
+
+  @VisibleForTesting
+  static boolean isSupported() {
+    return supported;
+  }
+
+  static boolean hasMemLimit(SolrQueryRequest req) {
+    return req.getParams().getFloat(CommonParams.MEM_ALLOWED, -1.0f) > 0.0f;
+  }
+
+  @Override
+  public boolean shouldExit() {
+    if (exitedAt > 0L) {
+      return true;
+    }
+
+    try {
+      long currentAllocatedBytes = (Long) GET_BYTES_METHOD.invoke(threadBean);
+      AtomicLong threadMem = threadLocalMem.get();
+      threadMem.compareAndExchange(-1L, currentAllocatedBytes);
+      long lastAllocatedBytes = threadMem.get();
+      accumulatedMem.addAndGet(currentAllocatedBytes - lastAllocatedBytes);

Review Comment:
   > On the first pass this appears to be zero, meaning that you are ignoring 
bytes allocated before the first invocation of the limit check? Is that the 
intent?
   
   Fixed. Added javadoc.



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