Search in sources :

Example 16 with TransactionOptions

use of com.google.firestore.v1beta1.TransactionOptions in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerProbes method partitionProber.

/**
 * Probe to test PartitionQuery and PartitionRead grpc call from Spanner stub.
 */
public static void partitionProber(SpannerGrpc.SpannerBlockingStub stub) {
    Session session = null;
    try {
        session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(DATABASE).build());
        // Probing partition query call.
        TransactionOptions options = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
        TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
        stub.partitionQuery(PartitionQueryRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + TABLE).setTransaction(selector).build());
        // Probing partition read call.
        stub.partitionRead(PartitionReadRequest.newBuilder().setSession(session.getName()).setTable(TABLE).setTransaction(selector).setKeySet(KeySet.newBuilder().setAll(true).build()).addColumns("username").addColumns("firstname").addColumns("lastname").build());
    } finally {
        deleteSession(stub, session);
    }
}
Also used : TransactionOptions(com.google.spanner.v1.TransactionOptions) TransactionSelector(com.google.spanner.v1.TransactionSelector) Session(com.google.spanner.v1.Session)

Example 17 with TransactionOptions

use of com.google.firestore.v1beta1.TransactionOptions in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerTestCases method testPartitionQuery.

void testPartitionQuery() throws InterruptedException {
    System.out.println("\nTestPartitionQuery");
    ManagedChannel channel = getChannel();
    SpannerBlockingStub stub = getBlockingStub(channel);
    Session session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    TransactionOptions options = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
    TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
    PartitionQueryRequest request = PartitionQueryRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + LARGE_TABLE).setTransaction(selector).build();
    BlockingCall<PartitionQueryRequest, PartitionResponse> blockingCall = (PartitionQueryRequest req) -> stub.partitionQuery(req);
    doTestBlocking(channel, request, blockingCall);
    stub.deleteSession(DeleteSessionRequest.newBuilder().setName(session.getName()).build());
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
Also used : PartitionResponse(com.google.spanner.v1.PartitionResponse) SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) TransactionOptions(com.google.spanner.v1.TransactionOptions) TransactionSelector(com.google.spanner.v1.TransactionSelector) PartitionQueryRequest(com.google.spanner.v1.PartitionQueryRequest) ManagedChannel(io.grpc.ManagedChannel) GcpManagedChannel(com.google.grpc.gcp.GcpManagedChannel) Session(com.google.spanner.v1.Session)

Example 18 with TransactionOptions

use of com.google.firestore.v1beta1.TransactionOptions in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerIntegrationTest method testExecuteBatchDmlFuture.

@Test
public void testExecuteBatchDmlFuture() throws Exception {
    SpannerFutureStub stub = getSpannerFutureStub();
    List<String> futureNames = createFutureSessions(stub);
    for (String futureName : futureNames) {
        TransactionOptions options = TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build();
        TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
        // Will use only one session for the whole batch.
        ListenableFuture<ExecuteBatchDmlResponse> responseFuture = stub.executeBatchDml(ExecuteBatchDmlRequest.newBuilder().setSession(futureName).setTransaction(selector).addStatements(Statement.newBuilder().setSql("select * FROM Users").build()).build());
        // The ChannelRef which is bound with the current affinity key.
        GcpManagedChannel.ChannelRef currentChannel = gcpChannel.affinityKeyToChannelRef.get(futureName);
        // Verify the channel is in use.
        assertEquals(1, currentChannel.getActiveStreamsCount());
        ExecuteBatchDmlResponse response = responseFuture.get();
        assertEquals(0, currentChannel.getActiveStreamsCount());
    }
    deleteFutureSessions(stub, futureNames);
}
Also used : TransactionOptions(com.google.spanner.v1.TransactionOptions) TransactionSelector(com.google.spanner.v1.TransactionSelector) SpannerFutureStub(com.google.spanner.v1.SpannerGrpc.SpannerFutureStub) ExecuteBatchDmlResponse(com.google.spanner.v1.ExecuteBatchDmlResponse) Test(org.junit.Test)

Example 19 with TransactionOptions

use of com.google.firestore.v1beta1.TransactionOptions in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerIntegrationTest method testPartitionQueryAsync.

@Test
public void testPartitionQueryAsync() throws Exception {
    SpannerStub stub = getSpannerStub();
    List<String> respNames = createAsyncSessions(stub);
    for (String respName : respNames) {
        TransactionOptions options = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
        TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
        AsyncResponseObserver<PartitionResponse> resp = new AsyncResponseObserver<>();
        stub.partitionQuery(PartitionQueryRequest.newBuilder().setSession(respName).setSql("select * FROM Users").setTransaction(selector).build(), resp);
        // The ChannelRef which is bound with the current affinity key.
        GcpManagedChannel.ChannelRef currentChannel = gcpChannel.affinityKeyToChannelRef.get(respName);
        // Verify the channel is in use.
        assertEquals(1, currentChannel.getActiveStreamsCount());
        PartitionResponse response = resp.get();
        assertEquals(0, currentChannel.getActiveStreamsCount());
    }
    deleteAsyncSessions(stub, respNames);
}
Also used : PartitionResponse(com.google.spanner.v1.PartitionResponse) SpannerStub(com.google.spanner.v1.SpannerGrpc.SpannerStub) TransactionOptions(com.google.spanner.v1.TransactionOptions) TransactionSelector(com.google.spanner.v1.TransactionSelector) Test(org.junit.Test)

Example 20 with TransactionOptions

use of com.google.firestore.v1beta1.TransactionOptions in project java-datastore by googleapis.

the class ITDatastoreTest method testRunInTransactionWithReadWriteOption.

@Test
public void testRunInTransactionWithReadWriteOption() {
    EasyMock.expect(rpcMock.beginTransaction(EasyMock.anyObject(BeginTransactionRequest.class))).andReturn(BeginTransactionResponse.getDefaultInstance());
    EasyMock.expect(rpcMock.rollback(EasyMock.anyObject(RollbackRequest.class))).andReturn(RollbackResponse.getDefaultInstance()).once();
    EasyMock.expect(rpcMock.beginTransaction(EasyMock.anyObject(BeginTransactionRequest.class))).andReturn(BeginTransactionResponse.getDefaultInstance());
    EasyMock.expect(rpcMock.commit(EasyMock.anyObject(CommitRequest.class))).andReturn(CommitResponse.newBuilder().build());
    EasyMock.replay(rpcFactoryMock, rpcMock);
    Datastore mockDatastore = rpcMockOptions.getService();
    Datastore.TransactionCallable<Integer> callable = new Datastore.TransactionCallable<Integer>() {

        private Integer attempts = 1;

        @Override
        public Integer run(DatastoreReaderWriter transaction) {
            if (attempts < 2) {
                ++attempts;
                throw new DatastoreException(10, "", "ABORTED", false, null);
            }
            return attempts;
        }
    };
    TransactionOptions options = TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build();
    Integer result = mockDatastore.runInTransaction(callable, options);
    EasyMock.verify(rpcFactoryMock, rpcMock);
    assertEquals(2, result.intValue());
}
Also used : CommitRequest(com.google.datastore.v1.CommitRequest) BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) TransactionOptions(com.google.datastore.v1.TransactionOptions) Test(org.junit.Test)

Aggregations

TransactionOptions (com.google.spanner.v1.TransactionOptions)16 Test (org.junit.Test)13 TransactionSelector (com.google.spanner.v1.TransactionSelector)6 Session (com.google.spanner.v1.Session)5 ArrayList (java.util.ArrayList)5 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 ByteString (com.google.protobuf.ByteString)4 Mutation (com.google.spanner.v1.Mutation)4 PartitionResponse (com.google.spanner.v1.PartitionResponse)4 SessionName (com.google.spanner.v1.SessionName)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 TransactionOptions (com.google.datastore.v1.TransactionOptions)3 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)3 CommitRequest (com.google.spanner.v1.CommitRequest)3 PartitionQueryRequest (com.google.spanner.v1.PartitionQueryRequest)3 Transaction (com.google.spanner.v1.Transaction)3 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)2 CommitRequest (com.google.datastore.v1.CommitRequest)2 GcpManagedChannel (com.google.grpc.gcp.GcpManagedChannel)2