Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
kvdb-rocksdb: better corruption detection on open
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilva committed Jan 19, 2018
1 parent 6c1acee commit ea5f53f
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions util/kvdb-rocksdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,18 @@ pub struct Database {
fn mark_corruption<T, P: AsRef<Path>>(path: P, res: result::Result<T, String>) -> result::Result<T, String> {
if let Err(ref s) = res {
if s.starts_with("Corruption:") {
warn!("DB corruption detected: {}. Repair will be triggered on next restart", s);
warn!("DB corrupted: {}. Repair will be triggered on next restart", s);
let _ = fs::File::create(path.as_ref().join("CORRUPTED"));
}
}

res
}

fn is_corrupted(s: &str) -> bool {
s.starts_with("Corruption:") || s.starts_with("Invalid argument: You have to open all column families.")
}

impl Database {
/// Open database with default settings.
pub fn open_default(path: &str) -> Result<Database> {
Expand Down Expand Up @@ -302,7 +306,7 @@ impl Database {
// attempt database repair if it has been previously marked as corrupted
let db_corrupted = Path::new(path).join("CORRUPTED");
if db_corrupted.exists() {
warn!("DB {} has been previously marked as corrupted, attempting repair.", path);
warn!("DB has been previously marked as corrupted, attempting repair");
DB::repair(&opts, path)?;
fs::remove_file(db_corrupted)?;
}
Expand All @@ -326,12 +330,11 @@ impl Database {

let mut cfs: Vec<Column> = Vec::new();
let db = match config.columns {
Some(columns) => {
Some(_) => {
match DB::open_cf(&opts, path, &cfnames, &cf_options) {
Ok(db) => {
cfs = cfnames.iter().map(|n| db.cf_handle(n)
.expect("rocksdb opens a cf_handle for each cfname; qed")).collect();
assert!(cfs.len() == columns as usize);
Ok(db)
}
Err(_) => {
Expand All @@ -341,7 +344,7 @@ impl Database {
cfs = cfnames.iter().enumerate().map(|(i, n)| db.create_cf(n, &cf_options[i])).collect::<::std::result::Result<_, _>>()?;
Ok(db)
},
err @ Err(_) => err,
err => err,
}
}
}
Expand All @@ -351,13 +354,17 @@ impl Database {

let db = match db {
Ok(db) => db,
Err(ref s) if s.starts_with("Corruption:") => {
info!("{}", s);
info!("Attempting DB repair for {}", path);
Err(ref s) if is_corrupted(s) => {
warn!("DB corrupted: {}, attempting repair", s);
DB::repair(&opts, path)?;

match cfnames.is_empty() {
true => DB::open(&opts, path)?,
true => {
let db = DB::open(&opts, path)?;
cfs = cfnames.iter().map(|n| db.cf_handle(n)
.expect("rocksdb opens a cf_handle for each cfname; qed")).collect();
db
},
false => DB::open_cf(&opts, path, &cfnames, &cf_options)?
}
},
Expand Down

0 comments on commit ea5f53f

Please sign in to comment.