use of com.google.cloud.spanner.v1.SpannerClient in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClientV1TestCases method testMaxConcurrentStream.
void testMaxConcurrentStream() throws InterruptedException {
System.out.println("\nTestMaxConcurrentStream");
SpannerClient client = getClient();
Session session = client.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 = client.executeStreamingSqlCallable().call(request).iterator();
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(client.executeStreamingSqlCallable().call(request).iterator());
}
System.out.println(String.format("Started %d ExecuteStreamingSql calls in %d ms", numOfRpcs, System.currentTimeMillis() - start));
// Start another rpc call using a new thread.
Thread t = new Thread(() -> listSessionsSingleCall(client));
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();
cleanUpClient(client, session.getName());
}
use of com.google.cloud.spanner.v1.SpannerClient in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClientV1TestCases method testRead.
void testRead() throws InterruptedException {
System.out.println("\nTestRead");
SpannerClient client = getClient();
Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
ReadRequest request = ReadRequest.newBuilder().setSession(session.getName()).setTable(TABLE).setKeySet(KeySet.newBuilder().setAll(true).build()).addColumns("users").addColumns("firstname").addColumns("lastname").build();
RpcCall<ReadRequest, ResultSet> rpcCall = (ReadRequest req) -> client.read(req);
doTestBlocking(request, rpcCall);
cleanUpClient(client, session.getName());
}
use of com.google.cloud.spanner.v1.SpannerClient in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClientV1TestCases method testListSessions.
void testListSessions() throws InterruptedException {
System.out.println("\nTestListSessions");
SpannerClient client = getClient();
ListSessionsRequest request = ListSessionsRequest.newBuilder().setDatabase(database).build();
for (int i = 0; i < NUM_WARMUP; i++) {
client.listSessions(request);
}
// for (Session element : spannerClient.listSessions(request).iterateAll())
RpcCall<ListSessionsRequest, ListSessionsPagedResponse> rpcCall = (ListSessionsRequest req) -> client.listSessions(req);
doTestBlocking(request, rpcCall);
client.shutdown();
client.awaitTermination(5, TimeUnit.SECONDS);
}
use of com.google.cloud.spanner.v1.SpannerClient in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClientV1TestCases method getClient.
private SpannerClient getClient() {
// Set up credentials.
GoogleCredentials creds;
try {
creds = GoogleCredentials.getApplicationDefault();
} catch (Exception e) {
return null;
}
ImmutableList<String> requiredScopes = ImmutableList.of(OAUTH_SCOPE);
creds = creds.createScoped(requiredScopes);
FixedCredentialsProvider provider = FixedCredentialsProvider.create(creds);
// Set up the Spanner client.
InstantiatingGrpcChannelProvider.Builder channelBuilder = InstantiatingGrpcChannelProvider.newBuilder().setPoolSize(DEFAULT_CHANNEL_POOL);
if (isGrpcGcp) {
File configFile = new File(SpannerTestCases.class.getClassLoader().getResource(API_FILE).getFile());
ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> apiFunction = (ManagedChannelBuilder builder) -> (GcpManagedChannelBuilder.forDelegateBuilder(builder).withApiConfigJsonFile(configFile));
channelBuilder = channelBuilder.setPoolSize(1).setChannelConfigurator(apiFunction);
}
SpannerClient client = null;
try {
SpannerSettings.Builder spannerSettingsBuilder = SpannerSettings.newBuilder();
spannerSettingsBuilder.getStubSettingsBuilder().setTransportChannelProvider(channelBuilder.build()).setCredentialsProvider(provider);
client = SpannerClient.create(spannerSettingsBuilder.build());
} catch (IOException e) {
System.out.println("Failed to create the client.");
}
return client;
}
Aggregations