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

feat(async_cache): support cloning #7293

Merged
merged 1 commit into from
Feb 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 3 additions & 12 deletions crates/turborepo-cache/src/async_cache.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::sync::{atomic::AtomicU8, Arc};

use futures::{stream::FuturesUnordered, StreamExt};
use tokio::{
sync::{mpsc, Semaphore},
task::JoinHandle,
};
use tokio::sync::{mpsc, Semaphore};
use tracing::{warn, Instrument, Level};
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf, AnchoredSystemPathBuf};
use turborepo_analytics::AnalyticsSender;
Expand All @@ -14,10 +11,10 @@ use crate::{multiplexer::CacheMultiplexer, CacheError, CacheHitMetadata, CacheOp

const WARNING_CUTOFF: u8 = 4;

#[derive(Clone)]
pub struct AsyncCache {
real_cache: Arc<CacheMultiplexer>,
writer_sender: mpsc::Sender<WorkerRequest>,
writer_thread: JoinHandle<()>,
}

enum WorkerRequest {
Expand Down Expand Up @@ -50,7 +47,7 @@ impl AsyncCache {

// start a task to manage workers
let worker_real_cache = real_cache.clone();
let writer_thread = tokio::spawn(async move {
tokio::spawn(async move {
let semaphore = Arc::new(Semaphore::new(max_workers));
let mut workers = FuturesUnordered::new();
let real_cache = worker_real_cache;
Expand Down Expand Up @@ -108,7 +105,6 @@ impl AsyncCache {
Ok(AsyncCache {
real_cache,
writer_sender,
writer_thread,
})
}

Expand Down Expand Up @@ -163,11 +159,6 @@ impl AsyncCache {
// Wait until flush callback is finished
rx.await.ok();
}

pub async fn shutdown(self) {
let Self { writer_thread, .. } = self;
writer_thread.await.unwrap();
}
Comment on lines -167 to -170
Copy link
Member Author

Choose a reason for hiding this comment

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

This was unused

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm that feels wrong. If we don't await on the writer thread, there's a chance that we could exit before we finish adding stuff to the cache

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea it does - I can return this and track it outside of the async_cache struct to make sure we close it down.

Copy link
Contributor

Choose a reason for hiding this comment

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

wait ensures that all workers have finished writing to the underlying cache. We could add a new WorkerRequest::Shutdown that does the same, but drops the receiving end so any future writes will fail. (IMO this is easier than passing around a join handle to all the correct places)

}

#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion crates/turborepo-lib/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use tracing::debug;
use turborepo_telemetry::events::command::CommandEventBuilder;

use crate::{commands::CommandBase, run, run::Run, signal::SignalHandler};
Expand Down