Search in sources :

Example 6 with ReadResponse

use of com.google.bytestream.ByteStreamProto.ReadResponse 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 7 with ReadResponse

use of com.google.bytestream.ByteStreamProto.ReadResponse 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 8 with ReadResponse

use of com.google.bytestream.ByteStreamProto.ReadResponse 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)

Example 9 with ReadResponse

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

the class GrpcCAS method get.

@Override
public void get(Digest digest, long offset, long count, ServerCallStreamObserver<ByteString> blobObserver, RequestMetadata requestMetadata) {
    ReadRequest request = ReadRequest.newBuilder().setResourceName(getBlobName(digest)).setReadOffset(offset).setReadLimit(count).build();
    ByteStreamGrpc.newStub(channel).withInterceptors(attachMetadataInterceptor(requestMetadata)).read(request, new DelegateServerCallStreamObserver<ReadResponse, ByteString>(blobObserver) {

        @Override
        public void onNext(ReadResponse response) {
            blobObserver.onNext(response.getData());
        }

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

        @Override
        public void onCompleted() {
            blobObserver.onCompleted();
        }
    });
}
Also used : ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) ByteString(com.google.protobuf.ByteString) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest)

Example 10 with ReadResponse

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

the class GrpcCASTest method getHandlesNotFound.

@Test
public void getHandlesNotFound() {
    Digest digest = DIGEST_UTIL.compute(ByteString.copyFromUtf8("nonexistent"));
    String instanceName = "test";
    final AtomicReference<Boolean> readCalled = new AtomicReference<>(false);
    serviceRegistry.addService(new ByteStreamImplBase() {

        @Override
        public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
            assertThat(request.getResourceName()).isEqualTo(String.format("%s/blobs/%s", instanceName, DigestUtil.toString(digest)));
            readCalled.compareAndSet(false, true);
            responseObserver.onError(Status.NOT_FOUND.asException());
        }
    });
    GrpcCAS cas = new GrpcCAS(instanceName, InProcessChannelBuilder.forName(fakeServerName).directExecutor().build(), mock(ByteStreamUploader.class), onExpirations);
    assertThat(cas.get(digest)).isNull();
    assertThat(readCalled.get()).isTrue();
}
Also used : ByteStreamUploader(build.buildfarm.instance.stub.ByteStreamUploader) Digest(build.bazel.remote.execution.v2.Digest) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(com.google.protobuf.ByteString) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Aggregations

ReadResponse (com.google.bytestream.ByteStreamProto.ReadResponse)15 ReadRequest (com.google.bytestream.ByteStreamProto.ReadRequest)11 ByteString (com.google.protobuf.ByteString)10 Test (org.junit.Test)9 Digest (build.bazel.remote.execution.v2.Digest)8 ByteStreamImplBase (com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase)7 IOException (java.io.IOException)7 InputStream (java.io.InputStream)6 Status (io.grpc.Status)5 NoSuchFileException (java.nio.file.NoSuchFileException)5 Instance (build.buildfarm.instance.Instance)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 UniformDelegateServerCallStreamObserver (build.buildfarm.common.grpc.UniformDelegateServerCallStreamObserver)2 ByteStreamUploader (build.buildfarm.instance.stub.ByteStreamUploader)2 StreamObserver (io.grpc.stub.StreamObserver)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 EntryLimitException (build.buildfarm.common.EntryLimitException)1 InvalidResourceNameException (build.buildfarm.common.UrlPath.InvalidResourceNameException)1