Skip to content

Commit 55732fd

Browse files
authored
fix: NullPointerException on AbstractReadContext.span (#3036)
* fix: NullPointerException on AbstractReadContext.span In case of a storm of requests for sessions leads to the session pool being temporarily exhausted, the session pool will return RESOURCE_EXHAUSTED errors for those requests. That error was however not propagated to any additional threads that were also waiting for the same transaction, and would cause these threads to continue with the transaction even though no valid session had been assigned. This fix ensures that the exception is also propagated to any thread waiting for the session. * chore: add default project ID
1 parent ca213bb commit 55732fd

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -2252,12 +2252,15 @@ private PooledSession pollUninterruptiblyWithTimeout(
22522252
interrupted = true;
22532253
} catch (TimeoutException e) {
22542254
if (acquireSessionTimeout != null) {
2255-
throw SpannerExceptionFactory.newSpannerException(
2256-
ErrorCode.RESOURCE_EXHAUSTED,
2257-
"Timed out after waiting "
2258-
+ acquireSessionTimeout.toMillis()
2259-
+ "ms for acquiring session. To mitigate error SessionPoolOptions#setAcquireSessionTimeout(Duration) to set a higher timeout"
2260-
+ " or increase the number of sessions in the session pool.");
2255+
SpannerException exception =
2256+
SpannerExceptionFactory.newSpannerException(
2257+
ErrorCode.RESOURCE_EXHAUSTED,
2258+
"Timed out after waiting "
2259+
+ acquireSessionTimeout.toMillis()
2260+
+ "ms for acquiring session. To mitigate error SessionPoolOptions#setAcquireSessionTimeout(Duration) to set a higher timeout"
2261+
+ " or increase the number of sessions in the session pool.");
2262+
waiter.setException(exception);
2263+
throw exception;
22612264
}
22622265
return null;
22632266
} catch (ExecutionException e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner;
18+
19+
import static org.junit.Assert.assertNull;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import com.google.cloud.NoCredentials;
23+
import com.google.cloud.spanner.connection.AbstractMockServerTest;
24+
import io.grpc.ManagedChannelBuilder;
25+
import java.util.ArrayList;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.ExecutorService;
28+
import java.util.concurrent.Executors;
29+
import java.util.concurrent.Future;
30+
import java.util.concurrent.TimeUnit;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
import org.threeten.bp.Duration;
35+
36+
@RunWith(JUnit4.class)
37+
public class SpanExceptionTest extends AbstractMockServerTest {
38+
39+
@Test
40+
public void testReadOnlyTransaction() throws InterruptedException, ExecutionException {
41+
try (Spanner spanner =
42+
SpannerOptions.newBuilder()
43+
.setProjectId("my-project")
44+
.setHost(String.format("http://localhost:%d", getPort()))
45+
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
46+
.setCredentials(NoCredentials.getInstance())
47+
.setSessionPoolOption(
48+
SessionPoolOptions.newBuilder()
49+
.setMaxSessions(10)
50+
.setAcquireSessionTimeout(Duration.ofMillis(10))
51+
// .setAcquireSessionTimeout(null)
52+
.build())
53+
.build()
54+
.getService()) {
55+
DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("p", "i", "d"));
56+
57+
int numThreads = 25;
58+
ExecutorService service = Executors.newFixedThreadPool(numThreads);
59+
ArrayList<Future<Void>> futures = new ArrayList<>(numThreads);
60+
try (ReadOnlyTransaction readOnlyTransaction = client.readOnlyTransaction()) {
61+
for (int i = 0; i < numThreads; i++) {
62+
futures.add(service.submit(() -> executeRandom(readOnlyTransaction)));
63+
}
64+
service.shutdown();
65+
assertTrue(service.awaitTermination(60L, TimeUnit.SECONDS));
66+
// Verify that all threads finished without any unexpected errors.
67+
for (Future<Void> future : futures) {
68+
assertNull(future.get());
69+
}
70+
}
71+
}
72+
}
73+
74+
private Void executeRandom(ReadOnlyTransaction readOnlyTransaction) {
75+
try (ResultSet resultSet = readOnlyTransaction.executeQuery(SELECT_RANDOM_STATEMENT)) {
76+
while (resultSet.next()) {
77+
// ignore
78+
}
79+
} catch (SpannerException spannerException) {
80+
if (spannerException.getErrorCode() == ErrorCode.RESOURCE_EXHAUSTED) {
81+
// This is the expected error code, so ignore.
82+
return null;
83+
}
84+
throw spannerException;
85+
}
86+
return null;
87+
}
88+
}

0 commit comments

Comments
 (0)