Search in sources :

Example 6 with ByteStreamStub

use of com.google.bytestream.ByteStreamGrpc.ByteStreamStub in project bazel-buildfarm by bazelbuild.

the class ByteStreamServiceTest method uploadsCanResetInLine.

@Test
public void uploadsCanResetInLine() 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 flush() throws IOException {
            output.flush();
        }

        @Override
        public void write(byte[] b) throws IOException {
            output.write(b);
        }

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

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

        @Override
        public boolean isReady() {
            return true;
        }
    };
    Write write = mock(Write.class);
    doAnswer((Answer<Void>) invocation -> {
        output.reset();
        return null;
    }).when(write).reset();
    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.onNext(WriteRequest.newBuilder().setWriteOffset(0).setData(content).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(1)).reset();
    verify(write, times(1)).getFuture();
}
Also used : Write(build.buildfarm.common.Write) 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) 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) AdditionalAnswers.answerVoid(org.mockito.AdditionalAnswers.answerVoid) Test(org.junit.Test)

Example 7 with ByteStreamStub

use of com.google.bytestream.ByteStreamGrpc.ByteStreamStub in project bazel-buildfarm by bazelbuild.

the class Extract method downloadActionContents.

@SuppressWarnings("ResultOfMethodCallIgnored")
static void downloadActionContents(Path root, String instanceName, Set<Digest> actionDigests, Channel channel) throws IOException, InterruptedException {
    ByteStreamStub bsStub = ByteStreamGrpc.newStub(channel);
    ExecutorService service = newSingleThreadExecutor();
    ListeningScheduledExecutorService retryService = listeningDecorator(newSingleThreadScheduledExecutor());
    Set<Digest> visitedDigests = Sets.newHashSet();
    Set<Digest> visitedDirectories = Sets.newHashSet();
    AtomicLong outstandingOperations = new AtomicLong(0);
    for (Digest actionDigest : actionDigests) {
        ByteString content = getBlobIntoFile("action", instanceName, actionDigest, bsStub, root);
        Action action = Action.parseFrom(content);
        Digest commandDigest = action.getCommandDigest();
        if (!visitedDigests.contains(commandDigest)) {
            visitedDigests.add(commandDigest);
            outstandingOperations.getAndIncrement();
            service.execute(blobGetter(root, instanceName, commandDigest, bsStub, outstandingOperations, retryService));
        }
        Digest inputRootDigest = action.getInputRootDigest();
        if (!visitedDigests.contains(inputRootDigest)) {
            visitedDirectories.add(inputRootDigest);
            visitedDigests.add(inputRootDigest);
            outstandingOperations.getAndIncrement();
            service.execute(directoryGetter(root, instanceName, inputRootDigest, visitedDirectories, visitedDigests, bsStub, service, outstandingOperations, retryService));
        }
    }
    while (outstandingOperations.get() > 0) {
        System.out.println("Waiting on " + outstandingOperations.get() + " operations");
        TimeUnit.SECONDS.sleep(5);
    }
    service.shutdown();
    service.awaitTermination(1, TimeUnit.MINUTES);
    retryService.shutdown();
    retryService.awaitTermination(1, TimeUnit.MINUTES);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Action(build.bazel.remote.execution.v2.Action) ListeningScheduledExecutorService(com.google.common.util.concurrent.ListeningScheduledExecutorService) Digest(build.bazel.remote.execution.v2.Digest) ByteStreamStub(com.google.bytestream.ByteStreamGrpc.ByteStreamStub) ByteString(com.google.protobuf.ByteString) ListeningScheduledExecutorService(com.google.common.util.concurrent.ListeningScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

ByteStreamStub (com.google.bytestream.ByteStreamGrpc.ByteStreamStub)7 ByteString (com.google.protobuf.ByteString)7 Digest (build.bazel.remote.execution.v2.Digest)6 HashCode (com.google.common.hash.HashCode)5 Channel (io.grpc.Channel)5 WriteRequest (com.google.bytestream.ByteStreamProto.WriteRequest)4 UUID (java.util.UUID)4 DigestUtil (build.buildfarm.common.DigestUtil)3 Write (build.buildfarm.common.Write)3 FeedbackOutputStream (build.buildfarm.common.io.FeedbackOutputStream)3 ByteStreamGrpc (com.google.bytestream.ByteStreamGrpc)3 ReadRequest (com.google.bytestream.ByteStreamProto.ReadRequest)3 ReadResponse (com.google.bytestream.ByteStreamProto.ReadResponse)3 WriteResponse (com.google.bytestream.ByteStreamProto.WriteResponse)3 SettableFuture (com.google.common.util.concurrent.SettableFuture)3 StreamObserver (io.grpc.stub.StreamObserver)3 IOException (java.io.IOException)3 List (java.util.List)3 TimeUnit (java.util.concurrent.TimeUnit)3 SECONDS (java.util.concurrent.TimeUnit.SECONDS)3