Compare commits

...

2 Commits

Author SHA1 Message Date
Hauser Zhi f6dc415580 Merge branch 'main' into houseme/experiment-per-disk-fsync-workers 2026-12-02 21:49:28 +08:00
Hauser Zhi db4002511c feat(ecstore): add an experimental per-disk fsync worker cap
Co-Authored-By: heihutu <heihutu@gmail.com>
Co-Authored-By: zhi22915 <qiuzgang@gmail.com>
Co-Authored-By: qunjiazhu <qunjiazhu@gmail.com>
2026-12-02 14:13:02 +08:00
2 changed files with 79 additions and 23 deletions
+72 -22
View File
@@ -2728,18 +2728,25 @@ const DEFAULT_FSYNC_WORKER_IDLE_SPIN_ITERS: usize = 256;
const MAX_FSYNC_WORKER_IDLE_SPIN_ITERS: usize = 1024;
const FALLBACK_NAMESPACE_QUEUE_SHARDS: usize = 8;
const ENV_FSYNC_WORKERS_PER_DISK: &str = "RUSTFS_EXPERIMENTAL_FSYNC_WORKERS_PER_DISK";
const ENV_FSYNC_WORKER_IDLE_SPIN_ITERS: &str = "RUSTFS_EXPERIMENTAL_FSYNC_WORKER_IDLE_SPIN_ITERS";
const ENV_FSYNC_WORKER_KEEP_MULTIPLE_IDLE: &str = "RUSTFS_EXPERIMENTAL_FSYNC_WORKER_KEEP_MULTIPLE_IDLE";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct FsyncWorkerIdlePolicy {
struct FsyncWorkerPolicy {
max_workers: usize,
spin_iters: usize,
keep_multiple_idle: bool,
}
impl FsyncWorkerIdlePolicy {
impl FsyncWorkerPolicy {
fn from_env() -> Self {
let max_workers = match rustfs_utils::get_env_usize(ENV_FSYNC_WORKERS_PER_DISK, MAX_FSYNC_WORKERS_PER_DISK) {
4 => 4,
_ => MAX_FSYNC_WORKERS_PER_DISK,
};
Self {
max_workers,
spin_iters: rustfs_utils::get_env_usize(ENV_FSYNC_WORKER_IDLE_SPIN_ITERS, DEFAULT_FSYNC_WORKER_IDLE_SPIN_ITERS)
.min(MAX_FSYNC_WORKER_IDLE_SPIN_ITERS),
keep_multiple_idle: rustfs_utils::get_env_bool(ENV_FSYNC_WORKER_KEEP_MULTIPLE_IDLE, true),
@@ -2748,7 +2755,7 @@ impl FsyncWorkerIdlePolicy {
}
// Resolve once so each queue uses one policy throughout worker retirement and respawn.
static FSYNC_WORKER_IDLE_POLICY: LazyLock<FsyncWorkerIdlePolicy> = LazyLock::new(FsyncWorkerIdlePolicy::from_env);
static FSYNC_WORKER_POLICY: LazyLock<FsyncWorkerPolicy> = LazyLock::new(FsyncWorkerPolicy::from_env);
#[derive(Clone, Copy, PartialEq, Eq)]
enum DiskBlockingLane {
@@ -2788,7 +2795,7 @@ struct DiskBlockingQueueLane {
pub(crate) struct DiskBlockingQueue {
lanes: [DiskBlockingQueueLane; 3],
fsync_idle_policy: FsyncWorkerIdlePolicy,
fsync_worker_policy: FsyncWorkerPolicy,
#[cfg(test)]
before_idle_spin: Mutex<Option<Arc<dyn Fn() + Send + Sync>>>,
}
@@ -2857,13 +2864,13 @@ impl Drop for DiskBlockingWorkerGuard {
impl DiskBlockingQueue {
fn new() -> Arc<Self> {
Self::with_fsync_idle_policy(*FSYNC_WORKER_IDLE_POLICY)
Self::with_fsync_worker_policy(*FSYNC_WORKER_POLICY)
}
fn with_fsync_idle_policy(fsync_idle_policy: FsyncWorkerIdlePolicy) -> Arc<Self> {
fn with_fsync_worker_policy(fsync_worker_policy: FsyncWorkerPolicy) -> Arc<Self> {
Arc::new(Self {
lanes: std::array::from_fn(|_| DiskBlockingQueueLane::default()),
fsync_idle_policy,
fsync_worker_policy,
#[cfg(test)]
before_idle_spin: Mutex::new(None),
})
@@ -2878,7 +2885,7 @@ impl DiskBlockingQueue {
fn lane_max_workers(&self, lane: DiskBlockingLane) -> usize {
match lane {
DiskBlockingLane::Foreground | DiskBlockingLane::Isolated => MAX_NAMESPACE_WORKERS_PER_DISK,
DiskBlockingLane::Fsync => MAX_FSYNC_WORKERS_PER_DISK,
DiskBlockingLane::Fsync => self.fsync_worker_policy.max_workers,
}
}
@@ -2985,7 +2992,7 @@ impl DiskBlockingQueue {
.ready
.store(!state.jobs.is_empty(), std::sync::atomic::Ordering::Relaxed);
if job.is_none() {
if (lane != DiskBlockingLane::Fsync || !queue.fsync_idle_policy.keep_multiple_idle)
if (lane != DiskBlockingLane::Fsync || !queue.fsync_worker_policy.keep_multiple_idle)
&& state.idle_workers > 0
{
state.active_workers = state.active_workers.saturating_sub(1);
@@ -3013,7 +3020,7 @@ impl DiskBlockingQueue {
}
}
let spin_iters = if lane == DiskBlockingLane::Fsync {
queue.fsync_idle_policy.spin_iters
queue.fsync_worker_policy.spin_iters
} else {
WORKER_IDLE_SPIN_ITERS
};
@@ -14329,9 +14336,10 @@ mod tests {
assert_eq!(completed, 257, "yielding must not discard hot-disk jobs");
}
fn fsync_idle_test_policies() -> impl Iterator<Item = FsyncWorkerIdlePolicy> {
fn fsync_idle_test_policies() -> impl Iterator<Item = FsyncWorkerPolicy> {
[0, 256, 1024].into_iter().flat_map(|spin_iters| {
[false, true].map(|keep_multiple_idle| FsyncWorkerIdlePolicy {
[false, true].map(|keep_multiple_idle| FsyncWorkerPolicy {
max_workers: MAX_FSYNC_WORKERS_PER_DISK,
spin_iters,
keep_multiple_idle,
})
@@ -14352,13 +14360,15 @@ mod tests {
] {
temp_env::with_vars(
[
(ENV_FSYNC_WORKERS_PER_DISK, None),
(ENV_FSYNC_WORKER_IDLE_SPIN_ITERS, spin_value),
(ENV_FSYNC_WORKER_KEEP_MULTIPLE_IDLE, multiple_value),
],
|| {
assert_eq!(
FsyncWorkerIdlePolicy::from_env(),
FsyncWorkerIdlePolicy {
FsyncWorkerPolicy::from_env(),
FsyncWorkerPolicy {
max_workers: MAX_FSYNC_WORKERS_PER_DISK,
spin_iters: expected_spins,
keep_multiple_idle: expected_multiple,
},
@@ -14369,12 +14379,31 @@ mod tests {
}
}
#[test]
fn disk_blocking_queue_fsync_worker_limit_accepts_only_experiment_values() {
for (input, expected) in [
(None, 8),
(Some("4"), 4),
(Some("8"), 8),
(Some("0"), 8),
(Some("16"), 8),
(Some("invalid"), 8),
] {
temp_env::with_var(ENV_FSYNC_WORKERS_PER_DISK, input, || {
let queue = DiskBlockingQueue::with_fsync_worker_policy(FsyncWorkerPolicy::from_env());
assert_eq!(queue.lane_max_workers(DiskBlockingLane::Fsync), expected, "input={input:?}");
assert_eq!(queue.lane_max_workers(DiskBlockingLane::Foreground), MAX_NAMESPACE_WORKERS_PER_DISK);
assert_eq!(queue.lane_max_workers(DiskBlockingLane::Isolated), MAX_NAMESPACE_WORKERS_PER_DISK);
});
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn disk_blocking_queue_drains_bursts_across_worker_retirement() {
use std::sync::atomic::{AtomicUsize, Ordering};
for policy in fsync_idle_test_policies() {
for lane in [DiskBlockingLane::Foreground, DiskBlockingLane::Fsync] {
let queue = DiskBlockingQueue::with_fsync_idle_policy(policy);
let queue = DiskBlockingQueue::with_fsync_worker_policy(policy);
let completed = Arc::new(AtomicUsize::new(0));
for _ in 0..8 {
let mut producers = tokio::task::JoinSet::new();
@@ -14499,7 +14528,8 @@ mod tests {
for keep_multiple_idle in [false, true] {
tokio::time::timeout(Duration::from_secs(5), async {
let queue = DiskBlockingQueue::with_fsync_idle_policy(FsyncWorkerIdlePolicy {
let queue = DiskBlockingQueue::with_fsync_worker_policy(FsyncWorkerPolicy {
max_workers: MAX_FSYNC_WORKERS_PER_DISK,
spin_iters: 256,
keep_multiple_idle,
});
@@ -14621,8 +14651,14 @@ mod tests {
async fn disk_blocking_queue_fsync_lane_scales_workers_and_retires_idle() {
use std::sync::atomic::{AtomicUsize, Ordering};
for policy in fsync_idle_test_policies() {
let queue = DiskBlockingQueue::with_fsync_idle_policy(policy);
for policy in fsync_idle_test_policies().flat_map(|idle_policy| {
[4, MAX_FSYNC_WORKERS_PER_DISK].map(|max_workers| FsyncWorkerPolicy {
max_workers,
..idle_policy
})
}) {
let max_workers = policy.max_workers;
let queue = DiskBlockingQueue::with_fsync_worker_policy(policy);
let active = Arc::new(AtomicUsize::new(0));
let max_seen = Arc::new(AtomicUsize::new(0));
let entered = Arc::new(AtomicUsize::new(0));
@@ -14631,7 +14667,7 @@ mod tests {
let (release_tx, _) = tokio::sync::broadcast::channel::<()>(1);
let mut handles = Vec::new();
for i in 0..(MAX_FSYNC_WORKERS_PER_DISK + 4) {
for i in 0..(max_workers + 4) {
let q = Arc::clone(&queue);
let active = Arc::clone(&active);
let max_seen = Arc::clone(&max_seen);
@@ -14648,12 +14684,12 @@ mod tests {
let cur = active.fetch_add(1, Ordering::SeqCst) + 1;
max_seen.fetch_max(cur, Ordering::SeqCst);
let ord = entered.fetch_add(1, Ordering::SeqCst) + 1;
if ord == MAX_FSYNC_WORKERS_PER_DISK
if ord == max_workers
&& let Some(tx) = all_entered_tx.lock().expect("lock completion sender").take()
{
let _ = tx.send(());
}
if ord <= MAX_FSYNC_WORKERS_PER_DISK {
if ord <= max_workers {
let _ = release_rx.blocking_recv();
}
active.fetch_sub(1, Ordering::SeqCst);
@@ -14668,13 +14704,27 @@ mod tests {
.await
.expect("all fsync workers should enter within timeout")
.expect("all_entered channel");
tokio::time::timeout(Duration::from_secs(5), async {
loop {
{
let state = queue.lane(DiskBlockingLane::Fsync).state.lock();
assert_eq!(state.active_workers, max_workers);
if state.jobs.len() == 4 {
break;
}
}
tokio::task::yield_now().await;
}
})
.await
.expect("all excess jobs must queue while the fsync workers are busy");
let _ = release_tx.send(());
for (i, handle) in handles.into_iter().enumerate() {
assert_eq!(handle.await.expect("task join").expect("fsync job"), i);
}
assert_eq!(
max_seen.load(Ordering::SeqCst),
MAX_FSYNC_WORKERS_PER_DISK,
max_workers,
"fsync lane must run MAX_FSYNC_WORKERS_PER_DISK concurrent workers up to its configured bound"
);
tokio::time::timeout(std::time::Duration::from_secs(5), async {
@@ -76,6 +76,12 @@ iostat -xz 5 > telemetry/iostat.txt &
| `RUSTFS_DURABILITY_MODE` / `RUSTFS_DRIVE_SYNC_ENABLE` | mode-dependent | Per-shard fsync/sync discipline on commit | `set_disk_rename` P99 | Weakening it changes the durability contract — a deliberate tradeoff, never a free win |
| `RUSTFS_RUNTIME_WORKER_THREADS` / `RUSTFS_RUNTIME_MAX_BLOCKING_THREADS` | Tokio defaults | Async workers + `spawn_blocking` pool feeding per-block encode | `set_disk_encode` P95 + mpstat | Oversubscription |
#### Per-disk fsync worker concurrency experiment
Use `RUSTFS_EXPERIMENTAL_FSYNC_WORKERS_PER_DISK` to compare a cap of `4` active workers with the default `8` on the foreground Fsync lane of each disk. An unset value or a value other than `4` or `8` selects `8`. The process reads it at first queue creation. Restart every participating server per leg with the same binary, using `8` for A1/A2 and `4` for B1/B2.
Qualify the same-build A/A baseline before comparing the caps. Keep the effective bucket mode at `strict`, the offered workload and idle policy fixed, and record the start-of-round disk state. Compare throughput, PUT p99/p99.9, and the worker dispatch and execution stages. A lower cap can move time from filesystem waits into the worker queue, so use end-to-end PUT latency to evaluate it. The namespace and isolated lanes use their own worker policies.
#### Fsync worker idle policy experiment
The disk fsync queue exposes two independent experimental controls for measuring idle CPU cost. Both are cached at first queue creation and require restarting every participating server when changed. The default spin budget is 256 iterations per empty-queue episode, with multiple idle workers allowed.
@@ -85,7 +91,7 @@ The disk fsync queue exposes two independent experimental controls for measuring
| `RUSTFS_EXPERIMENTAL_FSYNC_WORKER_IDLE_SPIN_ITERS` | `256` | Spin budget per empty-queue episode, clamped to `0..=1024`. `0` immediately rechecks retirement under the queue mutex. Invalid values use the default. Set `1024` to restore the longer spin budget. |
| `RUSTFS_EXPERIMENTAL_FSYNC_WORKER_KEEP_MULTIPLE_IDLE` | `true` | `false` retires an empty fsync worker when another idle/reserved worker already exists. Invalid values use the default. |
These controls affect the foreground fsync lane's idle policy. Its maximum execution concurrency remains eight workers per disk, and the namespace and isolated cleanup lanes retain their own policy. The queue's internal idle count includes worker reservations awaiting execution, so measure actual worker and spin activity through the diagnostics below.
These controls affect the foreground fsync lane's idle policy. Keep its worker cap at eight per disk for the idle policy experiment. The queue's internal idle count includes worker reservations awaiting execution, so measure actual worker and spin activity through the diagnostics below.
Use one fixed binary and the four cells below to distinguish the two factors and their interaction. Keep effective bucket durability at `strict`, and record inline admission, versioning, EC layout, CPU quota, storage mapping, background activity and build identity for each run. A historical release comparison also requires fixing that release's source and build inputs.