Skip to content

Commit c44af90

Browse files
SOLR-17241: Honor wt on v2 collection responses from non-hosting nodes (#5266)
1 parent e8c7406 commit c44af90

5 files changed

Lines changed: 260 additions & 3 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: >
2+
Honor wt on v2 collection responses from nodes without a local replica,
3+
avoiding SolrJ response-format errors
4+
type: fixed
5+
authors:
6+
- name: Nick Shanin
7+
links:
8+
- name: SOLR-17241
9+
url: https://issues.apache.org/jira/browse/SOLR-17241

‎solr/core/src/java/org/apache/solr/jersey/MediaTypeOverridingFilter.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE;
2222
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
2323
import static org.apache.solr.common.params.CommonParams.WT;
24+
import static org.apache.solr.jersey.RequestContextKeys.SOLR_PARAMS;
2425
import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_REQUEST;
2526

2627
import jakarta.ws.rs.container.ContainerRequestContext;
@@ -31,6 +32,7 @@
3132
import java.io.IOException;
3233
import java.util.List;
3334
import org.apache.solr.api.JerseyResource;
35+
import org.apache.solr.common.params.SolrParams;
3436
import org.apache.solr.handler.admin.ZookeeperRead;
3537
import org.apache.solr.handler.api.V2ApiUtils;
3638
import org.apache.solr.request.SolrQueryRequest;
@@ -65,8 +67,11 @@ public void filter(
6567

6668
final SolrQueryRequest solrQueryRequest =
6769
(SolrQueryRequest) requestContext.getProperty(SOLR_QUERY_REQUEST);
68-
// TODO Is it valid for SQRequest to be null?
69-
final var params = (solrQueryRequest != null) ? solrQueryRequest.getParams() : null;
70+
// Collection-scoped Jersey requests can be served without a local SolrQueryRequest.
71+
final var params =
72+
(solrQueryRequest != null)
73+
? solrQueryRequest.getParams()
74+
: (SolrParams) requestContext.getProperty(SOLR_PARAMS);
7075
if (params != null && params.get(WT) != null) { // Override for 'wt'
7176
final String mediaType = V2ApiUtils.getMediaTypeFromWtParam(params, null);
7277
if (mediaType != null) {

‎solr/core/src/java/org/apache/solr/jersey/MessageBodyWriters.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.solr.jersey;
1919

2020
import static org.apache.solr.client.solrj.response.JavaBinResponseParser.JAVABIN_CONTENT_TYPE_V2;
21+
import static org.apache.solr.jersey.RequestContextKeys.SOLR_PARAMS;
2122
import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_REQUEST;
2223
import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_RESPONSE;
2324
import static org.apache.solr.response.QueryResponseWriter.CONTENT_TYPE_TEXT_UTF8;
@@ -34,8 +35,10 @@
3435
import java.io.OutputStream;
3536
import java.lang.annotation.Annotation;
3637
import java.lang.reflect.Type;
38+
import org.apache.solr.common.params.SolrParams;
3739
import org.apache.solr.handler.api.V2ApiUtils;
3840
import org.apache.solr.request.SolrQueryRequest;
41+
import org.apache.solr.request.SolrQueryRequestBase;
3942
import org.apache.solr.response.CSVResponseWriter;
4043
import org.apache.solr.response.JavaBinResponseWriter;
4144
import org.apache.solr.response.PrometheusResponseWriter;
@@ -164,8 +167,14 @@ public void writeTo(
164167
throws IOException, WebApplicationException {
165168
final ContainerRequestContext requestContext =
166169
resourceContext.getResource(ContainerRequestContext.class);
167-
final SolrQueryRequest solrQueryRequest =
170+
final SolrQueryRequest existingRequest =
168171
(SolrQueryRequest) requestContext.getProperty(SOLR_QUERY_REQUEST);
172+
// Collection-scoped requests served by a non-hosting node have no SolrQueryRequest
173+
final SolrQueryRequest solrQueryRequest =
174+
existingRequest != null
175+
? existingRequest
176+
: new SolrQueryRequestBase(
177+
null, (SolrParams) requestContext.getProperty(SOLR_PARAMS));
169178
final SolrQueryResponse solrQueryResponse =
170179
(SolrQueryResponse) requestContext.getProperty(SOLR_QUERY_RESPONSE);
171180

‎solr/core/src/test/org/apache/solr/handler/V2ApiIntegrationTest.java‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
import org.apache.solr.client.solrj.response.json.JsonMapResponseParser;
4242
import org.apache.solr.cloud.SolrCloudTestCase;
4343
import org.apache.solr.common.SolrDocumentList;
44+
import org.apache.solr.common.cloud.Replica;
4445
import org.apache.solr.common.params.ModifiableSolrParams;
4546
import org.apache.solr.common.util.NamedList;
4647
import org.apache.solr.common.util.Utils;
48+
import org.apache.solr.core.SolrCore;
49+
import org.apache.solr.embedded.JettySolrRunner;
4750
import org.eclipse.jetty.client.HttpClient;
4851
import org.junit.BeforeClass;
4952
import org.junit.Test;
@@ -175,6 +178,51 @@ public void testRespondsWithJsonWhenWtAndAcceptAreMissing() throws Exception {
175178
assertEquals("application/json", response.getHeaders().get("Content-type"));
176179
}
177180

181+
@Test
182+
public void testReloadHonorsWtOnNonHostingNode() throws Exception {
183+
final String collection = "reload_one_replica";
184+
CollectionAdminRequest.createCollection(collection, "conf1", 1, 1)
185+
.process(cluster.getSolrClient());
186+
try {
187+
cluster.waitForActiveCollection(collection, 1, 1);
188+
final Replica replica =
189+
getCollectionState(collection).getSlice("shard1").getReplicas().iterator().next();
190+
final JettySolrRunner hostingNode =
191+
cluster.getJettySolrRunners().stream()
192+
.filter(jetty -> jetty.getNodeName().equals(replica.getNodeName()))
193+
.findFirst()
194+
.orElseThrow();
195+
final JettySolrRunner nonHostingNode =
196+
cluster.getJettySolrRunners().stream()
197+
.filter(jetty -> !jetty.getNodeName().equals(replica.getNodeName()))
198+
.findFirst()
199+
.orElseThrow();
200+
201+
for (JettySolrRunner target : List.of(hostingNode, nonHostingNode)) {
202+
try (SolrCore before = hostingNode.getCoreContainer().getCore(replica.getCoreName());
203+
var client = new HttpJettySolrClient.Builder(target.getBaseUrl().toString()).build()) {
204+
assertNotNull(before);
205+
final V2Request request =
206+
new V2Request.Builder("/collections/" + collection + "/reload")
207+
.withMethod(SolrRequest.METHOD.POST)
208+
.withParams(params("wt", "javabin"))
209+
.withPayload("{}")
210+
.build();
211+
request.setResponseParser(new JavaBinResponseParser());
212+
213+
final V2Response response = request.process(client);
214+
assertEquals("Reload through " + target.getNodeName(), 0, response.getStatus());
215+
try (SolrCore after = hostingNode.getCoreContainer().getCore(replica.getCoreName())) {
216+
assertNotNull(after);
217+
assertNotSame("Reload must replace the hosted core", before, after);
218+
}
219+
}
220+
}
221+
} finally {
222+
CollectionAdminRequest.deleteCollection(collection).process(cluster.getSolrClient());
223+
}
224+
}
225+
178226
private HttpClient getRawClient() {
179227
return cluster.getRandomJetty(random()).getSolrClient().getHttpClient();
180228
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.solr.jersey;
19+
20+
import static jakarta.ws.rs.core.HttpHeaders.ACCEPT;
21+
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE;
22+
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
23+
import static jakarta.ws.rs.core.MediaType.APPLICATION_XML;
24+
import static org.apache.solr.SolrTestCaseJ4.assumeWorkingMockito;
25+
import static org.apache.solr.SolrTestCaseJ4.params;
26+
import static org.apache.solr.client.solrj.response.JavaBinResponseParser.JAVABIN_CONTENT_TYPE_V2;
27+
import static org.apache.solr.common.params.CommonParams.WT;
28+
import static org.apache.solr.jersey.RequestContextKeys.SOLR_PARAMS;
29+
import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_REQUEST;
30+
import static org.mockito.Mockito.doReturn;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.when;
33+
34+
import jakarta.ws.rs.container.ContainerRequestContext;
35+
import jakarta.ws.rs.container.ContainerResponseContext;
36+
import jakarta.ws.rs.container.ResourceInfo;
37+
import jakarta.ws.rs.core.MultivaluedHashMap;
38+
import jakarta.ws.rs.core.MultivaluedMap;
39+
import java.util.Map;
40+
import org.apache.solr.SolrTestCase;
41+
import org.apache.solr.handler.admin.ZookeeperRead;
42+
import org.apache.solr.handler.admin.api.ReloadCollectionAPI;
43+
import org.apache.solr.request.SolrQueryRequest;
44+
import org.junit.After;
45+
import org.junit.Before;
46+
import org.junit.BeforeClass;
47+
import org.junit.Test;
48+
import org.mockito.InjectMocks;
49+
import org.mockito.Mock;
50+
import org.mockito.MockitoAnnotations;
51+
52+
/** Checks response negotiation when a Jersey resource has no local SolrQueryRequest. */
53+
public class MediaTypeOverridingFilterTest extends SolrTestCase {
54+
55+
@Mock private ResourceInfo resourceInfo;
56+
@InjectMocks private MediaTypeOverridingFilter filter;
57+
58+
private AutoCloseable mocks;
59+
private ContainerRequestContext request;
60+
private ContainerResponseContext response;
61+
private MultivaluedMap<String, String> requestHeaders;
62+
private MultivaluedMap<String, Object> responseHeaders;
63+
64+
@BeforeClass
65+
public static void ensureWorkingMockito() {
66+
assumeWorkingMockito();
67+
}
68+
69+
@Before
70+
public void setUpFilter() {
71+
mocks = MockitoAnnotations.openMocks(this);
72+
request = mock(ContainerRequestContext.class);
73+
response = mock(ContainerResponseContext.class);
74+
requestHeaders = new MultivaluedHashMap<>();
75+
responseHeaders = new MultivaluedHashMap<>();
76+
responseHeaders.putSingle(CONTENT_TYPE, APPLICATION_XML);
77+
doReturn(ReloadCollectionAPI.class).when(resourceInfo).getResourceClass();
78+
when(request.getHeaders()).thenReturn(requestHeaders);
79+
when(request.getHeaderString(ACCEPT)).thenAnswer(ignored -> requestHeaders.getFirst(ACCEPT));
80+
when(response.getHeaders()).thenReturn(responseHeaders);
81+
when(response.getStatus()).thenReturn(200);
82+
}
83+
84+
@After
85+
public void closeMocks() throws Exception {
86+
mocks.close();
87+
}
88+
89+
@Test
90+
public void testWtFromQueryParamsWithoutSolrQueryRequest() throws Exception {
91+
requestHeaders.putSingle(ACCEPT, "text/plain");
92+
final Map<String, String> formats =
93+
Map.of(
94+
"json", APPLICATION_JSON, "xml", APPLICATION_XML, "javabin", JAVABIN_CONTENT_TYPE_V2);
95+
for (var format : formats.entrySet()) {
96+
responseHeaders.putSingle(CONTENT_TYPE, "text/plain");
97+
when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, format.getKey()));
98+
99+
filter.filter(request, response);
100+
101+
assertEquals(format.getKey(), format.getValue(), responseHeaders.getFirst(CONTENT_TYPE));
102+
}
103+
}
104+
105+
@Test
106+
public void testSolrQueryRequestParamsTakePrecedence() throws Exception {
107+
responseHeaders.putSingle(CONTENT_TYPE, "text/plain");
108+
final SolrQueryRequest solrRequest = mock(SolrQueryRequest.class);
109+
when(solrRequest.getParams()).thenReturn(params(WT, "xml"));
110+
when(request.getProperty(SOLR_QUERY_REQUEST)).thenReturn(solrRequest);
111+
when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, "javabin"));
112+
113+
filter.filter(request, response);
114+
115+
assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
116+
}
117+
118+
@Test
119+
public void testAcceptHeaderUsedWithoutWt() throws Exception {
120+
when(request.getProperty(SOLR_PARAMS)).thenReturn(params());
121+
requestHeaders.putSingle(ACCEPT, APPLICATION_XML);
122+
123+
filter.filter(request, response);
124+
125+
assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
126+
}
127+
128+
@Test
129+
public void testDefaultsToJsonWithoutWt() throws Exception {
130+
when(request.getProperty(SOLR_PARAMS)).thenReturn(params());
131+
for (String accept : new String[] {null, "*/*"}) {
132+
requestHeaders.clear();
133+
if (accept != null) {
134+
requestHeaders.putSingle(ACCEPT, accept);
135+
}
136+
responseHeaders.putSingle(CONTENT_TYPE, APPLICATION_XML);
137+
138+
filter.filter(request, response);
139+
140+
assertEquals(APPLICATION_JSON, responseHeaders.getFirst(CONTENT_TYPE));
141+
}
142+
}
143+
144+
@Test
145+
public void testMissingParamsStillDefaultsToJson() throws Exception {
146+
filter.filter(request, response);
147+
148+
assertEquals(APPLICATION_JSON, responseHeaders.getFirst(CONTENT_TYPE));
149+
}
150+
151+
@Test
152+
public void testErrorsPreserveMediaType() throws Exception {
153+
when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, "javabin"));
154+
for (int status : new int[] {400, 500}) {
155+
when(response.getStatus()).thenReturn(status);
156+
157+
filter.filter(request, response);
158+
159+
assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
160+
}
161+
}
162+
163+
@Test
164+
public void testExemptResourcesPreserveMediaType() throws Exception {
165+
when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, "javabin"));
166+
for (Class<?> resourceClass : new Class<?>[] {null, ZookeeperRead.class}) {
167+
doReturn(resourceClass).when(resourceInfo).getResourceClass();
168+
169+
filter.filter(request, response);
170+
171+
assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
172+
}
173+
}
174+
175+
@Test
176+
public void testBlankAndUnknownWtDoNotOverrideNegotiatedType() throws Exception {
177+
requestHeaders.putSingle(ACCEPT, APPLICATION_XML);
178+
for (String wt : new String[] {"", "unknown"}) {
179+
when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, wt));
180+
181+
filter.filter(request, response);
182+
183+
assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
184+
}
185+
}
186+
}

0 commit comments

Comments
 (0)