use of com.google.datastore.v1.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);
}
}
use of com.google.datastore.v1.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);
}
use of com.google.datastore.v1.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);
}
use of com.google.datastore.v1.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);
}
use of com.google.datastore.v1.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());
}
Aggregations