use of com.google.cloud.spanner.MetricRegistryTestUtils.PointWithFunction in project java-spanner by googleapis.
the class SessionPoolTest method testSessionMetrics.
@Test
public void testSessionMetrics() throws Exception {
// Create a session pool with max 2 session and a low timeout for waiting for a session.
options = SessionPoolOptions.newBuilder().setMinSessions(1).setMaxSessions(2).setMaxIdleSessions(0).setInitialWaitForSessionTimeoutMillis(50L).build();
FakeClock clock = new FakeClock();
clock.currentTimeMillis = System.currentTimeMillis();
FakeMetricRegistry metricRegistry = new FakeMetricRegistry();
List<LabelValue> labelValues = Arrays.asList(LabelValue.create("client1"), LabelValue.create("database1"), LabelValue.create("instance1"), LabelValue.create("1.0.0"));
setupMockSessionCreation();
pool = createPool(clock, metricRegistry, labelValues);
PooledSessionFuture session1 = pool.getSession();
PooledSessionFuture session2 = pool.getSession();
session1.get();
session2.get();
MetricsRecord record = metricRegistry.pollRecord();
assertThat(record.getMetrics().size()).isEqualTo(6);
List<PointWithFunction> maxInUseSessions = record.getMetrics().get(MetricRegistryConstants.MAX_IN_USE_SESSIONS);
assertThat(maxInUseSessions.size()).isEqualTo(1);
assertThat(maxInUseSessions.get(0).value()).isEqualTo(2L);
assertThat(maxInUseSessions.get(0).keys()).isEqualTo(SPANNER_LABEL_KEYS);
assertThat(maxInUseSessions.get(0).values()).isEqualTo(labelValues);
List<PointWithFunction> getSessionsTimeouts = record.getMetrics().get(MetricRegistryConstants.GET_SESSION_TIMEOUTS);
assertThat(getSessionsTimeouts.size()).isEqualTo(1);
assertThat(getSessionsTimeouts.get(0).value()).isAtMost(1L);
assertThat(getSessionsTimeouts.get(0).keys()).isEqualTo(SPANNER_LABEL_KEYS);
assertThat(getSessionsTimeouts.get(0).values()).isEqualTo(labelValues);
List<PointWithFunction> numAcquiredSessions = record.getMetrics().get(MetricRegistryConstants.NUM_ACQUIRED_SESSIONS);
assertThat(numAcquiredSessions.size()).isEqualTo(1);
assertThat(numAcquiredSessions.get(0).value()).isEqualTo(2L);
assertThat(numAcquiredSessions.get(0).keys()).isEqualTo(SPANNER_LABEL_KEYS);
assertThat(numAcquiredSessions.get(0).values()).isEqualTo(labelValues);
List<PointWithFunction> numReleasedSessions = record.getMetrics().get(MetricRegistryConstants.NUM_RELEASED_SESSIONS);
assertThat(numReleasedSessions.size()).isEqualTo(1);
assertThat(numReleasedSessions.get(0).value()).isEqualTo(0);
assertThat(numReleasedSessions.get(0).keys()).isEqualTo(SPANNER_LABEL_KEYS);
assertThat(numReleasedSessions.get(0).values()).isEqualTo(labelValues);
List<PointWithFunction> maxAllowedSessions = record.getMetrics().get(MetricRegistryConstants.MAX_ALLOWED_SESSIONS);
assertThat(maxAllowedSessions.size()).isEqualTo(1);
assertThat(maxAllowedSessions.get(0).value()).isEqualTo(options.getMaxSessions());
assertThat(maxAllowedSessions.get(0).keys()).isEqualTo(SPANNER_LABEL_KEYS);
assertThat(maxAllowedSessions.get(0).values()).isEqualTo(labelValues);
List<PointWithFunction> numSessionsInPool = record.getMetrics().get(MetricRegistryConstants.NUM_SESSIONS_IN_POOL);
assertThat(numSessionsInPool.size()).isEqualTo(4);
PointWithFunction beingPrepared = numSessionsInPool.get(0);
List<LabelValue> labelValuesWithBeingPreparedType = new ArrayList<>(labelValues);
labelValuesWithBeingPreparedType.add(NUM_SESSIONS_BEING_PREPARED);
assertThat(beingPrepared.value()).isEqualTo(0L);
assertThat(beingPrepared.keys()).isEqualTo(SPANNER_LABEL_KEYS_WITH_TYPE);
assertThat(beingPrepared.values()).isEqualTo(labelValuesWithBeingPreparedType);
PointWithFunction numSessionsInUse = numSessionsInPool.get(1);
List<LabelValue> labelValuesWithInUseType = new ArrayList<>(labelValues);
labelValuesWithInUseType.add(NUM_IN_USE_SESSIONS);
assertThat(numSessionsInUse.value()).isEqualTo(2L);
assertThat(numSessionsInUse.keys()).isEqualTo(SPANNER_LABEL_KEYS_WITH_TYPE);
assertThat(numSessionsInUse.values()).isEqualTo(labelValuesWithInUseType);
PointWithFunction readSessions = numSessionsInPool.get(2);
List<LabelValue> labelValuesWithReadType = new ArrayList<>(labelValues);
labelValuesWithReadType.add(NUM_READ_SESSIONS);
assertThat(readSessions.value()).isEqualTo(0L);
assertThat(readSessions.keys()).isEqualTo(SPANNER_LABEL_KEYS_WITH_TYPE);
assertThat(readSessions.values()).isEqualTo(labelValuesWithReadType);
PointWithFunction writePreparedSessions = numSessionsInPool.get(3);
List<LabelValue> labelValuesWithWriteType = new ArrayList<>(labelValues);
labelValuesWithWriteType.add(NUM_WRITE_SESSIONS);
assertThat(writePreparedSessions.value()).isEqualTo(0L);
assertThat(writePreparedSessions.keys()).isEqualTo(SPANNER_LABEL_KEYS_WITH_TYPE);
assertThat(writePreparedSessions.values()).isEqualTo(labelValuesWithWriteType);
final CountDownLatch latch = new CountDownLatch(1);
// Try asynchronously to take another session. This attempt should time out.
Future<Void> fut = executor.submit(() -> {
latch.countDown();
Session session = pool.getSession();
session.close();
return null;
});
// Wait until the background thread is actually waiting for a session.
latch.await();
// Wait until the request has timed out.
int waitCount = 0;
while (pool.getNumWaiterTimeouts() == 0L && waitCount < 1000) {
Thread.sleep(5L);
waitCount++;
}
// Return the checked out session to the pool so the async request will get a session and
// finish.
session2.close();
// Verify that the async request also succeeds.
fut.get(10L, TimeUnit.SECONDS);
executor.shutdown();
session1.close();
numAcquiredSessions = record.getMetrics().get(MetricRegistryConstants.NUM_ACQUIRED_SESSIONS);
assertThat(numAcquiredSessions.size()).isEqualTo(1);
assertThat(numAcquiredSessions.get(0).value()).isEqualTo(3L);
numReleasedSessions = record.getMetrics().get(MetricRegistryConstants.NUM_RELEASED_SESSIONS);
assertThat(numReleasedSessions.size()).isEqualTo(1);
assertThat(numReleasedSessions.get(0).value()).isEqualTo(3L);
maxInUseSessions = record.getMetrics().get(MetricRegistryConstants.MAX_IN_USE_SESSIONS);
assertThat(maxInUseSessions.size()).isEqualTo(1);
assertThat(maxInUseSessions.get(0).value()).isEqualTo(2L);
numSessionsInPool = record.getMetrics().get(MetricRegistryConstants.NUM_SESSIONS_IN_POOL);
assertThat(numSessionsInPool.size()).isEqualTo(4);
beingPrepared = numSessionsInPool.get(0);
assertThat(beingPrepared.value()).isEqualTo(0L);
numSessionsInUse = numSessionsInPool.get(1);
assertThat(numSessionsInUse.value()).isEqualTo(0L);
readSessions = numSessionsInPool.get(2);
assertThat(readSessions.value()).isEqualTo(2L);
writePreparedSessions = numSessionsInPool.get(3);
assertThat(writePreparedSessions.value()).isEqualTo(0L);
}
Aggregations