Search in sources :

Example 6 with ReadRequest

use of com.google.bytestream.ByteStreamProto.ReadRequest in project bazel-buildfarm by bazelbuild.

the class ByteStreamServiceTest method readSlicesLargeChunksFromInstance.

@SuppressWarnings("unchecked")
@Test
public void readSlicesLargeChunksFromInstance() throws Exception {
    // pick a large chunk size
    long size = CHUNK_SIZE * 10 + CHUNK_SIZE - 47;
    ByteString content;
    try (ByteString.Output out = ByteString.newOutput(ByteStreamService.CHUNK_SIZE * 10 + ByteStreamService.CHUNK_SIZE - 47)) {
        for (long i = 0; i < size; i++) {
            out.write((int) (i & 0xff));
        }
        content = out.toByteString();
    }
    Digest digest = DIGEST_UTIL.compute(content);
    String resourceName = "blobs/" + DigestUtil.toString(digest);
    ReadRequest request = ReadRequest.newBuilder().setResourceName(resourceName).build();
    doAnswer(answerVoid((blobDigest, offset, limit, chunkObserver, metadata) -> {
    })).when(instance).getBlob(eq(digest), eq(request.getReadOffset()), eq((long) content.size()), any(ServerCallStreamObserver.class), eq(RequestMetadata.getDefaultInstance()));
    Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
    ByteStreamStub service = ByteStreamGrpc.newStub(channel);
    CountingReadObserver readObserver = new CountingReadObserver();
    service.read(request, readObserver);
    ArgumentCaptor<ServerCallStreamObserver<ByteString>> observerCaptor = ArgumentCaptor.forClass(ServerCallStreamObserver.class);
    verify(instance, times(1)).getBlob(eq(digest), eq(request.getReadOffset()), eq((long) content.size()), observerCaptor.capture(), eq(RequestMetadata.getDefaultInstance()));
    StreamObserver<ByteString> responseObserver = observerCaptor.getValue();
    // supply entire content
    responseObserver.onNext(content);
    responseObserver.onCompleted();
    assertThat(readObserver.isCompleted()).isTrue();
    assertThat(readObserver.getData()).isEqualTo(content);
    List<Integer> sizes = readObserver.getSizesList();
    // 10 + 1 incomplete chunk
    assertThat(sizes.size()).isEqualTo(11);
    assertThat(sizes.stream().filter((responseSize) -> responseSize > CHUNK_SIZE).collect(Collectors.toList())).isEmpty();
}
Also used : SHA256(build.buildfarm.common.DigestUtil.HashFunction.SHA256) RequestMetadata(build.bazel.remote.execution.v2.RequestMetadata) SettableFuture(com.google.common.util.concurrent.SettableFuture) Channel(io.grpc.Channel) ByteStreamGrpc(com.google.bytestream.ByteStreamGrpc) MockitoAnnotations(org.mockito.MockitoAnnotations) StreamObserver(io.grpc.stub.StreamObserver) Digest(build.bazel.remote.execution.v2.Digest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) After(org.junit.After) AdditionalAnswers.answerVoid(org.mockito.AdditionalAnswers.answerVoid) Status(io.grpc.Status) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Instance(build.buildfarm.instance.Instance) ByteString(com.google.protobuf.ByteString) ByteStreamUploader(build.buildfarm.instance.stub.ByteStreamUploader) List(java.util.List) CHUNK_SIZE(build.buildfarm.server.ByteStreamService.CHUNK_SIZE) Mockito.any(org.mockito.Mockito.any) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) DigestUtil(build.buildfarm.common.DigestUtil) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) Answer(org.mockito.stubbing.Answer) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) ByteStreamStub(com.google.bytestream.ByteStreamGrpc.ByteStreamStub) FeedbackOutputStream(build.buildfarm.common.io.FeedbackOutputStream) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) Server(io.grpc.Server) Before(org.junit.Before) HashCode(com.google.common.hash.HashCode) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Write(build.buildfarm.common.Write) Mockito.verify(org.mockito.Mockito.verify) WriteResponse(com.google.bytestream.ByteStreamProto.WriteResponse) TimeUnit(java.util.concurrent.TimeUnit) WriteRequest(com.google.bytestream.ByteStreamProto.WriteRequest) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) ByteStreamStub(com.google.bytestream.ByteStreamGrpc.ByteStreamStub) Channel(io.grpc.Channel) ByteString(com.google.protobuf.ByteString) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Example 7 with ReadRequest

use of com.google.bytestream.ByteStreamProto.ReadRequest in project bazel-buildfarm by bazelbuild.

the class ByteStreamServiceTest method missingBlobReadIsNotFound.

@Test
public void missingBlobReadIsNotFound() {
    ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!");
    Digest digest = DIGEST_UTIL.compute(helloWorld);
    Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
    ByteStreamBlockingStub service = ByteStreamGrpc.newBlockingStub(channel);
    when(simpleBlobStore.get(eq(digest.getHash()), any(OutputStream.class))).thenReturn(immediateFuture(false));
    ReadRequest request = ReadRequest.newBuilder().setResourceName(createBlobDownloadResourceName(digest)).build();
    StatusRuntimeException notFoundException = null;
    try {
        if (service.read(request).hasNext()) {
            fail("no responses should be available");
        }
    } catch (StatusRuntimeException e) {
        assertThat(Status.fromThrowable(e).getCode()).isEqualTo(Code.NOT_FOUND);
        notFoundException = e;
    }
    assertThat(notFoundException).isNotNull();
}
Also used : Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) Channel(io.grpc.Channel) ByteStreamBlockingStub(com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub) OutputStream(java.io.OutputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Example 8 with ReadRequest

use of com.google.bytestream.ByteStreamProto.ReadRequest in project bazel-buildfarm by bazelbuild.

the class ByteStreamServiceTest method skippedInputIsNotInResponse.

@Test
public void skippedInputIsNotInResponse() throws ExecutionException, IOException, InterruptedException {
    ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!");
    Digest digest = DIGEST_UTIL.compute(helloWorld);
    Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
    ByteStreamStub service = ByteStreamGrpc.newStub(channel);
    SettableFuture<Boolean> getComplete = SettableFuture.create();
    when(simpleBlobStore.get(eq(digest.getHash()), any(OutputStream.class))).thenReturn(getComplete);
    ArgumentCaptor<OutputStream> outputStreamCaptor = ArgumentCaptor.forClass(OutputStream.class);
    ReadRequest request = ReadRequest.newBuilder().setResourceName(createBlobDownloadResourceName(digest)).setReadOffset(6).build();
    SettableFuture<ByteString> readComplete = SettableFuture.create();
    service.read(request, new StreamObserver<ReadResponse>() {

        ByteString content = ByteString.EMPTY;

        @Override
        public void onNext(ReadResponse response) {
            content = content.concat(response.getData());
        }

        @Override
        public void onError(Throwable t) {
            readComplete.setException(t);
        }

        @Override
        public void onCompleted() {
            readComplete.set(content);
        }
    });
    verify(simpleBlobStore, times(1)).get(eq(digest.getHash()), outputStreamCaptor.capture());
    try (OutputStream outputStream = outputStreamCaptor.getValue()) {
        outputStream.write(helloWorld.toByteArray());
        getComplete.set(true);
    }
    assertThat(readComplete.get()).isEqualTo(helloWorld.substring(6));
}
Also used : Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) ByteStreamStub(com.google.bytestream.ByteStreamGrpc.ByteStreamStub) Channel(io.grpc.Channel) OutputStream(java.io.OutputStream) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Example 9 with ReadRequest

use of com.google.bytestream.ByteStreamProto.ReadRequest in project bazel-buildfarm by bazelbuild.

the class ByteStreamHelper method newInput.

@SuppressWarnings("Guava")
public static InputStream newInput(String resourceName, long offset, Supplier<ByteStreamStub> bsStubSupplier, Supplier<Backoff> backoffSupplier, Predicate<Status> isRetriable, @Nullable ListeningScheduledExecutorService retryService) throws IOException {
    ReadRequest request = ReadRequest.newBuilder().setResourceName(resourceName).setReadOffset(offset).build();
    BlockingQueue<ByteString> queue = new ArrayBlockingQueue<>(1);
    ByteStringQueueInputStream inputStream = new ByteStringQueueInputStream(queue);
    // this interface needs to operate similar to open, where it
    // throws an exception on creation. We will need to wait around
    // for the response to come back in order to supply the stream or
    // throw the exception it receives
    SettableFuture<InputStream> streamReadyFuture = SettableFuture.create();
    StreamObserver<ReadResponse> responseObserver = new StreamObserver<ReadResponse>() {

        long requestOffset = offset;

        long currentOffset = offset;

        Backoff backoff = backoffSupplier.get();

        @Override
        public void onNext(ReadResponse response) {
            streamReadyFuture.set(inputStream);
            ByteString data = response.getData();
            try {
                queue.put(data);
                currentOffset += data.size();
            } catch (InterruptedException e) {
                // cancel context?
                inputStream.setException(e);
            }
        }

        private void retryRequest() {
            requestOffset = currentOffset;
            bsStubSupplier.get().read(request.toBuilder().setReadOffset(requestOffset).build(), this);
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            long nextDelayMillis = backoff.nextDelayMillis();
            if (status.getCode() == Status.Code.DEADLINE_EXCEEDED && currentOffset != requestOffset) {
                backoff = backoffSupplier.get();
                retryRequest();
            } else if (retryService == null || nextDelayMillis < 0 || !isRetriable.test(status)) {
                streamReadyFuture.setException(t);
                inputStream.setException(t);
            } else {
                try {
                    ListenableFuture<?> schedulingResult = retryService.schedule(this::retryRequest, nextDelayMillis, TimeUnit.MILLISECONDS);
                    schedulingResult.addListener(() -> {
                        try {
                            schedulingResult.get();
                        } catch (ExecutionException e) {
                            inputStream.setException(e.getCause());
                        } catch (InterruptedException e) {
                            inputStream.setException(e);
                        }
                    }, MoreExecutors.directExecutor());
                } catch (RejectedExecutionException e) {
                    inputStream.setException(e);
                }
            }
        }

        @Override
        public void onCompleted() {
            inputStream.setCompleted();
        }
    };
    bsStubSupplier.get().read(request, responseObserver);
    // perfectly reasonable to be used as a wait point
    try {
        return streamReadyFuture.get();
    } catch (InterruptedException e) {
        try {
            inputStream.close();
        } catch (RuntimeException closeEx) {
            e.addSuppressed(e);
        }
        IOException ioEx = new ClosedByInterruptException();
        ioEx.addSuppressed(e);
        throw ioEx;
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        Status status = Status.fromThrowable(cause);
        if (status.getCode() == Status.Code.NOT_FOUND) {
            IOException ioEx = new NoSuchFileException(resourceName);
            ioEx.addSuppressed(cause);
            throw ioEx;
        }
        Throwables.throwIfInstanceOf(cause, IOException.class);
        throw new IOException(cause);
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Status(io.grpc.Status) ByteString(com.google.protobuf.ByteString) ByteStringQueueInputStream(build.buildfarm.common.io.ByteStringQueueInputStream) InputStream(java.io.InputStream) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) ByteStringQueueInputStream(build.buildfarm.common.io.ByteStringQueueInputStream) Backoff(build.buildfarm.common.grpc.Retrier.Backoff) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest)

Example 10 with ReadRequest

use of com.google.bytestream.ByteStreamProto.ReadRequest in project bazel-buildfarm by bazelbuild.

the class StubInstanceTest method inputStreamThrowsOnDeadlineExceededWithoutProgress.

@Test
public void inputStreamThrowsOnDeadlineExceededWithoutProgress() throws IOException, InterruptedException {
    serviceRegistry.addService(new ByteStreamImplBase() {

        @Override
        public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
            responseObserver.onError(Status.DEADLINE_EXCEEDED.asException());
        }
    });
    OutputStream out = mock(OutputStream.class);
    IOException ioException = null;
    Instance instance = newStubInstance("input-stream-deadline-exceeded");
    Digest timeoutDigest = Digest.newBuilder().setHash("timeout-blob-name").setSizeBytes(1).build();
    try (InputStream in = instance.newBlobInput(timeoutDigest, 0, 1, SECONDS, RequestMetadata.getDefaultInstance())) {
        ByteStreams.copy(in, out);
    } catch (IOException e) {
        ioException = e;
    }
    assertThat(ioException).isNotNull();
    Status status = Status.fromThrowable(ioException);
    assertThat(status.getCode()).isEqualTo(Code.DEADLINE_EXCEEDED);
    verifyZeroInteractions(out);
    instance.stop();
}
Also used : Status(io.grpc.Status) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) Instance(build.buildfarm.instance.Instance) Digest(build.bazel.remote.execution.v2.Digest) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) IOException(java.io.IOException) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Aggregations

ReadRequest (com.google.bytestream.ByteStreamProto.ReadRequest)14 ReadResponse (com.google.bytestream.ByteStreamProto.ReadResponse)12 Test (org.junit.Test)12 ByteString (com.google.protobuf.ByteString)11 Digest (build.bazel.remote.execution.v2.Digest)10 ByteStreamImplBase (com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase)7 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 Instance (build.buildfarm.instance.Instance)4 Status (io.grpc.Status)4 OutputStream (java.io.OutputStream)4 ByteStreamUploader (build.buildfarm.instance.stub.ByteStreamUploader)3 Channel (io.grpc.Channel)3 StreamObserver (io.grpc.stub.StreamObserver)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ByteStreamStub (com.google.bytestream.ByteStreamGrpc.ByteStreamStub)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 RequestMetadata (build.bazel.remote.execution.v2.RequestMetadata)1 DigestUtil (build.buildfarm.common.DigestUtil)1 SHA256 (build.buildfarm.common.DigestUtil.HashFunction.SHA256)1