You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@solr.apache.org by "dsmiley (via GitHub)" <gi...@apache.org> on 2026/10/12 23:12:27 UTC
[PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
dsmiley opened a new pull request, #5112:
URL: https://github.com/apache/solr/pull/5112
https://issues.apache.org/jira/browse/SOLR-18537
> It can be very useful for Solr to proxy requests to another node, specified in a header or param. The intended scenario is when there's an HTTP proxy in front of Solr, and the backing Solr nodes aren't directly reachable. Solr is doing that now for a few admin endpoints that have already been coded for it, using GenericV1RequestProxy internally (e.g. for logging, info, ...). The issue here instead proposes a generic mechanism that doesn't require Solr handlers to be coded any differently.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #5112:
URL: https://github.com/apache/solr/pull/5112#discussion_r4335209153
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
Review Comment:
I'll change to 400 because the problem is that of the client. 502 would be a problem in actually doing the proxy request but that's not the issue for this spot.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #5112:
URL: https://github.com/apache/solr/pull/5112#discussion_r4350028010
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
+ }
+ if (resolvedNode.equals(ownNodeName)) {
+ return false; // already on the target node; let it process normally
+ }
+
+ String url =
+ zkStateReader.getBaseUrlForNodeName(resolvedNode) + ServletUtils.getPathAfterContext(req);
+ String queryString = req.getQueryString();
+ if (queryString != null) {
+ url = url + "?" + queryString;
+ }
+ try {
+ HttpClient httpClient =
+ ((HttpJettySolrClient) cores.getDefaultHttpSolrClient()).getHttpClient();
+ HttpSolrProxy.doHttpProxy(httpClient, req, resp, url);
Review Comment:
I'm investigating this separately.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on PR #5112:
URL: https://github.com/apache/solr/pull/5112#issuecomment-6506171085
will merge tomorrow
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "janhoy (via GitHub)" <gi...@apache.org>.
janhoy commented on code in PR #5112:
URL: https://github.com/apache/solr/pull/5112#discussion_r4334913369
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
Review Comment:
Is 404 the right response here - it usually says that the path is not found on the host.
Since Solr is acting as a gateway with this header, would perhaps 502 Bad Gateway or 503 Service Unavailable be more precise?
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
Review Comment:
You say "header requires", but it could also be a request-param, not only a header :)
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
+ }
+ if (resolvedNode.equals(ownNodeName)) {
+ return false; // already on the target node; let it process normally
+ }
+
+ String url =
+ zkStateReader.getBaseUrlForNodeName(resolvedNode) + ServletUtils.getPathAfterContext(req);
+ String queryString = req.getQueryString();
+ if (queryString != null) {
+ url = url + "?" + queryString;
Review Comment:
Should we use the URL class for url manipulation instead of String?
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #5112:
URL: https://github.com/apache/solr/pull/5112#discussion_r4344180220
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
+ }
+ if (resolvedNode.equals(ownNodeName)) {
+ return false; // already on the target node; let it process normally
Review Comment:
This was the most serious issue I addressed.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "nick-boss-tech (via GitHub)" <gi...@apache.org>.
nick-boss-tech commented on PR #5112:
URL: https://github.com/apache/solr/pull/5112#issuecomment-6392310976
**AI Review:**
**P2, new:** A GET query such as ?foo=X-Solr-targetNode causes the filter to overwrite a valid targeting header with null. The request then runs on the receiving node instead of the selected node. Preserve the header unless the actual targeting parameter exists. [Code](https://github.com/apache/solr/blob/b4fa46f1ecb11a60cc2cfd4783492639bb4e3a85/solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java#L79-L85)
**P3:** The guide promises 404 for unknown nodes; the agreed implementation and tests use 400.
**Existing concern:** Certificate-based caller identity remains unresolved. The new Basic Auth tests do not close that already-raised issue.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on PR #5112:
URL: https://github.com/apache/solr/pull/5112#issuecomment-6446799224
> P2, new: A GET query such as ?foo=X-Solr-targetNode causes the filter to overwrite a valid targeting header with null.
@nick-boss-tech can you elaborate; I don't understand.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "epugh (via GitHub) via issues" <is...@solr.apache.org>.
epugh commented on PR #5112:
URL: https://github.com/apache/solr/pull/5112#issuecomment-6586554984
@dsmiley now that this is merged, are there follow up tasks to leverage this? For example, in the various V2 apis or in the Admin UI feature that @iprithv put together...? I'd love to knw where this fits into our development plan.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub) via issues" <is...@solr.apache.org>.
dsmiley merged PR #5112:
URL: https://github.com/apache/solr/pull/5112
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on PR #5112:
URL: https://github.com/apache/solr/pull/5112#issuecomment-6345241757
It's really remarkable how much this PR has improved/evolved as a result of such peer-review/scrutiny.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #5112:
URL: https://github.com/apache/solr/pull/5112#discussion_r4335037476
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
+ }
+ if (resolvedNode.equals(ownNodeName)) {
+ return false; // already on the target node; let it process normally
Review Comment:
When the selected node has no replica of the requested collection, `HttpSolrCall.sendRemoteProxy()` forwards to a replica with this routing directive intact. The replica's filter sends it back to the selected node, repeating until `_forwardedCount` causes a 510 error despite available replicas. Consume both the routing header and GET parameter before downstream processing on the selected node, including requests initially received there. Add a test targeting a non-replica node.
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
+ }
+ if (resolvedNode.equals(ownNodeName)) {
+ return false; // already on the target node; let it process normally
+ }
+
+ String url =
+ zkStateReader.getBaseUrlForNodeName(resolvedNode) + ServletUtils.getPathAfterContext(req);
+ String queryString = req.getQueryString();
+ if (queryString != null) {
+ url = url + "?" + queryString;
+ }
+ try {
+ HttpClient httpClient =
+ ((HttpJettySolrClient) cores.getDefaultHttpSolrClient()).getHttpClient();
+ HttpSolrProxy.doHttpProxy(httpClient, req, resp, url);
Review Comment:
The raw Jetty client bypasses the authentication listeners in `HttpJettySolrClient.decorateRequest()`, and this filter establishes no caller-bound `SolrRequestInfo`. With `CertAuthPlugin`, the target authenticates the proxy node's TLS certificate instead of the original client's identity. Authorization can then reject valid requests or grant permissions belonging to the node certificate rather than the caller. Use caller-scoped internode authentication and test distinct client/node certificates with authorization enabled.
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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);
+ }
Review Comment:
With authentication enabled, exceptions from `proxyToNode()` escape through `AuthenticationFilter`, which wraps downstream exceptions as authentication failures and runs failure cleanup before `RequiredSolrRequestFilter` can send the original status. An authenticated unknown-node request therefore does not receive the documented 404; standalone's 400 is affected too. Handle these `SolrException`s inside this filter and return normally. Add an authenticated unknown-node test.
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
+ }
+ if (resolvedNode.equals(ownNodeName)) {
+ return false; // already on the target node; let it process normally
+ }
+
+ String url =
+ zkStateReader.getBaseUrlForNodeName(resolvedNode) + ServletUtils.getPathAfterContext(req);
Review Comment:
`ServletUtils.getPathAfterContext()` concatenates decoded `servletPath` and `pathInfo`, which cannot safely be appended to a URL verbatim. An encoded `%23` becomes a fragment delimiter rather than part of the upstream path; spaces and percent signs can also produce invalid or changed URLs. Preserve the encoded request URI when building the upstream URL, and test an encoded path segment.
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
+ }
+ if (resolvedNode.equals(ownNodeName)) {
+ return false; // already on the target node; let it process normally
+ }
+
+ String url =
+ zkStateReader.getBaseUrlForNodeName(resolvedNode) + ServletUtils.getPathAfterContext(req);
+ String queryString = req.getQueryString();
+ if (queryString != null) {
+ url = url + "?" + queryString;
+ }
+ try {
+ HttpClient httpClient =
+ ((HttpJettySolrClient) cores.getDefaultHttpSolrClient()).getHttpClient();
+ HttpSolrProxy.doHttpProxy(httpClient, req, resp, url);
+ } 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(".")) {
Review Comment:
Complete live-node names can have single-label hosts. Resolving `solr2:8983_solr` against `solr1:8983_solr` produces `solr2:8983_solr:8983_solr`, so valid targets, including self-targets, incorrectly fail the live-node check. Preserve names containing the port separator before applying bare-hostname shorthand, and add a complete dotless-node-name test.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "nick-boss-tech (via GitHub)" <gi...@apache.org>.
nick-boss-tech commented on PR #5112:
URL: https://github.com/apache/solr/pull/5112#issuecomment-6450355228
🤖 *AI text below* 🤖 *(reviewed by Nick Shanin)*
@dsmiley, the example has **no targeting query parameter**. Suppose this request reaches node A, with the header naming live node B:
```http
GET /solr/admin/info/system?foo=X-Solr-targetNode HTTP/1.1
X-Solr-targetNode: node-B.example.com:8983_solr
```
The [substring check](https://github.com/apache/solr/blob/b4fa46f1ecb11a60cc2cfd4783492639bb4e3a85/solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java#L79-L87) sees `X-Solr-targetNode` inside `foo`'s value, so it parses the query. But `params.get(TARGET_NODE_HEADER)` returns null: the only parameter is `foo`. Assigning that null to `targetNode` discards the header, and `/admin/info/system` runs on A instead of B.
I prepared a [fix and regression tests on top of your current head](https://github.com/nick-boss-tech/solr/compare/b4fa46f1ecb11a60cc2cfd4783492639bb4e3a85...46bc7e428b5be519a497ecf65dcd0256b65fa84c). The fix only replaces the header target when the actual targeting parameter exists. Supplying both still returns 400. The diff also corrects the guide's 404 to the agreed 400 and clarifies the hostname shorthand rule.
<details>
<summary>Linux verification: 29 tests passed; both routing regressions fail before the fix</summary>
Tested at `46bc7e428b5b`, seed `5112C0DE`:
- `NodeTargetProxyFilterTest` and `NodeTargetProxyFilterAuthTest`: 18 passed, no failures, errors or skips.
- `HttpSolrProxyTest` and `SolrRequestParserTest`: 11 passed, no failures, errors or skips.
- Both new header-loss regressions fail on your original production code with the new test file: they expect the selected node but receive the original node. These cover the header name appearing in an unrelated query value and in a longer parameter name. The other two new tests, for no-header handling and header/query conflict, passed with the fix; they were not run against the original code.
- `tidy` leaves a clean tree. `check -x test` passed with `NODE_ENV` unset after reinstalling the Antora tabs dependency. Two earlier attempts failed because `NODE_ENV=production` caused npm to omit that devDependency.
[Verification details](https://github.com/nick-boss-tech/solr/blob/c7c5777b87fed3f1ed8578da8090ebb643ab4509/receipts/PR-5112-target-header-followup.md).
</details>
To take the fix and its Javadoc formatting follow-up:
```sh
git switch SOLR-18537-proxyRequestsByHeader
git fetch https://github.com/nick-boss-tech/solr.git solr-18537-submit
git merge --ff-only 46bc7e428b5be519a497ecf65dcd0256b65fa84c
```
You're welcome to take the commits or adapt the changes.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on PR #5112:
URL: https://github.com/apache/solr/pull/5112#issuecomment-6452859818
Much appreciated Nick!
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "janhoy (via GitHub)" <gi...@apache.org>.
janhoy commented on code in PR #5112:
URL: https://github.com/apache/solr/pull/5112#discussion_r4335515592
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
Review Comment:
It could be that the caller is unlucky and hits a glitch where a node is restarting. Then it is technically not the caller's fault. But it would require us to consult the wider cluster state to know if the node COULD EVER be a live node.
A 400 would not be retryable since it's an error by the caller to try to reference a non-approved node. But if not live right now, but in 5s it would be retryable. Problem I believe is that unless there's a collection/replica registered on nodeX, we'd have no way to know that the node was part of the cluster 30s ago and will come bac in 5s.
Let's keep it simple and do 400.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #5112:
URL: https://github.com/apache/solr/pull/5112#discussion_r4354183654
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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 org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST;
+import static org.apache.solr.common.SolrException.ErrorCode.SERVER_ERROR;
+import static org.apache.solr.common.SolrException.ErrorCode.SERVICE_UNAVAILABLE;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.UnavailableException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletRequestWrapper;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+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.common.params.ModifiableSolrParams;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.core.CoreContainer;
+import org.eclipse.jetty.client.HttpClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 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 {
+
+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+ /**
+ * 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 res, FilterChain chain)
+ throws IOException, ServletException {
+
+ String targetNode = req.getHeader(TARGET_NODE_HEADER);
+
+ // 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 queryString = req.getQueryString();
+ if ("GET".equals(req.getMethod())
+ && queryString != null
+ && queryString.contains(TARGET_NODE_HEADER)) { // cheap; avoid double-parse
+ SolrParams params = SolrRequestParsers.parseQueryString(queryString);
+ if (targetNode != null && params.get(TARGET_NODE_HEADER) != null) {
+ res.sendError(400, TARGET_NODE_HEADER + " must EITHER be a header or url param");
+ return;
+ }
+ targetNode = params.get(TARGET_NODE_HEADER);
+ if (targetNode != null) {
+ queryString = filterQueryString(params);
+ }
+ }
+
Review Comment:
there is non-trivial complexity to support the query string option, particularly for removal. It's a sunk costs as this point -- it works, tested, etc. The motivation is to make this easy for a user with their browser.
##########
solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java:
##########
Review Comment:
technically, this aspect is a small bug fix to possible encoding issue in the path. That's very unlikely to happen in practice IMO.
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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 org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST;
+import static org.apache.solr.common.SolrException.ErrorCode.SERVER_ERROR;
+import static org.apache.solr.common.SolrException.ErrorCode.SERVICE_UNAVAILABLE;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.UnavailableException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletRequestWrapper;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+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.common.params.ModifiableSolrParams;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.core.CoreContainer;
+import org.eclipse.jetty.client.HttpClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 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 {
+
+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+ /**
+ * 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 res, FilterChain chain)
+ throws IOException, ServletException {
+
+ String targetNode = req.getHeader(TARGET_NODE_HEADER);
+
+ // 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 queryString = req.getQueryString();
+ if ("GET".equals(req.getMethod())
+ && queryString != null
+ && queryString.contains(TARGET_NODE_HEADER)) { // cheap; avoid double-parse
+ SolrParams params = SolrRequestParsers.parseQueryString(queryString);
+ if (targetNode != null && params.get(TARGET_NODE_HEADER) != null) {
+ res.sendError(400, TARGET_NODE_HEADER + " must EITHER be a header or url param");
+ return;
+ }
+ targetNode = params.get(TARGET_NODE_HEADER);
+ if (targetNode != null) {
+ queryString = filterQueryString(params);
+ }
+ }
+
+ // wrap the request to filter out the header & param.
+ // NOTE: we assume downstream processing only accesses such data via what we override !!
Review Comment:
an alternative would be to enhance HttpSolrProxy to directly do this. The header part would trivially be one line in hop-by-hop headers with a comment justification. I don't love the coupling but it's in the same package.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #5112:
URL: https://github.com/apache/solr/pull/5112#discussion_r4350342961
##########
solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java:
##########
@@ -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");
+ }
+ 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);
+ }
+ if (resolvedNode.equals(ownNodeName)) {
+ return false; // already on the target node; let it process normally
+ }
+
+ String url =
+ zkStateReader.getBaseUrlForNodeName(resolvedNode) + ServletUtils.getPathAfterContext(req);
+ String queryString = req.getQueryString();
+ if (queryString != null) {
+ url = url + "?" + queryString;
Review Comment:
I'm rather sympathetic. We have an addiction to untyped Strings, and it'd behove us to adopt `URI` as Jetty has done. I'll push an improvement.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-18537: Proxy requests to a node via X-Solr-targetNode [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on PR #5112:
URL: https://github.com/apache/solr/pull/5112#issuecomment-6134414349
FYI I've been using this for awhile at work.
Marking as Draft because needs a test and documentation.
Are there security concerns here? I don't think so. This is live-nodes only.
--
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: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org