Search in sources :

Example 6 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerProbes method transactionProber.

/**
 * Probe to test BeginTransaction, Commit and Rollback grpc from Spanner stub.
 */
public static void transactionProber(SpannerGrpc.SpannerBlockingStub stub) {
    Session session = null;
    try {
        session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(DATABASE).build());
        // Probing begin transaction call.
        TransactionOptions options = TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build();
        BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(options).build();
        Transaction txn = stub.beginTransaction(request);
        // Probing commit call.
        stub.commit(CommitRequest.newBuilder().setSession(session.getName()).setTransactionId(txn.getId()).build());
        // Probing rollback call.
        txn = stub.beginTransaction(request);
        stub.rollback(RollbackRequest.newBuilder().setSession(session.getName()).setTransactionId(txn.getId()).build());
    } finally {
        deleteSession(stub, session);
    }
}
Also used : Transaction(com.google.spanner.v1.Transaction) TransactionOptions(com.google.spanner.v1.TransactionOptions) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Session(com.google.spanner.v1.Session)

Example 7 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.

the class BeginTransaction method beginTransactionCall.

public void beginTransactionCall() {
    FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
    TransactionOptions tOpts = TransactionOptions.newBuilder().build();
    BeginTransactionRequest beginTransactionRequest = BeginTransactionRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").setOptions(tOpts).build();
    BeginTransactionResponse response;
    try {
        response = blockingStub.beginTransaction(beginTransactionRequest);
    } catch (Exception e) {
        System.out.println("Error during call: " + e.getMessage() + e.getCause());
        return;
    }
    System.out.println("Began Transaction: " + response.getTransaction().toString());
    Main.transactionId = response.getTransaction();
    Menu menu = new Menu();
    menu.draw();
}
Also used : GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) TransactionOptions(com.google.firestore.v1beta1.TransactionOptions) BeginTransactionRequest(com.google.firestore.v1beta1.BeginTransactionRequest) Menu(org.roguewave.grpc.util.gfx.Menu) BeginTransactionResponse(com.google.firestore.v1beta1.BeginTransactionResponse)

Example 8 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerClientV1TestCases method prepareTestData.

void prepareTestData() throws InterruptedException {
    int columnBytes = Integer.min(payload, MAX_SIZE_PER_COLUMN);
    int rows = (payload - 1) / columnBytes + 1;
    char[] charArray = new char[columnBytes];
    Arrays.fill(charArray, 'z');
    String colContent = new String(charArray);
    SpannerClient client = getClient();
    Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    long start = System.currentTimeMillis();
    // Clean the existing data.
    BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build()).build();
    Transaction txn = client.beginTransaction(request);
    client.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setDelete(Mutation.Delete.newBuilder().setTable(LARGE_TABLE).setKeySet(KeySet.newBuilder().setAll(true).build()).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
    System.out.println(String.format("\nDeleted the previous large_table in %d ms.", System.currentTimeMillis() - start));
    // Add the payload data.
    start = System.currentTimeMillis();
    for (int i = 0; i < rows; i++) {
        txn = client.beginTransaction(request);
        client.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setInsertOrUpdate(Mutation.Write.newBuilder().addColumns("id").addColumns("data").addValues(ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("payload" + i)).addValues(Value.newBuilder().setStringValue(colContent)).build()).setTable(LARGE_TABLE).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
    }
    System.out.println(String.format("Successfully added ColumnBytes: %d, Rows: %d to large_table in %d ms.", columnBytes, rows, System.currentTimeMillis() - start));
    cleanUpClient(client, session.getName());
}
Also used : Transaction(com.google.spanner.v1.Transaction) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) SpannerClient(com.google.cloud.spanner.v1.SpannerClient) Session(com.google.spanner.v1.Session)

Example 9 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerTestCases method prepareTestData.

void prepareTestData() throws InterruptedException {
    ManagedChannel channel = getChannel();
    SpannerBlockingStub stub = getBlockingStub(channel);
    // Because of max data size, we need to separate into different rows.
    int columnBytes = Integer.min(payload, MAX_SIZE_PER_COLUMN);
    int rows = (payload - 1) / columnBytes + 1;
    char[] charArray = new char[columnBytes];
    Arrays.fill(charArray, 'z');
    String colContent = new String(charArray);
    Session session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    long start = System.currentTimeMillis();
    // Clean the data in the table.
    BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build()).build();
    Transaction txn = stub.beginTransaction(request);
    stub.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setDelete(Mutation.Delete.newBuilder().setTable(LARGE_TABLE).setKeySet(KeySet.newBuilder().setAll(true).build()).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
    System.out.println(String.format("\nDeleted the previous large_table in %d ms.", System.currentTimeMillis() - start));
    // Add the payload data.
    start = System.currentTimeMillis();
    for (int i = 0; i < rows; i++) {
        txn = stub.beginTransaction(request);
        stub.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setInsertOrUpdate(Mutation.Write.newBuilder().addColumns("id").addColumns("data").addValues(ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("payload" + i)).addValues(Value.newBuilder().setStringValue(colContent)).build()).setTable(LARGE_TABLE).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
    }
    System.out.println(String.format("Successfully added ColumnBytes: %d, Rows: %d to large_table in %d ms.", columnBytes, rows, System.currentTimeMillis() - start));
    stub.deleteSession(DeleteSessionRequest.newBuilder().setName(session.getName()).build());
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
Also used : SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) Transaction(com.google.spanner.v1.Transaction) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) ManagedChannel(io.grpc.ManagedChannel) GcpManagedChannel(com.google.grpc.gcp.GcpManagedChannel) Session(com.google.spanner.v1.Session)

Example 10 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.

the class CloudDatastoreV1ClientImplTest method testRetries_MaxRetriesExceeded.

@Test
public void testRetries_MaxRetriesExceeded() throws Exception {
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 2);
    DatastoreException lastException = retryableException();
    when(datastore.beginTransaction(req)).thenThrow(retryableException()).thenThrow(retryableException()).thenThrow(lastException);
    try {
        client.beginTransaction(req).get();
        fail("expected exception");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseThat().hasCauseThat().isEqualTo(lastException);
    }
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) DatastoreException(com.google.datastore.v1.client.DatastoreException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

Transaction (com.google.spanner.v1.Transaction)11 Test (org.junit.Test)10 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)8 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)5 ByteString (com.google.protobuf.ByteString)5 ExecutionException (java.util.concurrent.ExecutionException)5 DatastoreException (com.google.datastore.v1.client.DatastoreException)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 Session (com.google.spanner.v1.Session)4 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)3 PartialResultSet (com.google.spanner.v1.PartialResultSet)3 TransactionOptions (com.google.spanner.v1.TransactionOptions)3 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2 FirestoreGrpc (com.google.firestore.v1.FirestoreGrpc)2 RollbackRequest (com.google.firestore.v1.RollbackRequest)2 TransactionOptions (com.google.firestore.v1.TransactionOptions)2 Empty (com.google.protobuf.Empty)2 Nullable (org.springframework.lang.Nullable)2