goosefs-sdk 0.1.8

Goosefs Rust gRPC Client - Direct gRPC client for Goosefs Master/Worker
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright (C) 2026 Tencent. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Page-cache backend A/B benchmark: io_uring vs tokio::fs, **local-only**
//! (no GooseFS cluster required).
//!
//! Directly exercises the `PageStore` trait with both backends to isolate the
//! IO-layer overhead. Each iteration is one `get()` call — a cache hit — so
//! ops/s is the primary metric.
//!
//! ## Usage (Linux 5.1+)
//! ```bash
//! cargo run --release --example cache_uring_bench
//! ```
//! On non-Linux platforms only the `LocalPageStore` (tokio::fs) path is
//! benchmarked; `UringPageStore` is `#[cfg(target_os = "linux")]`-gated.
//!
//! ## Env knobs
//! - `BENCH_ITERATIONS` — single-threaded iterations per backend (default 100_000)
//! - `BENCH_CONCURRENCY` — concurrent task count (default 32)
//! - `BENCH_CONCURRENT_ITERATIONS` — iterations per concurrent task (default 10_000)
//! - `BENCH_PAGE_SIZE` — page size in bytes (default 1024)
//!
//! See `docs/CLIENT_PAGE_CACHE_DESIGN.md` §10.3 for expected results.

use std::sync::Arc;
use std::time::Instant;

#[cfg(target_os = "linux")]
use goosefs_sdk::cache::store::UringPageStore;
use goosefs_sdk::cache::store::{LocalPageStore, PageStore};
use goosefs_sdk::cache::PageId;

fn env_or<T: std::str::FromStr>(key: &str, default: T) -> T {
    std::env::var(key)
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(default)
}

struct BenchResult {
    label: &'static str,
    ops_per_sec: f64,
    p50_ns: u64,
    p99_ns: u64,
    total_ns: u64,
}

/// Run `iterations` single-threaded `get()` calls against `store` on `page_id`,
/// measuring per-op latency.
///
/// A warm-up read is performed first (to fill the fd cache on the io_uring
/// backend) so all measured iterations are cache-hit + fd-cache-hit.
async fn bench_single_threaded(
    store: &Arc<dyn PageStore>,
    page_id: &PageId,
    page_size: usize,
    iterations: usize,
    label: &'static str,
) -> BenchResult {
    let mut dst = vec![0u8; page_size];

    // Warm-up (fd cache fill for UringPageStore). Timeout after 5s to avoid
    // hanging the whole benchmark if the io_uring backend is broken.
    match tokio::time::timeout(
        std::time::Duration::from_secs(5),
        store.get(page_id, 0, &mut dst),
    )
    .await
    {
        Ok(Ok(_)) => {}
        Ok(Err(e)) => panic!("{label} warm-up failed: {e}"),
        Err(_) => panic!(
            "{label} warm-up TIMED OUT after 5s — io_uring backend may be broken; \
             check RUST_LOG=trace output and `dmesg | tail` for kernel errors"
        ),
    }

    let mut latencies_ns: Vec<u64> = Vec::with_capacity(iterations);
    let start = Instant::now();
    for _ in 0..iterations {
        let op_start = Instant::now();
        let n = store.get(page_id, 0, &mut dst).await.expect("get failed");
        debug_assert_eq!(n, page_size, "short read in benchmark");
        latencies_ns.push(op_start.elapsed().as_nanos() as u64);
    }
    let total = start.elapsed();

    latencies_ns.sort_unstable();
    let p50 = latencies_ns[latencies_ns.len() / 2];
    let p99 = latencies_ns[latencies_ns.len() * 99 / 100];
    let ops_per_sec = iterations as f64 / total.as_secs_f64().max(1e-9);

    BenchResult {
        label,
        ops_per_sec,
        p50_ns: p50,
        p99_ns: p99,
        total_ns: total.as_nanos() as u64,
    }
}

/// Run `concurrency` tasks, each doing `iterations_per_task` `get()` calls.
async fn bench_concurrent(
    store: Arc<dyn PageStore>,
    page_id: PageId,
    page_size: usize,
    concurrency: usize,
    iterations_per_task: usize,
    label: &'static str,
) -> BenchResult {
    // Warm-up.
    {
        let mut dst = vec![0u8; page_size];
        let _ = store.get(&page_id, 0, &mut dst).await;
    }

    let start = Instant::now();
    let mut handles = Vec::with_capacity(concurrency);
    for _ in 0..concurrency {
        let store = Arc::clone(&store);
        let pid = page_id.clone();
        handles.push(tokio::spawn(async move {
            let mut dst = vec![0u8; page_size];
            let mut latencies: Vec<u64> = Vec::with_capacity(iterations_per_task);
            for _ in 0..iterations_per_task {
                let op_start = Instant::now();
                let n = store.get(&pid, 0, &mut dst).await.expect("get failed");
                debug_assert_eq!(n, page_size);
                latencies.push(op_start.elapsed().as_nanos() as u64);
            }
            latencies
        }));
    }

    let mut all_latencies: Vec<u64> = Vec::with_capacity(concurrency * iterations_per_task);
    for h in handles {
        all_latencies.extend(h.await.unwrap());
    }
    let total = start.elapsed();

    all_latencies.sort_unstable();
    let p50 = all_latencies[all_latencies.len() / 2];
    let p99 = all_latencies[all_latencies.len() * 99 / 100];
    let total_ops = concurrency * iterations_per_task;
    let ops_per_sec = total_ops as f64 / total.as_secs_f64().max(1e-9);

    BenchResult {
        label,
        ops_per_sec,
        p50_ns: p50,
        p99_ns: p99,
        total_ns: total.as_nanos() as u64,
    }
}

fn print_result(r: &BenchResult) {
    println!(
        "  {:<16} {:>10.0} ops/s   p50={:>6}µs   p99={:>6}µs   total={:.2}s",
        r.label,
        r.ops_per_sec,
        r.p50_ns / 1000,
        r.p99_ns / 1000,
        r.total_ns as f64 / 1e9,
    );
}

fn print_header(title: &str) {
    println!("\n── {title} ────────────────────────────────────────");
}

/// Like `bench_concurrent` but each task reads from a different file
/// (round-robin). This exercises the dir fd cache's main benefit
/// (eliminating VFS lock contention on concurrent `open()`) and the
/// multi-file scaling of the `LocalCacheManager` metadata layer.
async fn bench_concurrent_multi_file(
    store: Arc<dyn PageStore>,
    page_ids: Vec<PageId>,
    page_size: usize,
    concurrency: usize,
    iterations_per_task: usize,
    label: &'static str,
) -> BenchResult {
    // Warm-up: read each file once.
    for id in &page_ids {
        let mut dst = vec![0u8; page_size];
        let _ = store.get(id, 0, &mut dst).await;
    }

    let start = Instant::now();
    let mut handles = Vec::with_capacity(concurrency);
    for task_id in 0..concurrency {
        let store = Arc::clone(&store);
        let page_ids = page_ids.clone();
        handles.push(tokio::spawn(async move {
            let mut dst = vec![0u8; page_size];
            let mut latencies: Vec<u64> = Vec::with_capacity(iterations_per_task);
            for i in 0..iterations_per_task {
                // Round-robin: each task reads a different file each iteration.
                let id = &page_ids[(i + task_id) % page_ids.len()];
                let op_start = Instant::now();
                let n = store.get(id, 0, &mut dst).await.expect("get failed");
                debug_assert_eq!(n, page_size);
                latencies.push(op_start.elapsed().as_nanos() as u64);
            }
            latencies
        }));
    }

    let mut all_latencies: Vec<u64> = Vec::with_capacity(concurrency * iterations_per_task);
    for h in handles {
        all_latencies.extend(h.await.unwrap());
    }
    let total = start.elapsed();

    all_latencies.sort_unstable();
    let p50 = all_latencies[all_latencies.len() / 2];
    let p99 = all_latencies[all_latencies.len() * 99 / 100];
    let total_ops = concurrency * iterations_per_task;
    let ops_per_sec = total_ops as f64 / total.as_secs_f64().max(1e-9);

    BenchResult {
        label,
        ops_per_sec,
        p50_ns: p50,
        p99_ns: p99,
        total_ns: total.as_nanos() as u64,
    }
}

#[tokio::main]
async fn main() {
    let iterations: usize = env_or("BENCH_ITERATIONS", 100_000);
    let concurrency: usize = env_or("BENCH_CONCURRENCY", 32);
    let concurrent_iterations: usize = env_or("BENCH_CONCURRENT_ITERATIONS", 10_000);
    let page_size: usize = env_or("BENCH_PAGE_SIZE", 1024);

    let ts = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    let base_dir = std::env::temp_dir().join(format!("gfs_uring_bench_{ts}"));

    println!("╔══════════════════════════════════════════════════════════════╗");
    println!("║  Page-Cache Backend Benchmark: io_uring vs tokio::fs        ║");
    println!("╚══════════════════════════════════════════════════════════════╝");
    println!("  page_size={page_size}B  iterations={iterations}  concurrency={concurrency}×{concurrent_iterations}");
    println!("  cache_dir={}", base_dir.display());

    #[cfg(target_os = "linux")]
    {
        // Probe io_uring availability early — if it fails the user should
        // see it here rather than waiting for the warm-up timeout below.
        match goosefs_sdk::cache::store::is_uring_available() {
            true => println!("  io_uring: available"),
            false => panic!(
                "io_uring is NOT available on this platform. \
                 Set GOOSEFS_USER_CLIENT_CACHE_URING_ENABLED=false to skip the io_uring benchmark."
            ),
        }
    }

    // ── Create stores ──────────────────────────────────────────
    let local_dir = base_dir.join("tokio_fs");
    let local_store: Arc<dyn PageStore> = Arc::new(
        LocalPageStore::create(&local_dir, page_size as u64)
            .await
            .expect("LocalPageStore create"),
    );

    #[cfg(target_os = "linux")]
    let uring_store: Arc<dyn PageStore> = {
        // Use the new default of 8 threads (B2 fix). The old bench used 2,
        // which masked the concurrency benefit of the non-blocking driver
        // loop (B1 fix). Set to 0 to use the default (8).
        goosefs_sdk::cache::store::init_uring_config(16384, 0);
        // The background thread pool is lazily initialised on the first
        // submit_request() call (inside the first get()). The warm-up
        // timeout above will surface any hang.
        let uring_dir = base_dir.join("uring");
        Arc::new(
            UringPageStore::create(&uring_dir, page_size as u64)
                .await
                .expect("UringPageStore create"),
        )
    };

    let page_id = PageId::new("bench-file", 0);
    let page_data = vec![0x42u8; page_size];

    // ── Write the page to both stores ──────────────────────────
    local_store
        .put(&page_id, &page_data)
        .await
        .expect("local put");
    #[cfg(target_os = "linux")]
    uring_store
        .put(&page_id, &page_data)
        .await
        .expect("uring put");

    // ── Single-threaded benchmark ──────────────────────────────
    print_header("Single-threaded cache-hit throughput");

    let r_local =
        bench_single_threaded(&local_store, &page_id, page_size, iterations, "tokio::fs").await;
    print_result(&r_local);

    #[cfg(target_os = "linux")]
    let r_uring = Some(
        bench_single_threaded(&uring_store, &page_id, page_size, iterations, "io_uring").await,
    );
    #[cfg(not(target_os = "linux"))]
    let r_uring: Option<BenchResult> = None;

    if let Some(r) = &r_uring {
        print_result(r);
        let speedup = r.ops_per_sec / r_local.ops_per_sec.max(1.0);
        println!("  → io_uring speedup: {speedup:.2}×");
    }
    #[cfg(not(target_os = "linux"))]
    {
        println!("  (io_uring backend not available on this platform)");
    }

    // ── Concurrent benchmark ───────────────────────────────────
    print_header(&format!(
        "Concurrent cache-hit throughput ({concurrency} tasks)"
    ));

    let rc_local = bench_concurrent(
        Arc::clone(&local_store),
        page_id.clone(),
        page_size,
        concurrency,
        concurrent_iterations,
        "tokio::fs",
    )
    .await;
    print_result(&rc_local);

    #[cfg(target_os = "linux")]
    let rc_uring = Some(
        bench_concurrent(
            Arc::clone(&uring_store),
            page_id.clone(),
            page_size,
            concurrency,
            concurrent_iterations,
            "io_uring",
        )
        .await,
    );
    #[cfg(not(target_os = "linux"))]
    let rc_uring: Option<BenchResult> = None;

    if let Some(rc) = &rc_uring {
        print_result(rc);
        let speedup = rc.ops_per_sec / rc_local.ops_per_sec.max(1.0);
        println!("  → io_uring speedup: {speedup:.2}×");
    }
    #[cfg(not(target_os = "linux"))]
    {
        println!("  (io_uring backend not available on this platform)");
    }

    // ── Multi-file benchmark (exercises dir fd cache) ───────────
    // The single-file bench above doesn't trigger the dir fd cache's main
    // benefit (4-level → 1-level path resolution is a constant saving
    // regardless of file count, but VFS lock contention is only visible
    // with multiple files). This benchmark uses N files, each task
    // accesses its own file — this is the workload that exposes the
    // VFS lock contention that the dir fd cache is designed to fix.
    let n_files = env_or("BENCH_MULTI_FILE_COUNT", 64);
    print_header(&format!(
        "Multi-file cache-hit throughput ({n_files} files, {concurrency} concurrent tasks)"
    ));

    // Pre-populate N files in both stores.
    let multi_file_ids: Vec<PageId> = (0..n_files)
        .map(|i| PageId::new(format!("bench-file-{i}"), 0))
        .collect();
    for id in &multi_file_ids {
        local_store
            .put(id, &page_data)
            .await
            .expect("local put multi");
        #[cfg(target_os = "linux")]
        uring_store
            .put(id, &page_data)
            .await
            .expect("uring put multi");
    }

    // Run concurrent reads: each task picks a file in round-robin.
    // This pattern matches real workloads where many Lance queries read
    // pages from different blocks/files concurrently.
    let rc_local_multi = bench_concurrent_multi_file(
        Arc::clone(&local_store),
        multi_file_ids.clone(),
        page_size,
        concurrency,
        concurrent_iterations,
        "tokio::fs",
    )
    .await;
    print_result(&rc_local_multi);

    #[cfg(target_os = "linux")]
    let rc_uring_multi = Some(
        bench_concurrent_multi_file(
            Arc::clone(&uring_store),
            multi_file_ids.clone(),
            page_size,
            concurrency,
            concurrent_iterations,
            "io_uring",
        )
        .await,
    );
    #[cfg(not(target_os = "linux"))]
    let rc_uring_multi: Option<BenchResult> = None;

    if let Some(rc) = &rc_uring_multi {
        print_result(rc);
        let speedup = rc.ops_per_sec / rc_local_multi.ops_per_sec.max(1.0);
        println!("  → io_uring speedup: {speedup:.2}×");
    }

    // ── Summary table ──────────────────────────────────────────
    println!("\n═══════════════════════════════════════════════════════════════");
    println!("  Summary (page_size={page_size}B)");
    println!("───────────────────────────────────────────────────────────────");
    println!(
        "  {:<16} {:>14} {:>14} {:>10} {:>10}",
        "Backend", "Single ops/s", "Conc ops/s", "p99(1T)", "p99(32T)"
    );
    println!("───────────────────────────────────────────────────────────────");
    println!(
        "  {:<16} {:>14.0} {:>14.0} {:>8}µs {:>8}µs",
        "tokio::fs",
        r_local.ops_per_sec,
        rc_local.ops_per_sec,
        r_local.p99_ns / 1000,
        rc_local.p99_ns / 1000,
    );
    if let (Some(r), Some(rc)) = (&r_uring, &rc_uring) {
        println!(
            "  {:<16} {:>14.0} {:>14.0} {:>8}µs {:>8}µs",
            "io_uring",
            r.ops_per_sec,
            rc.ops_per_sec,
            r.p99_ns / 1000,
            rc.p99_ns / 1000,
        );
    }
    println!("───────────────────────────────────────────────────────────────");

    // ── Cleanup ────────────────────────────────────────────────
    let _ = tokio::fs::remove_dir_all(&base_dir).await;
}