gus-asf commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489655442


##########
solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java:
##########
@@ -250,17 +253,17 @@ public void handleRequest(SolrQueryRequest req, 
SolrQueryResponse rsp) {
       long elapsed = timer.stop();
       metrics.totalTime.inc(elapsed);
 
-      if (cpuStats != null) {
-        Optional<Long> cpuTime = cpuStats.getCpuTimeMs();
+      if (publishCpuTime) {

Review Comment:
   It looks like the present implementation cannot avoid logging cpu time if 
cpuAllowed is used?



##########
solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java:
##########
@@ -622,8 +622,8 @@ public void handleRequestBody(SolrQueryRequest req, 
SolrQueryResponse rsp) throw
       } while (nextStage != Integer.MAX_VALUE);
 
       if (publishCpuTime) {
-        rsp.getResponseHeader().add(ThreadStats.CPU_TIME, totalShardCpuTime);
-        rsp.addToLog(ThreadStats.CPU_TIME, totalShardCpuTime);
+        rsp.getResponseHeader().add(ThreadCpuTime.CPU_TIME, totalShardCpuTime);

Review Comment:
   No null check here?



##########
solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java:
##########
@@ -236,12 +240,29 @@ private void initQueryLimits() {
    * empty) {@link QueryLimits} object if it has not been created, and will 
then return the same
    * object on every subsequent invocation.
    *
-   * @return The {@code QueryLimits} object for the current requet.
+   * @return The {@code QueryLimits} object for the current request.
    */
   public QueryLimits getLimits() {
+    // make sure the ThreadCpuTime is always initialized
+    getThreadCpuTime();
     return req == null
         ? QueryLimits.NONE
-        : (QueryLimits) req.getContext().computeIfAbsent(LIMITS_KEY, (k) -> 
new QueryLimits(req));
+        : (QueryLimits)
+            req.getContext()
+                .computeIfAbsent(LIMITS_KEY, (k) -> new QueryLimits(req, 
getThreadCpuTime()));

Review Comment:
   I don't like this API for QueryLimits constructor. Do we really want the 
possibility that it takes time limit from the request but the CPU time tracker 
from somewhere else? The `getThreadCpuTime()` call is just checking the request 
here, but this API leaves room for someone to do otherwise, and seems 
needlessly verbose.



##########
solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java:
##########
@@ -250,17 +253,17 @@ public void handleRequest(SolrQueryRequest req, 
SolrQueryResponse rsp) {
       long elapsed = timer.stop();
       metrics.totalTime.inc(elapsed);
 
-      if (cpuStats != null) {
-        Optional<Long> cpuTime = cpuStats.getCpuTimeMs();
+      if (publishCpuTime) {
+        Optional<Long> cpuTime = threadCpuTime.getCpuTimeMs();
         if (cpuTime.isPresent()) {
           // add CPU_TIME if not already added by SearchHandler
           NamedList<Object> header = rsp.getResponseHeader();
           if (header != null) {
-            if (header.get(ThreadStats.CPU_TIME) == null) {
-              header.add(ThreadStats.CPU_TIME, cpuTime.get());
+            if (header.get(ThreadCpuTime.CPU_TIME) == null) {

Review Comment:
   Is this correct? When would this get called twice for the same `rsp`? if so 
it appears that we would ignore a second invocation (but the `addToLog` below 
doesn't ignore it so they will differ?



##########
solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java:
##########
@@ -76,7 +77,7 @@ public abstract class RequestHandlerBase
 
   private PluginInfo pluginInfo;
 
-  protected boolean publishCpuTime = 
Boolean.getBoolean(ThreadStats.ENABLE_CPU_TIME);
+  protected boolean publishCpuTime = 
Boolean.getBoolean(ThreadCpuTime.ENABLE_CPU_TIME);

Review Comment:
   why does this default to false? This might make sense for logging but is 
strange for a query parameter. I think the logging and queryLimit aspects have 
become tightly coupled...



##########
solr/core/src/java/org/apache/solr/search/CpuQueryTimeLimit.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.util.Objects;
+import java.util.concurrent.TimeUnit;
+import org.apache.lucene.index.QueryTimeout;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.util.ThreadCpuTime;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CpuQueryTimeLimit implements QueryTimeout {
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  private final long limitAtNs;
+  private final ThreadCpuTime threadCpuTime;
+
+  public CpuQueryTimeLimit(SolrQueryRequest req, ThreadCpuTime threadCpuTime) {

Review Comment:
   I'm wondering if ThreadCpuTimer would be clearer, this sounds like passing a 
value.



##########
solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java:
##########
@@ -250,17 +253,17 @@ public void handleRequest(SolrQueryRequest req, 
SolrQueryResponse rsp) {
       long elapsed = timer.stop();
       metrics.totalTime.inc(elapsed);
 
-      if (cpuStats != null) {
-        Optional<Long> cpuTime = cpuStats.getCpuTimeMs();
+      if (publishCpuTime) {
+        Optional<Long> cpuTime = threadCpuTime.getCpuTimeMs();
         if (cpuTime.isPresent()) {
           // add CPU_TIME if not already added by SearchHandler
           NamedList<Object> header = rsp.getResponseHeader();

Review Comment:
   I see lots of places where this is checked for null, but I'm wondering why 
it's allowed to return null (not a topic  for this ticket however)



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