Repository navigation
SOLR-18537: Proxy requests to a node via X-Solr-targetNode #5112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dsmiley
merged 13 commits into
apache:main
from
dsmiley:SOLR-18537-proxyRequestsByHeader
Nov 2, 2026
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ab0ebb8
SOLR-18537: Proxy requests to a node via X-Solr-targetNode
dsmiley 4616e92
test + ref guide.
dsmiley d1882d9
NodeTargetProxyFilter: review tweaks
dsmiley 176ffcb
NodeTargetProxyFilter: preserve full node names with dotless hosts
dsmiley 7c66491
rename var to match super
dsmiley 1d7fc1a
NodeTargetProxyFilter: consume the targeting header/param to avoid ro…
dsmiley b5cb63b
NodeTargetProxyFilter: proxy using the still-encoded request path
dsmiley b4dba65
NodeTargetProxyFilterTest: use SolrJ so it works with SSL
dsmiley de525ca
NodeTargetProxyFilter: report errors directly so they survive Authent…
dsmiley d033e8b
NodeTargetProxyFilterAuthTest: proxied requests are authorized as the…
dsmiley b4fa46f
HttpSolrProxy: take a URI; build it with an encoded path and query
dsmiley 3d762be
SOLR-18537: preserve header targeting with unrelated query parameters
nick-boss-tech 46bc7e4
SOLR-18537: wrap NodeTargetProxyFilter Javadoc for tidy
nick-boss-tech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| title: > | ||
| Any request can now be routed to a specific SolrCloud node by setting the `X-Solr-targetNode` header | ||
| (or, for GET requests, a query parameter of the same name). The receiving node proxies the request there. | ||
| This is useful for reaching a particular node behind a load balancer, including for node-level admin APIs. | ||
| type: added | ||
| authors: | ||
| - name: David Smiley | ||
| links: | ||
| - name: SOLR-18537 | ||
| url: https://issues.apache.org/jira/browse/SOLR-18537 |
138 changes: 138 additions & 0 deletions
138
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * 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.servlet; | ||
|
|
||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.UnavailableException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
| import org.apache.solr.client.solrj.jetty.HttpJettySolrClient; | ||
| import org.apache.solr.common.SolrException; | ||
| import org.apache.solr.common.cloud.ZkStateReader; | ||
| import org.apache.solr.core.CoreContainer; | ||
| import org.eclipse.jetty.client.HttpClient; | ||
|
|
||
| /** | ||
| * If the request carries the {@link #TARGET_NODE_HEADER} header naming a different live node, | ||
| * proxies the request there verbatim and returns; otherwise passes the request through. Unlike | ||
| * {@link HttpSolrCall}'s collection/core-driven proxying, this works for any endpoint, including | ||
| * node-level ones (e.g. {@code /solr/admin/**}, {@code /api/node/**}), so a request can be pinned | ||
| * to a specific node even when reached through a load balancer that hides which node answers. | ||
| */ | ||
| public class NodeTargetProxyFilter extends CoreContainerAwareHttpFilter { | ||
|
|
||
| /** | ||
| * Header naming a live node (as it appears in ZK live_nodes) to proxy this request to. Two | ||
| * shorthand forms are also accepted, both resolved relative to the current node: a value of all | ||
| * digits replaces the trailing {@code -<digits>} ordinal in the current node's hostname (e.g. | ||
| * {@code 8} turns {@code myhost-3.mydomain.com:3000} into {@code myhost-8.mydomain.com:3000}); | ||
| * otherwise, a value with no '.' is treated as a bare peer hostname, with the current node's | ||
| * domain suffix and port appended. | ||
| */ | ||
| public static final String TARGET_NODE_HEADER = "X-Solr-targetNode"; | ||
|
|
||
| @Override | ||
| protected void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) | ||
| throws IOException, ServletException { | ||
| // also accept it as a query param on GET requests, for convenience when a header can't | ||
| // easily be set (e.g. a browser address bar) | ||
| String targetNode = req.getHeader(TARGET_NODE_HEADER); | ||
| String queryString = req.getQueryString(); | ||
| if (targetNode == null | ||
| && "GET".equals(req.getMethod()) | ||
| && queryString != null | ||
| && queryString.contains(TARGET_NODE_HEADER)) { | ||
| targetNode = SolrRequestParsers.parseQueryString(queryString).get(TARGET_NODE_HEADER); | ||
| } | ||
| if (targetNode == null || targetNode.isBlank() || !proxyToNode(targetNode, req, resp)) { | ||
| chain.doFilter(req, resp); | ||
| } | ||
| } | ||
|
|
||
| /** Returns true if the request was proxied to {@code targetNode}. */ | ||
| private boolean proxyToNode(String targetNode, HttpServletRequest req, HttpServletResponse resp) { | ||
| CoreContainer cores; | ||
| try { | ||
| cores = getCores(); | ||
| } catch (UnavailableException e) { | ||
| throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Core Container Unavailable"); | ||
| } | ||
| if (!cores.isZooKeeperAware()) { | ||
| throw new SolrException( | ||
| SolrException.ErrorCode.BAD_REQUEST, TARGET_NODE_HEADER + " header requires SolrCloud"); | ||
|
dsmiley marked this conversation as resolved.
Resolved
|
||
| } | ||
| String ownNodeName = cores.getZkController().getNodeName(); | ||
| String resolvedNode = resolvePeerShorthand(targetNode, ownNodeName); | ||
| ZkStateReader zkStateReader = cores.getZkController().getZkStateReader(); | ||
| if (!zkStateReader.getClusterState().getLiveNodes().contains(resolvedNode)) { | ||
| throw new SolrException( | ||
| SolrException.ErrorCode.NOT_FOUND, "Unknown or non-live node: " + resolvedNode); | ||
|
dsmiley marked this conversation as resolved.
Resolved
|
||
| } | ||
| if (resolvedNode.equals(ownNodeName)) { | ||
| return false; // already on the target node; let it process normally | ||
|
dsmiley marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| String url = | ||
| zkStateReader.getBaseUrlForNodeName(resolvedNode) + ServletUtils.getPathAfterContext(req); | ||
|
dsmiley marked this conversation as resolved.
Resolved
|
||
| String queryString = req.getQueryString(); | ||
| if (queryString != null) { | ||
| url = url + "?" + queryString; | ||
|
janhoy marked this conversation as resolved.
Resolved
|
||
| } | ||
| try { | ||
| HttpClient httpClient = | ||
| ((HttpJettySolrClient) cores.getDefaultHttpSolrClient()).getHttpClient(); | ||
| HttpSolrProxy.doHttpProxy(httpClient, req, resp, url); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm investigating this separately. |
||
| } catch (Exception e) { | ||
| throw new SolrException( | ||
| SolrException.ErrorCode.SERVER_ERROR, | ||
| "Error proxying request to node: " + resolvedNode, | ||
| e); | ||
| } catch (Error e) { | ||
| throw e; | ||
| } catch (Throwable e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * If {@code targetNode} is all digits, it's an ordinal shorthand: {@code ownNodeName}'s first | ||
| * hostname label is expected to end in {@code -<digits>}, which gets replaced. Otherwise, if it | ||
| * has no '.', it's a bare peer hostname; expand it using {@code ownNodeName}'s domain suffix and | ||
| * {@code :port_context}. Otherwise, returned unchanged. | ||
| */ | ||
| static String resolvePeerShorthand(String targetNode, String ownNodeName) { | ||
| if (targetNode.contains(".")) { | ||
|
dsmiley marked this conversation as resolved.
Resolved
|
||
| return targetNode; | ||
| } | ||
| int colonIdx = ownNodeName.indexOf(':'); | ||
| String ownHost = ownNodeName.substring(0, colonIdx); | ||
| String portAndContext = ownNodeName.substring(colonIdx); | ||
| int dotIdx = ownHost.indexOf('.'); | ||
| String domainSuffix = dotIdx >= 0 ? ownHost.substring(dotIdx) : ""; | ||
|
|
||
| if (targetNode.matches("\\d+")) { | ||
| String firstLabel = dotIdx >= 0 ? ownHost.substring(0, dotIdx) : ownHost; | ||
| targetNode = firstLabel.replaceFirst("-\\d+$", "-" + targetNode); | ||
| } | ||
|
|
||
| return targetNode + domainSuffix + portAndContext; | ||
| } | ||
| } | ||
130 changes: 130 additions & 0 deletions
130
solr/core/src/test/org/apache/solr/servlet/NodeTargetProxyFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * 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.servlet; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.apache.solr.servlet.NodeTargetProxyFilter.TARGET_NODE_HEADER; | ||
|
|
||
| import java.net.HttpURLConnection; | ||
| import java.net.URI; | ||
| import java.net.URLEncoder; | ||
| import java.util.Map; | ||
| import org.apache.solr.SolrTestCaseJ4; | ||
| import org.apache.solr.cloud.SolrCloudTestCase; | ||
| import org.apache.solr.common.util.Utils; | ||
| import org.apache.solr.embedded.JettySolrRunner; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| /** Tests {@link NodeTargetProxyFilter}. */ | ||
| @SolrTestCaseJ4.SuppressSSL | ||
| public class NodeTargetProxyFilterTest extends SolrCloudTestCase { | ||
|
|
||
| private static JettySolrRunner receivingNode; | ||
| private static JettySolrRunner targetNode; | ||
|
|
||
| @BeforeClass | ||
| public static void setupCluster() throws Exception { | ||
| configureCluster(2).addConfig("conf", configset("cloud-minimal")).configure(); | ||
| receivingNode = cluster.getJettySolrRunner(0); | ||
| targetNode = cluster.getJettySolrRunner(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHeaderProxiesToTargetNode() throws Exception { | ||
| HttpURLConnection conn = open(receivingNode.getBaseUrl() + "/admin/info/system?wt=json"); | ||
| conn.setRequestProperty(TARGET_NODE_HEADER, targetNode.getNodeName()); | ||
| assertEquals(targetNode.getNodeName(), respondingNode(conn)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHeaderProxiesV2() throws Exception { | ||
| HttpURLConnection conn = open(receivingNode.getBaseURLV2() + "/node/system"); | ||
| conn.setRequestProperty(TARGET_NODE_HEADER, targetNode.getNodeName()); | ||
| assertEquals(targetNode.getNodeName(), respondingNode(conn)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testQueryParamProxiesGet() throws Exception { | ||
| HttpURLConnection conn = | ||
| open( | ||
| receivingNode.getBaseUrl() | ||
| + "/admin/info/system?wt=json&" | ||
| + TARGET_NODE_HEADER | ||
| + "=" | ||
| + URLEncoder.encode(targetNode.getNodeName(), UTF_8)); | ||
| assertEquals(targetNode.getNodeName(), respondingNode(conn)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testQueryParamIgnoredForPost() throws Exception { | ||
| HttpURLConnection conn = | ||
| open( | ||
| receivingNode.getBaseUrl() | ||
| + "/admin/info/system?wt=json&" | ||
| + TARGET_NODE_HEADER | ||
| + "=" | ||
| + URLEncoder.encode(targetNode.getNodeName(), UTF_8)); | ||
| conn.setRequestMethod("POST"); | ||
| conn.setDoOutput(true); | ||
| conn.setFixedLengthStreamingMode(0); | ||
| conn.getOutputStream().close(); | ||
| assertEquals(receivingNode.getNodeName(), respondingNode(conn)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTargetingSelfIsHandledLocally() throws Exception { | ||
| HttpURLConnection conn = open(receivingNode.getBaseUrl() + "/admin/info/system?wt=json"); | ||
| conn.setRequestProperty(TARGET_NODE_HEADER, receivingNode.getNodeName()); | ||
| assertEquals(receivingNode.getNodeName(), respondingNode(conn)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnknownNodeIs404() throws Exception { | ||
| HttpURLConnection conn = open(receivingNode.getBaseUrl() + "/admin/info/system?wt=json"); | ||
| conn.setRequestProperty(TARGET_NODE_HEADER, "no.such.host:1234_solr"); | ||
| assertEquals(404, conn.getResponseCode()); | ||
| conn.disconnect(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResolvePeerShorthand() { | ||
| String own = "myhost-3.mydomain.com:3000_solr"; | ||
| assertEquals( | ||
| "otherhost.mydomain.com:3000_solr", | ||
| NodeTargetProxyFilter.resolvePeerShorthand("otherhost", own)); | ||
| assertEquals( | ||
| "otherhost.elsewhere.com:8983_solr", | ||
| NodeTargetProxyFilter.resolvePeerShorthand("otherhost.elsewhere.com:8983_solr", own)); | ||
| assertEquals( | ||
| "otherhost:3000_solr", | ||
| NodeTargetProxyFilter.resolvePeerShorthand("otherhost", "myhost:3000_solr")); | ||
| } | ||
|
|
||
| private static HttpURLConnection open(String url) throws Exception { | ||
| return (HttpURLConnection) URI.create(url).toURL().openConnection(); | ||
| } | ||
|
|
||
| /** Returns the "node" field from a system-info response. */ | ||
| private static String respondingNode(HttpURLConnection conn) throws Exception { | ||
| assertEquals(200, conn.getResponseCode()); | ||
| String body = new String(conn.getInputStream().readAllBytes(), UTF_8); | ||
| conn.disconnect(); | ||
| return (String) ((Map<?, ?>) Utils.fromJSONString(body)).get("node"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.