Skip to content

fix(cache): preserve full object identity in shared chunk cache - #5225

Open
singh4jitendra wants to merge 6 commits into
GoogleCloudPlatform:masterfrom
singh4jitendra:fix/shared-cache-prefix-isolation
Open

singh4jitendra wants to merge 6 commits into
GoogleCloudPlatform:masterfrom
singh4jitendra:fix/shared-cache-prefix-isolation

Conversation

@singh4jitendra

@singh4jitendra singh4jitendra commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

The shared chunk cache was using mount-relative object names, so different views of the same bucket could miss or mix cache entries.

  • Use bucket.GCSName(object) for cache identity. The bucket supplies the full name, so the reader needs no extra onlyDir argument or duplicated configuration.
  • Keep the existing hash format and use fmt.Appendf. Existing root-mount cache entries remain reusable.
  • Cover prefix separation, root and overlapping views, wrapper delegation, literal object names, and existing-cache reuse. The E2E test checks a root mount sharing a cached chunk with an --only-dir mount.

The equal-generation cases use fake buckets to exercise the identity boundary. This is a cache-correctness fix, not a claim that an attacker can choose a GCS generation.

Link to the issue in case of a bug fix.

Fixes #5224

Testing details

Latest validation used Go 1.27.4 and the v4 module on Ubuntu 24.04 under WSL.

  1. Manual -
    • make build - passed, including generation, formatting, vet, lint (0 issues), and compilation.
  2. Unit tests -
    • Before the review update: go test -mod=readonly -p 1 -count=1 -timeout=90s -run 'TestSharedChunkCacheSeparatesOnlyDirPrefixes|TestSharedChunkCacheReaderTestSuite/TestReadAt_ReusesExistingRootCacheEntries' ./internal/gcsx ./internal/gcsx/read_manager - failed as expected, demonstrating dependence on duplicated config and the unnecessary root-cache invalidation.
    • go test -mod=readonly -p 1 -count=1 -race -timeout=5m ./internal/gcsx ./internal/gcsx/read_manager ./internal/cache/file - passed after the update.
  3. Integration tests -
    • go test -mod=readonly -c -o "$binary" ./tools/integration_tests/shared_chunk_cache - passed (compilation only; the task-owned binary was removed).
    • TestCacheHitWithOnlyDir checks matching bytes, one cached chunk, and unchanged cache-file modification time across the two mounts.
    • The existing flat/HNS/zonal configuration is reused. Real cloud-backed execution was not run locally; the presubmit needs to rerun on this update.

Any backward incompatible change? If so, please explain.

No CLI, reader-constructor, or hash-format change. Correct root-mount entries remain usable. Prefixed views now use the full GCS name and naturally skip older mount-relative entries; this does not add a blanket invalidation or verification of old cache contents.

Keep wrapped bucket requests mount-relative while shared cache paths use full object names. Version cache keys to isolate legacy entries and cover prefix isolation, overlapping views, and legacy cache reuse with regressions.
@google-cla

google-cla Bot commented Nov 5, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the shared chunk cache manager to use versioned keys (v2) to prevent the reuse of older mount-relative entries, updating the hashing logic and corresponding unit tests. It also introduces prefix-aware caching by passing the onlyDir configuration parameter to the SharedChunkCacheReader and constructing the cache object name with this prefix. Regarding the review feedback, it is recommended to clean and normalize configuration parameters like onlyDir during the initial configuration validation phase rather than repeatedly in downstream utility functions.

Clarify the shared-reader normalized only-dir precondition. Exercise cfg.Rationalize in the cache-reuse regression, including redundant slashes, dot and parent segments, root paths, and literal object-name separators. Keep normalization centralized rather than cleaning object names in downstream readers.
@singh4jitendra
singh4jitendra marked this pull request as ready for review November 6, 2026 04:18
@singh4jitendra
singh4jitendra requested a review from a team as a code owner November 6, 2026 04:18
@singh4jitendra
singh4jitendra force-pushed the fix/shared-cache-prefix-isolation branch from d0a6b8e to 0837c9b Compare November 6, 2026 04:28
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @abhishek10004 ,

PR is Ready for review, please have a look.

@PranjalC100

Copy link
Copy Markdown
Member

Could you also add an e2e test in tools/integration_tests/shared_chunk_cache covering cross-mount cache reuse when one mount uses --only-dir?

For background on how our e2e suites are structured and configured, we have a step-by-step runbook in the repo at .agents/skills/integration-tests/SKILL.md (you can also point an coding agent at that skill directly).

Specifically for tools/integration_tests/shared_chunk_cache/shared_chunk_cache_test.go:

  1. BaseSuite.SetupTest already mounts two instances (primaryMount and secondaryMount) sharing the same t.sharedCacheDir, and TestCacheHit (lines 233–288) already tests reading a chunk from primaryMount and verifying that reading the same range from secondaryMount is a cache hit (getCachedChunkCount() stays 1 and getCacheFileModTimes() is unchanged).
  2. You can add a test (or a separate suite entry in tools/integration_tests/test_config.yaml under shared_chunk_cache:) where the secondary mount passes --only-dir=SharedChunkCacheTest (testDirName):
    • In a standalone test on SharedChunkCacheTestSuite (e.g. TestCacheHitWithOnlyDir), you can unmount t.secondaryMount (setup.UnmountGCSFuse(t.secondaryMount.mntDir)) and remount it with append(slices.Clone(t.secondaryFlags), "--only-dir="+testDirName) using static_mounting.MountGcsfuseWithStaticMounting(...).
    • One path detail to watch out for: t.createTestFile(testFileName, fileSize) creates the GCS object at SharedChunkCacheTest/<testFileName>. On primaryMount (bucket root), that file is at path.Join(t.primaryMount.testDirPath, testFileName). On secondaryMount mounted with --only-dir=SharedChunkCacheTest, the mount root is already SharedChunkCacheTest/, so read from path.Join(t.secondaryMount.mntDir, testFileName) (not t.secondaryMount.testDirPath, which would prepend SharedChunkCacheTest a second time).
    • Assert the same things as TestCacheHit: bytes match primaryChunk, getCachedChunkCount() remains 1, and the cached .bin file's ModTime() does not change.

Once you push, we'll trigger the e2e presubmit on the PR.

Remount the secondary shared-cache instance with the test prefix using config-based helpers. Verify matching range data, one cached chunk and unchanged file modification times, and fail cache snapshots on filesystem errors. Keep the existing bucket compatibility and managed-mount teardown.
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

@PranjalC100 Added TestCacheHitWithOnlyDir in 21a96cf.

  • Clones the secondary flags, remounts only that instance with --only-dir=SharedChunkCacheTest, and uses the config-based mounting helpers.
  • Reads the primary path under primaryMount.testDirPath and the secondary path directly under secondaryMount.mntDir, avoiding the doubled prefix.
  • Verifies matching 2 MiB range data, exactly one cached .bin chunk before and after the secondary read, and the same cache-file path/modification time.
  • Uses a unique object name and preserves the existing flat/HNS/zonal configurations. It does not remount pre-mounted GKE directories. Cache snapshots now fail on stat/walk errors rather than accepting partial evidence.

Local validation with Go 1.27.2:

  • go test -mod=readonly -c -o "$binary" ./tools/integration_tests/shared_chunk_cache - passed (compile-only; $binary was a task-owned temporary executable and was removed).
  • go test -mod=readonly -p 1 -count=1 -race -timeout=90s -run SharedChunkCache ./internal/gcsx ./internal/gcsx/read_manager ./internal/cache/file - passed in all three packages.
  • make build - passed with 0 lint issues.

No authorized cloud test bucket is configured locally, so the real E2E execution has not been run. Please trigger the E2E presubmit as offered.

For a manual run on a configured test VM, substitute an authorized compatible bucket:

BUCKET_NAME=<compatible-test-bucket> GODEBUG=asyncpreemptoff=1 go test -p 1 ./tools/integration_tests/shared_chunk_cache -run 'TestSharedChunkCacheTestSuite/TestCacheHitWithOnlyDir' --integrationTest -v --config-file="$(pwd)/tools/integration_tests/test_config.yaml"

Warning

Cloudtop/Jetski sessions can fail or hang on full integration tests because of proxy, FUSE, or credential-scope restrictions. If that occurs, use the designated GCP Linux test VM/sandbox or the presubmit environment instead.

@PranjalC100 PranjalC100 self-assigned this Nov 10, 2026
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @abhishek10004 @anushka567 @meet2mky, could one of you take a look when you get a chance? I've added the cross-mount --only-dir E2E test requested in review. The workflows are still waiting for approval, so I'd also appreciate a hand getting CI and the E2E presubmit running. Thanks!

@PranjalC100
PranjalC100 self-requested a review November 11, 2026 09:44
@PranjalC100

Copy link
Copy Markdown
Member

Please resolve the existing comments on the PR.

@PranjalC100 PranjalC100 added the kokoro:run Testing kokoro run label Nov 11, 2026
@kokoro-team kokoro-team removed the kokoro:run Testing kokoro run label Nov 11, 2026
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @PranjalC100, following up on your note: all the inline comments are resolved, and the cross-mount --only-dir E2E test is included. CI and Kokoro are green now too. Could you take another look when you get a chance? Thanks!

fmt.Fprintf(h, "%d:%s", len(bucketName), bucketName)
fmt.Fprintf(h, "%d:%s", len(objectName), objectName)
fmt.Fprintf(h, ":%d", generation)
// Version keys to avoid reusing entries written with mount-relative object names.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep the existing hash format without the v2: prefix?

The cache key already includes generation (<bucketLen>:<bucket><objLen>:<objectName>:<generation>). Once --only-dir mounts pass the full object name, they will look up <onlyDir>/<file> and naturally ignore any legacy entry written under <file>. A legacy entry written under --only-dir could only be mistakenly read by a root mount if a different object existed at the bucket root with the exact same name and the exact same 64-bit microsecond GCS generation number. @vadlakondaswetha What do you think?

--

Minor (allocation): fmt.Sprintf + []byte(key) allocates both a string and a []byte. Using fmt.Appendf avoids the extra copy:

hash := sha256.Sum256(fmt.Appendf(nil, "%d:%s%d:%s:%d", len(bucketName), bucketName, len(objectName), objectName, generation))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed v2: and used fmt.Appendf as suggested in 8612f4d. The original hash fixtures are restored. A new compatibility test pre-populates an old-format root entry and removes the fake backend object, so it proves the entry is reused rather than downloaded again. The prefixed-view test also checks that a mount-relative entry is skipped.

That keeps this focused on using the correct object name, without invalidating every existing root cache. It retains the pre-existing legacy-cache edge case you noted; I'm not claiming that old ambiguous entries are independently validated. I'll leave this thread open for @vadlakondaswetha's input since you asked for it. Happy to adjust if you'd prefer a different migration policy.

Merge current master without rewriting PR history. Preserve the PR behavior while adopting the upstream v4 module path, Go 1.27.4 and dependency security updates. Validated with relevant race tests and make build.
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Merged the latest master in 1728439 and resolved the v4 import conflict. The cache identity fix and cross-mount E2E test are unchanged.

Rechecked with Go 1.27.4:

  • go test -mod=readonly -p 1 -count=1 -race -timeout=3m -run SharedChunkCache ./internal/gcsx ./internal/gcsx/read_manager ./internal/cache/file - passed.
  • go test -mod=readonly -c -o "$binary" ./tools/integration_tests/shared_chunk_cache - passed (compile-only; the task-owned binary was removed).
  • make build - passed with 0 lint issues.

The branch is updated without rewriting history. The cloud-backed E2E presubmit will need to run on the new commit.

Use bucket.GCSName as the source of the full object name and restore the original reader signature. Preserve the existing hash format with fmt.Appendf and cover compatible root-cache reuse and prefixed cache isolation.
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @PranjalC100 @abhishek10004, I've pushed the updates from the last review. Could you take another look when you get a chance? A hand approving the latest CI run would be great too. Thanks!

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @PranjalC100 @abhishek10004, could you wrap up the review when you get a chance and merge once the checks pass and everything looks good? Thanks!

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @PranjalC100 @vadlakondaswetha, just following up on this one. The review changes and cross-mount test are in. Could you help confirm the remaining hash-policy decision and get the latest CI runs approved when you have a chance? Thanks!

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @PranjalC100 @vadlakondaswetha, could you help with the remaining hash-policy decision and CI approval when you have a moment? Please let me know if anything else is needed from my side. Thanks!

Match include/exclude rules against the bucket name and full GCS object name, including only-dir prefixes. Add prefixed-bucket read regressions for full and mount-relative patterns and exclusion precedence. Rename existing-format cache fixture variables for clarity.
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @abhishek10004, both review fixes are in. Could you take another look when you have a moment? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

execute-integration-tests Run only integration tests execute-integration-tests-on-zb To run E2E tests on zonal bucket.

4 participants