Search in sources :

Example 6 with BeginTransactionRequest

use of com.google.datastore.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 7 with BeginTransactionRequest

use of com.google.datastore.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 8 with BeginTransactionRequest

use of com.google.datastore.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)

Example 9 with BeginTransactionRequest

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

the class CloudDatastoreV1ClientImplTest method testStackTrace.

@Test
public void testStackTrace() throws Throwable {
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    when(datastore.beginTransaction(req)).thenThrow(new DatastoreException("beginTransaction", Code.INVALID_ARGUMENT, "message", null));
    CloudDatastoreV1ClientImpl client = newClient(datastore);
    Future<BeginTransactionResponse> future = makeBeginTransactionCallForTest(client, req);
    try {
        future.get();
        fail("expected exception");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
        // The test helper method used to call beginTransaction() appears in the error message
        // (even though it is no longer on the stack).
        assertThat(e).hasMessageThat().contains("makeBeginTransactionCallForTest");
        // The underlying cause is also present.
        assertThat(e).hasCauseThat().hasCauseThat().isInstanceOf(DatastoreException.class);
    }
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) DatastoreException(com.google.datastore.v1.client.DatastoreException) ExecutionException(java.util.concurrent.ExecutionException) BeginTransactionResponse(com.google.datastore.v1.BeginTransactionResponse) Test(org.junit.Test)

Example 10 with BeginTransactionRequest

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

the class CloudDatastoreV1ClientImplTest method testRetries.

@Test
public void testRetries() throws Exception {
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 2);
    when(datastore.beginTransaction(req)).thenThrow(retryableException()).thenThrow(retryableException()).thenReturn(BeginTransactionResponse.getDefaultInstance());
    client.beginTransaction(req).get();
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) Test(org.junit.Test)

Aggregations

BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)11 Test (org.junit.Test)10 Transaction (com.google.spanner.v1.Transaction)7 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 Connection (java.sql.Connection)4 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)3 UpdateDatabaseDdlRequest (com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest)3 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)3 Session (com.google.spanner.v1.Session)3 TransactionOptions (com.google.spanner.v1.TransactionOptions)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)2 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2