Search in sources :

Example 1 with SpannerBlockingStub

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

the class SpannerIntegrationTest method testBatchCreateSessionsBlocking.

@Test
public void testBatchCreateSessionsBlocking() throws Exception {
    int sessionCount = 10;
    SpannerBlockingStub stub = getSpannerBlockingStub();
    BatchCreateSessionsRequest req = BatchCreateSessionsRequest.newBuilder().setDatabase(DATABASE_PATH).setSessionCount(sessionCount).build();
    List<Session> sessions = new ArrayList<>();
    // The first MAX_CHANNEL requests (without affinity) should be distributed 1 per channel.
    for (int j = 0; j < MAX_CHANNEL; j++) {
        BatchCreateSessionsResponse resp = stub.batchCreateSessions(req);
        assertThat(resp.getSessionCount()).isEqualTo(sessionCount);
        sessions.addAll(resp.getSessionList());
    }
    checkChannelRefs(MAX_CHANNEL, 0, sessionCount);
    for (Session session : sessions) {
        deleteSession(stub, session);
    }
    checkChannelRefs(MAX_CHANNEL, 0, 0);
}
Also used : BatchCreateSessionsResponse(com.google.spanner.v1.BatchCreateSessionsResponse) SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) ArrayList(java.util.ArrayList) BatchCreateSessionsRequest(com.google.spanner.v1.BatchCreateSessionsRequest) Session(com.google.spanner.v1.Session) Test(org.junit.Test)

Example 2 with SpannerBlockingStub

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

the class SpannerIntegrationTest method testUnbindWithInvalidAffinityKey.

@Test
public void testUnbindWithInvalidAffinityKey() {
    SpannerBlockingStub stub = getSpannerBlockingStub();
    CreateSessionRequest req = CreateSessionRequest.newBuilder().setDatabase(DATABASE_PATH).build();
    Session session = stub.createSession(req);
    expectedEx.expect(StatusRuntimeException.class);
    expectedEx.expectMessage("INVALID_ARGUMENT: Invalid DeleteSession request.");
    // No channel bound with the key "invalid_session", will use the least busy one.
    stub.deleteSession(DeleteSessionRequest.newBuilder().setName("invalid_session").build());
}
Also used : CreateSessionRequest(com.google.spanner.v1.CreateSessionRequest) SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) Session(com.google.spanner.v1.Session) Test(org.junit.Test)

Example 3 with SpannerBlockingStub

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

the class SpannerIntegrationTest method cleanupSessions.

private static void cleanupSessions() {
    ManagedChannel channel = builder.build();
    GoogleCredentials creds = getCreds();
    SpannerBlockingStub stub = SpannerGrpc.newBlockingStub(channel).withCallCredentials(MoreCallCredentials.from(creds));
    ListSessionsResponse responseList = stub.listSessions(ListSessionsRequest.newBuilder().setDatabase(DATABASE_PATH).build());
    for (Session s : responseList.getSessionsList()) {
        deleteSession(stub, s);
    }
    channel.shutdownNow();
}
Also used : ListSessionsResponse(com.google.spanner.v1.ListSessionsResponse) SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) ManagedChannel(io.grpc.ManagedChannel) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Session(com.google.spanner.v1.Session)

Example 4 with SpannerBlockingStub

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

the class SpannerIntegrationTest method testBoundWithInvalidAffinityKey.

@Test
public void testBoundWithInvalidAffinityKey() {
    SpannerBlockingStub stub = getSpannerBlockingStub();
    CreateSessionRequest req = CreateSessionRequest.newBuilder().setDatabase(DATABASE_PATH).build();
    Session session = stub.createSession(req);
    expectedEx.expect(StatusRuntimeException.class);
    expectedEx.expectMessage("INVALID_ARGUMENT: Invalid GetSession request.");
    // No channel bound with the key "invalid_session", will use the least busy one.
    stub.getSession(GetSessionRequest.newBuilder().setName("invalid_session").build());
}
Also used : CreateSessionRequest(com.google.spanner.v1.CreateSessionRequest) SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) Session(com.google.spanner.v1.Session) Test(org.junit.Test)

Example 5 with SpannerBlockingStub

use of com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub 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)

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