diff --git a/include/raft_server.h b/include/raft_server.h index e1ec6956..87dee9ed 100644 --- a/include/raft_server.h +++ b/include/raft_server.h @@ -136,6 +136,8 @@ public: int init_db(); + void reset_db(); + rocksdb::DB *get_db() const; static constexpr const char* REPLICATION_MSG = "raft_replication"; diff --git a/src/raft_server.cpp b/src/raft_server.cpp index e6678b87..e1420fcd 100644 --- a/src/raft_server.cpp +++ b/src/raft_server.cpp @@ -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 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(); diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp index 8a38b6e3..750be208 100644 --- a/src/typesense_server_utils.cpp +++ b/src/typesense_server_utils.cpp @@ -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; }