8000 crates/rcache: split reader/writer, use compare-and-swap on index by bryan-minimal · Pull Request #155 · gominimal/minimal · GitHub
[go: up one dir, main page]

Skip to content
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
76 changes: 75 additions & 1 deletion Cargo.lock

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

14 changes: 14 additions & 0 deletions crates/common/src/fetchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ pub trait FetchResponse: std::fmt::Debug + Sized {
fn bytes(self) -> impl Future<Output = Result<Bytes, Self::Error>> + Send;

fn chunk(&mut self) -> impl Future<Output = Result<Option<Bytes>, Self::Error>>;

/// Returns the underlying object's GCS generation, if the backend exposes
/// one. Backends without versioned-object semantics (e.g. plain HTTPS)
/// return `None` via the default impl. Used by writers to do
/// compare-and-swap on subsequent writes.
fn generation(&self) -> Option<i64> {
None
}
}

impl FetchResponse for Response {
Expand Down Expand Up @@ -226,6 +234,12 @@ impl FetchResponse for Result<google_cloud_storage::read_object::ReadObjectRespo
Err(_) => None,
}
}
fn generation(&self) -> Option<i64> {
match self {
Ok(s) => Some(s.object().generation),
Err(_) => None,
}
}
}

/// Wiring to allow GCS buckets to be used as a [FetchBackend].
Expand Down
15 changes: 14 additions & 1 deletion crates/mctx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use common::{SpecOrigin, Target};
use google_cloud_storage::{Error as GcsError, client::Storage as GcsStorage};
use lcache::CacheBinProvider;
use ot::OpTracker;
use rcache::{Error as RemoteError, RemoteBinProvider, RemoteCache};
use rcache::{Error as RemoteError, RemoteBinProvider, RemoteCache, RemoteCacheWriter};

mod error;
pub use error::Error;
Expand Down Expand Up @@ -374,6 +374,19 @@ impl Context {
tracing::trace!("remote cache init took {:?}", start.elapsed());
res
}

/// Builds a writer for the shared cache. Always uses authenticated GCS
/// access and always fetches the index fresh (no local-cache fast path) —
/// the writer's compare-and-swap on commit requires the GCS generation
/// it observed, which a stale local index can't provide.
pub async fn remote_cache_writer(&self) -> Result<RemoteCacheWriter, RemoteError<GcsError>> {
let start = SystemTime::now();
let backend = GcsStorage::builder().build().await.unwrap();
let res =
RemoteCacheWriter::new(backend, "minimal-staging-cache", self.config.ot.clone()).await;
tracing::trace!("remote cache writer init took {:?}", start.elapsed());
res
}
pub async fn remote_storage(&self) -> Result<common::RemoteStorage, Error> {
let start = SystemTime::now();
let rs = common::RemoteStorage::new(self.config.downloads_dir(), false)
Expand Down
6 changes: 3 additions & 3 deletions crates/minimal/src/cmd_upload_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ pub async fn cmd_upload_cache(args: UploadArgs, ctx: &mut Context) -> Result<(),
});

s.spawn(move || {
let mut remote_cache = handle.block_on(ctx.remote_cache(true, true)).unwrap();
let mut writer = handle.block_on(ctx.remote_cache_writer()).unwrap();
for (bsr, (tar_file, sha256)) in rx {
let build = graph.get(&bsr).unwrap();
let bsh = graph.spec_hash(&bsr);
if handle
.block_on(remote_cache.upload(&bsh, (tar_file, sha256)))
.block_on(writer.upload(&bsh, (tar_file, sha256)))
.unwrap()
{
info!("Uploaded {} [{}]", build.name, bsh.0.to_hex());
} else {
info!("{} [{}] is up to date", build.name, bsh.0.to_hex());
}
}
handle.block_on(remote_cache.finish_uploads()).unwrap();
handle.block_on(writer.finish_uploads()).unwrap();
})
.join()
.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions crates/rcache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ license = "MIT OR Apache-2.0"
publish.workspace = true

[dev-dependencies]
mockall = "0.13"
tokio = { workspace = true, features = ["test-util"] }

[dependencies]
tracing.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/rcache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
mod remote;
pub use remote::{Error, INDEX_FILENAME, RemoteCache};

mod remote_writer;
pub use remote_writer::RemoteCacheWriter;

mod remote_index;

/// An adapter that lets you use a [RemoteCache] as a [graph::BinProvider].
Expand Down
Loading
0