Skip to content

Commit

Permalink
fix: try to repair a corrupted rocksdb automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed Apr 9, 2019
1 parent 32be9aa commit 24831f1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions db/Cargo.toml
Expand Up @@ -14,6 +14,7 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
failure = "0.1.5"
log = "0.4"

[dev-dependencies]
tempfile = "3.0"
17 changes: 16 additions & 1 deletion db/src/diskdb.rs
@@ -1,6 +1,7 @@
use crate::batch::{Batch, Col, Operation};
use crate::config::DBConfig;
use crate::kvdb::{ErrorKind, KeyValueDB, Result};
use log::warn;
use rocksdb::{ColumnFamily, Options, WriteBatch, DB};
use std::ops::Range;

Expand All @@ -21,7 +22,21 @@ impl RocksDB {

let cfnames: Vec<_> = (0..columns).map(|c| format!("c{}", c)).collect();
let cf_options: Vec<&str> = cfnames.iter().map(|n| n as &str).collect();
let db = DB::open_cf(&opts, &config.path, &cf_options).expect("Failed to open rocksdb");
let db = DB::open_cf(&opts, &config.path, &cf_options).unwrap_or_else(|err| {
if err.as_ref().starts_with("Corruption:") {
warn!("Try repairing the rocksdb since {} ...", err);
let mut repair_opts = Options::default();
repair_opts.create_if_missing(false);
repair_opts.create_missing_column_families(false);
DB::repair(repair_opts, &config.path)
.unwrap_or_else(|err| panic!("Failed to repair the rocksdb: {}", err));
warn!("Try opening the repaired rocksdb ...");
DB::open_cf(&opts, &config.path, &cf_options)
.unwrap_or_else(|err| panic!("Failed to open the repaired rocksdb: {}", err))
} else {
panic!("Failed to open rocksdb: {}", err);
}
});

if let Some(db_opt) = config.options.as_ref() {
let rocksdb_options: Vec<(&str, &str)> = db_opt
Expand Down

0 comments on commit 24831f1

Please sign in to comment.