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

Add generic read-only storage mode for a storage that fails the write check #2091

Merged
merged 6 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod gha;
pub mod memcached;
#[cfg(feature = "oss")]
pub mod oss;
pub mod readonly;
#[cfg(feature = "redis")]
pub mod redis;
#[cfg(feature = "s3")]
Expand Down
151 changes: 151 additions & 0 deletions src/cache/readonly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;

use crate::cache::{Cache, CacheMode, CacheWrite, Storage};
use crate::compiler::PreprocessorCacheEntry;
use crate::errors::*;

use super::PreprocessorCacheModeConfig;

pub struct ReadOnlyStorage(pub Arc<dyn Storage>);

#[async_trait]
impl Storage for ReadOnlyStorage {
async fn get(&self, key: &str) -> Result<Cache> {
self.0.get(key).await
}

/// Put `entry` in the cache under `key`.
///
/// Returns a `Future` that will provide the result or error when the put is
/// finished.
async fn put(&self, _key: &str, _entry: CacheWrite) -> Result<Duration> {
Err(anyhow!("Cannot write to read-only storage"))
}

/// Check the cache capability.
///
/// The ReadOnlyStorage cache is always read-only.
async fn check(&self) -> Result<CacheMode> {
Ok(CacheMode::ReadOnly)
}

/// Get the storage location.
fn location(&self) -> String {
self.0.location()
}

/// Get the current storage usage, if applicable.
async fn current_size(&self) -> Result<Option<u64>> {
self.0.current_size().await
}

/// Get the maximum storage size, if applicable.
async fn max_size(&self) -> Result<Option<u64>> {
self.0.max_size().await
}

/// Return the config for preprocessor cache mode if applicable
fn preprocessor_cache_mode_config(&self) -> PreprocessorCacheModeConfig {
self.0.preprocessor_cache_mode_config()
}

/// Return the preprocessor cache entry for a given preprocessor key,
/// if it exists.
/// Only applicable when using preprocessor cache mode.
fn get_preprocessor_cache_entry(
&self,
_key: &str,
) -> Result<Option<Box<dyn crate::lru_disk_cache::ReadSeek>>> {
self.0.get_preprocessor_cache_entry(_key)
}

/// Insert a preprocessor cache entry at the given preprocessor key,
/// overwriting the entry if it exists.
/// Only applicable when using preprocessor cache mode.
fn put_preprocessor_cache_entry(
&self,
_key: &str,
_preprocessor_cache_entry: PreprocessorCacheEntry,
) -> Result<()> {
Err(anyhow!("Cannot write to read-only storage"))
}
}

#[cfg(test)]
mod test {
use futures::FutureExt;

use super::*;
use crate::test::mock_storage::MockStorage;

#[test]
fn readonly_storage_is_readonly() {
let storage = ReadOnlyStorage(Arc::new(MockStorage::new(None, false)));
assert_eq!(
storage.check().now_or_never().unwrap().unwrap(),
CacheMode::ReadOnly
);
}

#[test]
fn readonly_storage_forwards_preprocessor_cache_mode_config() {
let storage_no_preprocessor_cache =
ReadOnlyStorage(Arc::new(MockStorage::new(None, false)));
assert!(
!storage_no_preprocessor_cache
.preprocessor_cache_mode_config()
.use_preprocessor_cache_mode
);

let storage_with_preprocessor_cache =
ReadOnlyStorage(Arc::new(MockStorage::new(None, true)));
assert!(
storage_with_preprocessor_cache
.preprocessor_cache_mode_config()
.use_preprocessor_cache_mode
);
}

#[test]
fn readonly_storage_put_err() {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.worker_threads(1)
.build()
.unwrap();

let storage = ReadOnlyStorage(Arc::new(MockStorage::new(None, true)));
runtime.block_on(async move {
assert_eq!(
storage
.put("test1", CacheWrite::default())
.await
.unwrap_err()
.to_string(),
"Cannot write to read-only storage"
);
assert_eq!(
storage
.put_preprocessor_cache_entry("test1", PreprocessorCacheEntry::default())
.unwrap_err()
.to_string(),
"Cannot write to read-only storage"
);
});
}
}
12 changes: 9 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.SCCACHE_MAX_FRAME_LENGTH

use crate::cache::{storage_from_config, Storage};
use crate::cache::readonly::ReadOnlyStorage;
use crate::cache::{storage_from_config, CacheMode, Storage};
use crate::compiler::{
get_compiler_info, CacheControl, CompileResult, Compiler, CompilerArguments, CompilerHasher,
CompilerKind, CompilerProxy, DistType, Language, MissType,
Expand Down Expand Up @@ -426,7 +427,7 @@ pub fn start_server(config: &Config, port: u16) -> Result<()> {

let notify = env::var_os("SCCACHE_STARTUP_NOTIFY");

let storage = match storage_from_config(config, &pool) {
let raw_storage = match storage_from_config(config, &pool) {
Ok(storage) => storage,
Err(err) => {
error!("storage init failed for: {err:?}");
Expand All @@ -443,7 +444,7 @@ pub fn start_server(config: &Config, port: u16) -> Result<()> {
};

let cache_mode = runtime.block_on(async {
match storage.check().await {
match raw_storage.check().await {
Ok(mode) => Ok(mode),
Err(err) => {
error!("storage check failed for: {err:?}");
Expand All @@ -461,6 +462,11 @@ pub fn start_server(config: &Config, port: u16) -> Result<()> {
})?;
info!("server has setup with {cache_mode:?}");

let storage = match cache_mode {
CacheMode::ReadOnly => Arc::new(ReadOnlyStorage(raw_storage)),
_ => raw_storage,
};

let res =
SccacheServer::<ProcessCommandCreator>::new(port, runtime, client, dist_client, storage);
match res {
Expand Down
61 changes: 61 additions & 0 deletions tests/sccache_cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,64 @@ fn test_rust_cargo_env_dep(test_info: SccacheTest) -> Result<()> {
drop(test_info);
Ok(())
}

/// Test that building a simple Rust crate with cargo using sccache in read-only mode with an empty cache results in
/// a cache miss that is produced by the readonly storage wrapper (and does not attempt to write to the underlying cache).
#[test]
#[serial]
fn test_rust_cargo_cmd_readonly_preemtive_block() -> Result<()> {
let test_info = SccacheTest::new(None)?;
// `cargo clean` first, just to be sure there's no leftover build objects.
cargo_clean(&test_info)?;

let sccache_log = test_info.tempdir.path().join("sccache.log");

stop_sccache()?;

restart_sccache(
&test_info,
Some(vec![
("SCCACHE_LOCAL_RW_MODE".into(), "READ_ONLY".into()),
("SCCACHE_LOG".into(), "trace".into()),
(
"SCCACHE_ERROR_LOG".into(),
sccache_log.to_str().unwrap().into(),
),
]),
)?;

// Now build the crate with cargo.
// Assert that our cache miss is due to the readonly storage wrapper, not due to the underlying disk cache.
Command::new(CARGO.as_os_str())
.args(["build", "--color=never"])
.envs(test_info.env.iter().cloned())
.current_dir(CRATE_DIR.as_os_str())
.assert()
.try_stderr(predicates::str::contains("\x1b[").from_utf8().not())?
.try_success()?;

let log_contents = fs::read_to_string(sccache_log)?;
assert!(predicates::str::contains("server has setup with ReadOnly").eval(log_contents.as_str()));
assert!(predicates::str::contains(
"Error executing cache write: Cannot write to read-only storage"
)
.eval(log_contents.as_str()));
assert!(predicates::str::contains("DiskCache::finish_put")
.not()
.eval(log_contents.as_str()));

// Stats reset on server restart, so this needs to be run for each build.
test_info
.show_stats()?
.try_stdout(
predicates::str::contains(r#""cache_hits":{"counts":{},"adv_counts":{}}"#).from_utf8(),
)?
.try_stdout(
predicates::str::contains(
r#""cache_misses":{"counts":{"Rust":2},"adv_counts":{"rust":2}}"#,
)
.from_utf8(),
)?
.try_success()?;
Ok(())
}
Loading