Search in sources :

Example 1 with SessionName

use of com.google.spanner.v1.SessionName in project google-cloud-java by GoogleCloudPlatform.

the class GrpcSpannerRpc method deleteSession.

@Override
public void deleteSession(String sessionName, @Nullable Map<Option, ?> options) {
    DeleteSessionRequest request = DeleteSessionRequest.newBuilder().setName(sessionName).build();
    get(doUnaryCall(SpannerGrpc.METHOD_DELETE_SESSION, request, sessionName, Option.CHANNEL_HINT.getLong(options)));
}
Also used : DeleteSessionRequest(com.google.spanner.v1.DeleteSessionRequest)

Example 2 with SessionName

use of com.google.spanner.v1.SessionName in project google-cloud-java by GoogleCloudPlatform.

the class SessionImplTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    SpannerImpl spanner = new SpannerImpl(rpc, 1, null);
    String dbName = "projects/p1/instances/i1/databases/d1";
    String sessionName = dbName + "/sessions/s1";
    DatabaseId db = DatabaseId.of(dbName);
    Session sessionProto = Session.newBuilder().setName(sessionName).build();
    Mockito.when(rpc.createSession(Mockito.eq(dbName), optionsCaptor.capture())).thenReturn(sessionProto);
    session = spanner.createSession(db);
    // We expect the same options, "options", on all calls on "session".
    options = optionsCaptor.getValue();
    Mockito.reset(rpc);
}
Also used : ByteString(com.google.protobuf.ByteString) Session(com.google.spanner.v1.Session) Before(org.junit.Before)

Example 3 with SessionName

use of com.google.spanner.v1.SessionName in project java-spanner by googleapis.

the class SpannerClient method rollback.

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
 * Rolls back a transaction, releasing any locks it holds. It is a good idea to call this for any
 * transaction that includes one or more [Read][google.spanner.v1.Spanner.Read] or
 * [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql] requests and ultimately decides not to
 * commit.
 *
 * <p>`Rollback` returns `OK` if it successfully aborts the transaction, the transaction was
 * already aborted, or the transaction is not found. `Rollback` never returns `ABORTED`.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * try (SpannerClient spannerClient = SpannerClient.create()) {
 *   SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
 *   ByteString transactionId = ByteString.EMPTY;
 *   spannerClient.rollback(session, transactionId);
 * }
 * }</pre>
 *
 * @param session Required. The session in which the transaction to roll back is running.
 * @param transactionId Required. The transaction to roll back.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
public final void rollback(SessionName session, ByteString transactionId) {
    RollbackRequest request = RollbackRequest.newBuilder().setSession(session == null ? null : session.toString()).setTransactionId(transactionId).build();
    rollback(request);
}
Also used : RollbackRequest(com.google.spanner.v1.RollbackRequest)

Example 4 with SessionName

use of com.google.spanner.v1.SessionName in project java-spanner by googleapis.

the class MockSpannerServiceImpl method partition.

private void partition(String sessionName, TransactionSelector transactionSelector, StreamObserver<PartitionResponse> responseObserver) {
    Session session = sessions.get(sessionName);
    if (session == null) {
        setSessionNotFound(sessionName, responseObserver);
        return;
    }
    sessionLastUsed.put(session.getName(), Instant.now());
    try {
        ByteString transactionId = getTransactionId(session, transactionSelector);
        responseObserver.onNext(PartitionResponse.newBuilder().addPartitions(Partition.newBuilder().setPartitionToken(generatePartitionToken(session.getName(), transactionId)).build()).build());
        responseObserver.onCompleted();
    } catch (StatusRuntimeException e) {
        responseObserver.onError(e);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) StatusRuntimeException(io.grpc.StatusRuntimeException) Session(com.google.spanner.v1.Session)

Example 5 with SessionName

use of com.google.spanner.v1.SessionName in project java-spanner by googleapis.

the class SessionPoolTest method testSessionNotFoundReadOnlyTransaction.

@Test
public void testSessionNotFoundReadOnlyTransaction() {
    Statement statement = Statement.of("SELECT 1");
    final SessionImpl closedSession = mockSession();
    when(closedSession.readOnlyTransaction()).thenThrow(SpannerExceptionFactoryTest.newSessionNotFoundException(sessionName));
    final SessionImpl openSession = mockSession();
    ReadOnlyTransaction openTransaction = mock(ReadOnlyTransaction.class);
    ResultSet openResultSet = mock(ResultSet.class);
    when(openResultSet.next()).thenReturn(true, false);
    when(openTransaction.executeQuery(statement)).thenReturn(openResultSet);
    when(openSession.readOnlyTransaction()).thenReturn(openTransaction);
    doAnswer(invocation -> {
        executor.submit(() -> {
            SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
            consumer.onSessionReady(closedSession);
        });
        return null;
    }).doAnswer(invocation -> {
        executor.submit(() -> {
            SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
            consumer.onSessionReady(openSession);
        });
        return null;
    }).when(sessionClient).asyncBatchCreateSessions(Mockito.eq(1), Mockito.anyBoolean(), any(SessionConsumer.class));
    FakeClock clock = new FakeClock();
    clock.currentTimeMillis = System.currentTimeMillis();
    pool = createPool(clock);
    ReadOnlyTransaction transaction = pool.getSession().readOnlyTransaction();
    ResultSet resultSet = transaction.executeQuery(statement);
    assertThat(resultSet.next()).isTrue();
}
Also used : TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) Arrays(java.util.Arrays) ClosedException(com.google.cloud.spanner.SpannerImpl.ClosedException) QueryAnalyzeMode(com.google.cloud.spanner.ReadContext.QueryAnalyzeMode) CommitResponse(com.google.spanner.v1.CommitResponse) Timestamp(com.google.cloud.Timestamp) Clock(com.google.cloud.spanner.SessionPool.Clock) PooledSession(com.google.cloud.spanner.SessionPool.PooledSession) Mockito.doThrow(org.mockito.Mockito.doThrow) Empty(com.google.protobuf.Empty) ResultSetStats(com.google.spanner.v1.ResultSetStats) Future(java.util.concurrent.Future) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) Parameterized(org.junit.runners.Parameterized) ResultStreamConsumer(com.google.cloud.spanner.spi.v1.SpannerRpc.ResultStreamConsumer) PrintWriter(java.io.PrintWriter) PointWithFunction(com.google.cloud.spanner.MetricRegistryTestUtils.PointWithFunction) ApiFutures(com.google.api.core.ApiFutures) Collection(java.util.Collection) NUM_READ_SESSIONS(com.google.cloud.spanner.MetricRegistryConstants.NUM_READ_SESSIONS) Executors(java.util.concurrent.Executors) ByteString(com.google.protobuf.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) NUM_SESSIONS_BEING_PREPARED(com.google.cloud.spanner.MetricRegistryConstants.NUM_SESSIONS_BEING_PREPARED) List(java.util.List) LabelValue(io.opencensus.metrics.LabelValue) Mockito.atMost(org.mockito.Mockito.atMost) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Mockito.any(org.mockito.Mockito.any) MetricRegistry(io.opencensus.metrics.MetricRegistry) SPANNER_LABEL_KEYS_WITH_TYPE(com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_TYPE) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Mock(org.mockito.Mock) SPANNER_LABEL_KEYS(com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) SpannerRpc(com.google.cloud.spanner.spi.v1.SpannerRpc) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) CommitRequest(com.google.spanner.v1.CommitRequest) PooledSessionFuture(com.google.cloud.spanner.SessionPool.PooledSessionFuture) MetricsRecord(com.google.cloud.spanner.MetricRegistryTestUtils.MetricsRecord) SessionConsumerImpl(com.google.cloud.spanner.SessionPool.SessionConsumerImpl) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) FakeMetricRegistry(com.google.cloud.spanner.MetricRegistryTestUtils.FakeMetricRegistry) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) NUM_IN_USE_SESSIONS(com.google.cloud.spanner.MetricRegistryConstants.NUM_IN_USE_SESSIONS) Assert.assertNotNull(org.junit.Assert.assertNotNull) Parameter(org.junit.runners.Parameterized.Parameter) StringWriter(java.io.StringWriter) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Span(io.opencensus.trace.Span) Mockito.verify(org.mockito.Mockito.verify) RollbackRequest(com.google.spanner.v1.RollbackRequest) NUM_WRITE_SESSIONS(com.google.cloud.spanner.MetricRegistryConstants.NUM_WRITE_SESSIONS) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Mockito.anyInt(org.mockito.Mockito.anyInt) TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SessionConsumer(com.google.cloud.spanner.SessionClient.SessionConsumer) SessionConsumer(com.google.cloud.spanner.SessionClient.SessionConsumer) SessionConsumerImpl(com.google.cloud.spanner.SessionPool.SessionConsumerImpl) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 ByteString (com.google.protobuf.ByteString)13 SessionName (com.google.spanner.v1.SessionName)12 ArrayList (java.util.ArrayList)10 CommitRequest (com.google.spanner.v1.CommitRequest)9 CommitResponse (com.google.spanner.v1.CommitResponse)9 RollbackRequest (com.google.spanner.v1.RollbackRequest)9 Empty (com.google.protobuf.Empty)8 Before (org.junit.Before)8 ApiFutures (com.google.api.core.ApiFutures)6 Timestamp (com.google.cloud.Timestamp)6 NUM_IN_USE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_IN_USE_SESSIONS)6 NUM_READ_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_READ_SESSIONS)6 NUM_SESSIONS_BEING_PREPARED (com.google.cloud.spanner.MetricRegistryConstants.NUM_SESSIONS_BEING_PREPARED)6 NUM_WRITE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_WRITE_SESSIONS)6 SPANNER_LABEL_KEYS (com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS)6 SPANNER_LABEL_KEYS_WITH_TYPE (com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_TYPE)6 FakeMetricRegistry (com.google.cloud.spanner.MetricRegistryTestUtils.FakeMetricRegistry)6 MetricsRecord (com.google.cloud.spanner.MetricRegistryTestUtils.MetricsRecord)6 PointWithFunction (com.google.cloud.spanner.MetricRegistryTestUtils.PointWithFunction)6