Search in sources :

Example 1 with PooledSessionFuture

use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.

the class DatabaseClientImplTest method testTransactionManager_usesOptions.

@Test
public void testTransactionManager_usesOptions() {
    SessionPool pool = mock(SessionPool.class);
    PooledSessionFuture session = mock(PooledSessionFuture.class);
    when(pool.getSession()).thenReturn(session);
    TransactionOption option = mock(TransactionOption.class);
    DatabaseClientImpl client = new DatabaseClientImpl(pool);
    client.transactionManager(option);
    verify(session).transactionManager(option);
}
Also used : PooledSessionFuture(com.google.cloud.spanner.SessionPool.PooledSessionFuture) TransactionOption(com.google.cloud.spanner.Options.TransactionOption) Test(org.junit.Test)

Example 2 with PooledSessionFuture

use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.

the class DatabaseClientImplTest method testReadWriteTransaction_usesOptions.

@Test
public void testReadWriteTransaction_usesOptions() {
    SessionPool pool = mock(SessionPool.class);
    PooledSessionFuture session = mock(PooledSessionFuture.class);
    when(pool.getSession()).thenReturn(session);
    TransactionOption option = mock(TransactionOption.class);
    DatabaseClientImpl client = new DatabaseClientImpl(pool);
    client.readWriteTransaction(option);
    verify(session).readWriteTransaction(option);
}
Also used : PooledSessionFuture(com.google.cloud.spanner.SessionPool.PooledSessionFuture) TransactionOption(com.google.cloud.spanner.Options.TransactionOption) Test(org.junit.Test)

Example 3 with PooledSessionFuture

use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.

the class ITSessionPoolIntegrationTest method sessionCreation.

@Test
public void sessionCreation() {
    try (PooledSessionFuture session = pool.getSession()) {
        assertThat(session.get()).isNotNull();
    }
    try (PooledSessionFuture session = pool.getSession();
        PooledSessionFuture session2 = pool.getSession()) {
        assertThat(session.get()).isNotNull();
        assertThat(session2.get()).isNotNull();
    }
}
Also used : PooledSessionFuture(com.google.cloud.spanner.SessionPool.PooledSessionFuture) Test(org.junit.Test)

Example 4 with PooledSessionFuture

use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.

the class SessionPoolStressTest method stressTest.

@Test
public void stressTest() throws Exception {
    int concurrentThreads = 10;
    final int numOperationsPerThread = 1000;
    final CountDownLatch releaseThreads = new CountDownLatch(1);
    final CountDownLatch threadsDone = new CountDownLatch(concurrentThreads);
    setupSpanner(db);
    int minSessions = 2;
    int maxSessions = concurrentThreads / 2;
    SessionPoolOptions.Builder builder = SessionPoolOptions.newBuilder().setMinSessions(minSessions).setMaxSessions(maxSessions);
    if (shouldBlock) {
        builder.setBlockIfPoolExhausted();
    } else {
        builder.setFailIfPoolExhausted();
    }
    pool = SessionPool.createPool(builder.build(), new TestExecutorFactory(), mockSpanner.getSessionClient(db), clock);
    pool.idleSessionRemovedListener = pooled -> {
        String name = pooled.getName();
        synchronized (lock) {
            sessions.remove(name);
            return null;
        }
    };
    for (int i = 0; i < concurrentThreads; i++) {
        new Thread(() -> {
            Uninterruptibles.awaitUninterruptibly(releaseThreads);
            for (int j = 0; j < numOperationsPerThread; j++) {
                try {
                    PooledSessionFuture session = pool.getSession();
                    session.get();
                    Uninterruptibles.sleepUninterruptibly(random.nextInt(2), TimeUnit.MILLISECONDS);
                    resetTransaction(session.get().delegate);
                    session.close();
                } catch (SpannerException e) {
                    if (e.getErrorCode() != ErrorCode.RESOURCE_EXHAUSTED || shouldBlock) {
                        setFailed(e);
                    }
                } catch (Exception e) {
                    setFailed(e);
                }
            }
            threadsDone.countDown();
        }).start();
    }
    // Start maintenance threads in tight loop
    final AtomicBoolean stopMaintenance = new AtomicBoolean(false);
    new Thread(() -> {
        while (!stopMaintenance.get()) {
            runMaintenanceLoop(clock, pool, 1);
        }
    }).start();
    releaseThreads.countDown();
    threadsDone.await();
    synchronized (lock) {
        assertThat(maxAliveSessions).isAtMost(maxSessions);
    }
    stopMaintenance.set(true);
    pool.closeAsync(new SpannerImpl.ClosedException()).get();
    Exception e = getFailedError();
    if (e != null) {
        throw e;
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PooledSessionFuture(com.google.cloud.spanner.SessionPool.PooledSessionFuture) ByteString(com.google.protobuf.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with PooledSessionFuture

use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.

the class SessionPoolTest method poolClosureClosesLeakedSessions.

@Test
public void poolClosureClosesLeakedSessions() throws Exception {
    SessionImpl mockSession1 = mockSession();
    SessionImpl mockSession2 = mockSession();
    final LinkedList<SessionImpl> sessions = new LinkedList<>(Arrays.asList(mockSession1, mockSession2));
    doAnswer(invocation -> {
        executor.submit(() -> {
            SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
            consumer.onSessionReady(sessions.pop());
        });
        return null;
    }).when(sessionClient).asyncBatchCreateSessions(Mockito.eq(1), Mockito.anyBoolean(), any(SessionConsumer.class));
    pool = createPool();
    Session session1 = pool.getSession();
    // Leaked sessions
    PooledSessionFuture leakedSession = pool.getSession();
    // Clear the leaked exception to suppress logging of expected exceptions.
    leakedSession.clearLeakedException();
    session1.close();
    pool.closeAsync(new SpannerImpl.ClosedException()).get(5L, TimeUnit.SECONDS);
    verify(mockSession1).asyncClose();
    verify(mockSession2).asyncClose();
}
Also used : SessionConsumer(com.google.cloud.spanner.SessionClient.SessionConsumer) SessionConsumerImpl(com.google.cloud.spanner.SessionPool.SessionConsumerImpl) ClosedException(com.google.cloud.spanner.SpannerImpl.ClosedException) PooledSessionFuture(com.google.cloud.spanner.SessionPool.PooledSessionFuture) LinkedList(java.util.LinkedList) PooledSession(com.google.cloud.spanner.SessionPool.PooledSession) Test(org.junit.Test)

Aggregations

PooledSessionFuture (com.google.cloud.spanner.SessionPool.PooledSessionFuture)15 Test (org.junit.Test)15 SessionConsumer (com.google.cloud.spanner.SessionClient.SessionConsumer)7 SessionConsumerImpl (com.google.cloud.spanner.SessionPool.SessionConsumerImpl)7 ClosedException (com.google.cloud.spanner.SpannerImpl.ClosedException)7 PooledSession (com.google.cloud.spanner.SessionPool.PooledSession)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 FakeMetricRegistry (com.google.cloud.spanner.MetricRegistryTestUtils.FakeMetricRegistry)4 MetricsRecord (com.google.cloud.spanner.MetricRegistryTestUtils.MetricsRecord)4 PointWithFunction (com.google.cloud.spanner.MetricRegistryTestUtils.PointWithFunction)4 TransactionOption (com.google.cloud.spanner.Options.TransactionOption)4 ByteString (com.google.protobuf.ByteString)4 LabelValue (io.opencensus.metrics.LabelValue)4 LinkedList (java.util.LinkedList)4 ApiFutures (com.google.api.core.ApiFutures)3 Timestamp (com.google.cloud.Timestamp)3 NUM_IN_USE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_IN_USE_SESSIONS)3 NUM_READ_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_READ_SESSIONS)3 NUM_SESSIONS_BEING_PREPARED (com.google.cloud.spanner.MetricRegistryConstants.NUM_SESSIONS_BEING_PREPARED)3 NUM_WRITE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_WRITE_SESSIONS)3