sarkaramrit1993 commented on code in PR #5238:
URL: https://github.com/apache/solr/pull/5238#discussion_r4409179850


##########
solr/core/src/test-files/solr/configsets/search-request-listener/conf/solrconfig.xml:
##########


Review Comment:
   Good call, dropped both in 
[aa03c95](https://github.com/apache/solr/pull/5238/commits/aa03c9544968f1d9de9b6b36e2823bcbe76268b9).
 The dispatch collection now uses `cloud-minimal`, and the test reshapes it 
with one Config API call: `/select` re-registered for v2 only (so 
`EmbeddedSolrServer` finds no v1 `/select` and falls back to the default 
handler), plus `/v2-search` as a v2-only handler. Both tests still fail on the 
old guard with `expected:<1> but was:<0>`. I also renamed the embedded test to 
`testEmbeddedDefaultHandlerReported`, since SolrJ strips `qt` and that's the 
path it actually takes.
   



##########
solr/core/src/test-files/solr/configsets/search-request-listener/conf/schema.xml:
##########


Review Comment:
   Removed in 
[aa03c95](https://github.com/apache/solr/pull/5238/commits/aa03c9544968f1d9de9b6b36e2823bcbe76268b9)
 along with the custom solrconfig.xml, details on that thread.
   



##########
solr/core/src/test/org/apache/solr/handler/component/SearchRequestListenerTest.java:
##########
@@ -0,0 +1,402 @@
+/*
+ * 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.handler.component;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Queue;
+import java.util.Set;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrRequest;
+import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.request.SolrPing;
+import org.apache.solr.client.solrj.request.SolrQuery;
+import org.apache.solr.client.solrj.request.UpdateRequest;
+import org.apache.solr.client.solrj.request.V2Request;
+import org.apache.solr.client.solrj.response.InputStreamResponseParser;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.SimpleOrderedMap;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.embedded.JettySolrRunner;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.request.SolrQueryRequestBase;
+import org.apache.solr.request.SolrRequestInfo;
+import org.apache.solr.response.SolrQueryResponse;
+import org.apache.solr.search.CallerSpecificQueryLimit;
+import org.apache.solr.util.TestInjection;
+import org.apache.solr.util.ThreadCpuTimer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class SearchRequestListenerTest extends SolrCloudTestCase {
+  private static final String COLLECTION = "listener";
+  private static final String DISPATCH_COLLECTION = "listener_dispatch";
+  private static final int NUM_SHARDS = 2;
+  private static final Queue<Event> events = new ConcurrentLinkedQueue<>();
+
+  private static final List<SearchRequestListener> listeners = new 
ArrayList<>();
+
+  private record Event(String node, SearchRequestInfo info) {}
+
+  @BeforeClass
+  public static void setupCluster() throws Exception {
+    configureCluster(2)
+        .addConfig("conf", configset("cloud-minimal"))
+        .addConfig("dispatch", configset("search-request-listener"))
+        .configure();
+    createCollection(COLLECTION);
+    createCollection(DISPATCH_COLLECTION, "dispatch");
+  }
+
+  @Before
+  public void registerListeners() {
+    events.clear();
+    addListeners(false);
+  }
+
+  @After
+  public void removeListeners() {
+    for (JettySolrRunner jetty : cluster.getJettySolrRunners()) {
+      listeners.forEach(jetty.getCoreContainer()::removeSearchRequestListener);
+    }
+    listeners.clear();
+  }
+
+  private static void createCollection(String name) throws Exception {
+    createCollection(name, "conf");
+  }
+
+  private static void createCollection(String name, String config) throws 
Exception {
+    CollectionAdminRequest.createCollection(name, config, NUM_SHARDS, 1)
+        .processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);
+    cluster.waitForActiveCollection(name, NUM_SHARDS, NUM_SHARDS);
+    new UpdateRequest()
+        .add("id", "1")
+        .add("id", "2")
+        .add("id", "3")
+        .commit(cluster.getSolrClient(), name);
+  }
+
+  private static void addListeners(boolean withThrowingListener) {
+    for (JettySolrRunner jetty : cluster.getJettySolrRunners()) {
+      String node = jetty.getNodeName();
+      if (withThrowingListener) {
+        addListener(
+            jetty,
+            info -> {
+              throw new RuntimeException("listener failure");
+            });
+      }
+      addListener(jetty, info -> events.add(new Event(node, info)));
+    }
+  }
+
+  private static void addListener(JettySolrRunner jetty, SearchRequestListener 
listener) {
+    listeners.add(listener);
+    jetty.getCoreContainer().addSearchRequestListener(listener);
+  }
+
+  private static SolrClient nodeClient() {

Review Comment:
   `query()` now uses `SolrClient#query` 
([aa03c95](https://github.com/apache/solr/pull/5238/commits/aa03c9544968f1d9de9b6b36e2823bcbe76268b9)).
 I kept `nodeClient()` on purpose: it pins requests to node 0 so the tests can 
assert which node coordinated. `cluster.getSolrClient()` would land on a random 
node.
   



##########
changelog/unreleased/SOLR-11994-search-request-listener.yml:
##########
@@ -0,0 +1,8 @@
+title: Add a SearchRequestListener hook so modules can observe each completed 
search a client sends, on the node that coordinated it.

Review Comment:
   Partly, I think. The recording side of `UBIComponent` in 
[#2452](https://github.com/apache/solr/pull/2452) (the query plus the doc ids 
it returned, written through the logging stream) could sit on this listener, 
since it gets `rb` and `rsp` once per client search on the coordinator, after 
every stage, so the merged results are already there. The `ubi=true` switch, 
the per-handler setup and the `ubi` section with `query_id` belong in a 
`SearchComponent`, because the listener is node-wide and isn't configured per 
handler. Happy to try moving the recording side onto it once this lands, so 
neither PR waits on the other.
   



##########
solr/core/src/java/org/apache/solr/handler/component/SearchRequestInfo.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.handler.component;
+
+import java.util.List;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.request.SolrRequestInfo;
+import org.apache.solr.response.SolrQueryResponse;
+import org.apache.solr.util.ThreadCpuTimer;
+
+/**
+ * A completed search request, as passed to {@link SearchRequestListener}. 
Listeners must not retain
+ * or modify {@code req}, {@code rsp} or {@code rb}; copy what they need.
+ *
+ * @param req the request
+ * @param rsp the response
+ * @param rb null if the handler failed before creating it or it belongs to 
another request
+ * @param handlerPath the path the request log uses
+ * @param elapsedMs time since the request started
+ * @param cpuTimeMs null unless {@code solr.log.cputime=true}; includes the 
CPU shards reported when
+ *     distributed
+ * @param hits null if not available
+ * @param shardCount number of shards targeted; 0 for a non-distributed request
+ * @param failure the exception set on the response, if any
+ * @param partialResults whether the response header marks the results as 
partial, for example after
+ *     a timeout
+ * @lucene.experimental
+ */
+public record SearchRequestInfo(
+    SolrQueryRequest req,
+    SolrQueryResponse rsp,
+    ResponseBuilder rb,
+    String handlerPath,
+    long elapsedMs,
+    Long cpuTimeMs,
+    Long hits,
+    int shardCount,
+    Throwable failure,
+    boolean partialResults) {
+
+  static SearchRequestInfo from(SolrQueryRequest req, SolrQueryResponse rsp) {

Review Comment:
   I kept this one search-only on purpose. Ingest already has a per-request 
hook: the update processor chain. `LogUpdateProcessorFactory` uses it to log 
updates over `slowUpdateThresholdMillis`, the same way `SolrCore` does for 
`slowQueryThresholdMillis`, and anyone can add their own processor to a chain 
today. Search has no equivalent point that sees the whole request once it's 
done, which is the gap this fills. The fields here (hits, shards, partial 
results) are search-specific too. If we want a node-wide hook for updates 
later, it'd be a sibling with its own fields, and I can file a JIRA for 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to