Search in sources :

Example 81 with Digest

use of com.google.cloud.kms.v1.Digest 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 82 with Digest

use of com.google.cloud.kms.v1.Digest in project bazel-buildfarm by bazelbuild.

the class ByteStreamServiceTest method uploadsCanProgressAfterCancellation.

@Test
public void uploadsCanProgressAfterCancellation() throws Exception {
    ByteString content = ByteString.copyFromUtf8("Hello, World!");
    Digest digest = DIGEST_UTIL.compute(content);
    UUID uuid = UUID.randomUUID();
    SettableFuture<Long> writtenFuture = SettableFuture.create();
    ByteString.Output output = ByteString.newOutput((int) digest.getSizeBytes());
    FeedbackOutputStream out = new FeedbackOutputStream() {

        @Override
        public void close() {
            if (output.size() == digest.getSizeBytes()) {
                writtenFuture.set(digest.getSizeBytes());
            }
        }

        @Override
        public void write(byte[] b, int off, int len) {
            output.write(b, off, len);
        }

        @Override
        public void write(int b) {
            output.write(b);
        }

        @Override
        public boolean isReady() {
            return true;
        }
    };
    Write write = mock(Write.class);
    when(write.getOutput(any(Long.class), any(TimeUnit.class), any(Runnable.class))).thenReturn(out);
    doAnswer(invocation -> (long) output.size()).when(write).getCommittedSize();
    when(write.getFuture()).thenReturn(writtenFuture);
    when(instance.getBlobWrite(digest, uuid, RequestMetadata.getDefaultInstance())).thenReturn(write);
    HashCode hash = HashCode.fromString(digest.getHash());
    String resourceName = ByteStreamUploader.uploadResourceName(/* instanceName=*/
    null, uuid, hash, digest.getSizeBytes());
    Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
    ByteStreamStub service = ByteStreamGrpc.newStub(channel);
    FutureWriteResponseObserver futureResponder = new FutureWriteResponseObserver();
    StreamObserver<WriteRequest> requestObserver = service.write(futureResponder);
    ByteString shortContent = content.substring(0, 6);
    requestObserver.onNext(WriteRequest.newBuilder().setWriteOffset(0).setResourceName(resourceName).setData(shortContent).build());
    requestObserver.onError(Status.CANCELLED.asException());
    // should be done
    assertThat(futureResponder.isDone()).isTrue();
    futureResponder = new FutureWriteResponseObserver();
    requestObserver = service.write(futureResponder);
    requestObserver.onNext(WriteRequest.newBuilder().setWriteOffset(6).setResourceName(resourceName).setData(content.substring(6)).setFinishWrite(true).build());
    assertThat(futureResponder.get()).isEqualTo(WriteResponse.newBuilder().setCommittedSize(content.size()).build());
    requestObserver.onCompleted();
    verify(write, atLeastOnce()).getCommittedSize();
    verify(write, atLeastOnce()).getOutput(any(Long.class), any(TimeUnit.class), any(Runnable.class));
    verify(write, times(2)).getFuture();
}
Also used : Write(build.buildfarm.common.Write) Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) ByteStreamStub(com.google.bytestream.ByteStreamGrpc.ByteStreamStub) WriteRequest(com.google.bytestream.ByteStreamProto.WriteRequest) Channel(io.grpc.Channel) ByteString(com.google.protobuf.ByteString) FeedbackOutputStream(build.buildfarm.common.io.FeedbackOutputStream) HashCode(com.google.common.hash.HashCode) TimeUnit(java.util.concurrent.TimeUnit) UUID(java.util.UUID) Test(org.junit.Test)

Example 83 with Digest

use of com.google.cloud.kms.v1.Digest in project bazel-buildfarm by bazelbuild.

the class ByteStreamServiceTest method missingWriteQueryIsNotFound.

@Test
public void missingWriteQueryIsNotFound() throws IOException {
    ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!");
    Digest digest = DIGEST_UTIL.compute(helloWorld);
    String uuid = UUID.randomUUID().toString();
    String resourceName = createBlobUploadResourceName(uuid, digest);
    Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
    ByteStreamBlockingStub service = ByteStreamGrpc.newBlockingStub(channel);
    StatusRuntimeException notFoundException = null;
    try {
        service.queryWriteStatus(QueryWriteStatusRequest.newBuilder().setResourceName(resourceName).build());
    } 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) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 84 with Digest

use of com.google.cloud.kms.v1.Digest 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 85 with Digest

use of com.google.cloud.kms.v1.Digest in project bazel-buildfarm by bazelbuild.

the class ByteStreamServiceTest method writePutsIntoBlobStore.

@Test
public void writePutsIntoBlobStore() throws IOException, InterruptedException {
    ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!");
    Digest digest = DIGEST_UTIL.compute(helloWorld);
    String uuid = UUID.randomUUID().toString();
    String resourceName = createBlobUploadResourceName(uuid, digest);
    Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
    ClientCall<WriteRequest, WriteResponse> call = channel.newCall(ByteStreamGrpc.getWriteMethod(), CallOptions.DEFAULT);
    ClientCall.Listener<WriteResponse> callListener = new ClientCall.Listener<WriteResponse>() {

        boolean complete = false;

        boolean callHalfClosed = false;

        @Override
        public void onReady() {
            while (call.isReady()) {
                if (complete) {
                    if (!callHalfClosed) {
                        call.halfClose();
                        callHalfClosed = true;
                    }
                    return;
                }
                call.sendMessage(WriteRequest.newBuilder().setResourceName(resourceName).setData(helloWorld).setFinishWrite(true).build());
                complete = true;
            }
        }
    };
    call.start(callListener, new Metadata());
    call.request(1);
    verify(simpleBlobStore, times(1)).put(eq(digest.getHash()), eq(digest.getSizeBytes()), any(InputStream.class));
}
Also used : Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) WriteRequest(com.google.bytestream.ByteStreamProto.WriteRequest) ClientCall(io.grpc.ClientCall) InputStream(java.io.InputStream) Channel(io.grpc.Channel) WriteResponse(com.google.bytestream.ByteStreamProto.WriteResponse) Metadata(io.grpc.Metadata) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Aggregations

Digest (build.bazel.remote.execution.v2.Digest)191 ByteString (com.google.protobuf.ByteString)110 Test (org.junit.Test)99 IOException (java.io.IOException)55 Directory (build.bazel.remote.execution.v2.Directory)42 ImmutableList (com.google.common.collect.ImmutableList)35 Path (java.nio.file.Path)33 Status (io.grpc.Status)30 ExecutionException (java.util.concurrent.ExecutionException)29 RequestMetadata (build.bazel.remote.execution.v2.RequestMetadata)27 InputStream (java.io.InputStream)25 Instance (build.buildfarm.instance.Instance)24 Action (build.bazel.remote.execution.v2.Action)22 DigestUtil (build.buildfarm.common.DigestUtil)22 OutputStream (java.io.OutputStream)22 Write (build.buildfarm.common.Write)21 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)21 Operation (com.google.longrunning.Operation)21 UUID (java.util.UUID)20 ExecuteResponse (build.bazel.remote.execution.v2.ExecuteResponse)19