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

Commit

Permalink
kvdb-rocksdb: mark corruptions and attempt repair on db open
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilva committed Jan 19, 2018
1 parent 9768531 commit 6c1acee
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions util/kvdb-rocksdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::cmp;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::path::{PathBuf, Path};
use std::{mem, fs, io};
use std::{fs, io, mem, result};

use parking_lot::{Mutex, MutexGuard, RwLock};
use rocksdb::{
Expand Down Expand Up @@ -257,6 +257,18 @@ pub struct Database {
flushing_lock: Mutex<bool>,
}

#[inline]
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);
let _ = fs::File::create(path.as_ref().join("CORRUPTED"));
}
}

res
}

impl Database {
/// Open database with default settings.
pub fn open_default(path: &str) -> Result<Database> {
Expand Down Expand Up @@ -287,6 +299,14 @@ impl Database {
block_opts.set_cache(cache);
}

// 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);
DB::repair(&opts, path)?;
fs::remove_file(db_corrupted)?;
}

let columns = config.columns.unwrap_or(0) as usize;

let mut cf_options = Vec::with_capacity(columns);
Expand Down Expand Up @@ -425,7 +445,11 @@ impl Database {
}
}
}
db.write_opt(batch, &self.write_opts)?;

mark_corruption(
&self.path,
db.write_opt(batch, &self.write_opts))?;

for column in self.flushing.write().iter_mut() {
column.clear();
column.shrink_to_fit();
Expand Down Expand Up @@ -471,7 +495,10 @@ impl Database {
},
}
}
db.write_opt(batch, &self.write_opts).map_err(Into::into)

mark_corruption(
&self.path,
db.write_opt(batch, &self.write_opts)).map_err(Into::into)
},
None => Err("Database is closed".into())
}
Expand Down

0 comments on commit 6c1acee

Please sign in to comment.