Apache Solr open-source search software
SOLR-17151 Check limits between calls to components in SearchHandler (#2801)
Gus Heck125fc3fb70c6
diff --git a/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java b/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.javaindex 9f438fc4605..0fb625d58e6 100644--- a/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java+++ b/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java@@ -47,6 +47,7 @@ import org.apache.solr.request.SolrRequestHandler;import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.search.CpuAllowedLimit;import org.apache.solr.search.QueryLimits;+import org.apache.solr.search.QueryLimitsExceededException;import org.apache.solr.search.SyntaxError;import org.apache.solr.security.PermissionNameProvider;import org.apache.solr.update.processor.DistributedUpdateProcessor;@@ -241,6 +242,8 @@ public abstract class RequestHandlerBasemetrics.numTimeouts.mark();rsp.setHttpCaching(false);}+ } catch (QueryLimitsExceededException e) {+ rsp.setPartialResults(req);} catch (Exception e) {Exception normalized = normalizeReceivedException(req, e);processErrorMetricsOnException(normalized, metrics);diff --git a/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java b/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.javaindex 3acb78ed78f..2a2363dab3b 100644--- a/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java+++ b/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java@@ -615,17 +615,23 @@ public class QueryComponent extends SearchComponent {}protected int regularDistributedProcess(ResponseBuilder rb) {- if (rb.stage < ResponseBuilder.STAGE_PARSE_QUERY) return ResponseBuilder.STAGE_PARSE_QUERY;+ if (rb.stage < ResponseBuilder.STAGE_PARSE_QUERY) {+ return ResponseBuilder.STAGE_PARSE_QUERY;+ }if (rb.stage == ResponseBuilder.STAGE_PARSE_QUERY) {createDistributedStats(rb);return ResponseBuilder.STAGE_EXECUTE_QUERY;}- if (rb.stage < ResponseBuilder.STAGE_EXECUTE_QUERY) return ResponseBuilder.STAGE_EXECUTE_QUERY;+ if (rb.stage < ResponseBuilder.STAGE_EXECUTE_QUERY) {+ return ResponseBuilder.STAGE_EXECUTE_QUERY;+ }if (rb.stage == ResponseBuilder.STAGE_EXECUTE_QUERY) {createMainQuery(rb);return ResponseBuilder.STAGE_GET_FIELDS;}- if (rb.stage < ResponseBuilder.STAGE_GET_FIELDS) return ResponseBuilder.STAGE_GET_FIELDS;+ if (rb.stage < ResponseBuilder.STAGE_GET_FIELDS) {+ return ResponseBuilder.STAGE_GET_FIELDS;+ }if (rb.stage == ResponseBuilder.STAGE_GET_FIELDS && !rb.onePassDistributedQuery) {createRetrieveDocs(rb);return ResponseBuilder.STAGE_DONE;diff --git a/solr/core/src/java/org/apache/solr/handler/component/ResponseBuilder.java b/solr/core/src/java/org/apache/solr/handler/component/ResponseBuilder.javaindex 55addf143f8..cf513bb63c7 100644--- a/solr/core/src/java/org/apache/solr/handler/component/ResponseBuilder.java+++ b/solr/core/src/java/org/apache/solr/handler/component/ResponseBuilder.java@@ -122,13 +122,13 @@ public class ResponseBuilder {* public static final String LOCAL_SHARD = "local"; public static final String DOC_QUERY = "dq";* **/- public static int STAGE_START = 0;+ public static final int STAGE_START = 0;- public static int STAGE_PARSE_QUERY = 1000;- public static int STAGE_TOP_GROUPS = 1500;- public static int STAGE_EXECUTE_QUERY = 2000;- public static int STAGE_GET_FIELDS = 3000;- public static int STAGE_DONE = Integer.MAX_VALUE;+ public static final int STAGE_PARSE_QUERY = 1000;+ public static final int STAGE_TOP_GROUPS = 1500;+ public static final int STAGE_EXECUTE_QUERY = 2000;+ public static final int STAGE_GET_FIELDS = 3000;+ public static final int STAGE_DONE = Integer.MAX_VALUE;public int stage; // What stage is this current request at?diff --git a/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java b/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.javaindex 2c1503a907f..2d6b36ac3cb 100644--- a/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java@@ -18,8 +18,17 @@ package org.apache.solr.handler.component;import static org.apache.solr.common.params.CommonParams.DISTRIB;import static org.apache.solr.common.params.CommonParams.PATH;+import static org.apache.solr.handler.component.ResponseBuilder.STAGE_DONE;+import static org.apache.solr.handler.component.ResponseBuilder.STAGE_EXECUTE_QUERY;+import static org.apache.solr.handler.component.ResponseBuilder.STAGE_GET_FIELDS;+import static org.apache.solr.handler.component.ResponseBuilder.STAGE_PARSE_QUERY;+import static org.apache.solr.handler.component.ResponseBuilder.STAGE_START;+import static org.apache.solr.handler.component.ResponseBuilder.STAGE_TOP_GROUPS;+import static org.apache.solr.request.SolrRequestInfo.getQueryLimits;+import static org.apache.solr.response.SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY;import com.codahale.metrics.Counter;+import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;import java.lang.invoke.MethodHandles;@@ -31,6 +40,8 @@ import java.util.Map;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.atomic.AtomicLong;+import java.util.concurrent.atomic.AtomicReference;+import java.util.stream.Collectors;import org.apache.lucene.index.ExitableDirectoryReader;import org.apache.lucene.search.TotalHits;import org.apache.solr.client.solrj.SolrRequest.SolrRequestType;@@ -411,24 +422,58 @@ public class SearchHandler extends RequestHandlerBasereturn; // Circuit breaker tripped, return immediately}- // creates a ShardHandler object only if it's needed- final ShardHandler shardHandler1 = getAndPrepShardHandler(req, rb);+ processComponents(req, rsp, rb, timer, components);- if (timer == null) {- // non-debugging prepare phase- for (SearchComponent c : components) {- c.prepare(rb);- }- } else {- // debugging prepare phase- RTimerTree subt = timer.sub("prepare");- for (SearchComponent c : components) {- rb.setTimer(subt.sub(c.getName()));- c.prepare(rb);- rb.getTimer().stop();+ // SOLR-5550: still provide shards.info if requested even for a short-circuited distrib request+ if (!rb.isDistrib+ && req.getParams().getBool(ShardParams.SHARDS_INFO, false)+ && rb.shortCircuitedURL != null) {+ NamedList<Object> shardInfo = new SimpleOrderedMap<>();+ SimpleOrderedMap<Object> nl = new SimpleOrderedMap<>();+ if (rsp.getException() != null) {+ Throwable cause = rsp.getException();+ if (cause instanceof SolrServerException) {+ cause = ((SolrServerException) cause).getRootCause();+ } else {+ if (cause.getCause() != null) {+ cause = cause.getCause();+ }+ }+ nl.add("error", cause.toString());+ if (!core.getCoreContainer().hideStackTrace()) {+ StringWriter trace = new StringWriter();+ cause.printStackTrace(new PrintWriter(trace));+ nl.add("trace", trace.toString());+ }+ } else if (rb.getResults() != null) {+ nl.add("numFound", rb.getResults().docList.matches());+ nl.add(+ "numFoundExact",+ rb.getResults().docList.hitCountRelation() == TotalHits.Relation.EQUAL_TO);+ nl.add("maxScore", rb.getResults().docList.maxScore());}- subt.stop();+ nl.add("shardAddress", rb.shortCircuitedURL);+ nl.add("time", req.getRequestTimer().getTime()); // elapsed time of this request so far++ int pos = rb.shortCircuitedURL.indexOf("://");+ String shardInfoName =+ pos != -1 ? rb.shortCircuitedURL.substring(pos + 3) : rb.shortCircuitedURL;+ shardInfo.add(shardInfoName, nl);+ rsp.getValues().add(ShardParams.SHARDS_INFO, shardInfo);}+ }++ private void processComponents(+ SolrQueryRequest req,+ SolrQueryResponse rsp,+ ResponseBuilder rb,+ RTimerTree timer,+ List<SearchComponent> components)+ throws IOException {+ // creates a ShardHandler object only if it's needed+ final ShardHandler shardHandler1 = getAndPrepShardHandler(req, rb);++ if (!prepareComponents(req, rb, timer, components)) return;{ // Once all of our components have been prepared, check if this request involves a SortSpec.// If it does, and if our request includes a cursorMark param, then parse & init the@@ -445,19 +490,26 @@ public class SearchHandler extends RequestHandlerBaseif (!rb.isDistrib) {// a normal non-distributed request-try {// The semantics of debugging vs not debugging are different enough that// it makes sense to have two control loopsif (!rb.isDebug()) {// Processfor (SearchComponent c : components) {+ if (checkLimitsBefore(c, "process", rb.req, rb.rsp, components)) {+ shortCircuitedResults(req, rb);+ return;+ }c.process(rb);}} else {// ProcessRTimerTree subt = timer.sub("process");for (SearchComponent c : components) {+ if (checkLimitsBefore(c, "process debug", rb.req, rb.rsp, components)) {+ shortCircuitedResults(req, rb);+ return;+ }rb.setTimer(subt.sub(c.getName()));c.process(rb);rb.getTimer().stop();@@ -471,22 +523,7 @@ public class SearchHandler extends RequestHandlerBase}} catch (ExitableDirectoryReader.ExitingReaderException ex) {log.warn("Query: {}; ", req.getParamString(), ex);- if (rb.rsp.getResponse() == null) {- rb.rsp.addResponse(new SolrDocumentList());-- // If a cursorMark was passed, and we didn't progress, set- // the nextCursorMark to the same position- String cursorStr = rb.req.getParams().get(CursorMarkParams.CURSOR_MARK_PARAM);- if (null != cursorStr) {- rb.rsp.add(CursorMarkParams.CURSOR_MARK_NEXT, cursorStr);- }- }- if (rb.isDebug()) {- NamedList<Object> debug = new NamedList<>();- debug.add("explain", new NamedList<>());- rb.rsp.add("debug", debug);- }- rb.rsp.setPartialResults(rb.req);+ shortCircuitedResults(req, rb);}} else {// a distributed request@@ -504,7 +541,10 @@ public class SearchHandler extends RequestHandlerBase// call all componentsfor (SearchComponent c : components) {- // the next stage is the minimum of what all components report+ if (checkLimitsBefore(c, "distrib", rb.req, rb.rsp, components)) {+ shortCircuitedResults(req, rb);+ return;+ } // the next stage is the minimum of what all components reportnextStage = Math.min(nextStage, c.distributedProcess(rb));}@@ -558,6 +598,8 @@ public class SearchHandler extends RequestHandlerBase? shardHandler1.takeCompletedIncludingErrors(): shardHandler1.takeCompletedOrError();if (srsp == null) break; // no more requests to wait for+ AtomicReference<Object> detailMesg =+ new AtomicReference<>(); // or perhaps new Object[1] ?boolean anyResponsesPartial =srsp.getShardRequest().responses.stream()@@ -568,9 +610,20 @@ public class SearchHandler extends RequestHandlerBasereturn false;}Object recursive = resp.findRecursive("responseHeader", "partialResults");+ if (recursive != null) {+ Object message =+ "[Shard:"+ + response.getShardAddress()+ + "]"+ + resp.findRecursive(+ "responseHeader",+ RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY);+ detailMesg.compareAndSet(null, message); // first one, ingore rest+ }return recursive != null;});if (anyResponsesPartial) {+ rb.rsp.addPartialResponseDetail(detailMesg.get());rsp.setPartialResults(rb.req);}// Was there an exception?@@ -594,6 +647,11 @@ public class SearchHandler extends RequestHandlerBasethrowSolrException(srsp.getException());} else {rsp.setPartialResults(rb.req);+ if (publishCpuTime) {+ totalShardCpuTime += computeShardCpuTime(srsp.getShardRequest().responses);+ rsp.getResponseHeader().add(ThreadCpuTimer.CPU_TIME, totalShardCpuTime);+ rsp.addToLog(ThreadCpuTimer.CPU_TIME, totalShardCpuTime);+ }}}}@@ -602,6 +660,15 @@ public class SearchHandler extends RequestHandlerBase// let the components see the responses to the requestfor (SearchComponent c : components) {+ if (checkLimitsBefore(+ c,+ "handleResponses next stage:" + stageInEnglish(nextStage),+ rb.req,+ rb.rsp,+ components)) {+ shortCircuitedResults(req, rb);+ return;+ }c.handleResponses(rb, srsp.getShardRequest());}@@ -613,6 +680,10 @@ public class SearchHandler extends RequestHandlerBase}for (SearchComponent c : components) {+ if (checkLimitsBefore(+ c, "finishStage stage:" + stageInEnglish(nextStage), rb.req, rb.rsp, components)) {+ return;+ }c.finishStage(rb);}@@ -624,44 +695,97 @@ public class SearchHandler extends RequestHandlerBasersp.addToLog(ThreadCpuTimer.CPU_TIME, totalShardCpuTime);}}+ }- // SOLR-5550: still provide shards.info if requested even for a short circuited distrib request- if (!rb.isDistrib- && req.getParams().getBool(ShardParams.SHARDS_INFO, false)- && rb.shortCircuitedURL != null) {- NamedList<Object> shardInfo = new SimpleOrderedMap<>();- SimpleOrderedMap<Object> nl = new SimpleOrderedMap<>();- if (rsp.getException() != null) {- Throwable cause = rsp.getException();- if (cause instanceof SolrServerException) {- cause = ((SolrServerException) cause).getRootCause();- } else {- if (cause.getCause() != null) {- cause = cause.getCause();- }+ private static boolean prepareComponents(+ SolrQueryRequest req, ResponseBuilder rb, RTimerTree timer, List<SearchComponent> components)+ throws IOException {+ if (timer == null) {+ // non-debugging prepare phase+ for (SearchComponent component : components) {+ if (checkLimitsBefore(component, "prepare", rb.req, rb.rsp, components)) {+ shortCircuitedResults(req, rb);+ return false;}- nl.add("error", cause.toString());- if (!core.getCoreContainer().hideStackTrace()) {- StringWriter trace = new StringWriter();- cause.printStackTrace(new PrintWriter(trace));- nl.add("trace", trace.toString());+ component.prepare(rb);+ }+ } else {+ // debugging prepare phase+ RTimerTree subt = timer.sub("prepare");+ for (SearchComponent c : components) {+ if (checkLimitsBefore(c, "prepare debug", rb.req, rb.rsp, components)) {+ shortCircuitedResults(req, rb);+ return false;}- } else if (rb.getResults() != null) {- nl.add("numFound", rb.getResults().docList.matches());- nl.add(- "numFoundExact",- rb.getResults().docList.hitCountRelation() == TotalHits.Relation.EQUAL_TO);- nl.add("maxScore", rb.getResults().docList.maxScore());+ rb.setTimer(subt.sub(c.getName()));+ c.prepare(rb);+ rb.getTimer().stop();}- nl.add("shardAddress", rb.shortCircuitedURL);- nl.add("time", req.getRequestTimer().getTime()); // elapsed time of this request so far+ subt.stop();+ }+ return true;+ }- int pos = rb.shortCircuitedURL.indexOf("://");- String shardInfoName =- pos != -1 ? rb.shortCircuitedURL.substring(pos + 3) : rb.shortCircuitedURL;- shardInfo.add(shardInfoName, nl);- rsp.getValues().add(ShardParams.SHARDS_INFO, shardInfo);+ private static String stageInEnglish(int nextStage) {+ // This should probably be a enum, but that change should be its own ticket.+ switch (nextStage) {+ case STAGE_START:+ return "START";+ case STAGE_PARSE_QUERY:+ return "PARSE_QUERY";+ case STAGE_TOP_GROUPS:+ return "TOP_GROUPS";+ case STAGE_EXECUTE_QUERY:+ return "EXECUTE_QUERY";+ case STAGE_GET_FIELDS:+ return "GET_FIELDS";+ // nobody wants to think it was DONE and canceled after it completed...+ case STAGE_DONE:+ return "FINISHING";+ default:+ throw new SolrException(+ SolrException.ErrorCode.SERVER_ERROR, "Unrecognized stage:" + nextStage);+ }+ }++ private static void shortCircuitedResults(SolrQueryRequest req, ResponseBuilder rb) {++ if (rb.rsp.getResponse() == null) {+ rb.rsp.addResponse(new SolrDocumentList());++ // If a cursorMark was passed, and we didn't progress, set+ // the nextCursorMark to the same position+ String cursorStr = rb.req.getParams().get(CursorMarkParams.CURSOR_MARK_PARAM);+ if (null != cursorStr) {+ rb.rsp.add(CursorMarkParams.CURSOR_MARK_NEXT, cursorStr);+ }}+ if (rb.isDebug()) {+ NamedList<Object> debug = new NamedList<>();+ debug.add("explain", new NamedList<>());+ rb.rsp.add("debug", debug);+ }+ rb.rsp.setPartialResults(rb.req);+ }++ private static boolean checkLimitsBefore(+ SearchComponent c,+ String when,+ SolrQueryRequest req,+ SolrQueryResponse resp,+ List<SearchComponent> components) {++ return getQueryLimits(req, resp)+ .maybeExitWithPartialResults(+ () ->+ "["+ + when+ + "] Limit(s) exceeded prior to "+ + c.getName()+ + " in "+ + components.stream()+ .map(SearchComponent::getName)+ .collect(Collectors.toList()));}private long computeShardCpuTime(List<ShardResponse> responses) {diff --git a/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java b/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.javaindex 61b51a6af20..444867b430b 100644--- a/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java+++ b/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java@@ -253,10 +253,12 @@ public class SolrRequestInfo {*/public QueryLimits getLimits() {// make sure the ThreadCpuTime is always initialized- return req == null || rsp == null- ? QueryLimits.NONE- : (QueryLimits)- req.getContext().computeIfAbsent(LIMITS_KEY, (k) -> new QueryLimits(req, rsp));+ return req == null || rsp == null ? QueryLimits.NONE : getQueryLimits(req, rsp);+ }++ public static QueryLimits getQueryLimits(SolrQueryRequest request, SolrQueryResponse response) {+ return (QueryLimits)+ request.getContext().computeIfAbsent(LIMITS_KEY, (k) -> new QueryLimits(request, response));}public SolrDispatchFilter.Action getAction() {diff --git a/solr/core/src/java/org/apache/solr/response/SolrQueryResponse.java b/solr/core/src/java/org/apache/solr/response/SolrQueryResponse.javaindex b399ce57b79..5676a63fe10 100644--- a/solr/core/src/java/org/apache/solr/response/SolrQueryResponse.java+++ b/solr/core/src/java/org/apache/solr/response/SolrQueryResponse.java@@ -207,7 +207,10 @@ public class SolrQueryResponse {*/public void addPartialResponseDetail(Object detail) {NamedList<Object> header = getResponseHeader();- if (header != null && detail != null) {+ // never overwrite the original detail message. The first limit violation is the important one.+ if (header != null+ && detail != null+ && header.get(RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY) == null) {header.add(RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY, detail);}}diff --git a/solr/core/src/java/org/apache/solr/search/QueryLimits.java b/solr/core/src/java/org/apache/solr/search/QueryLimits.javaindex 7aee5657678..e6e0db5eed9 100644--- a/solr/core/src/java/org/apache/solr/search/QueryLimits.java+++ b/solr/core/src/java/org/apache/solr/search/QueryLimits.java@@ -16,12 +16,14 @@*/package org.apache.solr.search;+import static org.apache.solr.response.SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY;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 java.util.Optional;+import java.util.function.Supplier;import org.apache.lucene.index.QueryTimeout;import org.apache.solr.common.SolrException;import org.apache.solr.common.params.CommonParams;@@ -113,7 +115,8 @@ public class QueryLimits implements QueryTimeout {* @throws QueryLimitsExceededException if {@link #allowPartialResults} is false and limits have* been reached.*/- public boolean maybeExitWithPartialResults(String label) throws QueryLimitsExceededException {+ public boolean maybeExitWithPartialResults(Supplier<String> label)+ throws QueryLimitsExceededException {if (isLimitsEnabled() && shouldExit()) {if (allowPartialResults) {if (rsp != null) {@@ -124,17 +127,25 @@ public class QueryLimits implements QueryTimeout {"No request active, but attempting to exit with partial results?");}rsp.setPartialResults(requestInfo.getReq());- rsp.addPartialResponseDetail(formatExceptionMessage(label));+ if (rsp.getResponseHeader().get(RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY) == null) {+ // don't want to add duplicate keys. Although technically legal, there's a strong risk+ // that clients won't anticipate it and break.+ rsp.addPartialResponseDetail(formatExceptionMessage(label.get()));+ }}return true;} else {- throw new QueryLimitsExceededException(formatExceptionMessage(label));+ throw new QueryLimitsExceededException(formatExceptionMessage(label.get()));}} else {return false;}}+ public boolean maybeExitWithPartialResults(String label) throws QueryLimitsExceededException {+ return maybeExitWithPartialResults(() -> label);+ }+/*** Method to diagnose limit exceeded. Note that while this should always list the exceeded limit,* it may also nominate additional limits that have been exceeded since the actual check thatdiff --git a/solr/core/src/test/org/apache/solr/core/ExitableDirectoryReaderTest.java b/solr/core/src/test/org/apache/solr/core/ExitableDirectoryReaderTest.javaindex 1ceb9636b20..f2e4aaa3c8f 100644--- a/solr/core/src/test/org/apache/solr/core/ExitableDirectoryReaderTest.java+++ b/solr/core/src/test/org/apache/solr/core/ExitableDirectoryReaderTest.java@@ -32,7 +32,7 @@ import org.junit.Test;*/public class ExitableDirectoryReaderTest extends SolrTestCaseJ4 {- static int NUM_DOCS = 100;+ static final int NUM_DOCS = 100;static final String assertionString = "/response/numFound==" + NUM_DOCS;static final String failureAssertionString = "/responseHeader/partialResults==true]";static final String longTimeout = "10000";@@ -70,9 +70,26 @@ public class ExitableDirectoryReaderTest extends SolrTestCaseJ4 {// this time we should get a query cache hit and hopefully no exception? this may change in the// future if time checks are put into other places.- assertJQ(req("q", q, "timeAllowed", "1", "sleep", sleep), assertionString);++ // 2024-4-15: it did change..., and now this fails with 1 or 2 ms and passes with 3ms... I see+ // no way this won't be terribly brittle. Maybe TestInjection of some sort to bring this back?++ // assertJQ(req("q", q, "timeAllowed", "2", "sleep", sleep), assertionString);++ // The idea that the request won't time out due to caching is a flawed test methodology,+ // It relies on the test running quickly and not stalling. The above test should possibly+ // be doing something along the lines of this (but we lack api for it)+ //+ // SolrCores solrCores = ExitableDirectoryReaderTest.h.getCoreContainer().solrCores;+ // List<SolrCore> cores = solrCores.getCores();+ // for (SolrCore core : cores) {+ // if (<<< find the right core >>> ) {+ // ((SolrCache)core.getSearcher().get().<<<check cache for a key like name:a* >>>+ // }+ // }// now do the same for the filter cache+ // 2024-4-15: this still passes probably because *:* is so fast, but it still worries meassertJQ(req("q", "*:*", "fq", q, "timeAllowed", "1", "sleep", sleep), failureAssertionString);// make sure that the result succeeds this time, and that a bad filter wasn't cacheddiff --git a/solr/core/src/test/org/apache/solr/search/ComponentStageLimitsTest.java b/solr/core/src/test/org/apache/solr/search/ComponentStageLimitsTest.javanew file mode 100644index 00000000000..c3c5d72548e--- /dev/null+++ b/solr/core/src/test/org/apache/solr/search/ComponentStageLimitsTest.java@@ -0,0 +1,220 @@+/*+ * 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 static org.apache.solr.response.SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY;++import java.lang.invoke.MethodHandles;+import java.nio.file.Files;+import java.nio.file.Path;+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.SolrCloudTestCase;+import org.apache.solr.index.NoMergePolicyFactory;+import org.apache.solr.util.TestInjection;+import org.apache.solr.util.ThreadCpuTimer;+import org.junit.AfterClass;+import org.junit.Assume;+import org.junit.BeforeClass;+import org.junit.Test;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class ComponentStageLimitsTest extends SolrCloudTestCase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ private static final String COLLECTION = "test";++ 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");+ }++ @BeforeClass+ public static void setupClass() throws Exception {+ // Note: copied from TestCpuAllowedLimit.java, Segments not likely important+ // at the moment, but setup retained for now.++ // Using NoMergePolicy and 100 commits we should get 100 segments (across all shards).+ // At this point of writing MAX_SEGMENTS_PER_SLICE in lucene is 5, so we should be+ // ensured that any multithreaded testing will create 20 executable tasks for the+ // executor that was provided to index-searcher.+ systemSetPropertySolrTestsMergePolicyFactory(NoMergePolicyFactory.class.getName());+ System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true");+ Path configset = createConfigSet();+ configureCluster(1).addConfig("conf", configset).configure();+ SolrClient solrClient = cluster.getSolrClient();+ CollectionAdminRequest.Create create =+ CollectionAdminRequest.createCollection(COLLECTION, "conf", 3, 2);+ create.process(solrClient);+ waitForState("active", COLLECTION, clusterShape(3, 6));+ for (int j = 0; j < 100; j++) {+ solrClient.add(COLLECTION, sdoc("id", "id-" + j, "val_i", j % 5));+ solrClient.commit(COLLECTION); // need to commit every doc to create many segments.+ }+ }++ @AfterClass+ public static void tearDownClass() {+ TestInjection.cpuTimerDelayInjectedNS = null;+ systemClearPropertySolrTestsMergePolicyFactory();+ }++ @Test+ public void testLimitPrepare() throws Exception {+ Assume.assumeTrue("Thread CPU time monitoring is not available", ThreadCpuTimer.isSupported());+ SolrClient solrClient = cluster.getSolrClient();+ long sleepMs = 1000;+ log.info("--- 500ms limit, 1000ms prepare phase - partial results ---");+ QueryResponse rsp =+ solrClient.query(+ COLLECTION,+ params(+ "q",+ "id:*",+ "sort",+ "id asc",+ ExpensiveSearchComponent.SLEEP_MS_PARAM,+ String.valueOf(sleepMs),+ "stages",+ "prepare",+ "timeAllowed",+ "500"));+ System.err.println("rsp=" + rsp.jsonStr());+ assertEquals(rsp.getHeader().get("status"), 0);+ assertNotNull("should have partial results", rsp.getHeader().get("partialResults"));+ String details = (String) rsp.getHeader().get(RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY);+ assertNotNull(details);+ assertTrue(details.contains("[prepare]"));+ assertTrue(+ details.contains("exceeded prior to query in [expensiveSearchComponent, query, facet,"));+ }++ @Test+ public void testLimitProcess() throws Exception {+ Assume.assumeTrue("Thread CPU time monitoring is not available", ThreadCpuTimer.isSupported());+ SolrClient solrClient = cluster.getSolrClient();+ String msg = "--- 500ms limit, 1000ms process phase - partial results ---";+ log.info(msg);+ System.out.println(msg);+ int sleepMs = 2000;+ QueryResponse rsp =+ solrClient.query(+ COLLECTION,+ params(+ "q",+ "id:*",+ "sort",+ "id asc",+ ExpensiveSearchComponent.SLEEP_MS_PARAM,+ String.valueOf(sleepMs),+ "stages",+ "process",+ "timeAllowed",+ "1000"));+ System.err.println("rsp=" + rsp.jsonStr());+ assertEquals(rsp.getHeader().get("status"), 0);+ String details = (String) rsp.getHeader().get(RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY);+ System.out.println("details=" + details);+ assertNotNull(details);+ assertTrue(details.contains("[process]"));+ assertTrue(+ details.contains("exceeded prior to query in [expensiveSearchComponent, query, facet,"));+ assertNotNull("should have partial results", rsp.getHeader().get("partialResults"));+ }++ @Test+ public void testLimitFinish() throws Exception {+ Assume.assumeTrue("Thread CPU time monitoring is not available", ThreadCpuTimer.isSupported());+ SolrClient solrClient = cluster.getSolrClient();+ long sleepMs = 1000;+ log.info("--- 500ms limit, 1000ms finish phase - partial results ---");+ sleepMs = 1000;+ QueryResponse rsp =+ solrClient.query(+ COLLECTION,+ params(+ "q",+ "id:*",+ "sort",+ "id asc",+ ExpensiveSearchComponent.SLEEP_MS_PARAM,+ String.valueOf(sleepMs),+ "stages",+ "finish",+ "timeAllowed",+ "500"));+ System.err.println("rsp=" + rsp.jsonStr());+ assertEquals(rsp.getHeader().get("status"), 0);+ String details = (String) rsp.getHeader().get(RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY);+ assertNotNull(details);+ assertTrue(details.contains("[finishStage stage:PARSE_QUERY]"));+ assertTrue(+ details.contains("exceeded prior to query in [expensiveSearchComponent, query, facet,"));+ assertNotNull("should have partial results", rsp.getHeader().get("partialResults"));+ }++ @Test+ public void testLimitDistrib() throws Exception {+ Assume.assumeTrue("Thread CPU time monitoring is not available", ThreadCpuTimer.isSupported());+ SolrClient solrClient = cluster.getSolrClient();+ long sleepMs = 1000;+ log.info("--- 500ms limit, 1000ms distrib phase - partial results ---");+ sleepMs = 1000;+ QueryResponse rsp =+ solrClient.query(+ COLLECTION,+ params(+ "q",+ "id:*",+ "sort",+ "id asc",+ ExpensiveSearchComponent.SLEEP_MS_PARAM,+ String.valueOf(sleepMs),+ "stages",+ "distrib",+ "timeAllowed",+ "500"));+ System.err.println("rsp=" + rsp.jsonStr());+ assertEquals(rsp.getHeader().get("status"), 0);+ String details = (String) rsp.getHeader().get(RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY);+ assertNotNull(details);+ assertTrue(details.contains("[distrib]"));+ assertTrue(+ details.contains("exceeded prior to query in [expensiveSearchComponent, query, facet,"));+ assertNotNull("should have partial results", rsp.getHeader().get("partialResults"));+ }+}diff --git a/solr/core/src/test/org/apache/solr/search/TestCpuAllowedLimit.java b/solr/core/src/test/org/apache/solr/search/TestCpuAllowedLimit.javaindex 274994099f1..4611de10eca 100644--- a/solr/core/src/test/org/apache/solr/search/TestCpuAllowedLimit.java+++ b/solr/core/src/test/org/apache/solr/search/TestCpuAllowedLimit.java@@ -169,6 +169,7 @@ public class TestCpuAllowedLimit extends SolrCloudTestCase {assertEquals("should have partial results", true, rsp.getHeader().get("partialResults"));log.info("--- timeAllowed, partial results, multithreading ---");+rsp =solrClient.query(COLLECTION,@@ -201,11 +202,11 @@ public class TestCpuAllowedLimit extends SolrCloudTestCase {"stages","prepare,process","cpuAllowed",- "10000"));- // System.err.println("rsp=" + rsp.jsonStr());+ "100000"));+ System.err.println("rsp=" + rsp.jsonStr());assertNull("should have full results", rsp.getHeader().get("partialResults"));// cpuAllowed set, should return partial results- log.info("--- cpuAllowed 1, partial results ---");+ log.info("--- cpuAllowed 100, partial results ---");rsp =solrClient.query(COLLECTION,@@ -222,7 +223,7 @@ public class TestCpuAllowedLimit extends SolrCloudTestCase {"false"));// System.err.println("rsp=" + rsp.jsonStr());assertNotNull("should have partial results", rsp.getHeader().get("partialResults"));- log.info("--- cpuAllowed 1, partial results omitted ---");+ log.info("--- cpuAllowed 100, partial results omitted ---");rsp =solrClient.query(COLLECTION,@@ -243,10 +244,11 @@ public class TestCpuAllowedLimit extends SolrCloudTestCase {"foo"));String s = rsp.jsonStr();System.err.println("rsp=" + s);- assertEquals("should have partial results", "omitted", rsp.getHeader().get("partialResults"));+ assertEquals(+ "should not have partial results", "omitted", rsp.getHeader().get("partialResults"));// cpuAllowed set, should return partial results- log.info("--- cpuAllowed 2, partial results, multi-threaded ---");+ log.info("--- cpuAllowed 100, partial results, multi-threaded ---");rsp =solrClient.query(COLLECTION,diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.javaindex ea5ecf396f7..e31f3c08393 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.java@@ -306,9 +306,12 @@ public abstract class LBSolrClient extends SolrClient {suffix = ":" + zombieServers.keySet();}// Skipping check time exceeded for the first request- if (numServersTried > 0 && isTimeExceeded(timeAllowedNano, timeOutTime)) {+ // Ugly string based hack but no live servers message here is VERY misleading :(+ if ((previousEx != null && previousEx.getMessage().contains("Limits exceeded!"))+ || (numServersTried > 0 && isTimeExceeded(timeAllowedNano, timeOutTime))) {throw new SolrServerException(- "Time allowed to handle this request exceeded" + suffix, previousEx);+ "The processing limits for to this request were exceeded, see cause for details",+ previousEx);}if (endpoint == null) {throw new SolrServerException(