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

[dev] agate: fix lock order in compaction #73

Merged
merged 2 commits into from
Dec 2, 2020
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
23 changes: 14 additions & 9 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::sync::{Arc, RwLock};
use yatp::task::callback::Handle;

const KV_WRITE_CH_CAPACITY: usize = 1000;

Expand Down Expand Up @@ -85,16 +84,22 @@ impl Agate {
let agate = Self {
core,
closer: closer.clone(),
pool: yatp::Builder::new("agatedb").build_callback_pool(),
pool: yatp::Builder::new("compaction").build_callback_pool(),
};

std::thread::spawn(move || flush_core.flush_memtable().unwrap());

std::thread::spawn(move || {
writer_core
.do_writes(writer_core.closers.writes.clone())
.unwrap()
});
std::thread::Builder::new()
.name("memtable_flush".to_string())
.spawn(move || flush_core.flush_memtable().unwrap())
.unwrap();

std::thread::Builder::new()
.name("write_request".to_string())
.spawn(move || {
writer_core
.do_writes(writer_core.closers.writes.clone())
.unwrap()
})
.unwrap();

agate
.core
Expand Down
10 changes: 5 additions & 5 deletions src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,12 @@ impl Core {
/ self.opts.num_level_zero_tables as f64,
);

let cpt_status = self.cpt_status.read().unwrap();

// println!("{:?}", targets);

for i in 1..self.levels.len() {
let del_size = cpt_status.levels[i].del_size;
// We must ensure lock order, by only obtain one lock at a time.
let del_size = {
let cpt_status = self.cpt_status.read().unwrap();
cpt_status.levels[i].del_size
};
let level = self.levels[i].read().unwrap();
let size = level.total_size - del_size;
add_priority(i, size as f64 / targets.target_size[i] as f64);
Expand Down
7 changes: 3 additions & 4 deletions src/levels/compaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,11 @@ impl CompactDef {
impl CompactStatus {
pub fn delete(&mut self, compact_def: &CompactDef) {
// TODO: level is immutable, we could access it without read
let tl = compact_def.this_level.read().unwrap().level;
assert!(tl < self.levels.len() - 1);
let this_level_id = compact_def.this_level_id;
assert!(this_level_id < self.levels.len() - 1);

let this_level_id = compact_def.this_level.read().unwrap().level;
let this_level = &mut self.levels[this_level_id];
let next_level_id = compact_def.next_level.read().unwrap().level;
let next_level_id = compact_def.next_level_id;

this_level.del_size -= compact_def.this_size;
let mut found = this_level.remove(&compact_def.this_range);
Expand Down