Skip to content

Commit

Permalink
Hooked up config to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Oct 29, 2024
1 parent db58ef7 commit a2fe2af
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 61 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/turborepo-cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ workspace = true

[dependencies]
base64 = "0.21.0"
biome_deserialize = { workspace = true }
biome_deserialize_macros = { workspace = true }
biome_diagnostics = { workspace = true }
bytes.workspace = true
camino = { workspace = true }
futures = { workspace = true }
hmac = "0.12.1"
miette = { workspace = true }
os_str_bytes = "6.5.0"
path-clean = { workspace = true }
petgraph = "0.6.3"
Expand Down
42 changes: 32 additions & 10 deletions crates/turborepo-cache/src/async_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ mod tests {

use crate::{
test_cases::{get_test_cases, TestCase},
AsyncCache, CacheHitMetadata, CacheOpts, CacheSource, RemoteCacheOpts,
AsyncCache, CacheActions, CacheConfig, CacheHitMetadata, CacheOpts, CacheSource,
RemoteCacheOpts,
};

#[tokio::test]
Expand Down Expand Up @@ -255,9 +256,16 @@ mod tests {

let opts = CacheOpts {
cache_dir: Utf8PathBuf::from(".turbo/cache"),
remote_cache_read_only: false,
skip_remote: false,
skip_filesystem: true,
cache: CacheConfig {
fs: CacheActions {
read: false,
write: false,
},
remote: CacheActions {
read: true,
write: true,
},
},
workers: 10,
remote_cache_opts: Some(RemoteCacheOpts {
unused_team_id: Some("my-team".to_string()),
Expand Down Expand Up @@ -337,9 +345,16 @@ mod tests {

let opts = CacheOpts {
cache_dir: Utf8PathBuf::from(".turbo/cache"),
remote_cache_read_only: false,
skip_remote: true,
skip_filesystem: false,
cache: CacheConfig {
fs: CacheActions {
read: true,
write: true,
},
remote: CacheActions {
read: false,
write: false,
},
},
workers: 10,
remote_cache_opts: Some(RemoteCacheOpts {
unused_team_id: Some("my-team".to_string()),
Expand Down Expand Up @@ -429,9 +444,16 @@ mod tests {

let opts = CacheOpts {
cache_dir: Utf8PathBuf::from(".turbo/cache"),
remote_cache_read_only: false,
skip_remote: false,
skip_filesystem: false,
cache: CacheConfig {
fs: CacheActions {
read: true,
write: true,
},
remote: CacheActions {
read: true,
write: true,
},
},
workers: 10,
remote_cache_opts: Some(RemoteCacheOpts {
unused_team_id: Some("my-team".to_string()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;
use miette::{Diagnostic, SourceSpan};
use thiserror::Error;

use crate::config::{CacheActions, CacheConfig};
use crate::{CacheActions, CacheConfig};

#[derive(Debug, Error, Diagnostic, PartialEq)]
pub enum Error {
Expand Down
41 changes: 38 additions & 3 deletions crates/turborepo-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mod async_cache;
/// The core cache creation and restoration logic.
pub mod cache_archive;
pub mod config;
/// File system cache
pub mod fs;
/// Remote cache
Expand All @@ -24,6 +25,7 @@ mod upload_progress;
use std::{backtrace, backtrace::Backtrace};

pub use async_cache::AsyncCache;
use biome_deserialize_macros::Deserializable;
use camino::Utf8PathBuf;
use serde::{Deserialize, Serialize};
use thiserror::Error;
Expand Down Expand Up @@ -61,6 +63,8 @@ pub enum CacheError {
LinkTargetDoesNotExist(String, #[backtrace] Backtrace),
#[error("Invalid tar, link target does not exist on header")]
LinkTargetNotOnHeader(#[backtrace] Backtrace),
#[error(transparent)]
Config(#[from] config::Error),
#[error("attempted to restore unsupported file type: {0:?}")]
RestoreUnsupportedFileType(tar::EntryType, #[backtrace] Backtrace),
// We don't pass the `FileType` because there's no simple
Expand Down Expand Up @@ -105,12 +109,43 @@ pub struct CacheHitMetadata {
pub time_saved: u64,
}

#[derive(Serialize, Deserialize, Deserializable, Debug, PartialEq, Eq, Clone, Copy)]
pub struct CacheActions {
pub read: bool,
pub write: bool,
}

impl CacheActions {
pub fn should_use(&self) -> bool {
self.read || self.write
}
}

#[derive(Serialize, Deserialize, Deserializable, Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct CacheConfig {
pub fs: CacheActions,
pub remote: CacheActions,
}

impl CacheConfig {
pub fn skip_writes(&self) -> bool {
!self.fs.write && !self.remote.write
}
}

impl Default for CacheActions {
fn default() -> Self {
Self {
read: true,
write: true,
}
}
}

#[derive(Clone, Debug, Default)]
pub struct CacheOpts {
pub cache_dir: Utf8PathBuf,
pub remote_cache_read_only: bool,
pub skip_remote: bool,
pub skip_filesystem: bool,
pub cache: CacheConfig,
pub workers: u32,
pub remote_cache_opts: Option<RemoteCacheOpts>,
}
Expand Down
6 changes: 3 additions & 3 deletions crates/turborepo-cache/src/multiplexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl CacheMultiplexer {
api_auth: Option<APIAuth>,
analytics_recorder: Option<AnalyticsSender>,
) -> Result<Self, CacheError> {
let use_fs_cache = !opts.skip_filesystem;
let use_http_cache = !opts.skip_remote;
let use_fs_cache = opts.cache.fs.should_use();
let use_http_cache = opts.cache.remote.should_use();

// Since the above two flags are not mutually exclusive it is possible to
// configure yourself out of having a cache. We should tell you about it
Expand Down Expand Up @@ -67,7 +67,7 @@ impl CacheMultiplexer {
Ok(CacheMultiplexer {
should_print_skipping_remote_put: AtomicBool::new(true),
should_use_http_cache: AtomicBool::new(http_cache.is_some()),
remote_cache_read_only: opts.remote_cache_read_only,
remote_cache_read_only: opts.cache.remote.read && !opts.cache.remote.write,
fs: fs_cache,
http: http_cache,
})
Expand Down
3 changes: 3 additions & 0 deletions crates/turborepo-lib/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use camino::Utf8Path;
use serde::Serialize;
use turborepo_cache::CacheConfig;
use turborepo_repository::{
package_graph::PackageGraph, package_json::PackageJson, package_manager::PackageManager,
};
Expand All @@ -26,6 +27,7 @@ struct ConfigOutput<'a> {
scm_base: Option<&'a str>,
scm_head: Option<&'a str>,
cache_dir: &'a Utf8Path,
cache: CacheConfig,
}

pub async fn run(base: CommandBase) -> Result<(), cli::Error> {
Expand Down Expand Up @@ -58,6 +60,7 @@ pub async fn run(base: CommandBase) -> Result<(), cli::Error> {
scm_base: config.scm_base(),
scm_head: config.scm_head(),
cache_dir: config.cache_dir(),
cache: config.cache(),
})?
);
Ok(())
Expand Down
25 changes: 8 additions & 17 deletions crates/turborepo-lib/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
mod cache;
mod env;
mod file;
mod override_env;
mod turbo_json;

use std::{collections::HashMap, ffi::OsString, io};

use biome_deserialize_macros::Deserializable;
use camino::{Utf8Path, Utf8PathBuf};
use convert_case::{Case, Casing};
use derive_setters::Setters;
Expand All @@ -15,12 +13,13 @@ use file::{AuthFile, ConfigFile};
use merge::Merge;
use miette::{Diagnostic, NamedSource, SourceSpan};
use override_env::OverrideEnvVars;
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use struct_iterable::Iterable;
use thiserror::Error;
use tracing::debug;
use turbo_json::TurboJsonReader;
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf};
use turborepo_cache::CacheConfig;
use turborepo_errors::TURBO_SITE;
use turborepo_repository::package_graph::PackageName;

Expand Down Expand Up @@ -52,8 +51,6 @@ pub struct InvalidEnvPrefixError {
pub enum Error {
#[error("Authentication error: {0}")]
Auth(#[from] turborepo_auth::Error),
#[error(transparent)]
Cache(#[from] cache::Error),
#[error("Global config path not found")]
NoGlobalConfigPath,
#[error("Global auth file path not found")]
Expand Down Expand Up @@ -85,6 +82,8 @@ pub enum Error {
config_path: AbsoluteSystemPathBuf,
error: io::Error,
},
#[error(transparent)]
Cache(#[from] turborepo_cache::config::Error),
#[error(
"Package tasks (<package>#<task>) are not allowed in single-package repositories: found \
{task_id}"
Expand Down Expand Up @@ -203,18 +202,6 @@ const DEFAULT_LOGIN_URL: &str = "https://vercel.com";
const DEFAULT_TIMEOUT: u64 = 30;
const DEFAULT_UPLOAD_TIMEOUT: u64 = 60;

#[derive(Serialize, Deserialize, Deserializable, Debug, PartialEq, Eq, Clone, Default)]
pub struct CacheActions {
read: bool,
write: bool,
}

#[derive(Serialize, Deserialize, Deserializable, Debug, PartialEq, Eq, Clone, Default)]
pub struct CacheConfig {
fs: CacheActions,
remote: CacheActions,
}

// We intentionally don't derive Serialize so that different parts
// of the code that want to display the config can tune how they
// want to display and what fields they want to include.
Expand Down Expand Up @@ -385,6 +372,10 @@ impl ConfigurationOptions {
})
}

pub fn cache(&self) -> CacheConfig {
self.cache.unwrap_or_default()
}

pub fn force(&self) -> bool {
self.force.unwrap_or_default()
}
Expand Down
Loading

0 comments on commit a2fe2af

Please sign in to comment.