← Back to Pull requests

Read persistent action caches larger than a Java byte array #32320

Open
dzbarsky opened this pull request with 306 lines diff across 3 files
View on GitHub
dzbarsky dzbarsky commented on Nov 01, 2026 18:53 UTC
Author

Read persistent action caches of any size through a 64 KiB BufferedInputStream. MapCodec reads entries in file order, detects EOF with a read, and closes the stream with try-with-resources. The on-disk format is unchanged.

MapCodecTest decodes more than 2 GiB of valid records from a bounded in-memory fixture and verifies the final tombstone. It also covers corrupt entries and stream closure when header validation fails.

Diff: 306 lines (+178 / -128) across 3 files Reviewer: tjgq Review duration: 41748m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.40) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 306 lines diff across 3 files
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 5 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.343
Real PR Conversation Timeline
9 items
tjgq tjgq commented on code on Nov 02, 2026 14:16 UTC
Code Review
src/test/java/com/google/devtools/build/lib/util/MapCodecTest.java:1
@@ -142,6 +145,33 @@ public void createReader_nonEmptyFile() throws IOException {
     }
   }
 
+  @Test
+  public void createReader_fileLargerThanByteArray() throws IOException {
+    LargeFileSystem largeFs = new LargeFileSystem();
+    Path path = largeFs.getPath("/large");
+    writeByteContents(
+        path, "00000000 20071105 00000000 00000042 fe 12345678 01 87654321 fe 00abcdef 00");
+
+    try (var in = TEST_CODEC.createReader(path, 0x42)) {
+      assertThat(in.readEntry()).isEqualTo(new MapCodec.Entry<>(0x12345678, 0x87654321));
+      assertThat(in.readEntry()).isEqualTo(new MapCodec.Entry<>(0xabcdef, null));
+      assertThat(in.readEntry()).isNull();
+      assertThat(largeFs.streamClosed).isFalse();
+    }
+    assertThat(largeFs.streamClosed).isTrue();

Can we test the contract, not the implementation details? i.e., verify that we're able to successfully parse more than 2GB worth of data? (Hopefully we can do so in a way that isn't terribly slow.)

tjgq tjgq requested changes on Nov 02, 2026 14:23 UTC
Changes Requested Reviewer

Requested changes.

dzbarsky dzbarsky commented on code on Nov 08, 2026 23:22 UTC
Code Review
src/test/java/com/google/devtools/build/lib/util/MapCodecTest.java:1
@@ -142,6 +145,33 @@ public void createReader_nonEmptyFile() throws IOException {
     }
   }
 
+  @Test
+  public void createReader_fileLargerThanByteArray() throws IOException {
+    LargeFileSystem largeFs = new LargeFileSystem();
+    Path path = largeFs.getPath("/large");
+    writeByteContents(
+        path, "00000000 20071105 00000000 00000042 fe 12345678 01 87654321 fe 00abcdef 00");
+
+    try (var in = TEST_CODEC.createReader(path, 0x42)) {
+      assertThat(in.readEntry()).isEqualTo(new MapCodec.Entry<>(0x12345678, 0x87654321));
+      assertThat(in.readEntry()).isEqualTo(new MapCodec.Entry<>(0xabcdef, null));
+      assertThat(in.readEntry()).isNull();
+      assertThat(largeFs.streamClosed).isFalse();
+    }
+    assertThat(largeFs.streamClosed).isTrue();

I checked the parse contract with a generated stream that repeats a valid encoded 1 MiB value and puts a tombstone after 2 GiB. The parent rejected the 2,148,544,540-byte input as too long. The PR reader consumed all 2,049 values, read the final tombstone and EOF, and closed the stream with -Xmx128m. The fixture uses a shared encoded record buffer, so it does not need a 2 GiB disk file.

-zbarskybot

tjgq tjgq commented on code on Nov 17, 2026 21:42 UTC
Code Review
src/test/java/com/google/devtools/build/lib/util/MapCodecTest.java:1
@@ -142,6 +145,33 @@ public void createReader_nonEmptyFile() throws IOException {
     }
   }
 
+  @Test
+  public void createReader_fileLargerThanByteArray() throws IOException {
+    LargeFileSystem largeFs = new LargeFileSystem();
+    Path path = largeFs.getPath("/large");
+    writeByteContents(
+        path, "00000000 20071105 00000000 00000042 fe 12345678 01 87654321 fe 00abcdef 00");
+
+    try (var in = TEST_CODEC.createReader(path, 0x42)) {
+      assertThat(in.readEntry()).isEqualTo(new MapCodec.Entry<>(0x12345678, 0x87654321));
+      assertThat(in.readEntry()).isEqualTo(new MapCodec.Entry<>(0xabcdef, null));
+      assertThat(in.readEntry()).isNull();
+      assertThat(largeFs.streamClosed).isFalse();
+    }
+    assertThat(largeFs.streamClosed).isTrue();

I believe you haven't uploaded the commit where these changes were made.

tjgq tjgq commented on code on Nov 17, 2026 21:45 UTC
Code Review
src/main/java/com/google/devtools/build/lib/util/MapCodec.java:1
@@ -76,30 +77,37 @@ public Reader createReader(Path path, long version) throws IOException {
     if (size < MIN_MAPFILE_SIZE) {
       throw new IncompatibleFormatException("%s is too short: %s bytes".formatted(path, size));
     }
+    InputStream stream;
     if (size > MAX_ARRAY_SIZE) {
-      throw new IncompatibleFormatException("%s is too long: %s bytes".formatted(path, size));
-    }
-
-    // Read the whole file upfront as a performance optimization (minimize syscalls).
-    byte[] bytes;
-    try (InputStream in = path.getInputStream()) {
-      bytes = in.readAllBytes();
+      stream = new BufferedInputStream(path.getInputStream(), 64 * 1024);
+    } else {
+      // Read the whole file upfront as a performance optimization (minimize syscalls).
+      byte[] bytes;
+      try (InputStream in = path.getInputStream()) {
+        bytes = in.readAllBytes();
+      }
+      stream = new ByteArrayInputStream(bytes);
     }

Honestly, I think we're better off using a BufferedInputStream unconditionally to simplify. (readAllBytes uses a 16KB buffer, so it's actually less efficient.)

tjgq tjgq commented on code on Nov 17, 2026 21:45 UTC
Code Review
src/main/java/com/google/devtools/build/lib/util/MapCodec.java:1
@@ -76,30 +77,37 @@ public Reader createReader(Path path, long version) throws IOException {
     if (size < MIN_MAPFILE_SIZE) {
       throw new IncompatibleFormatException("%s is too short: %s bytes".formatted(path, size));
     }
+    InputStream stream;
     if (size > MAX_ARRAY_SIZE) {
-      throw new IncompatibleFormatException("%s is too long: %s bytes".formatted(path, size));
-    }
-
-    // Read the whole file upfront as a performance optimization (minimize syscalls).
-    byte[] bytes;
-    try (InputStream in = path.getInputStream()) {
-      bytes = in.readAllBytes();
+      stream = new BufferedInputStream(path.getInputStream(), 64 * 1024);

Please move the 64 * 1024 into a class-level constant.

tjgq tjgq commented on code on Nov 17, 2026 21:47 UTC
Code Review
src/main/java/com/google/devtools/build/lib/util/MapCodec.java:1
@@ -76,30 +77,37 @@ public Reader createReader(Path path, long version) throws IOException {
     if (size < MIN_MAPFILE_SIZE) {
       throw new IncompatibleFormatException("%s is too short: %s bytes".formatted(path, size));
     }
+    InputStream stream;
     if (size > MAX_ARRAY_SIZE) {
-      throw new IncompatibleFormatException("%s is too long: %s bytes".formatted(path, size));
-    }
-
-    // Read the whole file upfront as a performance optimization (minimize syscalls).
-    byte[] bytes;
-    try (InputStream in = path.getInputStream()) {
-      bytes = in.readAllBytes();
+      stream = new BufferedInputStream(path.getInputStream(), 64 * 1024);
+    } else {
+      // Read the whole file upfront as a performance optimization (minimize syscalls).
+      byte[] bytes;
+      try (InputStream in = path.getInputStream()) {
+        bytes = in.readAllBytes();
+      }
+      stream = new ByteArrayInputStream(bytes);
     }
 
-    DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
-
-    if (in.readLong() != MAGIC) {
-      // Not a PersistentMap.
-      throw new IncompatibleFormatException("Bad magic number");
-    }
-    long persistedVersion = in.readLong();
-    if (persistedVersion != version) {
-      // Incompatible version.
-      throw new IncompatibleFormatException(
-          "Incompatible version: want %d, got %d".formatted(version, persistedVersion));
+    DataInputStream in = new DataInputStream(stream);
+    try {
+      if (in.readLong() != MAGIC) {
+        throw new IncompatibleFormatException("Bad magic number");
+      }
+      long persistedVersion = in.readLong();
+      if (persistedVersion != version) {
+        throw new IncompatibleFormatException(
+            "Incompatible version: want %d, got %d".formatted(version, persistedVersion));
+      }
+      return new Reader(in);
+    } catch (IOException e) {
+      try {
+        in.close();

After unifying the under/over 2GB code paths, we can use a try-with-resources to avoid having to close manually.

tjgq tjgq requested changes on Nov 17, 2026 21:48 UTC
Changes Requested Reviewer

Requested changes.

Reviewers
Assignees
dzbarsky dzbarsky
Labels
risk: low confidence: 90.9% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 40.2% (MEDIUM) · Higher = larger blast radius
40.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 90.9% · Lower = rushed or shallow review
90.9%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 3.6% · Safe to merge
3.6%