-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UnixFileSystem: read cached hashes from extended attributes
There are certain workloads where Bazel's running time gets dominated by checksum computation. Examples include: - People adding local_repository()s to their project that point to networked file shares. - The use of repositories that contain very large input files. When using remote execution, we need to compute digests to be able to place such files in input roots. In many cases, a centralized CAS will already contain these files. It would be nice if Bazel could efficiently check for existence of such objects without needing to scan the file locally. This change extends UnixFileSystem to call getxattr() on an attribute prior to falling back to reading file contents. The name of the extended attribute that is used is configurable through a command line flag. Using extended attributes to store this information also seems to be a fairly common approach. Apparently it is also used within Google itself: https://groups.google.com/g/bazel-discuss/c/6VmjSOLySnY/m/v2dpwt8jBgAJ So far no code has been added to let Bazel write these attributes to disk. The main goal so far is to speed up access to read-only corpora, where the maintainers have spent the effort adding these attributes.
- Loading branch information
1 parent
052e9b7
commit fab90a6
Showing
23 changed files
with
201 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/test/java/com/google/devtools/build/lib/unix/UnixDigestHashAttributeNameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2014 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.unix; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.devtools.build.lib.actions.cache.DigestUtils; | ||
import com.google.devtools.build.lib.vfs.DigestHashFunction; | ||
import com.google.devtools.build.lib.vfs.FileSystem; | ||
import com.google.devtools.build.lib.vfs.FileSystemTest; | ||
import com.google.devtools.build.lib.vfs.Path; | ||
import java.io.IOException; | ||
import org.junit.Test; | ||
|
||
/** Test for {@link com.google.devtools.build.lib.unix.UnixFileSystem#getFastDigest()}. */ | ||
public class UnixDigestHashAttributeNameTest extends FileSystemTest { | ||
private static final byte FAKE_DIGEST[] = { | ||
0x18, 0x5f, 0x3d, 0x33, 0x22, 0x71, 0x7e, 0x25, | ||
0x55, 0x61, 0x26, 0x0c, 0x03, 0x6b, 0x2e, 0x26, | ||
0x43, 0x06, 0x7c, 0x30, 0x4e, 0x3a, 0x51, 0x20, | ||
0x07, 0x71, 0x76, 0x48, 0x26, 0x38, 0x19, 0x69, | ||
}; | ||
|
||
@Override | ||
protected FileSystem getFreshFileSystem(DigestHashFunction digestHashFunction) { | ||
return new FakeAttributeFileSystem(digestHashFunction); | ||
} | ||
|
||
@Test | ||
public void testFoo() throws Exception { | ||
// Instead of actually trying to access this file, a call to getxattr() should be made. We | ||
// intercept this call and return a fake extended attribute value, thereby causing the checksum | ||
// computation to be skipped entirely. | ||
assertThat(DigestUtils.getDigestWithManualFallback(absolutize("myfile"), 123)) | ||
.isEqualTo(FAKE_DIGEST); | ||
} | ||
|
||
private class FakeAttributeFileSystem extends UnixFileSystem { | ||
public FakeAttributeFileSystem(DigestHashFunction hashFunction) { | ||
super(hashFunction, "user.checksum.sha256"); | ||
} | ||
|
||
@Override | ||
public byte[] getxattr(Path path, String name, boolean followSymlinks) throws IOException { | ||
assertThat(path).isEqualTo(absolutize("myfile")); | ||
assertThat(name).isEqualTo("user.checksum.sha256"); | ||
assertThat(followSymlinks).isEqualTo(true); | ||
return FAKE_DIGEST; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/test/shell/bazel/unix_digest_hash_attribute_name_test.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/bin/bash | ||
# | ||
# Copyright 2020 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. | ||
|
||
# --- begin runfiles.bash initialization --- | ||
# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). | ||
set -euo pipefail | ||
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | ||
if [[ -f "$0.runfiles_manifest" ]]; then | ||
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" | ||
elif [[ -f "$0.runfiles/MANIFEST" ]]; then | ||
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" | ||
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then | ||
export RUNFILES_DIR="$0.runfiles" | ||
fi | ||
fi | ||
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then | ||
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" | ||
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | ||
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ | ||
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" | ||
else | ||
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" | ||
exit 1 | ||
fi | ||
# --- end runfiles.bash initialization --- | ||
|
||
source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \ | ||
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; } | ||
|
||
function test_xattr_operations_in_profile_log { | ||
touch WORKSPACE | ||
cat > BUILD << 'EOF' | ||
genrule( | ||
name = "foo", | ||
outs = ["foo.out"], | ||
cmd = "touch $@", | ||
) | ||
EOF | ||
# Place an extended attribute with the checksum on the BUILD file. | ||
# Only do this on Darwin for the time being, as not all Linux | ||
# distributions seem to ship with tools for setting extended | ||
# attributes. | ||
if [[ "$PLATFORM" == "darwin" ]]; then | ||
xattr -wx \ | ||
user.checksum.sha256 \ | ||
"$(openssl sha256 -binary BUILD | xxd -p)" \ | ||
BUILD | ||
fi | ||
|
||
bazel \ | ||
--unix_digest_hash_attribute_name=user.checksum.sha256 \ | ||
build \ | ||
--profile=profile_log \ | ||
--record_full_profiler_data \ | ||
//:foo || fail "Build failed" | ||
grep -q "VFS xattr.*BUILD" profile_log || \ | ||
fail "Bazel did not perform getxattr() calls" | ||
} | ||
|
||
run_suite "Integration tests for --unix_digest_hash_attribute_name" |