Delete DB cleanly before restore.

This commit is contained in:
kishorenc 2020-03-15 18:52:26 +05:30
parent b7f9a2ca28
commit 637ba52769
3 changed files with 14 additions and 9 deletions

View File

@ -136,6 +136,8 @@ public:
int init_db();
void reset_db();
rocksdb::DB *get_db() const;
static constexpr const char* REPLICATION_MSG = "raft_replication";

View File

@ -56,6 +56,7 @@ int ReplicationState::start(int port, int election_timeout_ms, int snapshot_inte
LOG(INFO) << "Snapshot does not exist. We will remove db dir and init db fresh.";
reset_db();
if (!butil::DeleteFile(butil::FilePath(db_path), true)) {
LOG(WARNING) << "rm " << db_path << " failed";
return -1;
@ -226,11 +227,6 @@ void ReplicationState::on_snapshot_save(braft::SnapshotWriter* writer, braft::Cl
}
int ReplicationState::init_db() {
if (db != nullptr) {
delete db;
db = nullptr;
}
if (!butil::CreateDirectory(butil::FilePath(db_path))) {
LOG(WARNING) << "CreateDirectory " << db_path << " failed";
return -1;
@ -270,9 +266,7 @@ int ReplicationState::on_snapshot_load(braft::SnapshotReader* reader) {
// Load snapshot from reader, replacing the running StateMachine
std::string snapshot_path = reader->get_path();
snapshot_path.append(std::string("/") + db_snapshot_name);
reset_db();
if (!butil::DeleteFile(butil::FilePath(db_path), true)) {
LOG(WARNING) << "rm " << db_path << " failed";
return -1;
@ -280,6 +274,9 @@ int ReplicationState::on_snapshot_load(braft::SnapshotReader* reader) {
LOG(TRACE) << "rm " << db_path << " success";
std::string snapshot_path = reader->get_path();
snapshot_path.append(std::string("/") + db_snapshot_name);
// tries to use link if possible, or else copies
if (!copy_dir(snapshot_path, db_path)) {
LOG(WARNING) << "copy snapshot " << snapshot_path << " to " << db_path << " failed";
@ -316,12 +313,18 @@ ReplicationState::ReplicationState(Store *store, http_message_dispatcher *messag
db_options = store->get_db_options();
}
void ReplicationState::reset_db() {
delete db;
db = nullptr;
}
void InitSnapshotClosure::Run() {
// Auto delete this after Run()
std::unique_ptr<InitSnapshotClosure> self_guard(this);
if(status().ok()) {
LOG(INFO) << "Init snapshot succeeded!";
replication_state->reset_db();
replication_state->init_db();
} else {
LOG(ERROR) << "Init snapshot failed, error: " << status().error_str();

View File

@ -176,7 +176,7 @@ int start_raft_server(ReplicationState& replication_state, const std::string& st
StringUtils::split(peer_ips_string, peer_ips, ",");
std::string peers = StringUtils::join(peer_ips, ":0,");
if (replication_state.start(raft_port, 1000, 60, state_dir, peers) != 0) {
if (replication_state.start(raft_port, 1000, 600, state_dir, peers) != 0) {
LOG(ERR) << "Failed to start raft state";
return -1;
}