-
Notifications
You must be signed in to change notification settings - Fork 852
SOLR-18324: Fix NPE/RTimer assertion when a V2 request fails before a SolrQueryRequest is attached #4676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
SOLR-18324: Fix NPE/RTimer assertion when a V2 request fails before a SolrQueryRequest is attached #4676
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| title: Fixed a race condition where V2 (JAX-RS) API requests that fail before a SolrQueryRequest is attached to the request context could throw a NullPointerException while building the error response, which could cascade into an internal request-metrics-timer assertion failure under Solr's security manager / assertions-enabled test builds. | ||
| type: fixed | ||
| authors: | ||
| - name: Eric Pugh | ||
| links: | ||
| - name: SOLR-18324 | ||
| url: https://issues.apache.org/jira/browse/SOLR-18324 |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH... I have little faith in unit testing low level plumbing like this. IMO they even have negative value as it's yet another thing to change if we change the plumbing. It's better to accomplish the high level goals (like what led you to uncover this bug). My comment applies to all your tests here. Just because you write a line of code doesn't mean it needs a direct test. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * 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.jersey; | ||
|
|
||
| import static org.apache.solr.jersey.RequestContextKeys.NOT_FOUND_FLAG; | ||
| import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_REQUEST; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import jakarta.ws.rs.container.ContainerRequestContext; | ||
| import jakarta.ws.rs.container.ContainerResponseContext; | ||
| import java.util.Set; | ||
| import org.apache.solr.SolrTestCaseJ4; | ||
| import org.apache.solr.client.api.model.SolrJerseyResponse; | ||
| import org.apache.solr.request.SolrQueryRequest; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| /** Unit tests for {@link PostRequestDecorationFilter} */ | ||
| public class PostRequestDecorationFilterTest extends SolrTestCaseJ4 { | ||
|
|
||
| @BeforeClass | ||
| public static void ensureWorkingMockito() { | ||
| assumeWorkingMockito(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFilterDoesNotThrowWhenNoSolrQueryRequestAttached() throws Exception { | ||
| // solrQueryRequest can be null when the request failed before Jersey attached one to the | ||
| // request context (e.g. via CatchAllExceptionMapper); filter() must not NPE in that case. | ||
| final var mockRequestContext = mock(ContainerRequestContext.class); | ||
| final var mockResponseContext = mock(ContainerResponseContext.class); | ||
|
|
||
| when(mockRequestContext.getPropertyNames()).thenReturn(Set.of()); | ||
| when(mockRequestContext.getProperty(SOLR_QUERY_REQUEST)).thenReturn(null); | ||
|
|
||
| final var response = new SolrJerseyResponse(); | ||
| when(mockResponseContext.hasEntity()).thenReturn(true); | ||
| when(mockResponseContext.getEntity()).thenReturn(response); | ||
|
|
||
| new PostRequestDecorationFilter().filter(mockRequestContext, mockResponseContext); | ||
|
|
||
| assertEquals(0, response.responseHeader.qTime); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFilterSetsQTimeWhenSolrQueryRequestPresent() throws Exception { | ||
| final var mockRequestContext = mock(ContainerRequestContext.class); | ||
| final var mockResponseContext = mock(ContainerResponseContext.class); | ||
| final var mockSolrQueryRequest = mock(SolrQueryRequest.class); | ||
| final var timer = new org.apache.solr.util.RTimerTree(); | ||
|
|
||
| when(mockRequestContext.getPropertyNames()).thenReturn(Set.of()); | ||
| when(mockRequestContext.getProperty(SOLR_QUERY_REQUEST)).thenReturn(mockSolrQueryRequest); | ||
| when(mockSolrQueryRequest.getRequestTimer()).thenReturn(timer); | ||
|
|
||
| final var response = new SolrJerseyResponse(); | ||
| when(mockResponseContext.hasEntity()).thenReturn(true); | ||
| when(mockResponseContext.getEntity()).thenReturn(response); | ||
|
|
||
| new PostRequestDecorationFilter().filter(mockRequestContext, mockResponseContext); | ||
|
|
||
| assertTrue(response.responseHeader.qTime >= 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFilterSkipsEntirelyForNotFoundRequests() throws Exception { | ||
| final var mockRequestContext = mock(ContainerRequestContext.class); | ||
| final var mockResponseContext = mock(ContainerResponseContext.class); | ||
| when(mockRequestContext.getPropertyNames()).thenReturn(Set.of(NOT_FOUND_FLAG)); | ||
|
|
||
| new PostRequestDecorationFilter().filter(mockRequestContext, mockResponseContext); | ||
|
|
||
| verify(mockResponseContext, never()).hasEntity(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * 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.jersey; | ||
|
|
||
| import static org.apache.solr.jersey.RequestContextKeys.HANDLER_METRICS; | ||
| import static org.apache.solr.jersey.RequestContextKeys.TIMER; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.metrics.LongCounter; | ||
| import io.opentelemetry.api.metrics.LongHistogram; | ||
| import jakarta.ws.rs.container.ContainerRequestContext; | ||
| import jakarta.ws.rs.container.ContainerResponseContext; | ||
| import java.util.Set; | ||
| import org.apache.solr.SolrTestCaseJ4; | ||
| import org.apache.solr.handler.RequestHandlerBase; | ||
| import org.apache.solr.metrics.SolrMetricsContext; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| /** Unit tests for {@link RequestMetricHandling} */ | ||
| public class RequestMetricHandlingTest extends SolrTestCaseJ4 { | ||
|
|
||
| @BeforeClass | ||
| public static void ensureWorkingMockito() { | ||
| assumeWorkingMockito(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPostRequestMetricsFilterToleratesBeingInvokedTwice() throws Exception { | ||
| // Jersey can re-invoke response filters a second time when an exception occurs while | ||
| // building the first response (e.g. via CatchAllExceptionMapper); the second invocation must | ||
| // not try to stop an already-stopped timer. | ||
| final var mockRequestContext = mock(ContainerRequestContext.class); | ||
| final var mockResponseContext = mock(ContainerResponseContext.class); | ||
| final RequestHandlerBase.HandlerMetrics metrics = createHandlerMetrics(); | ||
| final var timer = metrics.requestTimes.start(); | ||
|
|
||
| when(mockRequestContext.getPropertyNames()).thenReturn(Set.of()); | ||
| when(mockRequestContext.getProperty(HANDLER_METRICS)).thenReturn(metrics); | ||
| when(mockRequestContext.getProperty(TIMER)).thenReturn(timer); | ||
|
|
||
| final var filter = new RequestMetricHandling.PostRequestMetricsFilter(); | ||
| filter.filter(mockRequestContext, mockResponseContext); | ||
|
|
||
| // Simulate Jersey clearing the property after the first stop, as our fix does. | ||
| when(mockRequestContext.getProperty(TIMER)).thenReturn(null); | ||
|
|
||
| // A second invocation must not throw (would previously throw AssertionError with | ||
| // assertions enabled, from RTimer.stop() being called on an already-stopped timer). | ||
| filter.filter(mockRequestContext, mockResponseContext); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPostRequestMetricsFilterNoOpsWithoutMetrics() throws Exception { | ||
| final var mockRequestContext = mock(ContainerRequestContext.class); | ||
| final var mockResponseContext = mock(ContainerResponseContext.class); | ||
| when(mockRequestContext.getPropertyNames()).thenReturn(Set.of()); | ||
| when(mockRequestContext.getProperty(HANDLER_METRICS)).thenReturn(null); | ||
|
|
||
| new RequestMetricHandling.PostRequestMetricsFilter() | ||
| .filter(mockRequestContext, mockResponseContext); | ||
| } | ||
|
|
||
| private RequestHandlerBase.HandlerMetrics createHandlerMetrics() { | ||
| final SolrMetricsContext metricsContext = mock(SolrMetricsContext.class); | ||
| final LongCounter mockLongCounter = mock(LongCounter.class); | ||
| final LongHistogram mockLongHistogram = mock(LongHistogram.class); | ||
|
|
||
| when(metricsContext.getRegistryName()).thenReturn("solr.core"); | ||
| when(metricsContext.longCounter(any(), any())).thenReturn(mockLongCounter); | ||
| when(metricsContext.longCounter(any(), any(), any())).thenReturn(mockLongCounter); | ||
| when(metricsContext.longHistogram(any(), any())).thenReturn(mockLongHistogram); | ||
| when(metricsContext.longHistogram(any(), any(), any())).thenReturn(mockLongHistogram); | ||
|
|
||
| return new RequestHandlerBase.HandlerMetrics( | ||
| metricsContext, Attributes.of(AttributeKey.stringKey("source"), "test"), false); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trace