Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/SOLR-11994-search-request-listener.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Add a SearchRequestListener hook so modules can observe each completed top-level search on the node that coordinated it.
type: added
authors:
- name: Amrit Sarkar
links:
- name: SOLR-11994
url: https://issues.apache.org/jira/browse/SOLR-11994
19 changes: 19 additions & 0 deletions solr/core/src/java/org/apache/solr/core/CoreContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -121,6 +122,7 @@
import org.apache.solr.handler.admin.ZookeeperInfoHandler;
import org.apache.solr.handler.admin.ZookeeperRead;
import org.apache.solr.handler.admin.ZookeeperStatusHandler;
import org.apache.solr.handler.component.SearchRequestListener;
import org.apache.solr.handler.component.ShardHandlerFactory;
import org.apache.solr.handler.designer.SchemaDesigner;
import org.apache.solr.jersey.InjectionFactories;
Expand Down Expand Up @@ -212,6 +214,23 @@ public JerseyAppHandlerCache getJerseyAppHandlerCache() {
return appHandlersByConfigSetId;
}

private final List<SearchRequestListener> searchRequestListeners = new CopyOnWriteArrayList<>();

/** Register to be notified after each top-level search on this node. */
public void addSearchRequestListener(SearchRequestListener listener) {
searchRequestListeners.add(listener);
}

/** Remove a listener */
public boolean removeSearchRequestListener(SearchRequestListener listener) {
return searchRequestListeners.remove(listener);
}

/** The registered listeners. Empty unless a module adds one. */
public List<SearchRequestListener> getSearchRequestListeners() {
return Collections.unmodifiableList(searchRequestListeners);
}

/** Minimize exposure to CoreContainer. Mostly only ZK interface is required */
public final Supplier<SolrZkClient> zkClientSupplier = () -> getZkController().getZkClient();

Expand Down
5 changes: 5 additions & 0 deletions solr/core/src/java/org/apache/solr/handler/ExportHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public class ExportHandler extends SearchHandler {
private StreamContext initialStreamContext;
private String writerMetricsPath;

@Override
protected boolean notifiesSearchRequestListeners() {
return false;
}

public static class ExportHandlerStreamFactory extends SolrDefaultStreamFactory {
static final String[] forbiddenStreams =
new String[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throw
super.handleRequestBody(req, rsp);
}

@Override
protected boolean notifiesSearchRequestListeners() {
return false;
}

//////////////////////// SolrInfoMBeans methods //////////////////////

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ public HandlerMetrics getMetricsForThisRequest(SolrQueryRequest req) {
return req.getParams().getBool(ShardParams.IS_SHARD, false) ? this.metricsShard : this.metrics;
}

@Override
public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
super.handleRequest(req, rsp);
CoreContainer cc = req.getCoreContainer();
List<SearchRequestListener> listeners = cc == null ? List.of() : cc.getSearchRequestListeners();
if (listeners.isEmpty()
|| !notifiesSearchRequestListeners()
|| req.getParams().getBool(ShardParams.IS_SHARD, false)) {
return;
}
SearchRequestInfo info = SearchRequestInfo.from(req, rsp);
for (SearchRequestListener listener : listeners) {
try {
listener.onRequestEnd(info);
} catch (Exception e) {
log.warn("SearchRequestListener {} failed", listener.getClass().getName(), e);
}
}
}

/** Whether requests to this handler are reported to {@link SearchRequestListener}s. */
protected boolean notifiesSearchRequestListeners() {
return true;
}

@Override
public PermissionNameProvider.Name getPermissionName(AuthorizationContext ctx) {
return PermissionNameProvider.Name.READ_PERM;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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
* @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 shard CPU when distributed
* @param hits null if not available
* @param shardCount 0 for a non-distributed request
* @param failure the exception set on the response, if any
* @lucene.experimental
*/
public record SearchRequestInfo(
SolrQueryRequest req,
SolrQueryResponse rsp,
ResponseBuilder rb,
String handlerPath,
long elapsedMs,
Long cpuTimeMs,
Long hits,
int shardCount,
Throwable failure) {

static SearchRequestInfo from(SolrQueryRequest req, SolrQueryResponse rsp) {
SolrRequestInfo requestInfo = SolrRequestInfo.getRequestInfo();
ResponseBuilder rb = requestInfo == null ? null : requestInfo.getResponseBuilder();
if (rb != null && rb.req != req) {
rb = null;
}
boolean distributed = rb != null && rb.isDistributed() && rb.shards != null;
return new SearchRequestInfo(
req,
rsp,
rb,
rsp.getToLog().get("path") instanceof String path ? path : null,
(long) req.getRequestTimer().getTime(),
cpuTimeMs(rsp, distributed),
rsp.getToLog().get("hits") instanceof Number hits ? hits.longValue() : null,
distributed ? rb.shards.length : 0,
rsp.getException());
}

private static Long cpuTimeMs(SolrQueryResponse rsp, boolean distributed) {
if (rsp.getResponseHeader() == null) {
return null;
}
List<Object> cpuTimes = rsp.getResponseHeader().getAll(ThreadCpuTimer.CPU_TIME);
if (cpuTimes.isEmpty() || !(cpuTimes.getLast() instanceof Long cpuTime)) {
return null;
}
if (distributed && rsp.getToLog().get(ThreadCpuTimer.LOCAL_CPU_TIME) instanceof Long local) {
return cpuTime + local;
}
return cpuTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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;

/**
* Notified after a top-level search request completes on the node that coordinated it. Shard
* sub-requests, real-time get and export are not reported. Implementations must be fast and must
* not throw.
*
* @see org.apache.solr.core.CoreContainer#getSearchRequestListeners()
* @lucene.experimental
*/
public interface SearchRequestListener {
/**
* Called synchronously on the request thread after the response is built, before it is written.
*/
void onRequestEnd(SearchRequestInfo info);
}
Loading
Loading