Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement read-only local cache #2048

Merged
merged 11 commits into from
Jan 26, 2024
Merged

Implement read-only local cache #2048

merged 11 commits into from
Jan 26, 2024

Conversation

uellenberg
Copy link
Contributor

This allows the local cache to be set as read-only (similar to GCS read-only) when the SCCACHE_LOCAL_RW_MODE environment variable is set to READ_ONLY (or the corresponding config option).

Closes #1852.

Comment on lines -349 to +358
storage.put_preprocessor_cache_entry(

if let Err(e) = storage.put_preprocessor_cache_entry(
preprocessor_key,
preprocessor_cache_entry,
)?;
) {
debug!("Failed to update preprocessor cache: {}", e);
update_failed = true;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm unsure about this behavior. It's based on how compiler.rs handles errors.

Realistically, errors shouldn't be used here, but it seems to be how it's handled with GCS read-only, so I did it the same way. I thought about changing it, but I'd like to hear the input about someone more knowledgeable about sccache's internals first. Storage::check() seems to be a great candidate, although it only seems to be used for a print and is very expensive for GCS (which could be changed, of course).

Comment on lines -494 to +508
storage
.put_preprocessor_cache_entry(&preprocessor_key, preprocessor_cache_entry)?;

if let Err(e) = storage
.put_preprocessor_cache_entry(&preprocessor_key, preprocessor_cache_entry)
{
debug!("Failed to update preprocessor cache: {}", e);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above.

src/cache/gcs.rs Outdated
Comment on lines 40 to 48
impl From<config::CacheRWMode> for RWMode {
fn from(value: config::CacheRWMode) -> Self {
match value {
config::CacheRWMode::ReadOnly => RWMode::ReadOnly,
config::CacheRWMode::ReadWrite => RWMode::ReadWrite,
}
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are a few different types like this that have the same purpose. Should they be consolidated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've merged two of them together, although there's still CacheModeConfig and CacheMode, which do effectively the same thing.

@codecov-commenter
Copy link

codecov-commenter commented Jan 20, 2024

Codecov Report

Attention: 143 lines in your changes are missing coverage. Please review.

Comparison is base (8d3cd35) 30.86% compared to head (c765ec1) 30.85%.
Report is 10 commits behind head on main.

Files Patch % Lines
tests/sccache_cargo.rs 21.59% 9 Missing and 60 partials ⚠️
src/compiler/c.rs 14.28% 11 Missing and 13 partials ⚠️
src/cache/cache.rs 34.61% 2 Missing and 15 partials ⚠️
src/config.rs 40.00% 13 Missing and 2 partials ⚠️
src/cache/disk.rs 11.11% 1 Missing and 7 partials ⚠️
src/compiler/compiler.rs 0.00% 0 Missing and 5 partials ⚠️
src/cache/gcs.rs 33.33% 2 Missing and 2 partials ⚠️
src/test/tests.rs 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2048      +/-   ##
==========================================
- Coverage   30.86%   30.85%   -0.02%     
==========================================
  Files          51       52       +1     
  Lines       19653    19889     +236     
  Branches     9455     9620     +165     
==========================================
+ Hits         6066     6136      +70     
- Misses       7852     7911      +59     
- Partials     5735     5842     +107     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

docs/Local.md Outdated

## Read-only cache mode

By default, the local cache operates in read/write mode. The `SCCACHE_LOCAL_RW_MODE` environment variable can be set to `READ_ONLY` (or `READ_WRITE`) to modify this behavior.
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you please detail in which case someone might want to use READ_ONLY
thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could you please detail in which case someone might want to use READ_ONLY thanks

@sylvestre

Done, let me know if that's what you want. For a more specific use case, see the linked issue. I'm using sccache to speed up build times for untrusted (and short-lived) build tasks, where new items can't be added as they may not be valid, but caching commonly used libraries can still speed up build times immensely. Read-only is not being used as a security feature, as build tasks are already isolated correctly, but to reduce useless disk consumption.

docs/Local.md Outdated Show resolved Hide resolved
Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
@@ -130,6 +133,11 @@ impl Storage for DiskCache {
// We should probably do this on a background thread if we're going to buffer
// everything in memory...
trace!("DiskCache::finish_put({})", key);

if self.rw_mode == CacheMode::ReadOnly {
return Err(anyhow!("Cannot write to a read-only cache"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you please add a test to trigger this error ? thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could you please add a test to trigger this error ? thanks

@sylvestre Done.

Copy link
Collaborator

Choose a reason for hiding this comment

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

could you please check for the error message string too ?
thanks

@uellenberg
Copy link
Contributor Author

I'm not sure why the integration tests are failing. The only change I made (since they succeeded) was asserting the errors' messages and running rustfmt.

@sylvestre
Copy link
Collaborator

rate limiting on github, not your fault :)

@sylvestre sylvestre merged commit 742d126 into mozilla:main Jan 26, 2024
45 of 48 checks passed
@sylvestre
Copy link
Collaborator

thanks for your pr!

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.

Allow local cache to be made read-only
3 participants