Search in sources :

Example 1 with ReadResponse

use of com.google.bytestream.ByteStreamProto.ReadResponse in project tools_remote by bazelbuild.

the class GrpcRemoteCacheTest method testDownloadBlobSingleChunk.

@Test
public void testDownloadBlobSingleChunk() throws Exception {
    final GrpcRemoteCache client = newClient();
    final Digest digest = DIGEST_UTIL.computeAsUtf8("abcdefg");
    serviceRegistry.addService(new ByteStreamImplBase() {

        @Override
        public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
            assertThat(request.getResourceName().contains(digest.getHash())).isTrue();
            responseObserver.onNext(ReadResponse.newBuilder().setData(ByteString.copyFromUtf8("abcdefg")).build());
            responseObserver.onCompleted();
        }
    });
    assertThat(new String(client.downloadBlob(digest), UTF_8)).isEqualTo("abcdefg");
}
Also used : Digest(build.bazel.remote.execution.v2.Digest) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) ByteString(com.google.protobuf.ByteString) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Example 2 with ReadResponse

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

the class Extract method getBlob.

static ByteString getBlob(String instanceName, Digest digest, ByteStreamStub bsStub) throws InterruptedException {
    SettableFuture<ByteString> blobFuture = SettableFuture.create();
    bsStub.read(ReadRequest.newBuilder().setResourceName(blobName(instanceName, digest)).build(), new StreamObserver<ReadResponse>() {

        final ByteString.Output out = ByteString.newOutput((int) digest.getSizeBytes());

        @Override
        public void onNext(ReadResponse response) {
            try {
                response.getData().writeTo(out);
            } catch (IOException e) {
                blobFuture.setException(e);
            }
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            if (status.getCode() == Code.NOT_FOUND) {
                t = new NoSuchFileException(digest.getHash() + "/" + digest.getSizeBytes());
            }
            blobFuture.setException(t);
        }

        @Override
        public void onCompleted() {
            blobFuture.set(out.toByteString());
        }
    });
    try {
        return blobFuture.get();
    } catch (ExecutionException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw new RuntimeException(e.getCause());
        }
        throw new UncheckedExecutionException(e.getCause());
    }
}
Also used : Status(io.grpc.Status) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ByteString(com.google.protobuf.ByteString) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with ReadResponse

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

the class GrpcCASTest method onExpirationCalledWhenNotFound.

@Test
public void onExpirationCalledWhenNotFound() {
    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());
        }
    });
    Runnable onExpiration = mock(Runnable.class);
    onExpirations.put(digest, onExpiration);
    GrpcCAS cas = new GrpcCAS(instanceName, InProcessChannelBuilder.forName(fakeServerName).directExecutor().build(), mock(ByteStreamUploader.class), onExpirations);
    assertThat(cas.get(digest)).isNull();
    assertThat(readCalled.get()).isTrue();
    verify(onExpiration, times(1)).run();
}
Also used : ByteStreamUploader(build.buildfarm.instance.stub.ByteStreamUploader) Digest(build.bazel.remote.execution.v2.Digest) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(com.google.protobuf.ByteString) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Example 4 with ReadResponse

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

the class StubInstanceTest method inputStreamRetriesOnDeadlineExceededWithProgress.

@Test
public void inputStreamRetriesOnDeadlineExceededWithProgress() throws IOException, InterruptedException {
    ByteString content = ByteString.copyFromUtf8("1");
    serviceRegistry.addService(new ByteStreamImplBase() {

        boolean first = true;

        @Override
        public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
            if (first && request.getReadOffset() == 0) {
                first = false;
                responseObserver.onNext(ReadResponse.newBuilder().setData(content).build());
                responseObserver.onError(Status.DEADLINE_EXCEEDED.asException());
            } else if (request.getReadOffset() == 1) {
                responseObserver.onCompleted();
            } else {
                // all others fail with unimplemented
                responseObserver.onError(Status.UNIMPLEMENTED.asException());
            }
        }
    });
    Instance instance = newStubInstance("input-stream-stalled");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Digest delayedDigest = Digest.newBuilder().setHash("delayed-blob-name").setSizeBytes(1).build();
    try (InputStream in = instance.newBlobInput(delayedDigest, 0, 1, SECONDS, RequestMetadata.getDefaultInstance())) {
        ByteStreams.copy(in, out);
    }
    assertThat(ByteString.copyFrom(out.toByteArray())).isEqualTo(content);
    instance.stop();
}
Also used : ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) Instance(build.buildfarm.instance.Instance) Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) InputStream(java.io.InputStream) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Example 5 with ReadResponse

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

the class StubInstanceTest method inputStreamThrowsNonDeadlineExceededCausal.

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

        @Override
        public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
            responseObserver.onError(Status.UNAVAILABLE.asException());
        }
    });
    OutputStream out = mock(OutputStream.class);
    IOException ioException = null;
    Instance instance = newStubInstance("input-stream-non-deadline-exceeded");
    Digest unavailableDigest = Digest.newBuilder().setHash("unavailable-blob-name").setSizeBytes(1).build();
    try (InputStream in = instance.newBlobInput(unavailableDigest, 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.UNAVAILABLE);
    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

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