Search in sources :

Example 6 with ReadRequest

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

the class SpannerClientTest method streamingReadExceptionTest.

@Test
public void streamingReadExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSpanner.addException(exception);
    ReadRequest request = ReadRequest.newBuilder().setSession(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).setTransaction(TransactionSelector.newBuilder().build()).setTable("table110115790").setIndex("index100346066").addAllColumns(new ArrayList<String>()).setKeySet(KeySet.newBuilder().build()).setLimit(102976443).setResumeToken(ByteString.EMPTY).setPartitionToken(ByteString.EMPTY).setRequestOptions(RequestOptions.newBuilder().build()).build();
    MockStreamObserver<PartialResultSet> responseObserver = new MockStreamObserver<>();
    ServerStreamingCallable<ReadRequest, PartialResultSet> callable = client.streamingReadCallable();
    callable.serverStreamingCall(request, responseObserver);
    try {
        List<PartialResultSet> actualResponses = responseObserver.future().get();
        Assert.fail("No exception thrown");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof InvalidArgumentException);
        InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
        Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ExecutionException(java.util.concurrent.ExecutionException) PartialResultSet(com.google.spanner.v1.PartialResultSet) ReadRequest(com.google.spanner.v1.ReadRequest) PartitionReadRequest(com.google.spanner.v1.PartitionReadRequest) Test(org.junit.Test)

Example 7 with ReadRequest

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

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

the class DatabaseClientImplTest method testReadWriteExecuteReadWithPriority.

@Test
public void testReadWriteExecuteReadWithPriority() {
    DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
    TransactionRunner runner = client.readWriteTransaction();
    runner.run(transaction -> {
        try (ResultSet resultSet = transaction.read(READ_TABLE_NAME, KeySet.singleKey(Key.of(1L)), READ_COLUMN_NAMES, Options.priority(RpcPriority.HIGH))) {
            while (resultSet.next()) {
            }
        }
        return null;
    });
    List<ReadRequest> requests = mockSpanner.getRequestsOfType(ReadRequest.class);
    assertThat(requests).hasSize(1);
    ReadRequest request = requests.get(0);
    assertNotNull(request.getRequestOptions());
    assertEquals(Priority.PRIORITY_HIGH, request.getRequestOptions().getPriority());
}
Also used : ReadRequest(com.google.spanner.v1.ReadRequest) Test(org.junit.Test)

Example 9 with ReadRequest

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

the class MockSpannerServiceImpl method streamingRead.

@Override
public void streamingRead(final ReadRequest request, StreamObserver<PartialResultSet> responseObserver) {
    requests.add(request);
    Preconditions.checkNotNull(request.getSession());
    Session session = sessions.get(request.getSession());
    if (session == null) {
        setSessionNotFound(request.getSession(), responseObserver);
        return;
    }
    sessionLastUsed.put(session.getName(), Instant.now());
    try {
        streamingReadExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
        // Get or start transaction
        ByteString transactionId = getTransactionId(session, request.getTransaction());
        if (!request.getPartitionToken().isEmpty()) {
            List<ByteString> tokens = partitionTokens.get(partitionKey(session.getName(), transactionId));
            if (tokens == null || !tokens.contains(request.getPartitionToken())) {
                throw Status.INVALID_ARGUMENT.withDescription(String.format("Partition token %s is not a valid token for this transaction", request.getPartitionToken())).asRuntimeException();
            }
        }
        simulateAbort(session, transactionId);
        Iterable<String> cols = () -> request.getColumnsList().iterator();
        Statement statement = StatementResult.createReadStatement(request.getTable(), request.getKeySet().getAll() ? KeySet.all() : KeySet.singleKey(Key.of()), cols);
        StatementResult res = getResult(statement);
        if (res == null) {
            throw Status.NOT_FOUND.withDescription("No result found for " + statement.toString()).asRuntimeException();
        }
        if (res.getType() == StatementResult.StatementResultType.EXCEPTION) {
            throw res.getException();
        }
        returnPartialResultSet(res.getResultSet(), transactionId, request.getTransaction(), responseObserver, getStreamingReadExecutionTime());
    } catch (StatusRuntimeException e) {
        responseObserver.onError(e);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Session(com.google.spanner.v1.Session)

Example 10 with ReadRequest

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

the class MockSpannerServiceImpl method read.

@Override
public void read(final ReadRequest request, StreamObserver<ResultSet> responseObserver) {
    requests.add(request);
    Preconditions.checkNotNull(request.getSession());
    Session session = sessions.get(request.getSession());
    if (session == null) {
        setSessionNotFound(request.getSession(), responseObserver);
        return;
    }
    sessionLastUsed.put(session.getName(), Instant.now());
    try {
        readExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
        // Get or start transaction
        ByteString transactionId = getTransactionId(session, request.getTransaction());
        simulateAbort(session, transactionId);
        Iterable<String> cols = () -> request.getColumnsList().iterator();
        Statement statement = StatementResult.createReadStatement(request.getTable(), request.getKeySet().getAll() ? KeySet.all() : KeySet.singleKey(Key.of()), cols);
        StatementResult res = getResult(statement);
        returnResultSet(res.getResultSet(), transactionId, request.getTransaction(), responseObserver);
        responseObserver.onCompleted();
    } catch (StatusRuntimeException e) {
        responseObserver.onError(e);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Session(com.google.spanner.v1.Session)

Aggregations

ReadRequest (com.google.spanner.v1.ReadRequest)11 Test (org.junit.Test)8 ByteString (com.google.protobuf.ByteString)6 PartialResultSet (com.google.spanner.v1.PartialResultSet)6 Session (com.google.spanner.v1.Session)5 PartitionReadRequest (com.google.spanner.v1.PartitionReadRequest)4 ResultSet (com.google.spanner.v1.ResultSet)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)2 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)2 GcpManagedChannel (com.google.grpc.gcp.GcpManagedChannel)2 ListValue (com.google.protobuf.ListValue)2 ManagedChannel (io.grpc.ManagedChannel)2 SpannerClient (com.google.cloud.spanner.v1.SpannerClient)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 Value (com.google.protobuf.Value)1 SpannerBlockingStub (com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub)1 SpannerStub (com.google.spanner.v1.SpannerGrpc.SpannerStub)1 ExecutionException (java.util.concurrent.ExecutionException)1