Search in sources :

Example 1 with OutputDirectory

use of build.buildfarm.worker.OutputDirectory in project bazel-buildfarm by bazelbuild.

the class OperationQueueWorkerContext method createExecDir.

@Override
public Path createExecDir(String operationName, Map<Digest, Directory> directoriesIndex, Action action, Command command) throws IOException, InterruptedException {
    OutputDirectory outputDirectory = OutputDirectory.parse(command.getOutputFilesList(), command.getOutputDirectoriesList(), command.getEnvironmentVariablesList());
    Path execDir = root.resolve(operationName);
    if (Files.exists(execDir)) {
        Directories.remove(execDir);
    }
    Files.createDirectories(execDir);
    ImmutableList.Builder<String> inputFiles = new ImmutableList.Builder<>();
    ImmutableList.Builder<Digest> inputDirectories = new ImmutableList.Builder<>();
    boolean fetched = false;
    try {
        fetchInputs(execDir, action.getInputRootDigest(), directoriesIndex, outputDirectory, inputFiles, inputDirectories);
        fetched = true;
    } finally {
        if (!fetched) {
            fileCache.decrementReferences(inputFiles.build(), inputDirectories.build());
        }
    }
    rootInputFiles.put(execDir, inputFiles.build());
    rootInputDirectories.put(execDir, inputDirectories.build());
    boolean stamped = false;
    try {
        outputDirectory.stamp(execDir);
        stamped = true;
    } finally {
        if (!stamped) {
            destroyExecDir(execDir);
        }
    }
    if (owner != null) {
        Directories.setAllOwner(execDir, owner);
    }
    return execDir;
}
Also used : Path(java.nio.file.Path) OutputDirectory(build.buildfarm.worker.OutputDirectory) Digest(build.bazel.remote.execution.v2.Digest) ImmutableList(com.google.common.collect.ImmutableList) ByteString(com.google.protobuf.ByteString)

Example 2 with OutputDirectory

use of build.buildfarm.worker.OutputDirectory in project bazel-buildfarm by bazelbuild.

the class OperationQueueWorkerContext method fetchInputs.

private void fetchInputs(Path execDir, Digest inputRoot, Map<Digest, Directory> directoriesIndex, OutputDirectory outputDirectory, ImmutableList.Builder<String> inputFiles, ImmutableList.Builder<Digest> inputDirectories) throws IOException, InterruptedException {
    Directory directory;
    if (inputRoot.getSizeBytes() == 0) {
        directory = Directory.getDefaultInstance();
    } else {
        directory = directoriesIndex.get(inputRoot);
        if (directory == null) {
            throw new IOException("Directory " + DigestUtil.toString(inputRoot) + " is not in input index");
        }
    }
    getInterruptiblyOrIOException(allAsList(fileCache.putFiles(directory.getFilesList(), directory.getSymlinksList(), execDir, inputFiles, newDirectExecutorService())));
    for (DirectoryNode directoryNode : directory.getDirectoriesList()) {
        Digest digest = directoryNode.getDigest();
        String name = directoryNode.getName();
        OutputDirectory childOutputDirectory = outputDirectory != null ? outputDirectory.getChild(name) : null;
        Path dirPath = execDir.resolve(name);
        if (childOutputDirectory != null || !config.getLinkInputDirectories()) {
            Files.createDirectories(dirPath);
            fetchInputs(dirPath, digest, directoriesIndex, childOutputDirectory, inputFiles, inputDirectories);
        } else {
            inputDirectories.add(digest);
            linkDirectory(dirPath, digest, directoriesIndex);
        }
    }
}
Also used : Path(java.nio.file.Path) Digest(build.bazel.remote.execution.v2.Digest) OutputDirectory(build.buildfarm.worker.OutputDirectory) DirectoryNode(build.bazel.remote.execution.v2.DirectoryNode) Utils.getInterruptiblyOrIOException(build.buildfarm.common.io.Utils.getInterruptiblyOrIOException) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString) OutputDirectory(build.buildfarm.worker.OutputDirectory) Directory(build.bazel.remote.execution.v2.Directory)

Example 3 with OutputDirectory

use of build.buildfarm.worker.OutputDirectory in project bazel-buildfarm by bazelbuild.

the class CFCExecFileSystem method createExecDir.

@Override
public Path createExecDir(String operationName, Map<Digest, Directory> directoriesIndex, Action action, Command command) throws IOException, InterruptedException {
    Digest inputRootDigest = action.getInputRootDigest();
    OutputDirectory outputDirectory = OutputDirectory.parse(command.getOutputFilesList(), concat(command.getOutputDirectoriesList(), realDirectories(directoriesIndex, inputRootDigest)), command.getEnvironmentVariablesList());
    Path execDir = root.resolve(operationName);
    if (Files.exists(execDir)) {
        Directories.remove(execDir);
    }
    Files.createDirectories(execDir);
    ImmutableList.Builder<String> inputFiles = new ImmutableList.Builder<>();
    ImmutableList.Builder<Digest> inputDirectories = new ImmutableList.Builder<>();
    logger.log(Level.FINE, "ExecFileSystem::createExecDir(" + operationName + ") calling fetchInputs");
    Iterable<ListenableFuture<Void>> fetchedFutures = fetchInputs(execDir, inputRootDigest, directoriesIndex, outputDirectory, inputFiles, inputDirectories);
    boolean success = false;
    try {
        InterruptedException exception = null;
        boolean wasInterrupted = false;
        ImmutableList.Builder<Throwable> exceptions = ImmutableList.builder();
        for (ListenableFuture<Void> fetchedFuture : fetchedFutures) {
            if (exception != null || wasInterrupted) {
                fetchedFuture.cancel(true);
            } else {
                try {
                    fetchedFuture.get();
                } catch (ExecutionException e) {
                    // just to ensure that no other code can react to interrupt status
                    exceptions.add(e.getCause());
                } catch (InterruptedException e) {
                    fetchedFuture.cancel(true);
                    exception = e;
                }
            }
            wasInterrupted = Thread.interrupted() || wasInterrupted;
        }
        if (wasInterrupted) {
            Thread.currentThread().interrupt();
            // unlikely, but worth guarding
            if (exception == null) {
                exception = new InterruptedException();
            }
        }
        if (exception != null) {
            throw exception;
        }
        checkExecErrors(execDir, exceptions.build());
        success = true;
    } finally {
        if (!success) {
            fileCache.decrementReferences(inputFiles.build(), inputDirectories.build());
            Directories.remove(execDir);
        }
    }
    rootInputFiles.put(execDir, inputFiles.build());
    rootInputDirectories.put(execDir, inputDirectories.build());
    logger.log(Level.FINE, "ExecFileSystem::createExecDir(" + operationName + ") stamping output directories");
    boolean stamped = false;
    try {
        outputDirectory.stamp(execDir);
        stamped = true;
    } finally {
        if (!stamped) {
            destroyExecDir(execDir);
        }
    }
    if (owner != null) {
        Directories.setAllOwner(execDir, owner);
    }
    return execDir;
}
Also used : Path(java.nio.file.Path) Digest(build.bazel.remote.execution.v2.Digest) OutputDirectory(build.buildfarm.worker.OutputDirectory) ImmutableList(com.google.common.collect.ImmutableList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with OutputDirectory

use of build.buildfarm.worker.OutputDirectory in project bazel-buildfarm by bazelbuild.

the class CFCExecFileSystem method fetchInputs.

private Iterable<ListenableFuture<Void>> fetchInputs(Path path, Digest directoryDigest, Map<Digest, Directory> directoriesIndex, OutputDirectory outputDirectory, ImmutableList.Builder<String> inputFiles, ImmutableList.Builder<Digest> inputDirectories) throws IOException {
    Directory directory = directoriesIndex.get(directoryDigest);
    if (directory == null) {
        // not quite IO...
        throw new IOException("Directory " + DigestUtil.toString(directoryDigest) + " is not in directories index");
    }
    Iterable<ListenableFuture<Void>> downloads = directory.getFilesList().stream().map(fileNode -> put(path, fileNode, inputFiles)).collect(ImmutableList.toImmutableList());
    downloads = concat(downloads, directory.getSymlinksList().stream().map(symlinkNode -> putSymlink(path, symlinkNode)).collect(ImmutableList.toImmutableList()));
    for (DirectoryNode directoryNode : directory.getDirectoriesList()) {
        Digest digest = directoryNode.getDigest();
        String name = directoryNode.getName();
        OutputDirectory childOutputDirectory = outputDirectory != null ? outputDirectory.getChild(name) : null;
        Path dirPath = path.resolve(name);
        if (childOutputDirectory != null || !linkInputDirectories) {
            Files.createDirectories(dirPath);
            downloads = concat(downloads, fetchInputs(dirPath, digest, directoriesIndex, childOutputDirectory, inputFiles, inputDirectories));
        } else {
            downloads = concat(downloads, ImmutableList.of(transform(linkDirectory(dirPath, digest, directoriesIndex), (result) -> {
                // we saw null entries in the built immutable list without synchronization
                synchronized (inputDirectories) {
                    inputDirectories.add(digest);
                }
                return null;
            }, fetchService)));
        }
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
    }
    return downloads;
}
Also used : Directory(build.bazel.remote.execution.v2.Directory) MoreExecutors.listeningDecorator(com.google.common.util.concurrent.MoreExecutors.listeningDecorator) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Command(build.bazel.remote.execution.v2.Command) DirectoryNode(build.bazel.remote.execution.v2.DirectoryNode) Futures.transformAsync(com.google.common.util.concurrent.Futures.transformAsync) Executors.newWorkStealingPool(java.util.concurrent.Executors.newWorkStealingPool) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Utils.getInterruptiblyOrIOException(build.buildfarm.common.io.Utils.getInterruptiblyOrIOException) ContentAddressableStorage(build.buildfarm.cas.ContentAddressableStorage) DigestUtil(build.buildfarm.common.DigestUtil) Level(java.util.logging.Level) Action(build.bazel.remote.execution.v2.Action) UserPrincipal(java.nio.file.attribute.UserPrincipal) Directories(build.buildfarm.common.io.Directories) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Digest(build.bazel.remote.execution.v2.Digest) Iterables.concat(com.google.common.collect.Iterables.concat) Map(java.util.Map) FileNode(build.bazel.remote.execution.v2.FileNode) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) Utils.readdir(build.buildfarm.common.io.Utils.readdir) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) MoreExecutors.shutdownAndAwaitTermination(com.google.common.util.concurrent.MoreExecutors.shutdownAndAwaitTermination) Dirent(build.buildfarm.common.io.Dirent) CASFileCache(build.buildfarm.cas.cfc.CASFileCache) Futures.allAsList(com.google.common.util.concurrent.Futures.allAsList) Files(java.nio.file.Files) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Logger(java.util.logging.Logger) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) Futures.immediateFailedFuture(com.google.common.util.concurrent.Futures.immediateFailedFuture) Iterables.filter(com.google.common.collect.Iterables.filter) SymlinkNode(build.bazel.remote.execution.v2.SymlinkNode) OutputDirectory(build.buildfarm.worker.OutputDirectory) Futures.transform(com.google.common.util.concurrent.Futures.transform) Collections(java.util.Collections) InputStream(java.io.InputStream) Path(java.nio.file.Path) Digest(build.bazel.remote.execution.v2.Digest) OutputDirectory(build.buildfarm.worker.OutputDirectory) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DirectoryNode(build.bazel.remote.execution.v2.DirectoryNode) Utils.getInterruptiblyOrIOException(build.buildfarm.common.io.Utils.getInterruptiblyOrIOException) IOException(java.io.IOException) Directory(build.bazel.remote.execution.v2.Directory) OutputDirectory(build.buildfarm.worker.OutputDirectory)

Aggregations

Digest (build.bazel.remote.execution.v2.Digest)4 OutputDirectory (build.buildfarm.worker.OutputDirectory)4 Path (java.nio.file.Path)4 ImmutableList (com.google.common.collect.ImmutableList)3 Directory (build.bazel.remote.execution.v2.Directory)2 DirectoryNode (build.bazel.remote.execution.v2.DirectoryNode)2 Utils.getInterruptiblyOrIOException (build.buildfarm.common.io.Utils.getInterruptiblyOrIOException)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 ByteString (com.google.protobuf.ByteString)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Action (build.bazel.remote.execution.v2.Action)1 Command (build.bazel.remote.execution.v2.Command)1 FileNode (build.bazel.remote.execution.v2.FileNode)1 SymlinkNode (build.bazel.remote.execution.v2.SymlinkNode)1 ContentAddressableStorage (build.buildfarm.cas.ContentAddressableStorage)1 CASFileCache (build.buildfarm.cas.cfc.CASFileCache)1 DigestUtil (build.buildfarm.common.DigestUtil)1 Directories (build.buildfarm.common.io.Directories)1 Dirent (build.buildfarm.common.io.Dirent)1