Skip to content

Commit ab9f3c5

Browse files
committed
fix: update max_in_use_session at 10 mins interval
1 parent 8d295c4 commit ab9f3c5

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,7 @@ boolean isClosed() {
20482048

20492049
// Does various pool maintenance activities.
20502050
void maintainPool() {
2051+
Instant currTime = clock.instant();
20512052
synchronized (lock) {
20522053
if (SessionPool.this.isClosed()) {
20532054
return;
@@ -2059,8 +2060,14 @@ void maintainPool() {
20592060
/ (loopFrequency / 1000L);
20602061
}
20612062
this.prevNumSessionsAcquired = SessionPool.this.numSessionsAcquired;
2063+
2064+
// Reset the start time for recording the maximum number of sessions in the pool
2065+
if (currTime.isAfter(SessionPool.this.lastResetTime.plus(Duration.ofMinutes(10)))) {
2066+
SessionPool.this.maxSessionsInUse = SessionPool.this.numSessionsInUse;
2067+
SessionPool.this.lastResetTime = currTime;
2068+
}
20622069
}
2063-
Instant currTime = clock.instant();
2070+
20642071
removeIdleSessions(currTime);
20652072
// Now go over all the remaining sessions and see if they need to be kept alive explicitly.
20662073
keepAliveSessions(currTime);
@@ -2309,6 +2316,9 @@ enum Position {
23092316
@GuardedBy("lock")
23102317
private int maxSessionsInUse = 0;
23112318

2319+
@GuardedBy("lock")
2320+
private Instant lastResetTime = Clock.INSTANCE.instant();
2321+
23122322
@GuardedBy("lock")
23132323
private long numSessionsAcquired = 0;
23142324

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolTest.java

+48
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,54 @@ public void testWaitOnMinSessionsThrowsExceptionWhenTimeoutIsReached() {
22472247
pool.maybeWaitOnMinSessions();
22482248
}
22492249

2250+
@Test
2251+
public void reset_maxSessionsInUse() {
2252+
Clock clock = mock(Clock.class);
2253+
when(clock.instant()).thenReturn(Instant.now());
2254+
options =
2255+
SessionPoolOptions.newBuilder()
2256+
.setMinSessions(1)
2257+
.setMaxSessions(3)
2258+
.setIncStep(1)
2259+
.setMaxIdleSessions(0)
2260+
.setPoolMaintainerClock(clock)
2261+
.build();
2262+
setupForLongRunningTransactionsCleanup(options);
2263+
2264+
pool = createPool(clock);
2265+
// Make sure pool has been initialized
2266+
pool.getSession().close();
2267+
2268+
// All 3 sessions used. 100% of pool utilised.
2269+
PooledSessionFuture readSession1 = pool.getSession();
2270+
PooledSessionFuture readSession2 = pool.getSession();
2271+
PooledSessionFuture readSession3 = pool.getSession();
2272+
2273+
// complete the async tasks
2274+
readSession1.get().setEligibleForLongRunning(false);
2275+
readSession2.get().setEligibleForLongRunning(false);
2276+
readSession3.get().setEligibleForLongRunning(true);
2277+
2278+
assertEquals(3, pool.getMaxSessionsInUse());
2279+
assertEquals(3, pool.getNumberOfSessionsInUse());
2280+
2281+
// Release 1 session
2282+
readSession1.get().close();
2283+
2284+
// Verify that numSessionsInUse reduces to 2 while maxSessionsInUse remain 3
2285+
assertEquals(3, pool.getMaxSessionsInUse());
2286+
assertEquals(2, pool.getNumberOfSessionsInUse());
2287+
2288+
// ensure that the lastResetTime for maxSessionsInUse > 10 minutes
2289+
when(clock.instant()).thenReturn(Instant.now().plus(11, ChronoUnit.MINUTES));
2290+
2291+
pool.poolMaintainer.maintainPool();
2292+
2293+
// Verify that maxSessionsInUse is reset to numSessionsInUse
2294+
assertEquals(2, pool.getMaxSessionsInUse());
2295+
assertEquals(2, pool.getNumberOfSessionsInUse());
2296+
}
2297+
22502298
private void mockKeepAlive(ReadContext context) {
22512299
ResultSet resultSet = mock(ResultSet.class);
22522300
when(resultSet.next()).thenReturn(true, false);

0 commit comments

Comments
 (0)