Skip to content

Commit 7f224be

Browse files
SOLR-11994: Read search fields from req and rsp, expose one listener manager
Read QTime, hits, CPU time and partial results from SolrQueryResponse and the path from SolrQueryRequest instead of passing qtime through. Rename SearchRequestDispatcher to SearchRequestListenerManager, replace the three CoreContainer listener methods with one getter, and keep the manager's public surface to add, remove and lost. Skip capture once the manager is closed, and test that close waits for a listener call in progress.
1 parent d75f8c0 commit 7f224be

9 files changed

Lines changed: 185 additions & 121 deletions

File tree

‎solr/core/src/java/org/apache/solr/core/CoreContainer.java‎

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@
121121
import org.apache.solr.handler.admin.ZookeeperInfoHandler;
122122
import org.apache.solr.handler.admin.ZookeeperRead;
123123
import org.apache.solr.handler.admin.ZookeeperStatusHandler;
124-
import org.apache.solr.handler.component.SearchRequestDispatcher;
125-
import org.apache.solr.handler.component.SearchRequestListener;
124+
import org.apache.solr.handler.component.SearchRequestListenerManager;
126125
import org.apache.solr.handler.component.ShardHandlerFactory;
127126
import org.apache.solr.handler.designer.SchemaDesigner;
128127
import org.apache.solr.jersey.InjectionFactories;
@@ -214,25 +213,12 @@ public JerseyAppHandlerCache getJerseyAppHandlerCache() {
214213
return appHandlersByConfigSetId;
215214
}
216215

217-
final SearchRequestDispatcher searchRequestDispatcher = new SearchRequestDispatcher();
216+
private final SearchRequestListenerManager searchRequestListenerManager =
217+
new SearchRequestListenerManager();
218218

219-
/**
220-
* Register to be notified after each search a client sends to this node.
221-
*
222-
* @throws IllegalStateException if this container is shut down
223-
*/
224-
public void addSearchRequestListener(SearchRequestListener listener) {
225-
searchRequestDispatcher.add(listener);
226-
}
227-
228-
/** Remove a listener added with {@link #addSearchRequestListener}. */
229-
public boolean removeSearchRequestListener(SearchRequestListener listener) {
230-
return searchRequestDispatcher.remove(listener);
231-
}
232-
233-
/** The registered listeners. Empty unless a module adds one. */
234-
public List<SearchRequestListener> getSearchRequestListeners() {
235-
return searchRequestDispatcher.listeners();
219+
/** Where modules register to be notified after each search a client sends to this node. */
220+
public SearchRequestListenerManager getSearchRequestListenerManager() {
221+
return searchRequestListenerManager;
236222
}
237223

238224
/** Minimize exposure to CoreContainer. Mostly only ZK interface is required */
@@ -786,7 +772,7 @@ private void loadInternal() {
786772
solrMetricsContext.observableLongCounter(
787773
"solr.search.listener.lost",
788774
"Searches not delivered to search request listeners",
789-
measurement -> measurement.record(searchRequestDispatcher.lost()));
775+
measurement -> measurement.record(searchRequestListenerManager.lost()));
790776

791777
initGpuMetricsService(); // Initialize GPU metrics service
792778

@@ -1254,7 +1240,7 @@ public void shutdown() {
12541240
log.info("Shutting down CoreContainer instance={}", System.identityHashCode(this));
12551241
}
12561242

1257-
searchRequestDispatcher.close();
1243+
searchRequestListenerManager.close();
12581244
ExecutorUtil.shutdownAndAwaitTermination(coreContainerAsyncTaskExecutor);
12591245
ExecutorUtil.shutdownAndAwaitTermination(indexSearcherExecutor);
12601246
ExecutorUtil.shutdownNowAndAwaitTermination(indexFingerprintExecutor);

‎solr/core/src/java/org/apache/solr/core/SolrCore.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3009,7 +3009,7 @@ public static void postDecorateResponse(
30093009
}
30103010
CoreContainer cc = req.getCoreContainer();
30113011
if (cc != null) {
3012-
cc.searchRequestDispatcher.afterResponse(req, rsp, qtime);
3012+
cc.getSearchRequestListenerManager().afterResponse(req, rsp);
30133013
}
30143014
}
30153015

‎solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,20 @@ public HandlerMetrics getMetricsForThisRequest(SolrQueryRequest req) {
153153
public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
154154
super.handleRequest(req, rsp);
155155
CoreContainer cc = req.getCoreContainer();
156-
List<SearchRequestListener> listeners = cc == null ? List.of() : cc.getSearchRequestListeners();
157-
if (listeners.isEmpty()
156+
if (cc == null
157+
|| !cc.getSearchRequestListenerManager().hasListeners()
158158
|| !notifiesSearchRequestListeners()
159159
|| SolrRequestInfo.isNestedRequest()
160160
|| req.getParams().getBool(ShardParams.IS_SHARD, false)) {
161161
return;
162162
}
163-
String path = (String) req.getContext().get(PATH);
163+
String path = req.getPath();
164164
SolrRequestInfo requestInfo = SolrRequestInfo.getRequestInfo();
165165
ResponseBuilder rb = requestInfo == null ? null : requestInfo.getResponseBuilder();
166166
if (path == null || !isDispatchedHandler(req) || rb == null || rb.req != req) {
167167
return;
168168
}
169-
SearchRequestDispatcher.mark(req, rb);
169+
SearchRequestListenerManager.mark(req, rb);
170170
}
171171

172172
/** Whether requests to this handler are reported to {@link SearchRequestListener}s. */

‎solr/core/src/java/org/apache/solr/handler/component/SearchRequestInfo.java‎

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@
1717
package org.apache.solr.handler.component;
1818

1919
import java.security.Principal;
20-
import java.util.List;
2120
import org.apache.solr.logging.MDCLoggingContext;
2221
import org.apache.solr.request.SolrQueryRequest;
2322
import org.apache.solr.response.SolrQueryResponse;
24-
import org.apache.solr.util.ThreadCpuTimer;
2523

2624
/**
2725
* A completed search request, as passed to {@link SearchRequestListener}. It holds only values
2826
* copied from the request, so it can be kept and read on any thread.
2927
*
30-
* @param handlerPath the path the request log uses
28+
* @param handlerPath the request path
3129
* @param collection the collection of {@code core}, even when an alias or several collections were
3230
* searched; null in standalone mode
3331
* @param core the name of the core that coordinated the search
@@ -63,41 +61,29 @@ public record SearchRequestInfo(
6361

6462
public static final int MAX_FAILURE_MESSAGE_CHARS = 256;
6563

66-
static SearchRequestInfo from(
67-
SolrQueryRequest req, SolrQueryResponse rsp, ResponseBuilder rb, long qTimeMs) {
64+
static SearchRequestInfo from(SolrQueryRequest req, SolrQueryResponse rsp, ResponseBuilder rb) {
6865
boolean distributed = rb.isDistributed() && rb.shards != null;
6966
Principal user = req.getUserPrincipal();
7067
Exception failure = rsp.getException();
7168
return new SearchRequestInfo(
72-
rsp.getToLog().get("path") instanceof String path ? path : null,
69+
req.getPath(),
7370
req.getCore().getCoreDescriptor().getCollectionName(),
7471
req.getCore().getName(),
7572
user == null ? null : user.getName(),
7673
MDCLoggingContext.getTraceId(),
7774
req.getStartTime(),
78-
qTimeMs,
79-
cpuTimeMs(rsp),
80-
rsp.getToLog().get("hits") instanceof Number hits ? hits.longValue() : null,
75+
rsp.getQTime(),
76+
rsp.getCpuTimeMs(),
77+
rsp.getHits(),
8178
distributed ? rb.shards.length : 0,
8279
failure == null ? null : failure.getClass().getName(),
8380
failure == null ? null : truncate(failure.getMessage()),
84-
SolrQueryResponse.isPartialResults(rsp.getResponseHeader()));
81+
rsp.isPartialResults());
8582
}
8683

8784
private static String truncate(String message) {
8885
return message == null || message.length() <= MAX_FAILURE_MESSAGE_CHARS
8986
? message
9087
: message.substring(0, MAX_FAILURE_MESSAGE_CHARS);
9188
}
92-
93-
private static Long cpuTimeMs(SolrQueryResponse rsp) {
94-
if (!(rsp.getToLog().get(ThreadCpuTimer.LOCAL_CPU_TIME) instanceof Long local)) {
95-
return null;
96-
}
97-
List<Object> shardCpuTimes = rsp.getToLog().getAll(ThreadCpuTimer.CPU_TIME);
98-
if (!shardCpuTimes.isEmpty() && shardCpuTimes.getLast() instanceof Long shardCpuTime) {
99-
return shardCpuTime + local;
100-
}
101-
return local;
102-
}
10389
}

‎solr/core/src/java/org/apache/solr/handler/component/SearchRequestListener.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
* reported either.
2929
*
3030
* <p>{@link #capture} runs on the request thread; {@link #onRequestEnd} runs later on a single
31-
* delivery thread per {@link org.apache.solr.core.CoreContainer}, shared by all listeners through
32-
* one bounded queue. When the queue is full the search is dropped for every listener rather than
33-
* delaying the request, and counted in the {@code solr.search.listener.lost} metric. Size the queue
34-
* with {@code solr.search.listener.queue.size} (default 1000). One slow listener causes drops for
35-
* all of them. Queued searches are discarded at shutdown and counted the same way.
31+
* delivery thread per node, shared by all listeners through one bounded queue. When the queue is
32+
* full the search is dropped for every listener rather than delaying the request, and counted in
33+
* the {@code solr.search.listener.lost} metric. Size the queue with {@code
34+
* solr.search.listener.queue.size} (default 1000). One slow listener causes drops for all of them.
35+
* Queued searches are discarded at shutdown and counted the same way.
3636
*
3737
* <p>The delivery thread has no request MDC, so log lines from {@link #onRequestEnd} carry no trace
3838
* id; include {@link SearchRequestInfo#traceId()} where needed.
3939
*
40-
* @see org.apache.solr.core.CoreContainer#addSearchRequestListener(SearchRequestListener)
40+
* @see SearchRequestListenerManager#add(SearchRequestListener)
4141
* @lucene.experimental
4242
*/
4343
public interface SearchRequestListener {

solr/core/src/java/org/apache/solr/handler/component/SearchRequestDispatcher.java renamed to solr/core/src/java/org/apache/solr/handler/component/SearchRequestListenerManager.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.Closeable;
2020
import java.lang.invoke.MethodHandles;
2121
import java.util.ArrayList;
22-
import java.util.Collections;
2322
import java.util.List;
2423
import java.util.concurrent.ArrayBlockingQueue;
2524
import java.util.concurrent.BlockingQueue;
@@ -34,10 +33,10 @@
3433
import org.slf4j.LoggerFactory;
3534

3635
/** Holds the {@link SearchRequestListener}s of a node and delivers each reported search to them. */
37-
public class SearchRequestDispatcher implements Closeable {
36+
public class SearchRequestListenerManager implements Closeable {
3837
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
3938

40-
private static final String MARKER = SearchRequestDispatcher.class.getName();
39+
private static final String MARKER = SearchRequestListenerManager.class.getName();
4140

4241
public static final String QUEUE_SIZE_PROP = "solr.search.listener.queue.size";
4342

@@ -69,11 +68,11 @@ private record Event(SearchRequestInfo info, List<Delivery> deliveries) {}
6968

7069
private Thread worker;
7170

72-
public SearchRequestDispatcher() {
71+
public SearchRequestListenerManager() {
7372
this(EnvUtils.getPropertyAsInteger(QUEUE_SIZE_PROP, DEFAULT_QUEUE_SIZE));
7473
}
7574

76-
SearchRequestDispatcher(int queueSize) {
75+
SearchRequestListenerManager(int queueSize) {
7776
queue = new ArrayBlockingQueue<>(Math.max(1, queueSize));
7877
lastWarnNanos =
7978
new AtomicLong(System.nanoTime() - TimeUnit.MILLISECONDS.toNanos(LOST_WARN_INTERVAL_MS));
@@ -82,11 +81,11 @@ public SearchRequestDispatcher() {
8281
/**
8382
* Adds a listener, starting the delivery thread on first use.
8483
*
85-
* @throws IllegalStateException if this dispatcher is closed
84+
* @throws IllegalStateException if this manager is closed
8685
*/
8786
public synchronized void add(SearchRequestListener listener) {
8887
if (closed) {
89-
throw new IllegalStateException("SearchRequestDispatcher is closed");
88+
throw new IllegalStateException("SearchRequestListenerManager is closed");
9089
}
9190
listeners.addIfAbsent(listener);
9291
if (worker == null) {
@@ -100,8 +99,8 @@ public boolean remove(SearchRequestListener listener) {
10099
return listeners.remove(listener);
101100
}
102101

103-
public List<SearchRequestListener> listeners() {
104-
return Collections.unmodifiableList(listeners);
102+
boolean hasListeners() {
103+
return !listeners.isEmpty();
105104
}
106105

107106
static void mark(SolrQueryRequest req, ResponseBuilder rb) {
@@ -112,11 +111,15 @@ static void mark(SolrQueryRequest req, ResponseBuilder rb) {
112111
* Reports {@code req} to the listeners if a {@link SearchHandler} marked it. Called once the
113112
* response header has its status and QTime.
114113
*/
115-
public void afterResponse(SolrQueryRequest req, SolrQueryResponse rsp, long qTimeMs) {
114+
public void afterResponse(SolrQueryRequest req, SolrQueryResponse rsp) {
116115
if (!(req.getContext().remove(MARKER) instanceof Marker marker)) {
117116
return;
118117
}
119-
SearchRequestInfo info = SearchRequestInfo.from(req, rsp, marker.rb(), qTimeMs);
118+
if (closed) {
119+
lost.increment();
120+
return;
121+
}
122+
SearchRequestInfo info = SearchRequestInfo.from(req, rsp, marker.rb());
120123
List<Delivery> deliveries = null;
121124
for (SearchRequestListener listener : listeners) {
122125
Object captured;

‎solr/core/src/java/org/apache/solr/response/SolrQueryResponse.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Collection;
2323
import java.util.Date;
2424
import java.util.Iterator;
25+
import java.util.List;
2526
import java.util.Map;
2627
import java.util.Map.Entry;
2728
import org.apache.solr.common.SolrException;
@@ -30,6 +31,7 @@
3031
import org.apache.solr.request.SolrQueryRequest;
3132
import org.apache.solr.search.ReturnFields;
3233
import org.apache.solr.search.SolrReturnFields;
34+
import org.apache.solr.util.ThreadCpuTimer;
3335

3436
/**
3537
* <code>SolrQueryResponse</code> is used by a query handler to return the response to a query
@@ -188,6 +190,37 @@ public static boolean isPartialResults(NamedList<?> header) {
188190
}
189191
}
190192

193+
/** Whether the response header marks the results as partial. */
194+
public boolean isPartialResults() {
195+
return isPartialResults(getResponseHeader());
196+
}
197+
198+
/** The QTime in the response header, or -1 before it is set. */
199+
public long getQTime() {
200+
NamedList<Object> header = getResponseHeader();
201+
return header != null && header.get("QTime") instanceof Number qtime ? qtime.longValue() : -1;
202+
}
203+
204+
/** The hit count added to the request log, or null if none was. */
205+
public Long getHits() {
206+
return toLog.get("hits") instanceof Number hits ? hits.longValue() : null;
207+
}
208+
209+
/**
210+
* CPU time from the request log: this thread's plus, when distributed, the last shard total
211+
* reported. Null unless {@code solr.log.cputime=true}.
212+
*/
213+
public Long getCpuTimeMs() {
214+
if (!(toLog.get(ThreadCpuTimer.LOCAL_CPU_TIME) instanceof Long local)) {
215+
return null;
216+
}
217+
List<Object> shardCpuTimes = toLog.getAll(ThreadCpuTimer.CPU_TIME);
218+
if (!shardCpuTimes.isEmpty() && shardCpuTimes.getLast() instanceof Long shardCpuTime) {
219+
return shardCpuTime + local;
220+
}
221+
return local;
222+
}
223+
191224
/**
192225
* Test if the entire results have been returned, or if some form of limit/cancel/tolerant logic
193226
* has returned incomplete (or possibly empty) results.

0 commit comments

Comments
 (0)