Skip to content

Commit

Permalink
Use ctime in file digest cache key
Browse files Browse the repository at this point in the history
File digests are now additionally keyed by ctime for supported file
system implementations. Since Bazel has a non-zero default for
`--cache_computed_file_digests`, this may be required for correctness in
cases where different files have identical mtime and inode number.
For example, this can happen on Linux when files are extracted from a
tar file with fixed mtime and are then replaced with `mv`, which
preserves inodes.
  • Loading branch information
fmeum committed Apr 6, 2023
1 parent f03d58d commit 856188a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ private static class CacheKey {
/** File system identifier of the file (typically the inode number). */
private final long nodeId;

/** Creation time of the file. */
private final long createdTime;

/** Last modification time of the file. */
private final long modifiedTime;

Expand All @@ -62,6 +65,7 @@ private static class CacheKey {
public CacheKey(Path path, FileStatus status) throws IOException {
this.path = path.asFragment();
this.nodeId = status.getNodeId();
this.createdTime = status.getLastChangeTime();
this.modifiedTime = status.getLastModifiedTime();
this.size = status.getSize();
}
Expand All @@ -76,6 +80,7 @@ public boolean equals(Object object) {
CacheKey key = (CacheKey) object;
return path.equals(key.path)
&& nodeId == key.nodeId
&& createdTime == key.createdTime
&& modifiedTime == key.modifiedTime
&& size == key.size;
}
Expand All @@ -86,6 +91,7 @@ public int hashCode() {
int result = 17;
result = 31 * result + path.hashCode();
result = 31 * result + Longs.hashCode(nodeId);
result = 31 * result + Longs.hashCode(createdTime);
result = 31 * result + Longs.hashCode(modifiedTime);
result = 31 * result + Longs.hashCode(size);
return result;
Expand Down
7 changes: 7 additions & 0 deletions src/test/shell/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1366,3 +1366,10 @@ sh_test(
],
tags = ["no_windows"],
)

sh_test(
name = "digest_cache_invalidation_test",
srcs = ["digest_cache_invalidation_test.sh"],
data = [":test-deps"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
95 changes: 95 additions & 0 deletions src/test/shell/bazel/digest_cache_invalidation_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/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; }

# Regression test for https://github.com/bazelbuild/bazel/issues/14723
function test_fixed_mtime_move_detected_as_change() {
mkdir -p pkg
cat > pkg/BUILD <<'EOF'
load("rules.bzl", "my_expand")
genrule(
name = "my_templates",
srcs = ["template_archive.tar"],
outs = ["template1"],
cmd = "tar -C $(RULEDIR) -xf $<",
)
my_expand(
name = "expand1",
input = "template1",
output = "expanded1",
to_sub = {"test":"foo"}
)
EOF
cat > pkg/rules.bzl <<'EOF'
def _my_expand_impl(ctx):
ctx.actions.expand_template(
template = ctx.file.input,
output = ctx.outputs.output,
substitutions = ctx.attr.to_sub
)
my_expand = rule(
implementation = _my_expand_impl,
attrs = {
"input": attr.label(allow_single_file=True),
"output": attr.output(),
"to_sub" : attr.string_dict(),
}
)
EOF

echo "test : alpha" > template1
tar -cf pkg/template_archive_alpha.tar --mtime='1970-01-01' template1
echo "test : delta" > template1
tar -cf pkg/template_archive_delta.tar --mtime='1970-01-01' template1

mv pkg/template_archive_alpha.tar pkg/template_archive.tar
bazel build //pkg:expand1 || fail "Expected success"
assert_equals "foo : alpha" "$(cat bazel-bin/pkg/expanded1)"

mv pkg/template_archive_delta.tar pkg/template_archive.tar
bazel build //pkg:expand1 || fail "Expected success"
assert_equals "foo : delta" "$(cat bazel-bin/pkg/expanded1)"
}

run_suite "digest cache invalidation test"

0 comments on commit 856188a

Please sign in to comment.