Search in sources :

Example 1 with BatchCreateSessionsResponse

use of com.google.spanner.v1.BatchCreateSessionsResponse in project java-spanner by googleapis.

the class SpannerClientTest method batchCreateSessionsTest2.

@Test
public void batchCreateSessionsTest2() throws Exception {
    BatchCreateSessionsResponse expectedResponse = BatchCreateSessionsResponse.newBuilder().addAllSession(new ArrayList<Session>()).build();
    mockSpanner.addResponse(expectedResponse);
    String database = "database1789464955";
    int sessionCount = 185691686;
    BatchCreateSessionsResponse actualResponse = client.batchCreateSessions(database, sessionCount);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BatchCreateSessionsRequest actualRequest = ((BatchCreateSessionsRequest) actualRequests.get(0));
    Assert.assertEquals(database, actualRequest.getDatabase());
    Assert.assertEquals(sessionCount, actualRequest.getSessionCount());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : BatchCreateSessionsResponse(com.google.spanner.v1.BatchCreateSessionsResponse) AbstractMessage(com.google.protobuf.AbstractMessage) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) BatchCreateSessionsRequest(com.google.spanner.v1.BatchCreateSessionsRequest) Test(org.junit.Test)

Example 2 with BatchCreateSessionsResponse

use of com.google.spanner.v1.BatchCreateSessionsResponse 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 3 with BatchCreateSessionsResponse

use of com.google.spanner.v1.BatchCreateSessionsResponse in project java-spanner by googleapis.

the class MockSpannerServiceImpl method batchCreateSessions.

@Override
public void batchCreateSessions(BatchCreateSessionsRequest request, StreamObserver<BatchCreateSessionsResponse> responseObserver) {
    requests.add(request);
    Preconditions.checkNotNull(request.getDatabase());
    String name = null;
    try {
        if (request.getSessionCount() <= 0) {
            throw Status.INVALID_ARGUMENT.withDescription("Session count must be >= 0").asRuntimeException();
        }
        batchCreateSessionsExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
        if (sessions.size() >= maxTotalSessions) {
            throw Status.RESOURCE_EXHAUSTED.withDescription("Maximum number of sessions reached").asRuntimeException();
        }
        Timestamp now = getCurrentGoogleTimestamp();
        BatchCreateSessionsResponse.Builder response = BatchCreateSessionsResponse.newBuilder();
        int maxSessionsToCreate = Math.min(maxNumSessionsInOneBatch, request.getSessionCount());
        for (int i = 0; i < Math.min(maxTotalSessions - sessions.size(), maxSessionsToCreate); i++) {
            name = generateSessionName(request.getDatabase());
            Session session = Session.newBuilder().setCreateTime(now).setName(name).setApproximateLastUseTime(now).build();
            Session prev = sessions.putIfAbsent(name, session);
            if (prev == null) {
                if (sessions.size() <= maxTotalSessions) {
                    sessionLastUsed.put(name, Instant.now());
                    response.addSession(session);
                    numSessionsCreated.incrementAndGet();
                } else {
                    sessions.remove(name);
                }
            } else {
                // Someone else tried to create a session with the same id. This should not be possible
                throw Status.ALREADY_EXISTS.asRuntimeException();
            }
        }
        responseObserver.onNext(response.build());
        responseObserver.onCompleted();
    } catch (StatusRuntimeException e) {
        if (name != null) {
            sessions.remove(name);
        }
        responseObserver.onError(e);
    } catch (Throwable e) {
        if (name != null) {
            sessions.remove(name);
        }
        responseObserver.onError(Status.INTERNAL.withDescription("Batch create sessions failed: " + e.getMessage()).asRuntimeException());
    }
}
Also used : BatchCreateSessionsResponse(com.google.spanner.v1.BatchCreateSessionsResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Timestamp(com.google.protobuf.Timestamp) Session(com.google.spanner.v1.Session)

Example 4 with BatchCreateSessionsResponse

use of com.google.spanner.v1.BatchCreateSessionsResponse in project java-spanner by googleapis.

the class SpannerClientTest method batchCreateSessionsTest.

@Test
public void batchCreateSessionsTest() throws Exception {
    BatchCreateSessionsResponse expectedResponse = BatchCreateSessionsResponse.newBuilder().addAllSession(new ArrayList<Session>()).build();
    mockSpanner.addResponse(expectedResponse);
    DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");
    int sessionCount = 185691686;
    BatchCreateSessionsResponse actualResponse = client.batchCreateSessions(database, sessionCount);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BatchCreateSessionsRequest actualRequest = ((BatchCreateSessionsRequest) actualRequests.get(0));
    Assert.assertEquals(database.toString(), actualRequest.getDatabase());
    Assert.assertEquals(sessionCount, actualRequest.getSessionCount());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : BatchCreateSessionsResponse(com.google.spanner.v1.BatchCreateSessionsResponse) AbstractMessage(com.google.protobuf.AbstractMessage) ArrayList(java.util.ArrayList) DatabaseName(com.google.spanner.v1.DatabaseName) BatchCreateSessionsRequest(com.google.spanner.v1.BatchCreateSessionsRequest) Test(org.junit.Test)

Aggregations

BatchCreateSessionsResponse (com.google.spanner.v1.BatchCreateSessionsResponse)4 BatchCreateSessionsRequest (com.google.spanner.v1.BatchCreateSessionsRequest)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 AbstractMessage (com.google.protobuf.AbstractMessage)2 ByteString (com.google.protobuf.ByteString)2 Session (com.google.spanner.v1.Session)2 Timestamp (com.google.protobuf.Timestamp)1 DatabaseName (com.google.spanner.v1.DatabaseName)1 SpannerBlockingStub (com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1