Search in sources :

Example 1 with BatchCreateSessionsRequest

use of com.google.spanner.v1.BatchCreateSessionsRequest 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 BatchCreateSessionsRequest

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

the class GapicSpannerRpc method batchCreateSessions.

@Override
public List<Session> batchCreateSessions(String databaseName, int sessionCount, @Nullable Map<String, String> labels, @Nullable Map<Option, ?> options) throws SpannerException {
    BatchCreateSessionsRequest.Builder requestBuilder = BatchCreateSessionsRequest.newBuilder().setDatabase(databaseName).setSessionCount(sessionCount);
    if (labels != null && !labels.isEmpty()) {
        Session.Builder session = Session.newBuilder().putAllLabels(labels);
        requestBuilder.setSessionTemplate(session);
    }
    BatchCreateSessionsRequest request = requestBuilder.build();
    GrpcCallContext context = newCallContext(options, databaseName, request, SpannerGrpc.getBatchCreateSessionsMethod());
    return get(spannerStub.batchCreateSessionsCallable().futureCall(request, context)).getSessionList();
}
Also used : GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) BatchCreateSessionsRequest(com.google.spanner.v1.BatchCreateSessionsRequest) Session(com.google.spanner.v1.Session)

Example 3 with BatchCreateSessionsRequest

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

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

use of com.google.spanner.v1.BatchCreateSessionsRequest 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

BatchCreateSessionsRequest (com.google.spanner.v1.BatchCreateSessionsRequest)4 BatchCreateSessionsResponse (com.google.spanner.v1.BatchCreateSessionsResponse)4 Session (com.google.spanner.v1.Session)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 AbstractMessage (com.google.protobuf.AbstractMessage)2 ByteString (com.google.protobuf.ByteString)2 GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)1 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