You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@solr.apache.org by "sigram (via GitHub)" <gi...@apache.org> on 2024/02/05 21:00:41 UTC

[PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

sigram opened a new pull request, #2244:
URL: https://github.com/apache/solr/pull/2244

   Details in Jira. This builds on top of changes in PR 2237.
   


-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1488505233


##########
solr/core/src/java/org/apache/solr/search/QueryLimits.java:
##########
@@ -41,11 +43,15 @@ private QueryLimits() {}
    * statement will hinge on hasXXXLimit() static method attached to the implementation class.
    *
    * @param req the current SolrQueryRequest.
+   * @param threadCpuTime current thread CPU time monitor.
    */
-  public QueryLimits(SolrQueryRequest req) {
+  public QueryLimits(SolrQueryRequest req, ThreadCpuTime threadCpuTime) {
     if (hasTimeLimit(req)) {
       limits.add(new SolrQueryTimeLimit(req));
     }
+    if (hasCpuLimit(req)) {
+      limits.add(new CpuQueryTimeLimit(req, threadCpuTime));

Review Comment:
   Still sad to see "Query" embedded in these names but okay



##########
solr/core/src/java/org/apache/solr/search/CpuQueryTimeLimit.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.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 limitAt;

Review Comment:
   It's not evident this is in nanoseconds; incorporate as a name suffix.



##########
solr/core/src/test/org/apache/solr/search/TestCpuQueryTimeLimit.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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 java.lang.invoke.MethodHandles;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.CloudUtil;
+import org.apache.solr.cloud.MiniSolrCloudCluster;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.util.ThreadCpuTime;
+import org.junit.Assume;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuQueryTimeLimit extends SolrCloudTestCase {

Review Comment:
   Is this really SolrCloud related?  Notice how TestCpuTimeSearch does some distributed search yet doesn't need SolrCloud to do it.  I'm trying to keep our tests leaner.



##########
solr/core/src/java/org/apache/solr/search/CpuQueryTimeLimit.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.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 limitAt;
+  private final ThreadCpuTime threadCpuTime;
+
+  public CpuQueryTimeLimit(SolrQueryRequest req, ThreadCpuTime threadCpuTime) {
+    if (!ThreadCpuTime.isSupported()) {
+      throw new IllegalArgumentException("Thread CPU time monitoring is not available.");
+    }
+    if (threadCpuTime == null) {
+      throw new IllegalArgumentException("Thread CPU time monitor must not be null.");
+    }
+    this.threadCpuTime = threadCpuTime;

Review Comment:
   Try java.util.Objects#requireNonNull(T, java.lang.String) which is designed for this and is succinct



##########
solr/core/src/test-files/solr/configsets/query-limits/conf/schema.xml:
##########


Review Comment:
   Please don't add more test configs unless we truly need it!  See `org.apache.solr.SolrTestCaseJ4#copyMinConf(java.io.File)` and `HideStackTraceTest` for an example of how to use it.



##########
solr/core/src/test/org/apache/solr/search/ExpensiveSearchComponent.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import org.apache.lucene.tests.util.LuceneTestCase;
+import org.apache.lucene.tests.util.TestUtil;
+import org.apache.solr.handler.component.ResponseBuilder;
+import org.apache.solr.handler.component.SearchComponent;
+
+public class ExpensiveSearchComponent extends SearchComponent {

Review Comment:
   Looking forward to seeing this on https://solr.cool/ :-)



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1492271009


##########
solr/core/src/test/org/apache/solr/search/TestCpuQueryTimeLimit.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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 java.lang.invoke.MethodHandles;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.CloudUtil;
+import org.apache.solr.cloud.MiniSolrCloudCluster;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.util.ThreadCpuTime;
+import org.junit.Assume;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuQueryTimeLimit extends SolrCloudTestCase {

Review Comment:
   Changed it back to SolrCloudTestCase, it was too unstable for distrib multi-shard query.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489762243


##########
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:
   They are not. It's true that `ThreadCpuTimer` will be created on first call to `SolrRequestInfo.getThreadCpuTimer()`, whoever first calls it (either due to this sysprop being true, or due to the `cpuAllowed` query param). However, after that the logging will happen only if the sysprop is true, again regardless of the `cpuAllowed` query param, and the limiting will happen regardless of the logging sysprop.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1490946283


##########
solr/core/src/test/org/apache/solr/search/TestCpuQueryTimeLimit.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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 java.lang.invoke.MethodHandles;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.CloudUtil;
+import org.apache.solr.cloud.MiniSolrCloudCluster;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.util.ThreadCpuTime;
+import org.junit.Assume;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuQueryTimeLimit extends SolrCloudTestCase {

Review Comment:
   I changed the latest version of the test to use SolrJettyTestRule.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489156800


##########
solr/core/src/java/org/apache/solr/search/QueryLimits.java:
##########
@@ -41,11 +43,15 @@ private QueryLimits() {}
    * statement will hinge on hasXXXLimit() static method attached to the implementation class.
    *
    * @param req the current SolrQueryRequest.
+   * @param threadCpuTime current thread CPU time monitor.
    */
-  public QueryLimits(SolrQueryRequest req) {
+  public QueryLimits(SolrQueryRequest req, ThreadCpuTime threadCpuTime) {
     if (hasTimeLimit(req)) {
       limits.add(new SolrQueryTimeLimit(req));
     }
+    if (hasCpuLimit(req)) {
+      limits.add(new CpuQueryTimeLimit(req, threadCpuTime));

Review Comment:
   It's still not too late to rename the whole family, for example:
   * `SolrQueryTimeLimit` -> `TimeAllowedLimit`
   * `CpuQueryTimeLimit` -> `CpuAllowedLimit`
   * `MemQueryLimit` -> `MemAllowedLimit`
   
   WDYT?



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489775946


##########
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:
   This can never be null due to the assignment that depends on the same condition in the if() around line 221.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489782985


##########
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:
   I don't think that's the case - see above comment.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1492951008


##########
solr/core/src/test/org/apache/solr/search/TestCpuAllowedLimit.java:
##########
@@ -20,32 +20,61 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.concurrent.TimeUnit;
-import org.apache.solr.SolrTestCaseJ4;
 import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
 import org.apache.solr.client.solrj.response.QueryResponse;
-import org.apache.solr.util.SolrJettyTestRule;
+import org.apache.solr.cloud.CloudUtil;
+import org.apache.solr.cloud.SolrCloudTestCase;
 import org.apache.solr.util.ThreadCpuTimer;
-import org.junit.AfterClass;
 import org.junit.Assume;
 import org.junit.BeforeClass;
-import org.junit.ClassRule;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class TestCpuAllowedLimit extends SolrTestCaseJ4 {
+public class TestCpuAllowedLimit extends SolrCloudTestCase {
   private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
-  @ClassRule public static final SolrJettyTestRule solrRule = new SolrJettyTestRule();
+  private static final String COLLECTION = "test";
 
-  @BeforeClass
-  public static void setup() {
-    System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true");
+  private static Path createConfigSet() throws Exception {
+    Path configSet = createTempDir();
+    copyMinConf(configSet.toFile());
+    // insert an expensive search component
+    Path solrConfig = configSet.resolve("conf/solrconfig.xml");
+    Files.writeString(
+        solrConfig,
+        Files.readString(solrConfig)
+            .replace(
+                "<requestHandler",
+                "<searchComponent name=\"expensiveSearchComponent\"\n"
+                    + "                   class=\"org.apache.solr.search.ExpensiveSearchComponent\"/>\n"
+                    + "\n"
+                    + "  <requestHandler")
+            .replace(
+                "class=\"solr.SearchHandler\">",
+                "class=\"solr.SearchHandler\">\n"
+                    + "    <arr name=\"first-components\">\n"
+                    + "      <str>expensiveSearchComponent</str>\n"
+                    + "    </arr>\n"));
+    return configSet.resolve("conf");
   }
 
-  @AfterClass
-  public static void teardown() {
-    System.clearProperty(ThreadCpuTimer.ENABLE_CPU_TIME);
+  @BeforeClass
+  public static void setup() throws Exception {
+    System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true");
+    Path configset = createConfigSet();
+    configureCluster(2).addConfig("conf", configset).configure();

Review Comment:
   Do we actually need 2 nodes to test this?  (Trying to keep tests as lean as needed to test what they need to test; no more)



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489766927


##########
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:
   I believe the idea was to make sure the total CPU time is added to response from any handler, not just the `SearchHandler` where it's calculated differently as a sum of per-shard times.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1491030742


##########
solr/core/src/java/org/apache/solr/search/QueryLimits.java:
##########
@@ -16,14 +16,13 @@
  */
 package org.apache.solr.search;
 
-import static org.apache.solr.search.CpuQueryTimeLimit.hasCpuLimit;
-import static org.apache.solr.search.SolrQueryTimeLimit.hasTimeLimit;
+import static org.apache.solr.search.CpuAllowedLimit.hasCpuLimit;
+import static org.apache.solr.search.TimeAllowedLimit.hasTimeLimit;
 
 import java.util.ArrayList;
 import java.util.List;
 import org.apache.lucene.index.QueryTimeout;
 import org.apache.solr.request.SolrQueryRequest;
-import org.apache.solr.util.ThreadCpuTime;
 
 /**
  * Represents the limitations on the query. These limits might be wall clock time, cpu time, memory,

Review Comment:
   Maybe this becomes "RequestLimits" or I'm pushing my luck with my suggestions :-)



##########
solr/core/src/test/org/apache/solr/search/TestCpuAllowedLimit.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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 java.lang.invoke.MethodHandles;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.util.SolrJettyTestRule;
+import org.apache.solr.util.ThreadCpuTimer;
+import org.junit.AfterClass;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuAllowedLimit extends SolrTestCaseJ4 {
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  @ClassRule public static final SolrJettyTestRule solrRule = new SolrJettyTestRule();
+
+  @BeforeClass
+  public static void setup() {
+    System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true");
+  }
+
+  @AfterClass
+  public static void teardown() {
+    System.clearProperty(ThreadCpuTimer.ENABLE_CPU_TIME);
+  }
+
+  @Test
+  public void testCompareToWallClock() throws Exception {
+    Assume.assumeTrue("Thread CPU time monitoring is not available", ThreadCpuTimer.isSupported());
+    long limitMs = 100;
+    CpuAllowedLimit cpuLimit = new CpuAllowedLimit(limitMs);
+    int[] randoms = new int[100];
+    long startNs = System.nanoTime();
+    int wakeups = 0;
+    while (!cpuLimit.shouldExit()) {
+      Thread.sleep(1);
+      // do some busywork
+      for (int i = 0; i < randoms.length; i++) {
+        randoms[i] = random().nextInt();
+      }
+      wakeups++;
+    }
+    long endNs = System.nanoTime();
+    long wallTimeDeltaMs = TimeUnit.MILLISECONDS.convert(endNs - startNs, TimeUnit.NANOSECONDS);
+    log.info(
+        "CPU limit: {} ms, elapsed wall-clock: {} ms, wakeups: {}",
+        limitMs,
+        wallTimeDeltaMs,
+        wakeups);
+    assertTrue(
+        "Elapsed wall-clock time expected much larger than 100ms but was " + wallTimeDeltaMs,
+        limitMs < wallTimeDeltaMs);
+  }
+
+  private Path createConfigSet() throws Exception {
+    Path configSet = createTempDir();
+    copyMinConf(configSet.toFile());
+    // insert an expensive search component
+    Path solrConfig = configSet.resolve("conf/solrconfig.xml");
+    Files.writeString(
+        solrConfig,
+        Files.readString(solrConfig)
+            .replace(
+                "<requestHandler",
+                "<searchComponent name=\"expensiveSearchComponent\"\n"
+                    + "                   class=\"org.apache.solr.search.ExpensiveSearchComponent\"/>\n"
+                    + "\n"
+                    + "  <requestHandler")
+            .replace(
+                "class=\"solr.SearchHandler\">",
+                "class=\"solr.SearchHandler\">\n"
+                    + "    <arr name=\"first-components\">\n"
+                    + "      <str>expensiveSearchComponent</str>\n"
+                    + "    </arr>\n"));
+    return configSet.resolve("conf");
+  }
+
+  @Test
+  public void testDistribLimit() throws Exception {

Review Comment:
   no distributed-search  (e.g. via "shards")?  Not mandatory to test this but if I thought that's why you wanted SolrCloudTestCase.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1491036832


##########
solr/core/src/java/org/apache/solr/search/QueryLimits.java:
##########
@@ -16,14 +16,13 @@
  */
 package org.apache.solr.search;
 
-import static org.apache.solr.search.CpuQueryTimeLimit.hasCpuLimit;
-import static org.apache.solr.search.SolrQueryTimeLimit.hasTimeLimit;
+import static org.apache.solr.search.CpuAllowedLimit.hasCpuLimit;
+import static org.apache.solr.search.TimeAllowedLimit.hasTimeLimit;
 
 import java.util.ArrayList;
 import java.util.List;
 import org.apache.lucene.index.QueryTimeout;
 import org.apache.solr.request.SolrQueryRequest;
-import org.apache.solr.util.ThreadCpuTime;
 
 /**
  * Represents the limitations on the query. These limits might be wall clock time, cpu time, memory,

Review Comment:
   Haha ... it's not a user-facing API, we can always rename it when we want to generalize it.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489552446


##########
solr/core/src/test/org/apache/solr/search/TestCpuQueryTimeLimit.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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 java.lang.invoke.MethodHandles;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.CloudUtil;
+import org.apache.solr.cloud.MiniSolrCloudCluster;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.util.ThreadCpuTime;
+import org.junit.Assume;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuQueryTimeLimit extends SolrCloudTestCase {

Review Comment:
   I wouldn't choose SolrCloudTestCase merely because you are not sure of this.  Remember that distributed-search existed before SolrCloud!



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489778608


##########
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:
   Yeah, I don't like this either. `ThreadCpuTimer.isSupported()` is the only condition for this to return false and it should be enough to check it.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "gus-asf (via GitHub)" <gi...@apache.org>.
gus-asf commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489578308


##########
solr/core/src/java/org/apache/solr/search/QueryLimits.java:
##########
@@ -41,11 +43,15 @@ private QueryLimits() {}
    * statement will hinge on hasXXXLimit() static method attached to the implementation class.
    *
    * @param req the current SolrQueryRequest.
+   * @param threadCpuTime current thread CPU time monitor.
    */
-  public QueryLimits(SolrQueryRequest req) {
+  public QueryLimits(SolrQueryRequest req, ThreadCpuTime threadCpuTime) {
     if (hasTimeLimit(req)) {
       limits.add(new SolrQueryTimeLimit(req));
     }
+    if (hasCpuLimit(req)) {
+      limits.add(new CpuQueryTimeLimit(req, threadCpuTime));

Review Comment:
   Actually, I like the idea of tracking the parameter names.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489255103


##########
solr/core/src/test/org/apache/solr/search/TestCpuQueryTimeLimit.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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 java.lang.invoke.MethodHandles;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.CloudUtil;
+import org.apache.solr.cloud.MiniSolrCloudCluster;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.util.ThreadCpuTime;
+import org.junit.Assume;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuQueryTimeLimit extends SolrCloudTestCase {

Review Comment:
   I'm not sure if this pseudo-cluster covers the same code paths when making distributed calls.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram merged PR #2244:
URL: https://github.com/apache/solr/pull/2244


-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1492269713


##########
solr/core/src/test/org/apache/solr/search/TestCpuAllowedLimit.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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 java.lang.invoke.MethodHandles;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.util.SolrJettyTestRule;
+import org.apache.solr.util.ThreadCpuTimer;
+import org.junit.AfterClass;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuAllowedLimit extends SolrTestCaseJ4 {
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  @ClassRule public static final SolrJettyTestRule solrRule = new SolrJettyTestRule();
+
+  @BeforeClass
+  public static void setup() {
+    System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true");
+  }
+
+  @AfterClass
+  public static void teardown() {
+    System.clearProperty(ThreadCpuTimer.ENABLE_CPU_TIME);
+  }
+
+  @Test
+  public void testCompareToWallClock() throws Exception {
+    Assume.assumeTrue("Thread CPU time monitoring is not available", ThreadCpuTimer.isSupported());
+    long limitMs = 100;
+    CpuAllowedLimit cpuLimit = new CpuAllowedLimit(limitMs);
+    int[] randoms = new int[100];
+    long startNs = System.nanoTime();
+    int wakeups = 0;
+    while (!cpuLimit.shouldExit()) {
+      Thread.sleep(1);
+      // do some busywork
+      for (int i = 0; i < randoms.length; i++) {
+        randoms[i] = random().nextInt();
+      }
+      wakeups++;
+    }
+    long endNs = System.nanoTime();
+    long wallTimeDeltaMs = TimeUnit.MILLISECONDS.convert(endNs - startNs, TimeUnit.NANOSECONDS);
+    log.info(
+        "CPU limit: {} ms, elapsed wall-clock: {} ms, wakeups: {}",
+        limitMs,
+        wallTimeDeltaMs,
+        wakeups);
+    assertTrue(
+        "Elapsed wall-clock time expected much larger than 100ms but was " + wallTimeDeltaMs,
+        limitMs < wallTimeDeltaMs);
+  }
+
+  private Path createConfigSet() throws Exception {
+    Path configSet = createTempDir();
+    copyMinConf(configSet.toFile());
+    // insert an expensive search component
+    Path solrConfig = configSet.resolve("conf/solrconfig.xml");
+    Files.writeString(
+        solrConfig,
+        Files.readString(solrConfig)
+            .replace(
+                "<requestHandler",
+                "<searchComponent name=\"expensiveSearchComponent\"\n"
+                    + "                   class=\"org.apache.solr.search.ExpensiveSearchComponent\"/>\n"
+                    + "\n"
+                    + "  <requestHandler")
+            .replace(
+                "class=\"solr.SearchHandler\">",
+                "class=\"solr.SearchHandler\">\n"
+                    + "    <arr name=\"first-components\">\n"
+                    + "      <str>expensiveSearchComponent</str>\n"
+                    + "    </arr>\n"));
+    return configSet.resolve("conf");
+  }
+
+  @Test
+  public void testDistribLimit() throws Exception {

Review Comment:
   Reverted back to SolrCloudTestCase, the other method was too unstable.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1491040941


##########
solr/core/src/test/org/apache/solr/search/TestCpuAllowedLimit.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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 java.lang.invoke.MethodHandles;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.util.SolrJettyTestRule;
+import org.apache.solr.util.ThreadCpuTimer;
+import org.junit.AfterClass;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuAllowedLimit extends SolrTestCaseJ4 {
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  @ClassRule public static final SolrJettyTestRule solrRule = new SolrJettyTestRule();
+
+  @BeforeClass
+  public static void setup() {
+    System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true");
+  }
+
+  @AfterClass
+  public static void teardown() {
+    System.clearProperty(ThreadCpuTimer.ENABLE_CPU_TIME);
+  }
+
+  @Test
+  public void testCompareToWallClock() throws Exception {
+    Assume.assumeTrue("Thread CPU time monitoring is not available", ThreadCpuTimer.isSupported());
+    long limitMs = 100;
+    CpuAllowedLimit cpuLimit = new CpuAllowedLimit(limitMs);
+    int[] randoms = new int[100];
+    long startNs = System.nanoTime();
+    int wakeups = 0;
+    while (!cpuLimit.shouldExit()) {
+      Thread.sleep(1);
+      // do some busywork
+      for (int i = 0; i < randoms.length; i++) {
+        randoms[i] = random().nextInt();
+      }
+      wakeups++;
+    }
+    long endNs = System.nanoTime();
+    long wallTimeDeltaMs = TimeUnit.MILLISECONDS.convert(endNs - startNs, TimeUnit.NANOSECONDS);
+    log.info(
+        "CPU limit: {} ms, elapsed wall-clock: {} ms, wakeups: {}",
+        limitMs,
+        wallTimeDeltaMs,
+        wakeups);
+    assertTrue(
+        "Elapsed wall-clock time expected much larger than 100ms but was " + wallTimeDeltaMs,
+        limitMs < wallTimeDeltaMs);
+  }
+
+  private Path createConfigSet() throws Exception {
+    Path configSet = createTempDir();
+    copyMinConf(configSet.toFile());
+    // insert an expensive search component
+    Path solrConfig = configSet.resolve("conf/solrconfig.xml");
+    Files.writeString(
+        solrConfig,
+        Files.readString(solrConfig)
+            .replace(
+                "<requestHandler",
+                "<searchComponent name=\"expensiveSearchComponent\"\n"
+                    + "                   class=\"org.apache.solr.search.ExpensiveSearchComponent\"/>\n"
+                    + "\n"
+                    + "  <requestHandler")
+            .replace(
+                "class=\"solr.SearchHandler\">",
+                "class=\"solr.SearchHandler\">\n"
+                    + "    <arr name=\"first-components\">\n"
+                    + "      <str>expensiveSearchComponent</str>\n"
+                    + "    </arr>\n"));
+    return configSet.resolve("conf");
+  }
+
+  @Test
+  public void testDistribLimit() throws Exception {

Review Comment:
   Hmm... yes, that would be useful. `TestCpuTimeSearch` has an example, I'm going to use it (though it's concerning that the distrib test there is Ignored).



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489562698


##########
solr/core/src/test/org/apache/solr/search/TestCpuQueryTimeLimit.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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 java.lang.invoke.MethodHandles;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.CloudUtil;
+import org.apache.solr.cloud.MiniSolrCloudCluster;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.util.ThreadCpuTime;
+import org.junit.Assume;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestCpuQueryTimeLimit extends SolrCloudTestCase {

Review Comment:
   Thinking of the new SolrClientTestRule stuff; it'd be cool if basic sharded search/indexing could be done to allow a test like the one here to switch between Standalone & SolrCloud trivially.  I'm hoping to get there but the work isn't done yet.  FYI: https://issues.apache.org/jira/browse/SOLR-11872



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "gus-asf (via GitHub)" <gi...@apache.org>.
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489775946


##########
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:
   This can never be null due to the assignment that depends on the same condition in the if() around line 221.



-- 
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-unsubscribe@solr.apache.org

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


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


Re: [PR] SOLR-17141: Create CpuQueryLimit implementation [solr]

Posted by "sigram (via GitHub)" <gi...@apache.org>.
sigram commented on code in PR #2244:
URL: https://github.com/apache/solr/pull/2244#discussion_r1489784004


##########
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:
   Do you mean for the responseHeader?



-- 
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-unsubscribe@solr.apache.org

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


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