Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6aece3c
perf(workspace): audit shared alias content once per resume pass
Ivanbeethoven Oct 7, 2026
f926dce
Merge commit '7904321cd7169b97c3175a062e11288af0898188' into perf/wor…
Ivanbeethoven Oct 7, 2026
f178309
perf(mst2): move uniquely owned subtree facts
Ivanbeethoven Oct 7, 2026
baf49bc
perf(mst2): defer full snapshot manifests to final proof
Ivanbeethoven Oct 7, 2026
2adab47
perf(workspace): reuse bounded scope pages in lazy v3 mounts
Ivanbeethoven Oct 7, 2026
762da65
test(workspace): exercise page reuse through canonical typed resolve
Ivanbeethoven Oct 7, 2026
b4274ef
perf(bench): separate workspace oracle and process timings
Ivanbeethoven Oct 7, 2026
01604da
perf(workspace): share admitted CAS owners in v3 reads
Ivanbeethoven Oct 7, 2026
997250c
Merge commit 'b4274ef677d4f74a93e32f2ea1b7004aa68e0a50' into perf/wor…
Ivanbeethoven Oct 7, 2026
53cdbd3
fix(workspace): gate cached v3 metadata on current lease
Ivanbeethoven Oct 7, 2026
f226366
perf: admit owned workspace CAS range output and scratch
Ivanbeethoven Oct 7, 2026
cd9463c
Merge current workspace metadata lease checks into owned CAS ranges
Ivanbeethoven Oct 7, 2026
c7812d8
refactor(workspace): retire unaccounted coordinator fetch API
Ivanbeethoven Oct 7, 2026
ffadb40
Merge remote-tracking branch 'origin/perf/workspace-v3-cas-range-owne…
Ivanbeethoven Oct 7, 2026
8139f5f
fix(workspace): report stale leases and snapshots through FUSE
Ivanbeethoven Oct 7, 2026
1b48337
Merge remote-tracking branch 'origin/refactor/workspace-v3-owned-coor…
Ivanbeethoven Oct 7, 2026
fd818aa
test(cas): verify range admission before index work
Ivanbeethoven Oct 7, 2026
db5adf4
Merge remote-tracking branch 'origin/perf/workspace-v3-cas-range-owne…
Ivanbeethoven Oct 7, 2026
0adcc0b
Merge remote-tracking branch 'origin/refactor/workspace-v3-owned-coor…
Ivanbeethoven Oct 7, 2026
54614b5
refactor(workspace): retire unverified local CAS range API
Ivanbeethoven Oct 7, 2026
651ffcc
Merge remote-tracking branch 'origin/fix/workspace-v3-stale-errno' in…
Ivanbeethoven Oct 7, 2026
b4ddceb
perf(workspace): bound local CAS work outside async callbacks
Ivanbeethoven Oct 7, 2026
dbd95f5
Merge remote-tracking branch 'origin/refactor/workspace-v3-verified-c…
Ivanbeethoven Oct 7, 2026
1bb3423
test(workspace): verify shared CAS gates and accurate cancellation ev…
Ivanbeethoven Oct 7, 2026
303450a
test(snapshot): isolate CAS pool queue and real lease cancellation cases
Ivanbeethoven Oct 7, 2026
bb8c5df
fix(workspace): use a typed empty CAS pause marker
Ivanbeethoven Oct 7, 2026
d88ebd5
refactor(workspace): retire redundant Vec content wrappers
Ivanbeethoven Oct 7, 2026
4eefd8c
perf(snapshot): use owned CAS replies for canonical stored mounts
Ivanbeethoven Oct 7, 2026
d265e31
perf(workspace): bound readerless CAS work and own replies
Ivanbeethoven Oct 7, 2026
8897425
refactor(fuse): retire old online caches and own every reply
Ivanbeethoven Oct 7, 2026
20e9156
fix(test): use the declared FUSE module for online tokens
Ivanbeethoven Oct 7, 2026
afceb63
test: bind stored FUSE fixtures to reader authority
Ivanbeethoven Oct 7, 2026
ee420ce
fix: preserve metadata readiness errors in v3 readers
Ivanbeethoven Oct 7, 2026
efc0d15
refactor(snapshot): retire shared unaccounted range reader
Ivanbeethoven Oct 7, 2026
0fc90db
Merge commit 'ee420ce10600dcdb18b019e2f84fdd74cd3dd0ca' into refactor…
Ivanbeethoven Oct 7, 2026
e465352
style(snapshot): honor nightly import grouping
Ivanbeethoven Oct 7, 2026
6d63ac9
refactor(workspace): remove aggregate reader wrappers after owned mig…
Ivanbeethoven Oct 7, 2026
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
448 changes: 448 additions & 0 deletions src/snapshot/cas_content.rs

Large diffs are not rendered by default.

492 changes: 459 additions & 33 deletions src/snapshot/cas_index.rs

Large diffs are not rendered by default.

749 changes: 749 additions & 0 deletions src/snapshot/cas_range.rs

Large diffs are not rendered by default.

352 changes: 352 additions & 0 deletions src/snapshot/cas_worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,352 @@
//! Bounded local CAS work. A cancelled waiter never refunds a running job's
//! actual buffers or permits, and only the primitive can report a CAS miss.

use std::{
sync::{
atomic::{AtomicU64, AtomicU8, Ordering},
Arc, OnceLock,
},
time::Instant,
};

use tokio::sync::{OwnedSemaphorePermit, Semaphore, TryAcquireError};

use super::{
cas_index::LocalCasRangeMeters, fuse_owned::ReplyAdmission, SnapshotError, SnapshotErrorCode,
SnapshotReader,
};

const LOCAL_OUTSTANDING: usize = 16;
const LOCAL_RUNNING: usize = 2;
const PROCESS_OUTSTANDING: usize = 64;
const PROCESS_RUNNING: usize = 4;
const PENDING: u8 = 0;
const RUNNING: u8 = 1;
const CANCELLED: u8 = 2;
const FINISHED: u8 = 3;

struct ProcessAdmission {
outstanding: Arc<Semaphore>,
running: Arc<Semaphore>,
}

fn process_admission() -> Arc<ProcessAdmission> {
static PROCESS: OnceLock<Arc<ProcessAdmission>> = OnceLock::new();
PROCESS
.get_or_init(|| {
Arc::new(ProcessAdmission {
outstanding: Arc::new(Semaphore::new(PROCESS_OUTSTANDING)),
running: Arc::new(Semaphore::new(PROCESS_RUNNING)),
})
})
.clone()
}

/// Shared by reader clones and mounts using their same content budget. The
/// process gate bounds this component's jobs, not every Tokio blocking task.
pub(crate) struct CasReadScope {
outstanding: Arc<Semaphore>,
running: Arc<Semaphore>,
process: Arc<ProcessAdmission>,
}

/// Local constructors establish access before enabling byte reads. This is
/// not an online lease or a grant that expires again on each local read.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum LocalCasAccess {
CallerEstablished,
GrantCheckedOnReopen,
}

enum AccessCheck<F> {
Online(F),
Local(LocalCasAccess),
}

impl<F: Fn() -> Result<(), SnapshotError>> AccessCheck<F> {
fn check(&self) -> Result<(), SnapshotError> {
match self {
Self::Online(lease) => lease(),
Self::Local(
LocalCasAccess::CallerEstablished | LocalCasAccess::GrantCheckedOnReopen,
) => Ok(()),
}
}
}

pub(crate) struct WorkResult<T> {
pub(crate) result: Result<T, SnapshotError>,
// Small-object work has no range meters; never report it as zero I/O.
pub(crate) meters: Option<LocalCasRangeMeters>,
outcome: &'static str,
}

impl<T> WorkResult<Option<T>> {
pub(crate) fn local(
result: Result<Option<T>, SnapshotError>,
meters: Option<LocalCasRangeMeters>,
) -> Self {
let outcome = match &result {
Ok(Some(_)) => "hit",
Ok(None) => "miss",
Err(_) => "error",
};
Self {
result,
meters,
outcome,
}
}
}

pub(crate) struct Completion<T> {
pub(crate) result: Result<T, SnapshotError>,
pub(crate) admission: ReplyAdmission,
}

#[derive(Clone, Copy)]
pub(crate) struct RequestMeters {
pub(crate) kind: &'static str,
pub(crate) wanted: u64,
}

struct JobPermits {
_local_outstanding: OwnedSemaphorePermit,
_process_outstanding: OwnedSemaphorePermit,
_local_running: OwnedSemaphorePermit,
_process_running: OwnedSemaphorePermit,
}

// Pending -> Running and Pending -> Cancelled have a single atomic winner.
// Once started, cancelling the waiter only detaches; it does not stop I/O.
struct CancelOnDrop {
state: Arc<AtomicU8>,
id: u64,
kind: &'static str,
}
impl Drop for CancelOnDrop {
fn drop(&mut self) {
let result =
self.state
.compare_exchange(PENDING, CANCELLED, Ordering::AcqRel, Ordering::Acquire);
if result.is_ok() || result == Err(RUNNING) {
tracing::debug!(
target: "scorpiofs::workspace::performance",
job_id = self.id, kind = self.kind,
cancelled_before_start = result.is_ok(),
detached_running = result == Err(RUNNING),
"local CAS waiter dropped"
);
}
}
}

struct JobTrace {
state: Arc<AtomicU8>,
request: RequestMeters,
id: u64,
queued: Option<Instant>,
started: Option<Instant>,
outcome: &'static str,
meters: Option<LocalCasRangeMeters>,
}
impl Drop for JobTrace {
fn drop(&mut self) {
self.state.store(FINISHED, Ordering::Release);
if let (Some(queued), Some(started)) = (self.queued, self.started) {
let meters = self.meters.unwrap_or_default();
tracing::debug!(
target: "scorpiofs::workspace::performance",
job_id = self.id,
kind = self.request.kind,
wanted = self.request.wanted,
queue_wait_us = started.duration_since(queued).as_micros().min(u64::MAX as u128) as u64,
worker_wall_us = started.elapsed().as_micros().min(u64::MAX as u128) as u64,
outcome = if std::thread::panicking() { "panic" } else { self.outcome },
range_meters_available = self.meters.is_some(),
bytes_read = meters.bytes_read,
whole_sha256_bytes = meters.whole_sha256_bytes,
chunk_sha256_bytes = meters.chunk_sha256_bytes,
index_hit = meters.index_hit,
index_built = meters.index_built,
strict_fallback = meters.strict_fallback,
"local CAS worker finished"
);
}
}
}

impl CasReadScope {
pub(crate) fn new() -> Arc<Self> {
Arc::new(Self {
outstanding: Arc::new(Semaphore::new(LOCAL_OUTSTANDING)),
running: Arc::new(Semaphore::new(LOCAL_RUNNING)),
process: process_admission(),
})
}

pub(crate) async fn run<T: Send + 'static>(
&self,
reader: SnapshotReader,
admission: ReplyAdmission,
request: RequestMeters,
work: impl FnOnce() -> WorkResult<T> + Send + 'static,
) -> Result<Completion<T>, SnapshotError> {
self.run_checked(
move || reader.local_lease_status(),
admission,
request,
work,
)
.await
}

async fn run_checked<T: Send + 'static>(
&self,
lease: impl Fn() -> Result<(), SnapshotError> + Send + Sync + 'static,
admission: ReplyAdmission,
request: RequestMeters,
work: impl FnOnce() -> WorkResult<T> + Send + 'static,
) -> Result<Completion<T>, SnapshotError> {
self.run_admitted(AccessCheck::Online(lease), admission, request, work)
.await
}

pub(crate) async fn run_local<T: Send + 'static>(
&self,
access: LocalCasAccess,
admission: ReplyAdmission,
request: RequestMeters,
work: impl FnOnce() -> WorkResult<T> + Send + 'static,
) -> Result<Completion<T>, SnapshotError> {
tracing::debug!(
target: "scorpiofs::workspace::performance", mode = ?access,
"local CAS uses constructor-established access"
);
self.run_admitted(
AccessCheck::<fn() -> Result<(), SnapshotError>>::Local(access),
admission,
request,
work,
)
.await
}

async fn run_admitted<T: Send + 'static>(
&self,
access: AccessCheck<impl Fn() -> Result<(), SnapshotError> + Send + Sync + 'static>,
admission: ReplyAdmission,
request: RequestMeters,
work: impl FnOnce() -> WorkResult<T> + Send + 'static,
) -> Result<Completion<T>, SnapshotError> {
let local_outstanding = try_admit(&self.outstanding)?;
let process_outstanding = try_admit(&self.process.outstanding)?;
let enabled =
tracing::enabled!(target: "scorpiofs::workspace::performance", tracing::Level::DEBUG);
let queued = enabled.then(Instant::now);
let state = Arc::new(AtomicU8::new(PENDING));
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
let _cancel = CancelOnDrop {
state: state.clone(),
id,
kind: request.kind,
};
// No component can occupy global execution while awaiting its local
// execution slot. Waiting futures are already count-admitted.
let local_running = self.running.clone().acquire_owned().await.map_err(|_| {
state.store(FINISHED, Ordering::Release);
closed()
})?;
let process_running = self
.process
.running
.clone()
.acquire_owned()
.await
.map_err(|_| {
state.store(FINISHED, Ordering::Release);
closed()
})?;
if let Err(error) = access.check() {
state.store(FINISHED, Ordering::Release);
tracing::debug!(
target: "scorpiofs::workspace::performance",
job_id = id, kind = request.kind, wanted = request.wanted,
code = ?error.code, "local CAS dispatch lease rejected"
);
return Err(error);
}
let permits = JobPermits {
_local_outstanding: local_outstanding,
_process_outstanding: process_outstanding,
_local_running: local_running,
_process_running: process_running,
};
let parent = tracing::Span::current();
let dispatcher = tracing::dispatcher::get_default(|current| current.clone());
tokio::task::spawn_blocking(move || {
tracing::dispatcher::with_default(&dispatcher, || {
parent.in_scope(|| {
let _permits = permits;
let mut trace = JobTrace {
state: state.clone(),
request,
id,
queued,
started: enabled.then(Instant::now),
outcome: "not_started",
meters: None,
};
let result = if state
.compare_exchange(PENDING, RUNNING, Ordering::AcqRel, Ordering::Acquire)
.is_err()
{
trace.outcome = "cancelled_before_start";
Err(SnapshotError::new(
SnapshotErrorCode::Internal,
"local CAS waiter cancelled before worker start",
))
} else if let Err(error) = access.check() {
trace.outcome = "lease_error";
Err(error)
} else {
let output = work();
trace.meters = output.meters;
trace.outcome = output.outcome;
output.result
};
Completion { result, admission }
})
})
})
.await
.map_err(|error| {
SnapshotError::new(
SnapshotErrorCode::Internal,
format!("local CAS worker join failed: {error}"),
)
})
}
}

fn try_admit(semaphore: &Arc<Semaphore>) -> Result<OwnedSemaphorePermit, SnapshotError> {
semaphore.clone().try_acquire_owned().map_err(|error| match error {
TryAcquireError::NoPermits => {
tracing::debug!(target: "scorpiofs::workspace::performance", "local CAS outstanding count rejected");
SnapshotError::new(SnapshotErrorCode::LimitExceeded, "local CAS outstanding count limit reached")
}
TryAcquireError::Closed => closed(),
})
}

fn closed() -> SnapshotError {
SnapshotError::new(
SnapshotErrorCode::Internal,
"local CAS execution gate closed",
)
}

#[cfg(test)]
#[path = "cas_worker_tests.rs"]
mod tests;
Loading
Loading