-
Notifications
You must be signed in to change notification settings - Fork 101
/
SessionPoolMaintainerTest.java
327 lines (302 loc) · 14 KB
/
SessionPoolMaintainerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright 2020 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.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import com.google.cloud.spanner.SessionClient.SessionConsumer;
import com.google.cloud.spanner.SessionPool.PooledSession;
import com.google.cloud.spanner.SessionPool.PooledSessionFuture;
import com.google.cloud.spanner.SessionPool.Position;
import com.google.cloud.spanner.SessionPool.SessionConsumerImpl;
import io.opencensus.trace.Tracing;
import io.opentelemetry.api.OpenTelemetry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.Mockito;
@RunWith(JUnit4.class)
public class SessionPoolMaintainerTest extends BaseSessionPoolTest {
private ExecutorService executor = Executors.newSingleThreadExecutor();
private @Mock SpannerImpl client;
private @Mock SessionClient sessionClient;
private @Mock SpannerOptions spannerOptions;
private DatabaseId db = DatabaseId.of("projects/p/instances/i/databases/unused");
private SessionPoolOptions options;
private FakeClock clock = new FakeClock();
private List<PooledSession> idledSessions = new ArrayList<>();
private Map<String, Integer> pingedSessions = new HashMap<>();
@Before
public void setUp() {
initMocks(this);
when(client.getOptions()).thenReturn(spannerOptions);
when(client.getSessionClient(db)).thenReturn(sessionClient);
when(sessionClient.getSpanner()).thenReturn(client);
when(spannerOptions.getNumChannels()).thenReturn(4);
when(spannerOptions.getDatabaseRole()).thenReturn("role");
setupMockSessionCreation();
options =
SessionPoolOptions.newBuilder()
.setMinSessions(1)
.setMaxIdleSessions(1)
.setMaxSessions(5)
.setIncStep(1)
.setKeepAliveIntervalMinutes(2)
.build();
idledSessions.clear();
pingedSessions.clear();
}
private void setupMockSessionCreation() {
doAnswer(
invocation -> {
executor.submit(
() -> {
int sessionCount = invocation.getArgument(0, Integer.class);
SessionConsumerImpl consumer =
invocation.getArgument(2, SessionConsumerImpl.class);
for (int i = 0; i < sessionCount; i++) {
ReadContext mockContext = mock(ReadContext.class);
consumer.onSessionReady(
setupMockSession(buildMockSession(mockContext), mockContext));
}
});
return null;
})
.when(sessionClient)
.asyncBatchCreateSessions(
Mockito.anyInt(), Mockito.anyBoolean(), any(SessionConsumer.class));
}
private SessionImpl setupMockSession(final SessionImpl session, final ReadContext mockContext) {
final ResultSet mockResult = mock(ResultSet.class);
when(mockContext.executeQuery(any(Statement.class)))
.thenAnswer(
invocation -> {
Integer currentValue = pingedSessions.get(session.getName());
if (currentValue == null) {
currentValue = 0;
}
pingedSessions.put(session.getName(), ++currentValue);
return mockResult;
});
when(mockResult.next()).thenReturn(true);
return session;
}
private SessionPool createPool() throws Exception {
// Allow sessions to be added to the head of the pool in all cases in this test, as it is
// otherwise impossible to know which session exactly is getting pinged at what point in time.
SessionPool pool =
SessionPool.createPool(
options,
new TestExecutorFactory(),
client.getSessionClient(db),
clock,
Position.FIRST,
new TraceWrapper(Tracing.getTracer(), OpenTelemetry.noop().getTracer("")),
OpenTelemetry.noop());
pool.idleSessionRemovedListener =
input -> {
idledSessions.add(input);
return null;
};
// Wait until pool has initialized.
while (pool.totalSessions() < options.getMinSessions()) {
Thread.sleep(1L);
}
return pool;
}
@Test
public void testKeepAlive() throws Exception {
SessionPool pool = createPool();
assertThat(pingedSessions).isEmpty();
// Run one maintenance loop. No sessions should get a keep-alive ping.
runMaintenanceLoop(clock, pool, 1);
assertThat(pingedSessions).isEmpty();
// Checkout two sessions and do a maintenance loop. Still no sessions should be getting any
// pings.
Session session1 = pool.getSession();
Session session2 = pool.getSession();
runMaintenanceLoop(clock, pool, 1);
assertThat(pingedSessions).isEmpty();
// Check the sessions back into the pool and do a maintenance loop.
session2.close();
session1.close();
runMaintenanceLoop(clock, pool, 1);
assertThat(pingedSessions).isEmpty();
// Now advance the time enough for both sessions in the pool to be idled. Then do one
// maintenance loop. This should cause the last session to have been checked back into the pool
// to get a ping, but not the second session.
clock.currentTimeMillis.addAndGet(
TimeUnit.MINUTES.toMillis(options.getKeepAliveIntervalMinutes()) + 1);
runMaintenanceLoop(clock, pool, 1);
assertThat(pingedSessions).containsExactly(session1.getName(), 1);
// Do another maintenance loop. This should cause the other session to also get a ping.
runMaintenanceLoop(clock, pool, 1);
assertThat(pingedSessions).containsExactly(session1.getName(), 1, session2.getName(), 1);
// Now check out three sessions so the pool will create an additional session. The pool will
// only keep 2 sessions alive, as that is the setting for MinSessions.
Session session3 = pool.getSession();
Session session4 = pool.getSession();
Session session5 = pool.getSession();
// Pinging a session will put it at the back of the pool. A session that needed a ping to be
// kept alive is not one that should be preferred for use. This means that session2 is the last
// session in the pool, and session1 the second-to-last.
assertEquals(session1.getName(), session3.getName());
assertEquals(session2.getName(), session4.getName());
session5.close();
session4.close();
session3.close();
// Advance the clock to force pings for the sessions in the pool and do three maintenance loops.
// This should ping the sessions in the following order:
// 1. session3 (=session1)
// 2. session4 (=session2)
// The pinged sessions already contains: {session1: 1, session2: 1}
// Note that the pool only pings up to MinSessions sessions.
clock.currentTimeMillis.addAndGet(
TimeUnit.MINUTES.toMillis(options.getKeepAliveIntervalMinutes()) + 1);
runMaintenanceLoop(clock, pool, 3);
assertThat(pingedSessions).containsExactly(session1.getName(), 2, session2.getName(), 2);
// Advance the clock to idle all sessions in the pool again and then check out one session. This
// should cause only one session to get a ping.
clock.currentTimeMillis.addAndGet(
TimeUnit.MINUTES.toMillis(options.getKeepAliveIntervalMinutes()) + 1);
// This will be session1, as all sessions were pinged in the previous 3 maintenance loops, and
// this will have brought session1 back to the front of the pool.
Session session6 = pool.getSession();
// The session that was first in the pool now is equal to the initial first session as each full
// round of pings will swap the order of the first MinSessions sessions in the pool.
assertThat(session6.getName()).isEqualTo(session1.getName());
runMaintenanceLoop(clock, pool, 3);
// Running 3 cycles will only ping the 2 sessions in the pool once.
assertThat(pool.totalSessions()).isEqualTo(3);
assertThat(pingedSessions).containsExactly(session1.getName(), 2, session2.getName(), 3);
// Update the last use date and release the session to the pool and do another maintenance
// cycle. This should not ping any sessions.
((PooledSessionFuture) session6).get().markUsed();
session6.close();
runMaintenanceLoop(clock, pool, 3);
assertThat(pingedSessions).containsExactly(session1.getName(), 2, session2.getName(), 3);
// Now check out 3 sessions again and make sure the 'extra' session is checked in last. That
// will make it eligible for pings.
Session session7 = pool.getSession();
Session session8 = pool.getSession();
Session session9 = pool.getSession();
assertThat(session7.getName()).isEqualTo(session1.getName());
assertThat(session8.getName()).isEqualTo(session2.getName());
assertThat(session9.getName()).isEqualTo(session5.getName());
session7.close();
session8.close();
session9.close();
clock.currentTimeMillis.addAndGet(
TimeUnit.MINUTES.toMillis(options.getKeepAliveIntervalMinutes()) + 1);
runMaintenanceLoop(clock, pool, 3);
// session1 will not get a ping this time, as it was checked in first and is now the last
// session in the pool.
assertThat(pingedSessions)
.containsExactly(session1.getName(), 2, session2.getName(), 4, session5.getName(), 1);
}
@Test
public void testIdleSessions() throws Exception {
SessionPool pool = createPool();
long loopsToIdleSessions =
Double.valueOf(
Math.ceil(
(double) options.getRemoveInactiveSessionAfter().toMillis()
/ pool.poolMaintainer.loopFrequency))
.longValue()
+ 2L;
assertThat(idledSessions).isEmpty();
// Run one maintenance loop. No sessions should be removed from the pool.
runMaintenanceLoop(clock, pool, 1);
assertThat(idledSessions).isEmpty();
// Checkout two sessions and do a maintenance loop. Still no sessions should be removed.
Session session1 = pool.getSession();
Session session2 = pool.getSession();
runMaintenanceLoop(clock, pool, 1);
assertThat(idledSessions).isEmpty();
// Check the sessions back into the pool and do a maintenance loop.
session2.close();
session1.close();
runMaintenanceLoop(clock, pool, 1);
assertThat(idledSessions).isEmpty();
// Now advance the time enough for both sessions in the pool to be idled. Both sessions should
// be kept alive by the maintainer and remain in the pool.
runMaintenanceLoop(clock, pool, loopsToIdleSessions);
assertThat(idledSessions).isEmpty();
// Now check out three sessions so the pool will create an additional session. The pool will
// only keep 2 sessions alive, as that is the setting for MinSessions.
Session session3 = pool.getSession().get();
Session session4 = pool.getSession().get();
Session session5 = pool.getSession().get();
// Note that pinging sessions does not change the order of the pool. This means that session2
// is still the last session in the pool.
assertThat(session3.getName()).isEqualTo(session1.getName());
assertThat(session4.getName()).isEqualTo(session2.getName());
session5.close();
session4.close();
session3.close();
// Advance the clock to idle sessions. The pool will keep session4 and session3 alive, session5
// will be idled and removed.
runMaintenanceLoop(clock, pool, loopsToIdleSessions);
assertThat(idledSessions).containsExactly(session5);
assertThat(pool.totalSessions()).isEqualTo(2);
// Check out three sessions again and keep one session checked out.
Session session6 = pool.getSession().get();
Session session7 = pool.getSession().get();
Session session8 = pool.getSession().get();
session8.close();
session7.close();
// Now advance the clock to idle sessions. This should remove session8 from the pool.
runMaintenanceLoop(clock, pool, loopsToIdleSessions);
assertThat(idledSessions).containsExactly(session5, session8);
assertThat(pool.totalSessions()).isEqualTo(2);
((PooledSession) session6).markUsed();
session6.close();
// Check out three sessions and keep them all checked out. No sessions should be removed from
// the pool.
Session session9 = pool.getSession().get();
Session session10 = pool.getSession().get();
Session session11 = pool.getSession().get();
runMaintenanceLoop(clock, pool, loopsToIdleSessions);
assertThat(idledSessions).containsExactly(session5, session8);
assertThat(pool.totalSessions()).isEqualTo(3);
// Return the sessions to the pool. As they have not been used, they are all into idle time.
// Running the maintainer will now remove all the sessions from the pool and then start the
// replenish method.
session9.close();
session10.close();
session11.close();
runMaintenanceLoop(clock, pool, 1);
assertThat(idledSessions).containsExactly(session5, session8, session9, session10, session11);
// Check that the pool is replenished.
while (pool.totalSessions() < options.getMinSessions()) {
Thread.sleep(1L);
}
assertThat(pool.totalSessions()).isEqualTo(options.getMinSessions());
}
}