Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -386,6 +387,16 @@ public int sizeForDebugging() {
return size;
}

/** Visits the paths of present inputs without copying the map. */
public void forEachPresentPath(Consumer<? super PathFragment> consumer) {
for (int i = 0; i < size; i++) {
if (values[i] != FileArtifactValue.MISSING_FILE_MARKER
&& values[i] != TreeArtifactValue.MISSING_TREE_ARTIFACT) {
consumer.accept(keys[i].getExecPath());
}
}
}

public void put(ActionInput input, FileArtifactValue metadata) {
checkArgument(
!isTreeArtifact(input),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ public void createActionFsOutputDirectories(

PathFragment rootPath =
artifactPathResolver.convertPath(outputFile.getRoot().getRoot().asPath()).asFragment();
forceCreateDirectoryAndParents(outputDir, rootPath);
// The action filesystem may expose input directories over stale physical files. Repair
// the host path before populating the action filesystem's own directory state.
forceCreateDirectoryAndParents(outputDir.forHostFileSystem(), rootPath);
try {
outputDir.createDirectoryAndParents();
outputDir.setWritable(true);
} catch (IOException e) {
throw new CreateOutputDirectoryException(outputDir.asFragment(), e);
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/google/devtools/build/lib/remote/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,10 @@ java_library(

java_library(
name = "remote_action_file_system",
srcs = ["RemoteActionFileSystem.java"],
srcs = [
"InputDirectoryIndex.java",
"RemoteActionFileSystem.java",
],
visibility = [
"//src/main/java/com/google/devtools/build/lib:build_lib",
"//src/tools/remote:__subpackages__",
Expand All @@ -486,10 +489,13 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/actions:action_input",
"//src/main/java/com/google/devtools/build/lib/actions:artifacts",
"//src/main/java/com/google/devtools/build/lib/actions:file_metadata",
"//src/main/java/com/google/devtools/build/lib/actions:fileset_output_symlink",
"//src/main/java/com/google/devtools/build/lib/actions:fileset_output_tree",
"//src/main/java/com/google/devtools/build/lib/actions:important_output_handler",
"//src/main/java/com/google/devtools/build/lib/clock",
"//src/main/java/com/google/devtools/build/lib/remote/common:bulk_transfer_exception",
"//src/main/java/com/google/devtools/build/lib/remote/util:futures",
"//src/main/java/com/google/devtools/build/lib/skyframe:action_input_metadata_provider",
"//src/main/java/com/google/devtools/build/lib/skyframe:tree_artifact_value",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2026 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.remote;

import com.google.devtools.build.lib.actions.ActionInputMap;
import com.google.devtools.build.lib.actions.FilesetOutputSymlink;
import com.google.devtools.build.lib.actions.FilesetOutputTree;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;

/** Directories and children implied by an action's checked input paths. */
final class InputDirectoryIndex {
private final ActionInputMap inputs;
@Nullable private volatile PathFragment[] paths;

InputDirectoryIndex(ActionInputMap inputs) {
this.inputs = inputs;
}

/** Invalidates derived paths between action phases, when no filesystem operations are running. */
void clear() {
paths = null;
}

boolean isDirectory(PathFragment execPath) {
PathFragment[] current = paths();
int next = upperBound(current, execPath);
return next < current.length && current[next].startsWith(execPath);
}

List<PathFragment> children(PathFragment execPath) {
PathFragment[] current = paths();
List<PathFragment> children = new ArrayList<>();
int next = upperBound(current, execPath);
while (next < current.length && current[next].startsWith(execPath)) {
PathFragment child = current[next].subFragment(0, execPath.segmentCount() + 1);
children.add(child);
next++;
// Avoid a binary search for individual files.
if (next < current.length && current[next].startsWith(child)) {
next = endOfSubtree(current, child, next);
}
}
return children;
}

private PathFragment[] paths() {
PathFragment[] current = paths;
if (current == null) {
synchronized (this) {
current = paths;
if (current == null) {
List<PathFragment> sorted = new ArrayList<>();
inputs.forEachPresentPath(sorted::add);
for (FilesetOutputTree fileset : inputs.getFilesets().values()) {
for (FilesetOutputSymlink link : fileset.symlinks()) {
sorted.add(link.target().getExecPath());
}
}
sorted.sort(PathFragment.HIERARCHICAL_COMPARATOR);
paths = current = sorted.toArray(PathFragment[]::new);
}
}
}
return current;
}

/** Returns the first path strictly after the candidate, excluding an exact file match. */
private static int upperBound(PathFragment[] paths, PathFragment candidate) {
int low = 0;
int high = paths.length;
while (low < high) {
int mid = (low + high) >>> 1;
if (PathFragment.HIERARCHICAL_COMPARATOR.compare(paths[mid], candidate) <= 0) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}

/** Descendants are contiguous in hierarchical order; skip them without visiting every input. */
private static int endOfSubtree(PathFragment[] paths, PathFragment parent, int low) {
int high = paths.length;
while (low < high) {
int mid = (low + high) >>> 1;
if (paths[mid].startsWith(parent)) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ interface Resolver {
* @throws IOException if an I/O error occurs
*/
PathFragment readSymbolicLink(PathFragment path) throws IOException;

/** Whether a directory may be reused without checking its status on the next traversal. */
boolean cacheDirectory(FileStatus status);
}

/** A trie node. */
Expand All @@ -77,8 +80,15 @@ private record SymlinkNode(PathFragment targetPath) implements Node {}

/** A trie node corresponding to a directory. */
private static final class DirectoryNode extends ConcurrentHashMap<String, Node> implements Node {
private final boolean revalidate;

DirectoryNode() {
this(false);
}

DirectoryNode(boolean revalidate) {
super(/* initialCapacity= */ 1);
this.revalidate = revalidate;
}
}

Expand Down Expand Up @@ -134,21 +144,29 @@ private PathFragment resolveSymbolicLinks(PathFragment path, int maxLinks) throw
// or to the root path when `segmentIndex` is 0.
for (String segment : segments) {
Node nextNode = node.get(segment);
if (nextNode == null) {
if (nextNode == null || nextNode instanceof DirectoryNode directory && directory.revalidate) {
PathFragment naivePath = path.subFragment(0, segmentIndex + 1);
FileStatus status = resolver.statIfFound(naivePath);
if (status == null) {
throw new FileNotFoundException(naivePath.getPathString() + ERR_NO_SUCH_FILE_OR_DIR);
}
Node resolvedNode;
if (status.isSymbolicLink()) {
resolvedNode = new SymlinkNode(resolver.readSymbolicLink(naivePath));
} else if (status.isDirectory()) {
resolvedNode = new DirectoryNode();
} else {
resolvedNode = NonDirectoryNode.INSTANCE;
if (nextNode == null) {
Node resolvedNode;
if (status.isSymbolicLink()) {
resolvedNode = new SymlinkNode(resolver.readSymbolicLink(naivePath));
} else if (status.isDirectory()) {
resolvedNode = new DirectoryNode(/* revalidate= */ !resolver.cacheDirectory(status));
} else {
resolvedNode = NonDirectoryNode.INSTANCE;
}
nextNode = node.computeIfAbsent(segment, unused -> resolvedNode);
} else if (status.isSymbolicLink()) {
// Retain the virtual directory as a probe if the physical symlink is later removed.
nextNode = new SymlinkNode(resolver.readSymbolicLink(naivePath));
} else if (!status.isDirectory()) {
// Likewise, preserve its cached children if a physical file is later removed.
nextNode = NonDirectoryNode.INSTANCE;
}
nextNode = node.computeIfAbsent(segment, unused -> resolvedNode);
}

switch (nextNode) {
Expand Down
Loading