From 6bf07a0ef704e65bc4954adbcb82556d13ea9c7f Mon Sep 17 00:00:00 2001 From: dingxiaoshuai123 <2486016589@qq.com> Date: Mon, 22 Apr 2024 10:00:51 +0800 Subject: [PATCH] handle comments --- pikiwidb.conf | 4 ++-- src/db.cc | 6 ++---- src/praft/praft.cc | 4 ++-- src/pstd/pstd_string.cc | 2 +- src/pstd/pstd_string.h | 2 +- src/storage/include/storage/storage.h | 1 - src/store.cc | 11 +++-------- src/store.h | 15 +-------------- 8 files changed, 12 insertions(+), 33 deletions(-) diff --git a/pikiwidb.conf b/pikiwidb.conf index 55fe6d3d2..b375faeb0 100644 --- a/pikiwidb.conf +++ b/pikiwidb.conf @@ -38,7 +38,7 @@ logfile stdout # Set the number of databases. The default database is DB 0, you can select # a different one on a per-connection basis using SELECT where # dbid is a number between 0 and 'databases'-1 -databases 3 +databases 1 ################################ SNAPSHOTTING ################################# # @@ -347,7 +347,7 @@ backendpath dump # the frequency of dump to backend per second backendhz 10 # the rocksdb number per db -db-instance-num 5 +db-instance-num 3 # default 86400 * 7 rocksdb-ttl-second 604800 # default 86400 * 3 diff --git a/src/db.cc b/src/db.cc index 924055847..0129a925f 100644 --- a/src/db.cc +++ b/src/db.cc @@ -41,7 +41,7 @@ DB::DB(int db_index, const std::string& db_path, int rocksdb_inst_num) } void DB::DoCheckpoint(const std::string& path, int i) { - // 1) always hold storage's sharedLock + // 1) always hold the storage's shared lock std::shared_lock sharedLock(storage_mutex_); // 2)Create the checkpoint of rocksdb i. @@ -49,7 +49,7 @@ void DB::DoCheckpoint(const std::string& path, int i) { } void DB::LoadCheckpoint(const std::string& path, const std::string& db_path, int i) { - // 1) Already holding the mutual exclusion lock + // 1) Already holding the storage's exclusion lock // 2) Load the checkpoint of rocksdb i. auto status = storage_->LoadCheckpoint(path, db_path, i); @@ -97,8 +97,6 @@ void DB::LoadDBFromCheckPoint(const std::string& path, bool sync) { result.reserve(rocksdb_inst_num_); for (int i = 0; i < rocksdb_inst_num_; ++i) { // In a new thread, Load a checkpoint for the specified rocksdb i - // In DB::DoBgSave, a read lock is always held to protect the Storage - // corresponding to this rocksdb i. auto res = std::async(std::launch::async, &DB::LoadCheckpoint, this, checkpoint_path, db_path_, i); result.push_back(std::move(res)); } diff --git a/src/praft/praft.cc b/src/praft/praft.cc index 124a0bf8e..4db70e580 100644 --- a/src/praft/praft.cc +++ b/src/praft/praft.cc @@ -645,7 +645,7 @@ void PRaft::on_snapshot_save(braft::SnapshotWriter* writer, braft::Closure* done auto path = writer->get_path(); INFO("Saving snapshot to {}", path); TasksVector tasks(1, {TaskType::kCheckpoint, db_id_, {{TaskArg::kCheckpointPath, path}}, true}); - PSTORE.DoSomeThingSpecificDB(tasks); + PSTORE.HandleTaskSpecificDB(tasks); if (auto res = AddAllFiles(path, writer, path); res != 0) { done->status().set_error(EIO, "Fail to add file to writer"); } @@ -657,7 +657,7 @@ int PRaft::on_snapshot_load(braft::SnapshotReader* reader) { auto reader_path = reader->get_path(); // xx/snapshot_0000001 auto path = g_config.dbpath + std::to_string(db_id_); // db/db_id TasksVector tasks(1, {TaskType::kLoadDBFromCheckPoint, db_id_, {{TaskArg::kCheckpointPath, reader_path}}, true}); - PSTORE.DoSomeThingSpecificDB(tasks); + PSTORE.HandleTaskSpecificDB(tasks); return 0; } diff --git a/src/pstd/pstd_string.cc b/src/pstd/pstd_string.cc index deb9c431a..973656a2b 100755 --- a/src/pstd/pstd_string.cc +++ b/src/pstd/pstd_string.cc @@ -619,7 +619,7 @@ bool StringHasSpaces(const std::string& str) { return std::count_if(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c); }); } -void trimSlash(std::string& dirName) { +void TrimSlash(std::string& dirName) { while (dirName.back() == '/') { dirName.pop_back(); } diff --git a/src/pstd/pstd_string.h b/src/pstd/pstd_string.h index 8960e3242..d6ffd828a 100755 --- a/src/pstd/pstd_string.h +++ b/src/pstd/pstd_string.h @@ -93,6 +93,6 @@ std::string RandomStringWithNumber(size_t len); bool StringHasSpaces(const std::string& str); -void trimSlash(std::string& dirName); +void TrimSlash(std::string& dirName); } // namespace pstd diff --git a/src/storage/include/storage/storage.h b/src/storage/include/storage/storage.h index 3fc174ac8..aad21e337 100644 --- a/src/storage/include/storage/storage.h +++ b/src/storage/include/storage/storage.h @@ -1110,7 +1110,6 @@ class Storage { // Storage start the background thread for compaction task pthread_t bg_tasks_thread_id_ = 0; - std::thread t; pstd::Mutex bg_tasks_mutex_; pstd::CondVar bg_tasks_cond_var_; std::queue bg_tasks_queue_; diff --git a/src/store.cc b/src/store.cc index 1320c0e77..4ef54d93f 100644 --- a/src/store.cc +++ b/src/store.cc @@ -36,12 +36,7 @@ void PStore::Init() { } } -void PStore::Clear() { - std::lock_guard lock(dbs_mutex_); - backends_.clear(); -} - -void PStore::DoSomeThingSpecificDB(const TasksVector& tasks) { +void PStore::HandleTaskSpecificDB(const TasksVector& tasks) { std::for_each(tasks.begin(), tasks.end(), [this](const auto& task) { if (task.db < 0 || task.db >= dbNum_) { WARN("The database index is out of range."); @@ -55,7 +50,7 @@ void PStore::DoSomeThingSpecificDB(const TasksVector& tasks) { return; } auto path = task.args.find(kCheckpointPath)->second; - pstd::trimSlash(path); + pstd::TrimSlash(path); db->CreateCheckpoint(path, task.sync); break; } @@ -65,7 +60,7 @@ void PStore::DoSomeThingSpecificDB(const TasksVector& tasks) { return; } auto path = task.args.find(kCheckpointPath)->second; - pstd::trimSlash(path); + pstd::TrimSlash(path); db->LoadDBFromCheckPoint(path, task.sync); break; } diff --git a/src/store.h b/src/store.h index 76a8442ff..e8192f7e3 100644 --- a/src/store.h +++ b/src/store.h @@ -38,7 +38,6 @@ using TasksVector = std::vector; class PStore { public: - friend class CheckpointManager; static PStore& Instance(); PStore(const PStore&) = delete; @@ -46,29 +45,17 @@ class PStore { void Init(); - void Clear(); - std::unique_ptr& GetBackend(int32_t index) { return backends_[index]; }; - void DoSomeThingSpecificDB(const TasksVector& task); - - void WaitForCheckpointDone(); + void HandleTaskSpecificDB(const TasksVector& task); int GetDBNumber() const { return dbNum_; } - std::shared_mutex& SharedMutex() { return dbs_mutex_; } - private: PStore() = default; int dbNum_ = 0; - /** - * If you want to access all the DBs at the same time, - * then you must hold the lock. - * For example: you want to execute flushall or bgsave. - */ - std::shared_mutex dbs_mutex_; std::vector> backends_; };