Search in sources :

Example 6 with SpannerBlockingStub

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

the class SpannerTestCases method listSessionsSingleCall.

private void listSessionsSingleCall(SpannerBlockingStub stub) {
    ListSessionsRequest request = ListSessionsRequest.newBuilder().setDatabase(database).build();
    long start = System.currentTimeMillis();
    stub.listSessions(request);
    System.out.println(String.format("-- Finished executing listSessions in %d ms in another thread. -- ", System.currentTimeMillis() - start));
}
Also used : ListSessionsRequest(com.google.spanner.v1.ListSessionsRequest)

Example 7 with SpannerBlockingStub

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

the class SpannerTestCases method testListSessions.

void testListSessions() throws InterruptedException {
    System.out.println("\nTestListSessions");
    ManagedChannel channel = getChannel();
    SpannerBlockingStub stub = getBlockingStub(channel);
    ListSessionsRequest request = ListSessionsRequest.newBuilder().setDatabase(database).build();
    for (int i = 0; i < NUM_WARMUP; i++) {
        stub.listSessions(request);
    }
    BlockingCall<ListSessionsRequest, ListSessionsResponse> blockingCall = (ListSessionsRequest req) -> stub.listSessions(req);
    Func func = (List<Long> result) -> doBlockingCalls(result, numOfRpcs, request, blockingCall);
    runTest(channel, func);
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
Also used : ListSessionsResponse(com.google.spanner.v1.ListSessionsResponse) SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) ManagedChannel(io.grpc.ManagedChannel) GcpManagedChannel(com.google.grpc.gcp.GcpManagedChannel) ListSessionsRequest(com.google.spanner.v1.ListSessionsRequest)

Example 8 with SpannerBlockingStub

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

the class SpannerTestCases method testRead.

void testRead() throws InterruptedException {
    System.out.println("\nTestRead");
    ManagedChannel channel = getChannel();
    SpannerBlockingStub stub = getBlockingStub(channel);
    Session session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    ReadRequest request = ReadRequest.newBuilder().setSession(session.getName()).setTable("small_table").setKeySet(KeySet.newBuilder().setAll(true).build()).addColumns("users").addColumns("firstname").addColumns("lastname").build();
    BlockingCall<ReadRequest, ResultSet> blockingCall = (ReadRequest req) -> stub.read(req);
    doTestBlocking(channel, request, blockingCall);
    stub.deleteSession(DeleteSessionRequest.newBuilder().setName(session.getName()).build());
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
Also used : SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) PartialResultSet(com.google.spanner.v1.PartialResultSet) ResultSet(com.google.spanner.v1.ResultSet) ManagedChannel(io.grpc.ManagedChannel) GcpManagedChannel(com.google.grpc.gcp.GcpManagedChannel) Session(com.google.spanner.v1.Session) ReadRequest(com.google.spanner.v1.ReadRequest)

Example 9 with SpannerBlockingStub

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

the class SpannerTestCases method testExecuteSql.

void testExecuteSql() throws InterruptedException {
    System.out.println("\nTestExecuteSql");
    ManagedChannel channel = getChannel();
    SpannerBlockingStub stub = getBlockingStub(channel);
    Session session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    ExecuteSqlRequest request = ExecuteSqlRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + TABLE).build();
    BlockingCall<ExecuteSqlRequest, ResultSet> blockingCall = (ExecuteSqlRequest req) -> stub.executeSql(req);
    doTestBlocking(channel, request, blockingCall);
    stub.deleteSession(DeleteSessionRequest.newBuilder().setName(session.getName()).build());
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
Also used : SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) PartialResultSet(com.google.spanner.v1.PartialResultSet) ResultSet(com.google.spanner.v1.ResultSet) ManagedChannel(io.grpc.ManagedChannel) GcpManagedChannel(com.google.grpc.gcp.GcpManagedChannel) Session(com.google.spanner.v1.Session)

Example 10 with SpannerBlockingStub

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

the class SpannerTestCases method testMaxConcurrentStream.

void testMaxConcurrentStream() throws InterruptedException {
    System.out.println("\nTestMaxConcurrentStream");
    ManagedChannel channel = getChannel();
    SpannerBlockingStub stub = getBlockingStub(channel);
    Session session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    // Warm up.
    ExecuteSqlRequest request = ExecuteSqlRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + LARGE_TABLE).build();
    for (int i = 0; i < NUM_WARMUP; i++) {
        Iterator<PartialResultSet> iter = stub.executeStreamingSql(request);
        while (iter.hasNext()) {
            iter.next();
        }
    }
    // Start concurrent rpc calls.
    List<Iterator<PartialResultSet>> responses = new ArrayList<>();
    long start = System.currentTimeMillis();
    for (int i = 0; i < numOfRpcs; i++) {
        responses.add(stub.executeStreamingSql(request));
    }
    System.out.println(String.format("Started %d ExecuteStreamingSql calls in %dms", numOfRpcs, System.currentTimeMillis() - start));
    // Start another rpc call using a new thread.
    Thread t = new Thread(() -> listSessionsSingleCall(stub));
    t.start();
    System.out.println("I'm sleeping and will wake up after 2000ms zzzZZZZ.");
    Thread.sleep(2000);
    System.out.println("Good morning!");
    // Free one call.
    while (responses.get(0).hasNext()) {
        responses.get(0).next();
    }
    System.out.println(String.format("Freed one call in %dms.", System.currentTimeMillis() - start));
    // Free all the calls.
    for (int i = 1; i < responses.size(); i++) {
        Iterator<PartialResultSet> iter = responses.get(i);
        while (iter.hasNext()) {
            iter.next();
        }
    }
    System.out.println(String.format("Freed %d call(s) in %dms.", numOfRpcs, System.currentTimeMillis() - start));
    t.join();
    stub.deleteSession(DeleteSessionRequest.newBuilder().setName(session.getName()).build());
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
Also used : SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ManagedChannel(io.grpc.ManagedChannel) GcpManagedChannel(com.google.grpc.gcp.GcpManagedChannel) PartialResultSet(com.google.spanner.v1.PartialResultSet) Session(com.google.spanner.v1.Session)

Aggregations

SpannerBlockingStub (com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub)13 Session (com.google.spanner.v1.Session)11 ManagedChannel (io.grpc.ManagedChannel)7 GcpManagedChannel (com.google.grpc.gcp.GcpManagedChannel)6 Test (org.junit.Test)5 CreateSessionRequest (com.google.spanner.v1.CreateSessionRequest)4 PartialResultSet (com.google.spanner.v1.PartialResultSet)3 ArrayList (java.util.ArrayList)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)2 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)2 ListSessionsRequest (com.google.spanner.v1.ListSessionsRequest)2 ListSessionsResponse (com.google.spanner.v1.ListSessionsResponse)2 ResultSet (com.google.spanner.v1.ResultSet)2 BatchCreateSessionsRequest (com.google.spanner.v1.BatchCreateSessionsRequest)1 BatchCreateSessionsResponse (com.google.spanner.v1.BatchCreateSessionsResponse)1 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)1 PartitionQueryRequest (com.google.spanner.v1.PartitionQueryRequest)1 PartitionResponse (com.google.spanner.v1.PartitionResponse)1 ReadRequest (com.google.spanner.v1.ReadRequest)1 Transaction (com.google.spanner.v1.Transaction)1