Skip to content

chore: [cache-dir-size-fix] Part 4: Implement the fix for dir-size in file-cache#4417

Draft
gargnitingoogle wants to merge 1 commit intogargnitin/cache-dir-size-fix/v3from
gargnitin/cache-dir-size-fix/v4
Draft

chore: [cache-dir-size-fix] Part 4: Implement the fix for dir-size in file-cache#4417
gargnitingoogle wants to merge 1 commit intogargnitin/cache-dir-size-fix/v3from
gargnitin/cache-dir-size-fix/v4

Conversation

@gargnitingoogle
Copy link
Copy Markdown
Contributor

Description

Link to the issue in case of a bug fix.

b/477828938

Testing details

  1. Manual - NA
  2. Unit tests - NA
  3. Integration tests - NA

Any backward incompatible change? If so, please explain.

@gargnitingoogle gargnitingoogle changed the title Gargnitin/cache dir size fix/v4 chore: [cache-dir-size-fix] Part 4: Implement the fix for dir-size in file-cache Feb 25, 2026
@gargnitingoogle
Copy link
Copy Markdown
Contributor Author

/gemini summary

@gargnitingoogle
Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

This pull request is the fourth part of a series aimed at fixing directory size calculation within the file cache. It integrates new components for precise disk utilization tracking and introduces a shared locking mechanism for cache directories. These changes enhance the file cache's ability to manage disk space effectively and ensure thread-safe operations, particularly in scenarios involving sparse files and parallel downloads.

Highlights

  • File Cache Disk Utilization: Introduced FileCacheDiskUtilizationCalculator to accurately track the disk usage of the file cache, addressing a fix for directory size calculation.
  • Shared Directory Locking: Implemented a SharedDirLocker mechanism to manage concurrent access to cache directories, ensuring data consistency during file operations.
  • Cache Handler Enhancements: The CacheHandler now incorporates volumeBlockSize, diskSizeCalculator, and sharedDirLocker to provide more robust and accurate cache management.
  • Conditional Disk Size Scanning: The file cache initialization now conditionally enables disk size scanning and uses a custom size calculator for the LRU cache based on configuration settings.
  • File Creation with Locking: File creation within the downloader now utilizes a SafeCreateFile function, integrating the sharedDirLocker for safer concurrent file access.
Changelog
  • internal/cache/file/cache_handle_test.go
    • Updated NewCacheHandle calls to pass nil for the newly added parameter.
  • internal/cache/file/cache_handler.go
    • Added volumeBlockSize, diskSizeCalculator, and sharedDirLocker fields to the CacheHandler struct.
    • Modified NewCacheHandler to accept and initialize the new diskSizeCalculator parameter, retrieve the volume block size, and set the sharedDirLocker for the job manager and disk size calculator.
    • Integrated diskSizeCalculator.Stop() into the Destroy method for proper cleanup.
  • internal/cache/file/cache_handler_test.go
    • Updated NewCacheHandler calls to pass nil for the new diskSizeCalculator parameter.
  • internal/cache/file/downloader/downloader.go
    • Added a sharedDirLocker field to the JobManager struct.
    • Implemented the SetSharedDirLocker method to allow setting the shared directory locker.
    • Modified CreateJobIfNotExists to pass the sharedDirLocker to the NewJob constructor.
  • internal/cache/file/downloader/job.go
    • Added a sharedDirLocker field to the Job struct.
    • Updated the NewJob constructor to accept and initialize the sharedDirLocker.
    • Modified createCacheFile to use cacheutil.SafeCreateFile with the sharedDirLocker for file creation.
  • internal/cache/file/downloader/job_test.go
    • Updated NewJob calls to pass nil for the new sharedDirLocker parameter.
  • internal/cache/file/downloader/job_testify_test.go
    • Updated NewJob calls to pass nil for the new sharedDirLocker parameter.
  • internal/fs/fs.go
    • Modified createFileCacheHandler to conditionally create a FileCacheDiskUtilizationCalculator and use lru.NewCacheWithCustomSizeCalculator if SizeScanEnable is configured.
    • Updated the NewCacheHandler call to pass the diskSizeCalculator.
  • internal/fs/fs_test.go
    • Added SizeScanEnable, SizeScanFiles, and SizeScanFrequencySeconds fields to the defaultFileCacheConfig.
  • internal/gcsx/file_cache_reader_test.go
    • Updated NewCacheHandler calls to pass nil for the new diskSizeCalculator parameter.
  • internal/gcsx/random_reader_stretchr_test.go
    • Updated NewCacheHandler calls to pass nil for the new diskSizeCalculator parameter.
  • internal/gcsx/random_reader_test.go
    • Updated NewCacheHandler calls to pass nil for the new diskSizeCalculator parameter.
  • internal/gcsx/read_manager/read_manager_test.go
    • Updated NewCacheHandler calls to pass nil for the new diskSizeCalculator parameter.
  • internal/util/disk_util.go
    • Added a new utility function PrettyPrintOf to format unsigned 64-bit integers with comma separators.
  • internal/util/disk_util_test.go
    • Added unit tests for the PrettyPrintOf function.
Activity
  • gargnitingoogle requested a summary of the pull request.

Copy link
Copy Markdown
Contributor

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

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 implements the logic for more accurate file cache size calculation by introducing a diskSizeCalculator. The changes involve plumbing this new component and a sharedDirLocker through several layers of the cache and downloader packages. The overall approach seems correct, but I've found a critical issue with the error handling in the new NewCacheHandler function, which uses panic instead of returning errors. This could lead to application crashes and should be addressed.

Comment on lines +91 to +102
if jobManager != nil {
if err := jobManager.SetSharedDirLocker(sharedDirLocker); err != nil {
// TODO: Instead of panicking, return an error from NewCacheHandler and handle it in the caller.
panic(fmt.Errorf("failed to set shared directory locker for job manager: %w", err))
}
}
if diskSizeCalculator != nil {
if err := diskSizeCalculator.SetSharedDirLocker(sharedDirLocker); err != nil {
// TODO: Instead of panicking, return an error from NewCacheHandler and handle it in the caller.
panic(fmt.Errorf("failed to set shared directory locker for disk size calculator: %w", err))
}
}
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.

critical

Using panic for recoverable errors, such as a dependency being set more than once, is dangerous as it can crash the entire gcsfuse process. The TODO comments correctly identify that these errors should be returned to the caller for graceful handling.

I recommend refactoring NewCacheHandler to return an error, for example:
func NewCacheHandler(...) (*CacheHandler, error)

Then, you can replace the panic calls with return nil, fmt.Errorf(...). This change can be propagated up to createFileCacheHandler in internal/fs/fs.go, which already returns an error and can handle this failure condition.

@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v3 branch from b0703f9 to 1b337ac Compare February 25, 2026 10:46
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v4 branch from a114ba0 to 6ee30f0 Compare February 25, 2026 10:46
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v3 branch from 1b337ac to 19b157e Compare February 25, 2026 11:30
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v4 branch from 6ee30f0 to c32efd1 Compare February 25, 2026 11:30
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v3 branch from 19b157e to 8296b90 Compare February 26, 2026 06:11
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v4 branch from c32efd1 to 039676b Compare February 26, 2026 06:11
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v3 branch from 8296b90 to 8ca046f Compare March 12, 2026 05:14
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v4 branch from 039676b to fa4ad93 Compare March 12, 2026 05:14
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v3 branch from 8ca046f to 95670d6 Compare March 12, 2026 07:42
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v4 branch from fa4ad93 to f1c7923 Compare March 12, 2026 07:42
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v3 branch from 95670d6 to 14c98fc Compare March 20, 2026 08:00
@gargnitingoogle gargnitingoogle force-pushed the gargnitin/cache-dir-size-fix/v4 branch from f1c7923 to ff4b534 Compare March 20, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant