Search in sources :

Example 1 with Write

use of build.buildfarm.common.Write in project bazel-buildfarm by bazelbuild.

the class CASFileCache method expireEntryFallback.

private void expireEntryFallback(Entry e) throws IOException, InterruptedException {
    if (delegate != null) {
        FileEntryKey fileEntryKey = parseFileEntryKey(e.key, e.size);
        if (fileEntryKey == null) {
            logger.log(Level.SEVERE, format("error parsing expired key %s", e.key));
        } else {
            Write write = delegate.getWrite(fileEntryKey.getDigest(), UUID.randomUUID(), RequestMetadata.getDefaultInstance());
            performCopy(write, e);
        }
    }
}
Also used : CompleteWrite(build.buildfarm.common.Write.CompleteWrite) Write(build.buildfarm.common.Write)

Example 2 with Write

use of build.buildfarm.common.Write in project bazel-buildfarm by bazelbuild.

the class MemoryCAS method expireEntry.

@GuardedBy("this")
private void expireEntry(Entry e) {
    Digest digest = DigestUtil.buildDigest(e.key, e.value.size());
    logger.log(Level.INFO, "MemoryLRUCAS: expiring " + DigestUtil.toString(digest));
    if (delegate != null) {
        try {
            Write write = delegate.getWrite(digest, UUID.randomUUID(), RequestMetadata.getDefaultInstance());
            try (OutputStream out = write.getOutput(1, MINUTES, () -> {
            })) {
                e.value.getData().writeTo(out);
            }
        } catch (IOException ioEx) {
            logger.log(Level.SEVERE, String.format("error delegating %s", DigestUtil.toString(digest)), ioEx);
        }
    }
    storage.remove(e.key);
    e.expire();
    sizeInBytes -= digest.getSizeBytes();
}
Also used : Write(build.buildfarm.common.Write) Digest(build.bazel.remote.execution.v2.Digest) OutputStream(java.io.OutputStream) IOException(java.io.IOException) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 3 with Write

use of build.buildfarm.common.Write in project bazel-buildfarm by bazelbuild.

the class CASFileCacheTest method readThroughSwitchedToLocalContinues.

@Test
public void readThroughSwitchedToLocalContinues() throws Exception {
    ByteString content = ByteString.copyFromUtf8("Hello, World");
    Blob blob = new Blob(content, DIGEST_UTIL);
    ExecutorService service = newSingleThreadExecutor();
    SettableFuture<Void> writeComplete = SettableFuture.create();
    // we need to register callbacks on the shared write future
    Write write = new NullWrite() {

        @Override
        public ListenableFuture<Long> getFuture() {
            return Futures.transform(writeComplete, result -> blob.getDigest().getSizeBytes(), directExecutor());
        }

        @Override
        public FeedbackOutputStream getOutput(long deadlineAfter, TimeUnit deadlineAfterUnits, Runnable onReadyHandler) {
            return new FeedbackOutputStream() {

                int offset = 0;

                @Override
                public void write(int b) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public void write(byte[] buf, int ofs, int len) throws IOException {
                    // hangs on second read
                    if (offset == 6) {
                        service.submit(() -> writeComplete.set(null));
                        throw new ClosedChannelException();
                    }
                    offset += len;
                }

                @Override
                public boolean isReady() {
                    return true;
                }
            };
        }
    };
    when(delegate.getWrite(eq(blob.getDigest()), any(UUID.class), any(RequestMetadata.class))).thenReturn(write);
    when(delegate.newInput(eq(blob.getDigest()), eq(0L))).thenReturn(content.newInput());
    // the switch will reset to this point
    InputStream switchedIn = content.newInput();
    switchedIn.skip(6);
    when(delegate.newInput(eq(blob.getDigest()), eq(6L))).thenReturn(switchedIn);
    InputStream in = fileCache.newReadThroughInput(blob.getDigest(), 0, write);
    byte[] buf = new byte[content.size()];
    // advance to the middle of the content
    assertThat(in.read(buf, 0, 6)).isEqualTo(6);
    assertThat(ByteString.copyFrom(buf, 0, 6)).isEqualTo(content.substring(0, 6));
    verify(delegate, times(1)).newInput(blob.getDigest(), 0L);
    // read the remaining content
    int remaining = content.size() - 6;
    assertThat(in.read(buf, 6, remaining)).isEqualTo(remaining);
    assertThat(ByteString.copyFrom(buf)).isEqualTo(content);
    if (!shutdownAndAwaitTermination(service, 1, SECONDS)) {
        throw new RuntimeException("could not shut down service");
    }
}
Also used : NullWrite(build.buildfarm.common.Write.NullWrite) Write(build.buildfarm.common.Write) ClosedChannelException(java.nio.channels.ClosedChannelException) Blob(build.buildfarm.cas.ContentAddressableStorage.Blob) ByteString(com.google.protobuf.ByteString) InputStream(java.io.InputStream) FeedbackOutputStream(build.buildfarm.common.io.FeedbackOutputStream) RequestMetadata(build.bazel.remote.execution.v2.RequestMetadata) NullWrite(build.buildfarm.common.Write.NullWrite) ExecutorService(java.util.concurrent.ExecutorService) TimeUnit(java.util.concurrent.TimeUnit) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with Write

use of build.buildfarm.common.Write in project bazel-buildfarm by bazelbuild.

the class CASFileCacheTest method incompleteWriteFileIsResumed.

@Test
public void incompleteWriteFileIsResumed() throws IOException {
    ByteString content = ByteString.copyFromUtf8("Hello, World");
    Digest digest = DIGEST_UTIL.compute(content);
    UUID writeId = UUID.randomUUID();
    String key = fileCache.getKey(digest, false);
    Path writePath = fileCache.getPath(key).resolveSibling(key + "." + writeId);
    try (OutputStream out = Files.newOutputStream(writePath)) {
        content.substring(0, 6).writeTo(out);
    }
    Write write = fileCache.getWrite(digest, writeId, RequestMetadata.getDefaultInstance());
    AtomicBoolean notified = new AtomicBoolean(false);
    write.getFuture().addListener(() -> notified.set(true), directExecutor());
    assertThat(write.getCommittedSize()).isEqualTo(6);
    try (OutputStream out = write.getOutput(1, SECONDS, () -> {
    })) {
        content.substring(6).writeTo(out);
    }
    assertThat(notified.get()).isTrue();
    assertThat(write.getCommittedSize()).isEqualTo(digest.getSizeBytes());
    assertThat(write.isComplete()).isTrue();
}
Also used : Path(java.nio.file.Path) NullWrite(build.buildfarm.common.Write.NullWrite) Write(build.buildfarm.common.Write) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) FeedbackOutputStream(build.buildfarm.common.io.FeedbackOutputStream) OutputStream(java.io.OutputStream) ByteString(com.google.protobuf.ByteString) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with Write

use of build.buildfarm.common.Write in project bazel-buildfarm by bazelbuild.

the class GrpcCASTest method writeIsResumable.

@Test
public void writeIsResumable() throws Exception {
    UUID uuid = UUID.randomUUID();
    ByteString writeContent = ByteString.copyFromUtf8("written");
    Digest digest = DIGEST_UTIL.compute(writeContent);
    String instanceName = "test";
    HashCode hash = HashCode.fromString(digest.getHash());
    String resourceName = ByteStreamUploader.uploadResourceName(instanceName, uuid, hash, digest.getSizeBytes());
    // better test might just put a full gRPC CAS behind an in-process and validate state
    SettableFuture<ByteString> content = SettableFuture.create();
    serviceRegistry.addService(new ByteStreamServiceWriter(resourceName, content, (int) digest.getSizeBytes()));
    Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
    GrpcCAS cas = new GrpcCAS(instanceName, channel, /* uploader=*/
    null, onExpirations);
    RequestMetadata requestMetadata = RequestMetadata.getDefaultInstance();
    Write initialWrite = cas.getWrite(digest, uuid, requestMetadata);
    try (OutputStream writeOut = initialWrite.getOutput(1, SECONDS, () -> {
    })) {
        writeContent.substring(0, 4).writeTo(writeOut);
    }
    Write finalWrite = cas.getWrite(digest, uuid, requestMetadata);
    try (OutputStream writeOut = finalWrite.getOutput(1, SECONDS, () -> {
    })) {
        writeContent.substring(4).writeTo(writeOut);
    }
    assertThat(content.get(1, TimeUnit.SECONDS)).isEqualTo(writeContent);
}
Also used : Write(build.buildfarm.common.Write) HashCode(com.google.common.hash.HashCode) Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) Channel(io.grpc.Channel) OutputStream(java.io.OutputStream) ByteString(com.google.protobuf.ByteString) UUID(java.util.UUID) ByteStreamServiceWriter(build.buildfarm.common.grpc.ByteStreamServiceWriter) RequestMetadata(build.bazel.remote.execution.v2.RequestMetadata) Test(org.junit.Test)

Aggregations

Write (build.buildfarm.common.Write)24 ByteString (com.google.protobuf.ByteString)18 Digest (build.bazel.remote.execution.v2.Digest)15 Test (org.junit.Test)15 OutputStream (java.io.OutputStream)13 FeedbackOutputStream (build.buildfarm.common.io.FeedbackOutputStream)12 NullWrite (build.buildfarm.common.Write.NullWrite)10 IOException (java.io.IOException)10 UUID (java.util.UUID)8 RequestMetadata (build.bazel.remote.execution.v2.RequestMetadata)6 ExecutionException (java.util.concurrent.ExecutionException)5 TimeUnit (java.util.concurrent.TimeUnit)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 CompleteWrite (build.buildfarm.common.Write.CompleteWrite)4 Instance (build.buildfarm.instance.Instance)4 WriteRequest (com.google.bytestream.ByteStreamProto.WriteRequest)4 ServerCallStreamObserver (io.grpc.stub.ServerCallStreamObserver)4 InputStream (java.io.InputStream)4 Blob (build.buildfarm.cas.ContentAddressableStorage.Blob)3 DigestUtil (build.buildfarm.common.DigestUtil)3