|
| 1 | +/* |
| 2 | + * Copyright 2020 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 com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.api.gax.grpc.testing.LocalChannelProvider; |
| 22 | +import com.google.cloud.NoCredentials; |
| 23 | +import com.google.cloud.grpc.GrpcTransportOptions; |
| 24 | +import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory; |
| 25 | +import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime; |
| 26 | +import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; |
| 27 | +import com.google.cloud.spanner.TransactionRunner.TransactionCallable; |
| 28 | +import com.google.protobuf.ListValue; |
| 29 | +import com.google.spanner.v1.ResultSetMetadata; |
| 30 | +import com.google.spanner.v1.StructType; |
| 31 | +import com.google.spanner.v1.StructType.Field; |
| 32 | +import com.google.spanner.v1.TypeCode; |
| 33 | +import io.grpc.Server; |
| 34 | +import io.grpc.Status; |
| 35 | +import io.grpc.inprocess.InProcessServerBuilder; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.concurrent.Executors; |
| 38 | +import java.util.concurrent.ScheduledExecutorService; |
| 39 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | +import org.junit.After; |
| 42 | +import org.junit.AfterClass; |
| 43 | +import org.junit.Before; |
| 44 | +import org.junit.BeforeClass; |
| 45 | +import org.junit.Test; |
| 46 | +import org.junit.runner.RunWith; |
| 47 | +import org.junit.runners.JUnit4; |
| 48 | + |
| 49 | +/** |
| 50 | + * Tests that a degraded backend that can no longer create any new sessions will not cause an |
| 51 | + * application that already has a healthy session pool to stop functioning. |
| 52 | + */ |
| 53 | +@RunWith(JUnit4.class) |
| 54 | +public class BackendExhaustedTest { |
| 55 | + private static final String TEST_PROJECT = "my-project"; |
| 56 | + private static final String TEST_INSTANCE = "my-instance"; |
| 57 | + private static final String TEST_DATABASE = "my-database"; |
| 58 | + private static MockSpannerServiceImpl mockSpanner; |
| 59 | + private static Server server; |
| 60 | + private static LocalChannelProvider channelProvider; |
| 61 | + private static final Statement UPDATE_STATEMENT = |
| 62 | + Statement.of("UPDATE FOO SET BAR=1 WHERE BAZ=2"); |
| 63 | + private static final Statement INVALID_UPDATE_STATEMENT = |
| 64 | + Statement.of("UPDATE NON_EXISTENT_TABLE SET BAR=1 WHERE BAZ=2"); |
| 65 | + private static final long UPDATE_COUNT = 1L; |
| 66 | + private static final Statement SELECT1 = Statement.of("SELECT 1 AS COL1"); |
| 67 | + private static final ResultSetMetadata SELECT1_METADATA = |
| 68 | + ResultSetMetadata.newBuilder() |
| 69 | + .setRowType( |
| 70 | + StructType.newBuilder() |
| 71 | + .addFields( |
| 72 | + Field.newBuilder() |
| 73 | + .setName("COL1") |
| 74 | + .setType( |
| 75 | + com.google.spanner.v1.Type.newBuilder() |
| 76 | + .setCode(TypeCode.INT64) |
| 77 | + .build()) |
| 78 | + .build()) |
| 79 | + .build()) |
| 80 | + .build(); |
| 81 | + private static final com.google.spanner.v1.ResultSet SELECT1_RESULTSET = |
| 82 | + com.google.spanner.v1.ResultSet.newBuilder() |
| 83 | + .addRows( |
| 84 | + ListValue.newBuilder() |
| 85 | + .addValues(com.google.protobuf.Value.newBuilder().setStringValue("1").build()) |
| 86 | + .build()) |
| 87 | + .setMetadata(SELECT1_METADATA) |
| 88 | + .build(); |
| 89 | + private Spanner spanner; |
| 90 | + private DatabaseClientImpl client; |
| 91 | + |
| 92 | + @BeforeClass |
| 93 | + public static void startStaticServer() throws IOException { |
| 94 | + mockSpanner = new MockSpannerServiceImpl(); |
| 95 | + mockSpanner.setAbortProbability(0.0D); // We don't want any unpredictable aborted transactions. |
| 96 | + mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT)); |
| 97 | + mockSpanner.putStatementResult(StatementResult.query(SELECT1, SELECT1_RESULTSET)); |
| 98 | + mockSpanner.putStatementResult( |
| 99 | + StatementResult.exception( |
| 100 | + INVALID_UPDATE_STATEMENT, |
| 101 | + Status.INVALID_ARGUMENT.withDescription("invalid statement").asRuntimeException())); |
| 102 | + |
| 103 | + String uniqueName = InProcessServerBuilder.generateName(); |
| 104 | + server = |
| 105 | + InProcessServerBuilder.forName(uniqueName) |
| 106 | + // We need to use a real executor for timeouts to occur. |
| 107 | + .scheduledExecutorService(new ScheduledThreadPoolExecutor(1)) |
| 108 | + .addService(mockSpanner) |
| 109 | + .build() |
| 110 | + .start(); |
| 111 | + channelProvider = LocalChannelProvider.create(uniqueName); |
| 112 | + } |
| 113 | + |
| 114 | + @AfterClass |
| 115 | + public static void stopServer() throws InterruptedException { |
| 116 | + // Force a shutdown as there are still requests stuck in the server. |
| 117 | + server.shutdownNow(); |
| 118 | + server.awaitTermination(); |
| 119 | + } |
| 120 | + |
| 121 | + @Before |
| 122 | + public void setUp() throws Exception { |
| 123 | + SpannerOptions options = |
| 124 | + SpannerOptions.newBuilder() |
| 125 | + .setProjectId(TEST_PROJECT) |
| 126 | + .setChannelProvider(channelProvider) |
| 127 | + .setCredentials(NoCredentials.getInstance()) |
| 128 | + .build(); |
| 129 | + ExecutorFactory<ScheduledExecutorService> executorFactory = |
| 130 | + ((GrpcTransportOptions) options.getTransportOptions()).getExecutorFactory(); |
| 131 | + ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) executorFactory.get(); |
| 132 | + options = |
| 133 | + options |
| 134 | + .toBuilder() |
| 135 | + .setSessionPoolOption( |
| 136 | + SessionPoolOptions.newBuilder() |
| 137 | + .setMinSessions(executor.getCorePoolSize()) |
| 138 | + .setMaxSessions(executor.getCorePoolSize() * 3) |
| 139 | + .setWriteSessionsFraction(0.0f) |
| 140 | + .build()) |
| 141 | + .build(); |
| 142 | + executorFactory.release(executor); |
| 143 | + |
| 144 | + spanner = options.getService(); |
| 145 | + client = |
| 146 | + (DatabaseClientImpl) |
| 147 | + spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE)); |
| 148 | + // Wait until the session pool has initialized. |
| 149 | + while (client.pool.getNumberOfSessionsInPool() |
| 150 | + < spanner.getOptions().getSessionPoolOptions().getMinSessions()) { |
| 151 | + Thread.sleep(1L); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @After |
| 156 | + public void tearDown() throws Exception { |
| 157 | + mockSpanner.reset(); |
| 158 | + mockSpanner.removeAllExecutionTimes(); |
| 159 | + // This test case force-closes the Spanner instance as it would otherwise wait |
| 160 | + // forever on the BatchCreateSessions requests that are 'stuck'. |
| 161 | + try { |
| 162 | + ((SpannerImpl) spanner).close(100L, TimeUnit.MILLISECONDS); |
| 163 | + } catch (SpannerException e) { |
| 164 | + // ignore any errors during close as they are expected. |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void test() throws Exception { |
| 170 | + // Simulate very heavy load on the server by effectively stopping session creation. |
| 171 | + mockSpanner.setBatchCreateSessionsExecutionTime( |
| 172 | + SimulatedExecutionTime.ofMinimumAndRandomTime(Integer.MAX_VALUE, 0)); |
| 173 | + // Create an executor that can handle twice as many requests as the minimum number of sessions |
| 174 | + // in the pool and then start that many read requests. That will initiate the creation of |
| 175 | + // additional sessions. |
| 176 | + ScheduledExecutorService executor = |
| 177 | + Executors.newScheduledThreadPool( |
| 178 | + spanner.getOptions().getSessionPoolOptions().getMinSessions() * 2); |
| 179 | + // Also temporarily freeze the server to ensure that the requests that can be served will |
| 180 | + // continue to be in-flight and keep the sessions in the pool checked out. |
| 181 | + mockSpanner.freeze(); |
| 182 | + for (int i = 0; i < spanner.getOptions().getSessionPoolOptions().getMinSessions() * 2; i++) { |
| 183 | + executor.submit(new ReadRunnable()); |
| 184 | + } |
| 185 | + // Now schedule as many write requests as there can be sessions in the pool. |
| 186 | + for (int i = 0; i < spanner.getOptions().getSessionPoolOptions().getMaxSessions(); i++) { |
| 187 | + executor.submit(new WriteRunnable()); |
| 188 | + } |
| 189 | + // Now unfreeze the server and verify that all requests can be served using the sessions that |
| 190 | + // were already present in the pool. |
| 191 | + mockSpanner.unfreeze(); |
| 192 | + executor.shutdown(); |
| 193 | + assertThat(executor.awaitTermination(10, TimeUnit.SECONDS)).isTrue(); |
| 194 | + } |
| 195 | + |
| 196 | + private final class ReadRunnable implements Runnable { |
| 197 | + @Override |
| 198 | + public void run() { |
| 199 | + try (ResultSet rs = client.singleUse().executeQuery(SELECT1)) { |
| 200 | + while (rs.next()) {} |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private final class WriteRunnable implements Runnable { |
| 206 | + @Override |
| 207 | + public void run() { |
| 208 | + TransactionRunner runner = client.readWriteTransaction(); |
| 209 | + runner.run( |
| 210 | + new TransactionCallable<Long>() { |
| 211 | + @Override |
| 212 | + public Long run(TransactionContext transaction) throws Exception { |
| 213 | + return transaction.executeUpdate(UPDATE_STATEMENT); |
| 214 | + } |
| 215 | + }); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments