|
| 1 | +/* |
| 2 | + * Copyright 2019 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.it; |
| 18 | + |
| 19 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 20 | +import static org.hamcrest.CoreMatchers.is; |
| 21 | +import static org.junit.Assert.assertThat; |
| 22 | + |
| 23 | +import com.google.cloud.spanner.Database; |
| 24 | +import com.google.cloud.spanner.DatabaseAdminClient; |
| 25 | +import com.google.cloud.spanner.DatabaseClient; |
| 26 | +import com.google.cloud.spanner.InstanceAdminClient; |
| 27 | +import com.google.cloud.spanner.IntegrationTest; |
| 28 | +import com.google.cloud.spanner.IntegrationTestEnv; |
| 29 | +import com.google.cloud.spanner.ResultSet; |
| 30 | +import com.google.cloud.spanner.Spanner; |
| 31 | +import com.google.cloud.spanner.SpannerOptions; |
| 32 | +import com.google.cloud.spanner.Statement; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
| 35 | +import java.util.regex.Pattern; |
| 36 | +import org.junit.After; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.ClassRule; |
| 39 | +import org.junit.Rule; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.experimental.categories.Category; |
| 42 | +import org.junit.rules.ExpectedException; |
| 43 | +import org.junit.runner.RunWith; |
| 44 | +import org.junit.runners.JUnit4; |
| 45 | + |
| 46 | +@Category(IntegrationTest.class) |
| 47 | +@RunWith(JUnit4.class) |
| 48 | +public class ITSpannerOptionsTest { |
| 49 | + @ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv(); |
| 50 | + @Rule public ExpectedException expectedException = ExpectedException.none(); |
| 51 | + private static Database db; |
| 52 | + |
| 53 | + @Before |
| 54 | + public void setUp() throws Exception { |
| 55 | + db = env.getTestHelper().createTestDatabase(); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public void tearDown() throws Exception { |
| 60 | + db.drop(); |
| 61 | + } |
| 62 | + |
| 63 | + private static final int NUMBER_OF_TEST_RUNS = 2; |
| 64 | + private static final int DEFAULT_NUM_CHANNELS = 4; |
| 65 | + private static final int NUM_THREADS_PER_CHANNEL = 4; |
| 66 | + private static final String SPANNER_THREAD_NAME = "Cloud-Spanner-TransportChannel"; |
| 67 | + private static final String THREAD_PATTERN = "%s-[0-9]+"; |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testCloseAllThreadsWhenClosingSpanner() throws InterruptedException { |
| 71 | + // The IT environment has already started some worker threads. |
| 72 | + int baseThreadCount = getNumberOfThreadsWithName(SPANNER_THREAD_NAME); |
| 73 | + for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) { |
| 74 | + assertThat(getNumberOfThreadsWithName(SPANNER_THREAD_NAME), is(equalTo(baseThreadCount))); |
| 75 | + // Create Spanner instance. |
| 76 | + // We make a copy of the options instance, as SpannerOptions caches any service object |
| 77 | + // that has been handed out. |
| 78 | + SpannerOptions options = env.getTestHelper().getOptions().toBuilder().build(); |
| 79 | + Spanner spanner = options.getService(); |
| 80 | + // Get a database client and do a query. This should initiate threads for the Spanner service. |
| 81 | + DatabaseClient client = spanner.getDatabaseClient(db.getId()); |
| 82 | + List<ResultSet> resultSets = new ArrayList<>(); |
| 83 | + // SpannerStub affiliates a channel with a session, so we need to use multiple sessions |
| 84 | + // to ensure we also hit multiple channels. |
| 85 | + for (int i2 = 0; i2 < options.getSessionPoolOptions().getMaxSessions(); i2++) { |
| 86 | + ResultSet rs = |
| 87 | + client |
| 88 | + .singleUse() |
| 89 | + .executeQuery(Statement.of("SELECT 1 AS COL1 UNION ALL SELECT 2 AS COL2")); |
| 90 | + // Execute ResultSet#next() to send the query to Spanner. |
| 91 | + rs.next(); |
| 92 | + // Delay closing the result set in order to force the use of multiple sessions. |
| 93 | + // As each session is linked to one transport channel, using multiple different |
| 94 | + // sessions should initialize multiple transport channels. |
| 95 | + resultSets.add(rs); |
| 96 | + // Check whether the number of expected threads has been reached. |
| 97 | + if (getNumberOfThreadsWithName(SPANNER_THREAD_NAME) |
| 98 | + == DEFAULT_NUM_CHANNELS * NUM_THREADS_PER_CHANNEL + baseThreadCount) { |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + for (ResultSet rs : resultSets) { |
| 103 | + rs.close(); |
| 104 | + } |
| 105 | + // Check the number of threads after the query. Doing a request should initialize a thread |
| 106 | + // pool for the underlying SpannerClient. |
| 107 | + assertThat( |
| 108 | + getNumberOfThreadsWithName(SPANNER_THREAD_NAME), |
| 109 | + is(equalTo(DEFAULT_NUM_CHANNELS * NUM_THREADS_PER_CHANNEL + baseThreadCount))); |
| 110 | + |
| 111 | + // Then do a request to the InstanceAdmin service and check the number of threads. |
| 112 | + // Doing a request should initialize a thread pool for the underlying InstanceAdminClient. |
| 113 | + for (int i2 = 0; i2 < DEFAULT_NUM_CHANNELS * 2; i2++) { |
| 114 | + InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient(); |
| 115 | + instanceAdminClient.listInstances(); |
| 116 | + } |
| 117 | + assertThat( |
| 118 | + getNumberOfThreadsWithName(SPANNER_THREAD_NAME), |
| 119 | + is(equalTo(2 * DEFAULT_NUM_CHANNELS * NUM_THREADS_PER_CHANNEL + baseThreadCount))); |
| 120 | + |
| 121 | + // Then do a request to the DatabaseAdmin service and check the number of threads. |
| 122 | + // Doing a request should initialize a thread pool for the underlying DatabaseAdminClient. |
| 123 | + for (int i2 = 0; i2 < DEFAULT_NUM_CHANNELS * 2; i2++) { |
| 124 | + DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient(); |
| 125 | + databaseAdminClient.listDatabases(db.getId().getInstanceId().getInstance()); |
| 126 | + } |
| 127 | + assertThat( |
| 128 | + getNumberOfThreadsWithName(SPANNER_THREAD_NAME), |
| 129 | + is(equalTo(3 * DEFAULT_NUM_CHANNELS * NUM_THREADS_PER_CHANNEL + baseThreadCount))); |
| 130 | + |
| 131 | + // Now close the Spanner instance and check whether the threads are shutdown or not. |
| 132 | + spanner.close(); |
| 133 | + // Wait a little to allow the threads to actually shutdown. |
| 134 | + Thread.sleep(200L); |
| 135 | + assertThat(getNumberOfThreadsWithName(SPANNER_THREAD_NAME), is(equalTo(baseThreadCount))); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testMultipleSpannersFromSameSpannerOptions() throws InterruptedException { |
| 141 | + int baseThreadCount = getNumberOfThreadsWithName(SPANNER_THREAD_NAME); |
| 142 | + SpannerOptions options = env.getTestHelper().getOptions().toBuilder().build(); |
| 143 | + try (Spanner spanner1 = options.getService()) { |
| 144 | + // Having both in the try-with-resources block is not possible, as it is the same instance. |
| 145 | + // One will be closed before the other, and the closing of the second instance would fail. |
| 146 | + Spanner spanner2 = options.getService(); |
| 147 | + assertThat(spanner1 == spanner2, is(true)); |
| 148 | + DatabaseClient client1 = spanner1.getDatabaseClient(db.getId()); |
| 149 | + DatabaseClient client2 = spanner2.getDatabaseClient(db.getId()); |
| 150 | + assertThat(client1 == client2, is(true)); |
| 151 | + try (ResultSet rs1 = |
| 152 | + client1 |
| 153 | + .singleUse() |
| 154 | + .executeQuery(Statement.of("SELECT 1 AS COL1 UNION ALL SELECT 2 AS COL2")); |
| 155 | + ResultSet rs2 = |
| 156 | + client2 |
| 157 | + .singleUse() |
| 158 | + .executeQuery(Statement.of("SELECT 1 AS COL1 UNION ALL SELECT 2 AS COL2")); ) { |
| 159 | + while (rs1.next() && rs2.next()) { |
| 160 | + // Do nothing, just consume the result sets. |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + Thread.sleep(200L); |
| 165 | + assertThat(getNumberOfThreadsWithName(SPANNER_THREAD_NAME), is(equalTo(baseThreadCount))); |
| 166 | + } |
| 167 | + |
| 168 | + private int getNumberOfThreadsWithName(String serviceName) { |
| 169 | + Pattern pattern = Pattern.compile(String.format(THREAD_PATTERN, serviceName)); |
| 170 | + ThreadGroup group = Thread.currentThread().getThreadGroup(); |
| 171 | + while (group.getParent() != null) { |
| 172 | + group = group.getParent(); |
| 173 | + } |
| 174 | + Thread[] threads = new Thread[100 * NUMBER_OF_TEST_RUNS]; |
| 175 | + int numberOfThreads = group.enumerate(threads); |
| 176 | + int res = 0; |
| 177 | + for (int i = 0; i < numberOfThreads; i++) { |
| 178 | + if (pattern.matcher(threads[i].getName()).matches()) { |
| 179 | + res++; |
| 180 | + } |
| 181 | + } |
| 182 | + return res; |
| 183 | + } |
| 184 | +} |
0 commit comments