Skip to content

Commit 87d6e9a

Browse files
sushain97copybara-github
authored andcommitted
Support --unix_digest_hash_attribute_name in repository cache
We extend `RepositoryCache` and `DownloadManager` to support pulling checksums from extended attributes. In some example builds, I observe a 5x speedup in repository fetching: https://github.com/sushain97/bazel-faster-fetch-repro. I would like to make `xattrProvider` not-nullable in any context. However, it's not immediately clear how I could thread access to one in the `BazelPackageLoader` context: https://github.com/sushain97/bazel/blob/1518e04579e6a1c966eb8fbf6d65603e0a1d41d5/src/main/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoader.java#L108-L110. Open to any suggestions. Closes #18221. PiperOrigin-RevId: 529730007 Change-Id: Ic06b4585988a6b5c09f61cee68daf1dcdf3e46fc
1 parent 1cd3588 commit 87d6e9a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.hash.HashFunction;
2222
import com.google.common.hash.Hasher;
2323
import com.google.common.hash.Hashing;
24+
import com.google.common.io.BaseEncoding;
2425
import com.google.devtools.build.lib.vfs.FileSystemUtils;
2526
import com.google.devtools.build.lib.vfs.Path;
2627
import java.io.IOException;
@@ -310,6 +311,15 @@ public static void assertFileChecksum(String expectedChecksum, Path filePath, Ke
310311
*/
311312
public static String getChecksum(KeyType keyType, Path path)
312313
throws IOException, InterruptedException {
314+
// Attempt to use the fast digest if the hash function of the filesystem
315+
// matches `keyType` and it's available.
316+
if (path.getFileSystem().getDigestFunction().getHashFunction().equals(keyType.hashFunction)) {
317+
byte[] digest = path.getFastDigest();
318+
if (digest != null) {
319+
return BaseEncoding.base16().lowerCase().encode(digest);
320+
}
321+
}
322+
313323
Hasher hasher = keyType.newHasher();
314324
byte[] byteBuffer = new byte[BUFFER_SIZE];
315325
try (InputStream stream = path.getInputStream()) {

src/test/java/com/google/devtools/build/lib/bazel/repository/cache/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ java_library(
2020
"//src/main/java/com/google/devtools/build/lib/bazel/repository/cache",
2121
"//src/main/java/com/google/devtools/build/lib/vfs",
2222
"//src/test/java/com/google/devtools/build/lib/testutil",
23+
"//third_party:guava",
2324
"//third_party:junit4",
25+
"//third_party:mockito",
2426
"//third_party:truth",
2527
],
2628
)

src/test/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCacheTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
package com.google.devtools.build.lib.bazel.repository.cache;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18+
import static org.mockito.Mockito.doReturn;
19+
import static org.mockito.Mockito.spy;
1820

21+
import com.google.common.io.BaseEncoding;
1922
import com.google.devtools.build.lib.bazel.repository.cache.RepositoryCache.KeyType;
2023
import com.google.devtools.build.lib.testutil.Scratch;
2124
import com.google.devtools.build.lib.vfs.FileSystemUtils;
@@ -171,6 +174,18 @@ public void testGetChecksum() throws Exception {
171174
assertThat(actualChecksum).isEqualTo(downloadedFileSha256);
172175
}
173176

177+
@Test
178+
public void testGetChecksumWithFastDigest() throws Exception {
179+
String fastDigestChecksum = "cfe5ed57e6e323555b379c660aa8d35b70c2f8f07cf03ad6747266495ac13be0";
180+
downloadedFile = spy(downloadedFile);
181+
doReturn(BaseEncoding.base16().lowerCase().decode(fastDigestChecksum))
182+
.when(downloadedFile)
183+
.getFastDigest();
184+
185+
String actualChecksum = RepositoryCache.getChecksum(KeyType.SHA256, downloadedFile);
186+
assertThat(actualChecksum).isEqualTo(fastDigestChecksum);
187+
}
188+
174189
@Test
175190
public void testAssertFileChecksumPass() throws Exception {
176191
RepositoryCache.assertFileChecksum(downloadedFileSha256, downloadedFile, KeyType.SHA256);

0 commit comments

Comments
 (0)