spacejam / sled

the champagne of beta embedded databases
Apache License 2.0
7.89k stars 377 forks source link

Concurrent write to the db crashes #1505

Open akiradeveloper opened 1 month ago

akiradeveloper commented 1 month ago

I run this program with n_lanes=16 and datalen=10000 and it crashed. I am benchmarking sled in my project.

use clap::Parser;
use std::time::Duration;

#[derive(Parser)]
struct Opts {
    n_lanes: usize,
    datalen: usize,
    #[clap(long, default_value = "5")]
    du: u16,
}
fn main() {
    let opts = Opts::parse();
    let (collector, q) = stats::Collector::new();

    std::fs::remove_dir_all("db").ok();
    std::fs::create_dir("db").unwrap();
    let db: sled::Db = sled::Config::new()
        .path("db")
        .flush_every_ms(Some(100))
        .open()
        .unwrap();

    for lane_id in 0..opts.n_lanes {
        let tree = db.open_tree(format!("id={lane_id}")).unwrap();
        let mut reporter = stats::Reporter::new(q.clone());
        std::thread::spawn(move || {
            for i in 0.. {
                let k = i.to_string();
                let v = stats::randbytes(opts.datalen);

                reporter.start();
                tree.insert(&k, v).unwrap();
                reporter.stop(opts.datalen);
            }
        });
    }

    let du = Duration::from_secs(opts.du as u64);
    std::thread::sleep(du);
    eprintln!("{}", collector.show(du));
}
  1. expected result: I should complete
  2. actual result: crashed
  3. sled version: 1.0.0-alpha.121
  4. rustc version: 1.78
  5. operating system: Ubuntu 22.04
  6. minimal code sample that helps to reproduce the issue: shown above
  7. logs, panic messages, stack traces: https://github.com/akiradeveloper/kvs-perf-evaluation/actions/runs/9336280337/job/25696639010