-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add session pool option for modelling a timeout around session …
…acquisition. (#2641) * feat: add session pool option for modelling a timeout around session acquisition. * docs: modify existing documentation. * Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPoolOptions.java Co-authored-by: Knut Olav Løite <[email protected]> * Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPoolOptions.java Co-authored-by: Knut Olav Løite <[email protected]> * chore: address review comments. * fix: lint errors. * fix: broken unit test. * chore: add more unit test. * fix: review comments. * fix: NPE in unit test. * Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPoolOptions.java Co-authored-by: Knut Olav Løite <[email protected]> * chore: fix review comments. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Knut Olav Løite <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f03ce56
commit 428e294
Showing
5 changed files
with
349 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchCreateSessionsSlowTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed 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 com.google.cloud.spanner; | ||
|
||
import static com.google.cloud.spanner.MockSpannerTestUtil.READ_ONE_KEY_VALUE_RESULTSET; | ||
import static com.google.cloud.spanner.MockSpannerTestUtil.READ_ONE_KEY_VALUE_STATEMENT; | ||
import static com.google.cloud.spanner.MockSpannerTestUtil.SELECT1; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import com.google.api.gax.grpc.testing.LocalChannelProvider; | ||
import com.google.cloud.NoCredentials; | ||
import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime; | ||
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; | ||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.common.util.concurrent.ListeningExecutorService; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import io.grpc.Server; | ||
import io.grpc.inprocess.InProcessServerBuilder; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.threeten.bp.Duration; | ||
|
||
@Category(SlowTest.class) | ||
@RunWith(JUnit4.class) | ||
public class BatchCreateSessionsSlowTest { | ||
private static final String TEST_PROJECT = "my-project"; | ||
private static final String TEST_DATABASE_ROLE = "my-role"; | ||
private static MockSpannerServiceImpl mockSpanner; | ||
private static Server server; | ||
private static LocalChannelProvider channelProvider; | ||
private Spanner spanner; | ||
|
||
@BeforeClass | ||
public static void startStaticServer() throws IOException { | ||
mockSpanner = new MockSpannerServiceImpl(); | ||
mockSpanner.setAbortProbability(0.0D); // We don't want any unpredictable aborted transactions. | ||
mockSpanner.putStatementResult( | ||
StatementResult.query(SELECT1, MockSpannerTestUtil.SELECT1_RESULTSET)); | ||
mockSpanner.putStatementResult( | ||
StatementResult.query(READ_ONE_KEY_VALUE_STATEMENT, READ_ONE_KEY_VALUE_RESULTSET)); | ||
|
||
String uniqueName = InProcessServerBuilder.generateName(); | ||
server = | ||
InProcessServerBuilder.forName(uniqueName) | ||
// We need to use a real executor for timeouts to occur. | ||
.scheduledExecutorService(new ScheduledThreadPoolExecutor(1)) | ||
.addService(mockSpanner) | ||
.build() | ||
.start(); | ||
channelProvider = LocalChannelProvider.create(uniqueName); | ||
} | ||
|
||
@AfterClass | ||
public static void stopServer() throws InterruptedException { | ||
server.shutdown(); | ||
server.awaitTermination(); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
spanner = | ||
SpannerOptions.newBuilder() | ||
.setProjectId(TEST_PROJECT) | ||
.setDatabaseRole(TEST_DATABASE_ROLE) | ||
.setChannelProvider(channelProvider) | ||
.setCredentials(NoCredentials.getInstance()) | ||
.setSessionPoolOption(SessionPoolOptions.newBuilder().setFailOnSessionLeak().build()) | ||
.build() | ||
.getService(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
mockSpanner.unfreeze(); | ||
spanner.close(); | ||
mockSpanner.reset(); | ||
mockSpanner.removeAllExecutionTimes(); | ||
} | ||
|
||
@Test | ||
public void testBatchCreateSessionsTimesOut_whenDeadlineExceeded() throws Exception { | ||
// Simulate a minimum execution time of 1000 milliseconds for the BatchCreateSessions RPC. | ||
mockSpanner.setBatchCreateSessionsExecutionTime( | ||
SimulatedExecutionTime.ofMinimumAndRandomTime(1000, 0)); | ||
SpannerOptions.Builder builder = | ||
SpannerOptions.newBuilder() | ||
.setProjectId("my-project") | ||
.setChannelProvider(channelProvider) | ||
.setCredentials(NoCredentials.getInstance()); | ||
// Set the timeout and retry settings for BatchCreateSessions to a simple | ||
// single-attempt-and-timeout after 100ms. | ||
builder | ||
.getSpannerStubSettingsBuilder() | ||
.batchCreateSessionsSettings() | ||
.setSimpleTimeoutNoRetries(Duration.ofMillis(100)); | ||
|
||
try (Spanner spanner = builder.build().getService()) { | ||
DatabaseId databaseId = DatabaseId.of("my-project", "my-instance", "my-database"); | ||
DatabaseClient client = spanner.getDatabaseClient(databaseId); | ||
|
||
ListeningExecutorService service = | ||
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1000)); | ||
List<ListenableFuture<Void>> futures = new ArrayList<>(5000); | ||
AtomicInteger counter = new AtomicInteger(); | ||
for (int i = 0; i < 5000; i++) { | ||
final int index = i; | ||
futures.add( | ||
service.submit( | ||
() -> { | ||
// The following call is non-blocking and will not generate an exception. | ||
ResultSet rs = client.singleUse().executeQuery(SELECT1); | ||
// Actually trying to get any results will cause an exception. | ||
// The DEADLINE_EXCEEDED error of the BatchCreateSessions RPC is in this case | ||
// propagated to | ||
// the application. | ||
SpannerException e = assertThrows(SpannerException.class, rs::next); | ||
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode()); | ||
System.out.printf("finished test %d\n", counter.incrementAndGet()); | ||
|
||
return null; | ||
})); | ||
} | ||
service.shutdown(); | ||
assertEquals(5000, Futures.allAsList(futures).get().size()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testBatchCreateSessionsTimesOut_whenResourceExhausted() throws Exception { | ||
// Simulate a minimum execution time of 2000 milliseconds for the BatchCreateSessions RPC. | ||
mockSpanner.setBatchCreateSessionsExecutionTime( | ||
SimulatedExecutionTime.ofMinimumAndRandomTime(2000, 0)); | ||
// Add a timeout for the max amount of time (60ms) that a request waits when a session is | ||
// unavailable. | ||
SessionPoolOptions sessionPoolOptions = | ||
SessionPoolOptions.newBuilder().setAcquireSessionTimeout(Duration.ofMillis(60)).build(); | ||
SpannerOptions.Builder builder = | ||
SpannerOptions.newBuilder() | ||
.setProjectId("my-project") | ||
.setChannelProvider(channelProvider) | ||
.setCredentials(NoCredentials.getInstance()) | ||
.setSessionPoolOption(sessionPoolOptions); | ||
// Set the timeout and retry settings for BatchCreateSessions to a simple | ||
// single-attempt-and-timeout after 1000ms. This will ensure that session acquisition timeout of | ||
// 60ms will kick for all requests before the overall request RPC timeout is breached. | ||
builder | ||
.getSpannerStubSettingsBuilder() | ||
.batchCreateSessionsSettings() | ||
.setSimpleTimeoutNoRetries(Duration.ofMillis(1000)); | ||
|
||
try (Spanner spanner = builder.build().getService()) { | ||
DatabaseId databaseId = DatabaseId.of("my-project", "my-instance", "my-database"); | ||
DatabaseClient client = spanner.getDatabaseClient(databaseId); | ||
|
||
ListeningExecutorService service = | ||
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1000)); | ||
List<ListenableFuture<Void>> futures = new ArrayList<>(5000); | ||
AtomicInteger counter = new AtomicInteger(); | ||
for (int i = 0; i < 5000; i++) { | ||
final int index = i; | ||
futures.add( | ||
service.submit( | ||
() -> { | ||
// The following call is non-blocking and will not generate an exception. | ||
ResultSet rs = client.singleUse().executeQuery(SELECT1); | ||
// Actually trying to get any results will cause an exception. | ||
// When number of requests > MAX_SESSIONS, post setAcquireSessionTimeout | ||
// a few requests will timeout with RESOURCE_EXHAUSTED error. | ||
SpannerException e = assertThrows(SpannerException.class, rs::next); | ||
assertEquals(ErrorCode.RESOURCE_EXHAUSTED, e.getErrorCode()); | ||
System.out.printf("finished test %d\n", counter.incrementAndGet()); | ||
|
||
return null; | ||
})); | ||
} | ||
service.shutdown(); | ||
assertEquals(5000, Futures.allAsList(futures).get().size()); | ||
} | ||
} | ||
} |
Oops, something went wrong.