From dba76636245956b7c59261c40a6d3e25e45e0c31 Mon Sep 17 00:00:00 2001 From: wxnzb <3127484862@qq.com> Date: Thu, 2 Jan 2025 14:02:34 +0800 Subject: [PATCH 01/35] Make-p_config-a-singleton --- src/base_cmd.cc | 2 +- src/client.cc | 4 ++-- src/cmd_admin.cc | 30 +++++++++++++++--------------- src/cmd_hash.cc | 2 +- src/cmd_kv.cc | 2 +- src/cmd_raft.cc | 2 +- src/config.cc | 2 +- src/config.h | 9 +++++++-- src/db.cc | 29 ++++++++++++++--------------- src/kiwi.cc | 36 ++++++++++++++++++------------------ src/praft/praft.cc | 22 +++++++++++----------- src/replication.cc | 8 ++++---- src/store.cc | 2 +- 13 files changed, 77 insertions(+), 73 deletions(-) diff --git a/src/base_cmd.cc b/src/base_cmd.cc index 918ba365..3f7439f9 100644 --- a/src/base_cmd.cc +++ b/src/base_cmd.cc @@ -42,7 +42,7 @@ void BaseCmd::Execute(PClient* client) { DEBUG("execute command: {}", client->CmdName()); // read consistency (lease read) / write redirection - if (g_config.use_raft && (HasFlag(kCmdFlagsReadonly) || HasFlag(kCmdFlagsWrite))) { + if (kiwi::PConfig::GetInstance().use_raft && (HasFlag(kCmdFlagsReadonly) || HasFlag(kCmdFlagsWrite))) { if (!PRAFT.IsInitialized()) { return client->SetRes(CmdRes::kErrOther, "PRAFT is not initialized"); } diff --git a/src/client.cc b/src/client.cc index dae5c368..f9e8d282 100644 --- a/src/client.cc +++ b/src/client.cc @@ -239,7 +239,7 @@ void PClient::OnConnect() { SetName("MasterConnection"); SetFlag(kClientFlagMaster); - if (g_config.master_auth.empty()) { + if (kiwi::PConfig::GetInstance().master_auth.empty()) { SetAuth(); } @@ -247,7 +247,7 @@ void PClient::OnConnect() { PRAFT.SendNodeRequest(this); } } else { - if (g_config.password.empty()) { + if (kiwi::PConfig::GetInstance().password.empty()) { SetAuth(); } } diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 48991fe8..65556516 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -57,7 +57,7 @@ bool CmdConfigGet::DoInitial(PClient* client) { return true; } void CmdConfigGet::DoCmd(PClient* client) { std::vector results; for (int i = 0; i < client->argv_.size() - 2; i++) { - g_config.Get(client->argv_[i + 2], &results); + kiwi::PConfig::GetInstance().Get(client->argv_[i + 2], &results); } client->AppendStringVector(results); } @@ -68,7 +68,7 @@ CmdConfigSet::CmdConfigSet(const std::string& name, int16_t arity) bool CmdConfigSet::DoInitial(PClient* client) { return true; } void CmdConfigSet::DoCmd(PClient* client) { - auto s = g_config.Set(client->argv_[2], client->argv_[3]); + auto s = kiwi::PConfig::GetInstance().Set(client->argv_[2], client->argv_[3]); if (!s.ok()) { client->SetRes(CmdRes::kInvalidParameter); } else { @@ -87,7 +87,7 @@ void FlushdbCmd::DoCmd(PClient* client) { PSTORE.GetBackend(currentDBIndex).get()->Lock(); DEFER { PSTORE.GetBackend(currentDBIndex).get()->UnLock(); }; - std::string db_path = g_config.db_path + std::to_string(currentDBIndex); + std::string db_path = kiwi::PConfig::GetInstance().db_path + std::to_string(currentDBIndex); std::string path_temp = db_path; path_temp.append("_deleting/"); pstd::RenameFile(db_path, path_temp); @@ -108,9 +108,9 @@ FlushallCmd::FlushallCmd(const std::string& name, int16_t arity) bool FlushallCmd::DoInitial(PClient* client) { return true; } void FlushallCmd::DoCmd(PClient* client) { - for (size_t i = 0; i < g_config.databases; ++i) { + for (size_t i = 0; i < kiwi::PConfig::GetInstance().databases; ++i) { PSTORE.GetBackend(i).get()->Lock(); - std::string db_path = g_config.db_path + std::to_string(i); + std::string db_path = kiwi::PConfig::GetInstance().db_path + std::to_string(i); std::string path_temp = db_path; path_temp.append("_deleting/"); pstd::RenameFile(db_path, path_temp); @@ -134,12 +134,12 @@ void AuthCmd::DoCmd(PClient* client) { return; } - if (g_config.password == "") { + if (kiwi::PConfig::GetInstance().password == "") { client->SetRes(CmdRes::kErrOther, "Client sent AUTH, but no password is set"); } std::string password = client->argv_[1]; - if (password != g_config.password) { + if (password != kiwi::PConfig::GetInstance().password) { client->SetRes(CmdRes::kInvalidPwd); } else { client->SetAuth(); @@ -154,7 +154,7 @@ bool SelectCmd::DoInitial(PClient* client) { return true; } void SelectCmd::DoCmd(PClient* client) { int index = atoi(client->argv_[1].c_str()); - if (index < 0 || index >= g_config.databases) { + if (index < 0 || index >= kiwi::PConfig::GetInstance().databases) { client->SetRes(CmdRes::kInvalidIndex, kCmdNameSelect + " DB index is out of range"); return; } @@ -168,7 +168,7 @@ ShutdownCmd::ShutdownCmd(const std::string& name, int16_t arity) bool ShutdownCmd::DoInitial(PClient* client) { // For now, only shutdown need check local if (client->PeerIP().find("127.0.0.1") == std::string::npos && - client->PeerIP().find(g_config.ip) == std::string::npos) { + client->PeerIP().find(kiwi::PConfig::GetInstance().ip) == std::string::npos) { client->SetRes(CmdRes::kErrOther, kCmdNameShutdown + " should be localhost"); return false; } @@ -233,7 +233,7 @@ void HelloCmd::DoCmd(PClient* client) { if (client->GetAuth()) { continue; } - if (client->argv_[next_arg + 1] != g_config.password) { + if (client->argv_[next_arg + 1] != kiwi::PConfig::GetInstance().password) { client->SetRes(CmdRes::kErrOther, "invalid password"); return; } else { @@ -269,7 +269,7 @@ void HelloCmd::Hello(PClient* client) { client->AppendInteger(static_cast(client->GetUniqueID())); client->AppendString("mode"); - if (!g_config.use_raft) { + if (!kiwi::PConfig::GetInstance().use_raft) { client->AppendString("standalone"); } else { client->AppendString("cluster"); @@ -453,8 +453,8 @@ void InfoCmd::InfoServer(std::string& info) { tmp_stream << "os:" << host_info.sysname << " " << host_info.release << " " << host_info.machine << "\r\n"; tmp_stream << "arch_bits:" << (reinterpret_cast(&host_info.machine) + strlen(host_info.machine) - 2) << "\r\n"; tmp_stream << "process_id:" << getpid() << "\r\n"; - tmp_stream << "run_id:" << static_cast(g_config.run_id) << "\r\n"; - tmp_stream << "tcp_port:" << g_config.port << "\r\n"; + tmp_stream << "run_id:" << static_cast(kiwi::PConfig::GetInstance().run_id) << "\r\n"; + tmp_stream << "tcp_port:" << kiwi::PConfig::GetInstance().port << "\r\n"; tmp_stream << "uptime_in_seconds:" << (current_time_s - g_kiwi->Start_time_s()) << "\r\n"; tmp_stream << "uptime_in_days:" << (current_time_s / (24 * 3600) - g_kiwi->Start_time_s() / (24 * 3600) + 1) << "\r\n"; @@ -497,8 +497,8 @@ void InfoCmd::InfoCPU(std::string& info) { } void InfoCmd::InfoData(std::string& message) { - message += DATABASES_NUM + std::string(":") + std::to_string(kiwi::g_config.databases) + "\r\n"; - message += ROCKSDB_NUM + std::string(":") + std::to_string(kiwi::g_config.db_instance_num) + "\r\n"; + message += DATABASES_NUM + std::string(":") + std::to_string(kiwi::PConfig::GetInstance().databases) + "\r\n"; + message += ROCKSDB_NUM + std::string(":") + std::to_string(kiwi::PConfig::GetInstance().db_instance_num) + "\r\n"; message += ROCKSDB_VERSION + std::string(":") + ROCKSDB_NAMESPACE::GetRocksVersionAsString() + "\r\n"; } diff --git a/src/cmd_hash.cc b/src/cmd_hash.cc index 512a2a42..9e760e37 100644 --- a/src/cmd_hash.cc +++ b/src/cmd_hash.cc @@ -172,7 +172,7 @@ void HGetAllCmd::DoCmd(PClient* client) { int64_t total_fv = 0; int64_t cursor = 0; int64_t next_cursor = 0; - size_t raw_limit = g_config.max_client_response_size; + size_t raw_limit = kiwi::PConfig::GetInstance().max_client_response_size; std::string raw; std::vector fvs; storage::Status s; diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 025e7ca9..993a3eac 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -680,7 +680,7 @@ void SetRangeCmd::DoCmd(PClient* client) { return; } // Ref: https://redis.io/docs/latest/commands/setrange/ - if (g_config.redis_compatible_mode && std::atoi(client->argv_[2].c_str()) > 536870911) { + if (kiwi::PConfig::GetInstance().redis_compatible_mode && std::atoi(client->argv_[2].c_str()) > 536870911) { client->SetRes(CmdRes::kErrOther, "When Redis compatibility mode is enabled, the offset parameter must not exceed 536870911"); return; diff --git a/src/cmd_raft.cc b/src/cmd_raft.cc index 58a6026f..5880eaec 100644 --- a/src/cmd_raft.cc +++ b/src/cmd_raft.cc @@ -103,7 +103,7 @@ void RaftNodeCmd::DoCmdRemove(PClient* client) { // Connect target std::string peer_ip = butil::ip2str(leader_peer_id.addr.ip).c_str(); - auto port = leader_peer_id.addr.port - kiwi::g_config.raft_port_offset; + auto port = leader_peer_id.addr.port - kiwi::PConfig::GetInstance().raft_port_offset; auto peer_id = client->argv_[2]; auto ret = PRAFT.GetClusterCmdCtx().Set(ClusterCmdType::kRemove, client, std::move(peer_ip), port, std::move(peer_id)); diff --git a/src/config.cc b/src/config.cc index 9d8891f9..dda6e4a4 100644 --- a/src/config.cc +++ b/src/config.cc @@ -23,7 +23,7 @@ constexpr int DBNUMBER_MAX = 16; constexpr int THREAD_MAX = 129; constexpr int ROCKSDB_INSTANCE_NUMBER_MAX = 10; -PConfig g_config; +PConfig &g_config=kiwi::PConfig::GetInstance(); // preprocess func static void EraseQuotes(std::string& str) { diff --git a/src/config.h b/src/config.h index dad56b87..4bb66e24 100644 --- a/src/config.h +++ b/src/config.h @@ -31,7 +31,6 @@ using Status = rocksdb::Status; using CheckFunc = std::function; class PConfig; -extern PConfig g_config; class BaseValue { public: @@ -156,7 +155,12 @@ class PConfig { * PConfig() * Initialize kiwi's config & RocksDB's config. */ - PConfig(); + static PConfig& GetInstance(){ + static PConfig instance; + return instance; + } + PConfig(const PConfig&)=delete; + PConfig& operator=(const PConfig&)=delete; /*------------------------ * ~PConfig() @@ -492,5 +496,6 @@ class PConfig { // The file name of the config std::string config_file_name_; + PConfig(); }; } // namespace kiwi diff --git a/src/db.cc b/src/db.cc index 1820c4e4..9a45fea9 100644 --- a/src/db.cc +++ b/src/db.cc @@ -15,7 +15,6 @@ #include "praft/praft.h" #include "pstd/log.h" -extern kiwi::PConfig g_config; namespace kiwi { @@ -26,16 +25,16 @@ DB::~DB() { INFO("DB{} is closing...", db_index_); } rocksdb::Status DB::Open() { storage::StorageOptions storage_options; - storage_options.options = g_config.GetRocksDBOptions(); - storage_options.table_options = g_config.GetRocksDBBlockBasedTableOptions(); + storage_options.options = kiwi::PConfig::GetInstance().GetRocksDBOptions(); + storage_options.table_options = kiwi::PConfig::GetInstance().GetRocksDBBlockBasedTableOptions(); - storage_options.options.ttl = g_config.rocksdb_ttl_second; - storage_options.options.periodic_compaction_seconds = g_config.rocksdb_periodic_second; + storage_options.options.ttl = kiwi::PConfig::GetInstance().rocksdb_ttl_second; + storage_options.options.periodic_compaction_seconds = kiwi::PConfig::GetInstance().rocksdb_periodic_second; - storage_options.small_compaction_threshold = g_config.small_compaction_threshold; - storage_options.small_compaction_duration_threshold = g_config.small_compaction_duration_threshold; + storage_options.small_compaction_threshold = kiwi::PConfig::GetInstance().small_compaction_threshold; + storage_options.small_compaction_duration_threshold = kiwi::PConfig::GetInstance().small_compaction_duration_threshold; - if (g_config.use_raft) { + if (kiwi::PConfig::GetInstance().use_raft) { storage_options.append_log_function = [&r = PRAFT](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; @@ -45,7 +44,7 @@ rocksdb::Status DB::Open() { }; } - storage_options.db_instance_num = g_config.db_instance_num; + storage_options.db_instance_num = kiwi::PConfig::GetInstance().db_instance_num; storage_options.db_id = db_index_; std::unique_ptr old_storage = std::move(storage_); @@ -110,14 +109,14 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma } storage::StorageOptions storage_options; - storage_options.options = g_config.GetRocksDBOptions(); - storage_options.db_instance_num = g_config.db_instance_num; + storage_options.options = kiwi::PConfig::GetInstance().GetRocksDBOptions(); + storage_options.db_instance_num = kiwi::PConfig::GetInstance().db_instance_num; storage_options.db_id = db_index_; // options for CF - storage_options.options.ttl = g_config.rocksdb_ttl_second; - storage_options.options.periodic_compaction_seconds = g_config.rocksdb_periodic_second; - if (g_config.use_raft) { + storage_options.options.ttl = kiwi::PConfig::GetInstance().rocksdb_ttl_second; + storage_options.options.periodic_compaction_seconds = kiwi::PConfig::GetInstance().rocksdb_periodic_second; + if (kiwi::PConfig::GetInstance().use_raft) { storage_options.append_log_function = [&r = PRAFT](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; @@ -131,7 +130,7 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma } // in single-mode, kiwi will enable wal - if (!g_config.use_raft) { + if (!kiwi::PConfig::GetInstance().use_raft) { storage_->DisableWal(false); } diff --git a/src/kiwi.cc b/src/kiwi.cc index e324a7c4..ddaab8a7 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -163,46 +163,46 @@ void KiwiDB::OnNewConnection(uint64_t connId, std::shared_ptr& cl bool KiwiDB::Init() { char runid[kRunidSize + 1] = ""; getRandomHexChars(runid, kRunidSize); - g_config.Set("runid", {runid, kRunidSize}, true); + kiwi::PConfig::GetInstance().Set("runid", {runid, kRunidSize}, true); if (port_ != 0) { - g_config.Set("port", std::to_string(port_), true); + kiwi::PConfig::GetInstance().Set("port", std::to_string(port_), true); } if (!options_.GetLogLevel().empty()) { - g_config.Set("log-level", options_.GetLogLevel(), true); + kiwi::PConfig::GetInstance().Set("log-level", options_.GetLogLevel(), true); } if (options_.GetRedisCompatibleMode()) { - g_config.Set("redis_compatible_mode", std::to_string(options_.GetRedisCompatibleMode()), true); + kiwi::PConfig::GetInstance().Set("redis_compatible_mode", std::to_string(options_.GetRedisCompatibleMode()), true); } - auto num = g_config.worker_threads_num + g_config.slave_threads_num; + auto num = kiwi::PConfig::GetInstance().worker_threads_num + kiwi::PConfig::GetInstance().slave_threads_num; options_.SetThreadNum(num); // now we only use fast cmd thread pool - auto status = cmd_threads_.Init(g_config.fast_cmd_threads_num, 1, "kiwi-cmd"); + auto status = cmd_threads_.Init(kiwi::PConfig::GetInstance().fast_cmd_threads_num, 1, "kiwi-cmd"); if (!status.ok()) { ERROR("init cmd thread pool failed: {}", status.ToString()); return false; } - PSTORE.Init(g_config.databases); + PSTORE.Init(kiwi::PConfig::GetInstance().databases); - PSlowLog::Instance().SetThreshold(g_config.slow_log_time); - PSlowLog::Instance().SetLogLimit(static_cast(g_config.slow_log_max_len)); + PSlowLog::Instance().SetThreshold(kiwi::PConfig::GetInstance().slow_log_time); + PSlowLog::Instance().SetLogLimit(static_cast(kiwi::PConfig::GetInstance().slow_log_max_len)); // master ip - if (!g_config.master_ip.empty()) { - PREPL.SetMasterAddr(g_config.master_ip.c_str(), g_config.master_port); + if (!kiwi::PConfig::GetInstance().master_ip.empty()) { + PREPL.SetMasterAddr(kiwi::PConfig::GetInstance().master_ip.c_str(), kiwi::PConfig::GetInstance().master_port); } options_.SetRwSeparation(true); event_server_ = std::make_unique>>(options_); - net::SocketAddr addr(g_config.ip, g_config.port); - INFO("Add listen addr:{}, port:{}", g_config.ip, g_config.port); + net::SocketAddr addr(kiwi::PConfig::GetInstance().ip, kiwi::PConfig::GetInstance().port); + INFO("Add listen addr:{}, port:{}", kiwi::PConfig::GetInstance().ip, kiwi::PConfig::GetInstance().port); event_server_->AddListenAddr(addr); event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); @@ -273,7 +273,7 @@ static void InitLogs() { static int InitLimit() { rlimit limit; - rlim_t maxfiles = g_config.max_clients; + rlim_t maxfiles = kiwi::PConfig::GetInstance().max_clients; if (getrlimit(RLIMIT_NOFILE, &limit) == -1) { WARN("getrlimit error: {}", strerror(errno)); } else if (limit.rlim_cur < maxfiles) { @@ -324,13 +324,13 @@ int main(int argc, char* argv[]) { } if (!g_kiwi->GetConfigName().empty()) { - if (!g_config.LoadFromFile(g_kiwi->GetConfigName())) { + if (!kiwi::PConfig::GetInstance().LoadFromFile(g_kiwi->GetConfigName())) { std::cerr << "Load config file [" << g_kiwi->GetConfigName() << "] failed!\n"; return -1; } } - if (g_config.daemonize) { + if (kiwi::PConfig::GetInstance().daemonize) { daemonize(); } @@ -339,7 +339,7 @@ int main(int argc, char* argv[]) { InitLogs(); InitLimit(); - if (g_config.daemonize) { + if (kiwi::PConfig::GetInstance().daemonize) { closeStd(); } @@ -347,7 +347,7 @@ int main(int argc, char* argv[]) { // output logo to console char logo[1024] = ""; snprintf(logo, sizeof logo - 1, kiwiLogo, Kkiwi_VERSION, static_cast(sizeof(void*)) * 8, - static_cast(g_config.port)); + static_cast(kiwi::PConfig::GetInstance().port)); std::cout << logo; g_kiwi->Run(); } diff --git a/src/praft/praft.cc b/src/praft/praft.cc index 3882247d..d9762c22 100644 --- a/src/praft/praft.cc +++ b/src/praft/praft.cc @@ -97,7 +97,7 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { } server_ = std::make_unique(); - auto port = g_config.port + kiwi::g_config.raft_port_offset; + auto port = kiwi::PConfig::GetInstance().port + kiwi::PConfig::GetInstance().raft_port_offset; // Add your service into RPC server DummyServiceImpl service(&PRAFT); if (server_->AddService(&service, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) { @@ -126,10 +126,10 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { assert(group_id.size() == RAFT_GROUPID_LEN); this->group_id_ = group_id; - // FIXME: g_config.ip is default to 127.0.0.0, which may not work in cluster. - raw_addr_ = g_config.ip + ":" + std::to_string(port); + // FIXME: kiwi::PConfig::GetInstance().ip is default to 127.0.0.0, which may not work in cluster. + raw_addr_ = kiwi::PConfig::GetInstance().ip + ":" + std::to_string(port); butil::ip_t ip; - auto ret = butil::str2ip(g_config.ip.c_str(), &ip); + auto ret = butil::str2ip(kiwi::PConfig::GetInstance().ip.c_str(), &ip); if (ret != 0) { server_.reset(); return ERROR_LOG_AND_STATUS("Failed to convert str_ip to butil::ip_t"); @@ -157,7 +157,7 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { node_options_.fsm = this; node_options_.node_owns_fsm = false; node_options_.snapshot_interval_s = 0; - std::string prefix = "local://" + g_config.db_path + std::to_string(db_id_) + "/_praft"; + std::string prefix = "local://" + kiwi::PConfig::GetInstance().db_path + std::to_string(db_id_) + "/_praft"; node_options_.log_uri = prefix + "/log"; node_options_.raft_meta_uri = prefix + "/raft_meta"; node_options_.snapshot_uri = prefix + "/snapshot"; @@ -221,7 +221,7 @@ std::string PRaft::GetLeaderAddress() const { return std::string(); } - id.addr.port -= g_config.raft_port_offset; + id.addr.port -= kiwi::PConfig::GetInstance().raft_port_offset; auto addr = butil::endpoint2str(id.addr); return addr.c_str(); } @@ -323,8 +323,8 @@ void PRaft::SendNodeAddRequest(PClient* client) { // Node id in braft are ip:port, the node id param in RAFT.NODE ADD cmd will be ignored. int unused_node_id = 0; - auto port = g_config.port + kiwi::g_config.raft_port_offset; - auto raw_addr = g_config.ip + ":" + std::to_string(port); + auto port = kiwi::PConfig::GetInstance().port + kiwi::PConfig::GetInstance().raft_port_offset; + auto raw_addr = kiwi::PConfig::GetInstance().ip + ":" + std::to_string(port); client->AppendArrayLen(int64_t(4)); client->AppendString("RAFT.NODE"); @@ -394,8 +394,8 @@ void PRaft::CheckRocksDBConfiguration(PClient* client, PClient* join_client, con } } - int current_databases_num = kiwi::g_config.databases; - int current_rocksdb_num = kiwi::g_config.db_instance_num; + int current_databases_num = kiwi::PConfig::GetInstance().databases; + int current_rocksdb_num = kiwi::PConfig::GetInstance().db_instance_num; std::string current_rocksdb_version = ROCKSDB_NAMESPACE::GetRocksVersionAsString(); if (current_databases_num != databases_num || current_rocksdb_num != rocksdb_num || current_rocksdb_version != rockdb_version) { @@ -714,7 +714,7 @@ int PRaft::on_snapshot_load(braft::SnapshotReader* reader) { // 3. When a snapshot is installed on a node, you do not need to set a playback point. auto reader_path = reader->get_path(); // xx/snapshot_0000001 - auto path = g_config.db_path + std::to_string(db_id_); // db/db_id + auto path = kiwi::PConfig::GetInstance().db_path + std::to_string(db_id_); // db/db_id TasksVector tasks(1, {TaskType::kLoadDBFromCheckpoint, db_id_, {{TaskArg::kCheckpointPath, reader_path}}, true}); PSTORE.HandleTaskSpecificDB(tasks); INFO("load snapshot success!"); diff --git a/src/replication.cc b/src/replication.cc index 2a9f4d2d..b0c3e1e0 100644 --- a/src/replication.cc +++ b/src/replication.cc @@ -98,7 +98,7 @@ void PReplication::TryBgsave() { // if (ret == 0) { // { // PDBSaver qdb; - // qdb.Save(g_config.rdbfullname.c_str()); + // qdb.Save(kiwi::PConfig::GetInstance().rdbfullname.c_str()); // DEBUG("PReplication save rdb done, exiting child"); // } // _exit(0); @@ -177,7 +177,7 @@ void PReplication::Cron() { if (masterInfo_.addr.IsValid()) { switch (masterInfo_.state) { case kPReplStateNone: { - if (masterInfo_.addr.GetIP() == g_config.ip && masterInfo_.addr.GetPort() == g_config.port) { + if (masterInfo_.addr.GetIP() == kiwi::PConfig::GetInstance().ip && masterInfo_.addr.GetPort() == kiwi::PConfig::GetInstance().port) { ERROR("Fix config, master addr is self addr!"); assert(!!!"wrong config for master addr"); } @@ -224,12 +224,12 @@ void PReplication::Cron() { } else if (master->GetAuth()) { // send replconf char req[128]; - auto len = snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", g_config.port); + auto len = snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", kiwi::PConfig::GetInstance().port); std::string info(req, len); master->SendPacket(std::move(info)); masterInfo_.state = kPReplStateWaitReplconf; - INFO("Send replconf listening-port {}", g_config.port); + INFO("Send replconf listening-port {}", kiwi::PConfig::GetInstance().port); } else { WARN("Haven't auth to master yet, or check masterauth password"); } diff --git a/src/store.cc b/src/store.cc index 36c93a07..7b449f49 100644 --- a/src/store.cc +++ b/src/store.cc @@ -31,7 +31,7 @@ void PStore::Init(int db_number) { db_number_ = db_number; backends_.reserve(db_number_); for (int i = 0; i < db_number_; i++) { - auto db = std::make_unique(i, g_config.db_path); + auto db = std::make_unique(i, kiwi::PConfig::GetInstance().db_path); db->Open(); backends_.push_back(std::move(db)); INFO("Open DB_{} success!", i); From b026cfc24cf31d698cd94f223d77f2eff1b2c1d6 Mon Sep 17 00:00:00 2001 From: wxnzb <3127484862@qq.com> Date: Thu, 2 Jan 2025 14:30:21 +0800 Subject: [PATCH 02/35] Make-p_config-a-singleton --- src/config.cc | 2 +- src/db.cc | 4 ++-- src/praft/praft.cc | 2 +- src/replication.cc | 6 ++++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/config.cc b/src/config.cc index dda6e4a4..6633953a 100644 --- a/src/config.cc +++ b/src/config.cc @@ -23,7 +23,7 @@ constexpr int DBNUMBER_MAX = 16; constexpr int THREAD_MAX = 129; constexpr int ROCKSDB_INSTANCE_NUMBER_MAX = 10; -PConfig &g_config=kiwi::PConfig::GetInstance(); +PConfig& g_config = kiwi::PConfig::GetInstance(); // preprocess func static void EraseQuotes(std::string& str) { diff --git a/src/db.cc b/src/db.cc index 9a45fea9..db9337b8 100644 --- a/src/db.cc +++ b/src/db.cc @@ -15,7 +15,6 @@ #include "praft/praft.h" #include "pstd/log.h" - namespace kiwi { DB::DB(int db_index, const std::string& db_path) @@ -32,7 +31,8 @@ rocksdb::Status DB::Open() { storage_options.options.periodic_compaction_seconds = kiwi::PConfig::GetInstance().rocksdb_periodic_second; storage_options.small_compaction_threshold = kiwi::PConfig::GetInstance().small_compaction_threshold; - storage_options.small_compaction_duration_threshold = kiwi::PConfig::GetInstance().small_compaction_duration_threshold; + storage_options.small_compaction_duration_threshold = + kiwi::PConfig::GetInstance().small_compaction_duration_threshold; if (kiwi::PConfig::GetInstance().use_raft) { storage_options.append_log_function = [&r = PRAFT](const Binlog& log, std::promise&& promise) { diff --git a/src/praft/praft.cc b/src/praft/praft.cc index d9762c22..bfa77d1d 100644 --- a/src/praft/praft.cc +++ b/src/praft/praft.cc @@ -713,7 +713,7 @@ int PRaft::on_snapshot_load(braft::SnapshotReader* reader) { } // 3. When a snapshot is installed on a node, you do not need to set a playback point. - auto reader_path = reader->get_path(); // xx/snapshot_0000001 + auto reader_path = reader->get_path(); // xx/snapshot_0000001 auto path = kiwi::PConfig::GetInstance().db_path + std::to_string(db_id_); // db/db_id TasksVector tasks(1, {TaskType::kLoadDBFromCheckpoint, db_id_, {{TaskArg::kCheckpointPath, reader_path}}, true}); PSTORE.HandleTaskSpecificDB(tasks); diff --git a/src/replication.cc b/src/replication.cc index b0c3e1e0..4d9ee87e 100644 --- a/src/replication.cc +++ b/src/replication.cc @@ -177,7 +177,8 @@ void PReplication::Cron() { if (masterInfo_.addr.IsValid()) { switch (masterInfo_.state) { case kPReplStateNone: { - if (masterInfo_.addr.GetIP() == kiwi::PConfig::GetInstance().ip && masterInfo_.addr.GetPort() == kiwi::PConfig::GetInstance().port) { + if (masterInfo_.addr.GetIP() == kiwi::PConfig::GetInstance().ip && + masterInfo_.addr.GetPort() == kiwi::PConfig::GetInstance().port) { ERROR("Fix config, master addr is self addr!"); assert(!!!"wrong config for master addr"); } @@ -224,7 +225,8 @@ void PReplication::Cron() { } else if (master->GetAuth()) { // send replconf char req[128]; - auto len = snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", kiwi::PConfig::GetInstance().port); + auto len = + snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", kiwi::PConfig::GetInstance().port); std::string info(req, len); master->SendPacket(std::move(info)); masterInfo_.state = kPReplStateWaitReplconf; From f68c0389e45981436b5a31005331fac37bd0bddb Mon Sep 17 00:00:00 2001 From: wxnzb <3127484862@qq.com> Date: Thu, 2 Jan 2025 14:37:41 +0800 Subject: [PATCH 03/35] Make-p_config-a-singleton --- src/config.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/config.h b/src/config.h index 4bb66e24..aa995f86 100644 --- a/src/config.h +++ b/src/config.h @@ -31,7 +31,6 @@ using Status = rocksdb::Status; using CheckFunc = std::function; class PConfig; - class BaseValue { public: BaseValue(std::string key, CheckFunc check_func_ptr, bool rewritable = false) @@ -155,12 +154,12 @@ class PConfig { * PConfig() * Initialize kiwi's config & RocksDB's config. */ - static PConfig& GetInstance(){ + static PConfig& GetInstance() { static PConfig instance; return instance; - } - PConfig(const PConfig&)=delete; - PConfig& operator=(const PConfig&)=delete; + } + PConfig(const PConfig&) = delete; + PConfig& operator=(const PConfig&) = delete; /*------------------------ * ~PConfig() From 6fc3aa07e793debba6bbac0dab15ac01dd39936b Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sun, 5 Jan 2025 08:50:33 +0000 Subject: [PATCH 04/35] praft -> raft --- CMakeLists.txt | 2 +- src/base_cmd.cc | 12 +- src/client.cc | 8 +- src/cmd_admin.cc | 28 ++--- src/cmd_raft.cc | 32 ++--- src/db.cc | 10 +- src/kiwi.cc | 18 +-- src/kiwi.h | 18 +-- src/{praft => raft}/CMakeLists.txt | 22 ++-- src/{praft => raft}/binlog.proto | 0 src/{praft/praft.cc => raft/raft.cc} | 116 +++++++++--------- src/{praft/praft.h => raft/raft.h} | 13 +- src/{praft/praft.proto => raft/raft.proto} | 0 .../praft_service.h => raft/raft_service.h} | 6 +- src/{praft/psnapshot.cc => raft/snapshot.cc} | 16 +-- src/{praft/psnapshot.h => raft/snapshot.h} | 10 +- src/storage/src/storage.cc | 4 +- 17 files changed, 157 insertions(+), 158 deletions(-) rename src/{praft => raft}/CMakeLists.txt (68%) rename src/{praft => raft}/binlog.proto (100%) rename src/{praft/praft.cc => raft/raft.cc} (85%) rename src/{praft/praft.h => raft/raft.h} (96%) rename src/{praft/praft.proto => raft/raft.proto} (100%) rename src/{praft/praft_service.h => raft/raft_service.h} (86%) rename src/{praft/psnapshot.cc => raft/snapshot.cc} (91%) rename src/{praft/psnapshot.h => raft/snapshot.h} (78%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 199bb141..2eb2703c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -207,7 +207,7 @@ FILE(MAKE_DIRECTORY "${PROTO_OUTPUT_DIR}") ADD_SUBDIRECTORY(src/pstd) ADD_SUBDIRECTORY(src/net) -ADD_SUBDIRECTORY(src/praft) +ADD_SUBDIRECTORY(src/raft) ADD_SUBDIRECTORY(src/storage) ADD_SUBDIRECTORY(src/resp) diff --git a/src/base_cmd.cc b/src/base_cmd.cc index 3f7439f9..3c57a42f 100644 --- a/src/base_cmd.cc +++ b/src/base_cmd.cc @@ -11,13 +11,13 @@ #include "fmt/core.h" -#include "praft/praft.h" +#include "raft/raft.h" #include "common.h" #include "config.h" #include "kiwi.h" #include "log.h" -#include "praft/praft.h" +#include "raft/raft.h" namespace kiwi { @@ -43,12 +43,12 @@ void BaseCmd::Execute(PClient* client) { // read consistency (lease read) / write redirection if (kiwi::PConfig::GetInstance().use_raft && (HasFlag(kCmdFlagsReadonly) || HasFlag(kCmdFlagsWrite))) { - if (!PRAFT.IsInitialized()) { - return client->SetRes(CmdRes::kErrOther, "PRAFT is not initialized"); + if (!RAFT_INST.IsInitialized()) { + return client->SetRes(CmdRes::kErrOther, "RAFT_INST is not initialized"); } - if (!PRAFT.IsLeader()) { - auto leader_addr = PRAFT.GetLeaderAddress(); + if (!RAFT_INST.IsLeader()) { + auto leader_addr = RAFT_INST.GetLeaderAddress(); if (leader_addr.empty()) { return client->SetRes(CmdRes::kErrOther, std::string("-CLUSTERDOWN No Raft leader")); } diff --git a/src/client.cc b/src/client.cc index f9e8d282..57eb5964 100644 --- a/src/client.cc +++ b/src/client.cc @@ -16,7 +16,7 @@ #include "config.h" #include "env.h" #include "kiwi.h" -#include "praft/praft.h" +#include "raft/raft.h" #include "pstd/log.h" #include "pstd/pstd_string.h" #include "slow_log.h" @@ -119,7 +119,7 @@ int PClient::HandlePacket(std::string&& data) { if (isPeerMaster()) { if (isClusterCmdTarget()) { // Proccees the packet at one turn. - int len = PRAFT.ProcessClusterCmdResponse(this, start, bytes); // @todo + int len = RAFT_INST.ProcessClusterCmdResponse(this, start, bytes); // @todo if (len > 0) { return len; } @@ -244,7 +244,7 @@ void PClient::OnConnect() { } if (isClusterCmdTarget()) { - PRAFT.SendNodeRequest(this); + RAFT_INST.SendNodeRequest(this); } } else { if (kiwi::PConfig::GetInstance().password.empty()) { @@ -304,7 +304,7 @@ bool PClient::isPeerMaster() const { } bool PClient::isClusterCmdTarget() const { - return PRAFT.GetClusterCmdCtx().GetPeerIp() == PeerIP() && PRAFT.GetClusterCmdCtx().GetPort() == PeerPort(); + return RAFT_INST.GetClusterCmdCtx().GetPeerIp() == PeerIP() && RAFT_INST.GetClusterCmdCtx().GetPort() == PeerPort(); } uint64_t PClient::GetUniqueID() const { return GetConnId(); } diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 65556516..45f77ed4 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -36,7 +36,7 @@ #include "rocksdb/version.h" #include "kiwi.h" -#include "praft/praft.h" +#include "raft/raft.h" #include "pstd/env.h" #include "client_map.h" @@ -262,7 +262,7 @@ void HelloCmd::Hello(PClient* client) { client->AppendString("server"); client->AppendString("kiwi"); client->AppendString("version"); - client->AppendString(Kkiwi_VERSION); + client->AppendString(KIWI_VERSION); client->AppendString("proto"); client->AppendInteger(static_cast(2)); client->AppendString("id"); @@ -390,12 +390,12 @@ void InfoCmd::DoCmd(PClient* client) { raft_node1:id=1733428433,state=connected,voting=yes,addr=localhost,port=5001,last_conn_secs=5,conn_errors=0,conn_oks=1 */ void InfoCmd::InfoRaft(std::string& message) { - if (!PRAFT.IsInitialized()) { + if (!RAFT_INST.IsInitialized()) { message += "-ERR Not a cluster member.\r\n"; return; } - auto node_status = PRAFT.GetNodeStatus(); + auto node_status = RAFT_INST.GetNodeStatus(); if (node_status.state == braft::State::STATE_END) { message += "-ERR Node is not initialized.\r\n"; return; @@ -403,9 +403,9 @@ void InfoCmd::InfoRaft(std::string& message) { std::stringstream tmp_stream; - tmp_stream << "raft_group_id:" << PRAFT.GetGroupID() << "\r\n"; - tmp_stream << "raft_node_id:" << PRAFT.GetNodeID() << "\r\n"; - tmp_stream << "raft_peer_id:" << PRAFT.GetPeerID() << "\r\n"; + tmp_stream << "raft_group_id:" << RAFT_INST.GetGroupID() << "\r\n"; + tmp_stream << "raft_node_id:" << RAFT_INST.GetNodeID() << "\r\n"; + tmp_stream << "raft_peer_id:" << RAFT_INST.GetPeerID() << "\r\n"; if (braft::is_active_state(node_status.state)) { tmp_stream << "raft_state:up\r\n"; } else { @@ -415,9 +415,9 @@ void InfoCmd::InfoRaft(std::string& message) { tmp_stream << "raft_leader_id:" << node_status.leader_id.to_string() << "\r\n"; tmp_stream << "raft_current_term:" << std::to_string(node_status.term) << "\r\n"; - if (PRAFT.IsLeader()) { + if (RAFT_INST.IsLeader()) { std::vector peers; - auto status = PRAFT.GetListPeers(&peers); + auto status = RAFT_INST.GetListPeers(&peers); if (!status.ok()) { tmp_stream.str("-ERR "); tmp_stream << status.error_str() << "\r\n"; @@ -444,19 +444,19 @@ void InfoCmd::InfoServer(std::string& info) { time_t current_time_s = time(nullptr); std::stringstream tmp_stream; char version[32]; - snprintf(version, sizeof(version), "%s", Kkiwi_VERSION); + snprintf(version, sizeof(version), "%s", KIWI_VERSION); tmp_stream << "# Server\r\n"; tmp_stream << "kiwi_version:" << version << "\r\n"; - tmp_stream << "kiwi_build_git_sha:" << Kkiwi_GIT_COMMIT_ID << "\r\n"; - tmp_stream << "kiwi_build_compile_date: " << Kkiwi_BUILD_DATE << "\r\n"; + tmp_stream << "kiwi_build_git_sha:" << KIWI_GIT_COMMIT_ID << "\r\n"; + tmp_stream << "kiwi_build_compile_date: " << KIWI_BUILD_DATE << "\r\n"; tmp_stream << "os:" << host_info.sysname << " " << host_info.release << " " << host_info.machine << "\r\n"; tmp_stream << "arch_bits:" << (reinterpret_cast(&host_info.machine) + strlen(host_info.machine) - 2) << "\r\n"; tmp_stream << "process_id:" << getpid() << "\r\n"; tmp_stream << "run_id:" << static_cast(kiwi::PConfig::GetInstance().run_id) << "\r\n"; tmp_stream << "tcp_port:" << kiwi::PConfig::GetInstance().port << "\r\n"; - tmp_stream << "uptime_in_seconds:" << (current_time_s - g_kiwi->Start_time_s()) << "\r\n"; - tmp_stream << "uptime_in_days:" << (current_time_s / (24 * 3600) - g_kiwi->Start_time_s() / (24 * 3600) + 1) + tmp_stream << "uptime_in_seconds:" << (current_time_s - g_kiwi->GetStartTime()) << "\r\n"; + tmp_stream << "uptime_in_days:" << (current_time_s / (24 * 3600) - g_kiwi->GetStartTime() / (24 * 3600) + 1) << "\r\n"; tmp_stream << "config_file:" << g_kiwi->GetConfigName() << "\r\n"; diff --git a/src/cmd_raft.cc b/src/cmd_raft.cc index 5880eaec..3fee939e 100644 --- a/src/cmd_raft.cc +++ b/src/cmd_raft.cc @@ -17,7 +17,7 @@ #include // #include "net/event_loop.h" -#include "praft/praft.h" +#include "raft/raft.h" #include "pstd/log.h" #include "pstd/pstd_string.h" @@ -58,8 +58,8 @@ void RaftNodeCmd::DoCmd(PClient* client) { void RaftNodeCmd::DoCmdAdd(PClient* client) { // Check whether it is a leader. If it is not a leader, return the leader information - if (!PRAFT.IsLeader()) { - client->SetRes(CmdRes::kWrongLeader, PRAFT.GetLeaderID()); + if (!RAFT_INST.IsLeader()) { + client->SetRes(CmdRes::kWrongLeader, RAFT_INST.GetLeaderID()); return; } @@ -70,7 +70,7 @@ void RaftNodeCmd::DoCmdAdd(PClient* client) { // RedisRaft has nodeid, but in Braft, NodeId is IP:Port. // So we do not need to parse and use nodeid like redis; - auto s = PRAFT.AddPeer(client->argv_[3]); + auto s = RAFT_INST.AddPeer(client->argv_[3]); if (s.ok()) { client->SetRes(CmdRes::kOK); } else { @@ -80,7 +80,7 @@ void RaftNodeCmd::DoCmdAdd(PClient* client) { void RaftNodeCmd::DoCmdRemove(PClient* client) { // If the node has been initialized, it needs to close the previous initialization and rejoin the other group - if (!PRAFT.IsInitialized()) { + if (!RAFT_INST.IsInitialized()) { client->SetRes(CmdRes::kErrOther, "Don't already cluster member"); return; } @@ -91,9 +91,9 @@ void RaftNodeCmd::DoCmdRemove(PClient* client) { } // Check whether it is a leader. If it is not a leader, send remove request to leader - if (!PRAFT.IsLeader()) { + if (!RAFT_INST.IsLeader()) { // Get the leader information - braft::PeerId leader_peer_id(PRAFT.GetLeaderID()); + braft::PeerId leader_peer_id(RAFT_INST.GetLeaderID()); // @todo There will be an unreasonable address, need to consider how to deal with it if (leader_peer_id.is_empty()) { client->SetRes(CmdRes::kErrOther, @@ -106,11 +106,11 @@ void RaftNodeCmd::DoCmdRemove(PClient* client) { auto port = leader_peer_id.addr.port - kiwi::PConfig::GetInstance().raft_port_offset; auto peer_id = client->argv_[2]; auto ret = - PRAFT.GetClusterCmdCtx().Set(ClusterCmdType::kRemove, client, std::move(peer_ip), port, std::move(peer_id)); + RAFT_INST.GetClusterCmdCtx().Set(ClusterCmdType::kRemove, client, std::move(peer_ip), port, std::move(peer_id)); if (!ret) { // other clients have removed return client->SetRes(CmdRes::kErrOther, "Other clients have removed"); } - PRAFT.GetClusterCmdCtx().ConnectTargetNode(); + RAFT_INST.GetClusterCmdCtx().ConnectTargetNode(); INFO("Sent remove request to leader successfully"); // Not reply any message here, we will reply after the connection is established. @@ -118,7 +118,7 @@ void RaftNodeCmd::DoCmdRemove(PClient* client) { return; } - auto s = PRAFT.RemovePeer(client->argv_[2]); + auto s = RAFT_INST.RemovePeer(client->argv_[2]); if (s.ok()) { client->SetRes(CmdRes::kOK); } else { @@ -129,7 +129,7 @@ void RaftNodeCmd::DoCmdRemove(PClient* client) { void RaftNodeCmd::DoCmdSnapshot(PClient* client) { auto self_snapshot_index = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetSmallestFlushedLogIndex(); INFO("DoCmdSnapshot self_snapshot_index:{}", self_snapshot_index); - auto s = PRAFT.DoSnapshot(self_snapshot_index); + auto s = RAFT_INST.DoSnapshot(self_snapshot_index); if (s.ok()) { client->SetRes(CmdRes::kOK); } @@ -149,7 +149,7 @@ bool RaftClusterCmd::DoInitial(PClient* client) { } void RaftClusterCmd::DoCmd(PClient* client) { - if (PRAFT.IsInitialized()) { + if (RAFT_INST.IsInitialized()) { return client->SetRes(CmdRes::kErrOther, "Already cluster member"); } @@ -177,7 +177,7 @@ void RaftClusterCmd::DoCmdInit(PClient* client) { } else { cluster_id = pstd::RandomHexChars(RAFT_GROUPID_LEN); } - auto s = PRAFT.Init(cluster_id, false); + auto s = RAFT_INST.Init(cluster_id, false); if (!s.ok()) { return client->SetRes(CmdRes::kErrOther, fmt::format("Failed to init node: ", s.error_str())); } @@ -197,7 +197,7 @@ static inline std::optional> GetIpAndPortFromEnd void RaftClusterCmd::DoCmdJoin(PClient* client) { // If the node has been initialized, it needs to close the previous initialization and rejoin the other group - if (PRAFT.IsInitialized()) { + if (RAFT_INST.IsInitialized()) { return client->SetRes(CmdRes::kErrOther, "A node that has been added to a cluster must be removed \ from the old cluster before it can be added to the new cluster"); @@ -224,11 +224,11 @@ void RaftClusterCmd::DoCmdJoin(PClient* client) { auto& [peer_ip, port] = *ip_port; // Connect target - auto ret = PRAFT.GetClusterCmdCtx().Set(ClusterCmdType::kJoin, client, std::move(peer_ip), port); + auto ret = RAFT_INST.GetClusterCmdCtx().Set(ClusterCmdType::kJoin, client, std::move(peer_ip), port); if (!ret) { // other clients have joined return client->SetRes(CmdRes::kErrOther, "Other clients have joined"); } - PRAFT.GetClusterCmdCtx().ConnectTargetNode(); + RAFT_INST.GetClusterCmdCtx().ConnectTargetNode(); INFO("Sent join request to leader successfully"); // Not reply any message here, we will reply after the connection is established. diff --git a/src/db.cc b/src/db.cc index db9337b8..67e27486 100644 --- a/src/db.cc +++ b/src/db.cc @@ -12,7 +12,7 @@ #include #include "config.h" -#include "praft/praft.h" +#include "raft/raft.h" #include "pstd/log.h" namespace kiwi { @@ -35,10 +35,10 @@ rocksdb::Status DB::Open() { kiwi::PConfig::GetInstance().small_compaction_duration_threshold; if (kiwi::PConfig::GetInstance().use_raft) { - storage_options.append_log_function = [&r = PRAFT](const Binlog& log, std::promise&& promise) { + storage_options.append_log_function = [&r = RAFT_INST](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; - storage_options.do_snapshot_function = [raft = &kiwi::PRAFT](auto&& self_snapshot_index, auto&& is_sync) { + storage_options.do_snapshot_function = [raft = &kiwi::RAFT_INST](auto&& self_snapshot_index, auto&& is_sync) { raft->DoSnapshot(std::forward(self_snapshot_index), std::forward(is_sync)); }; @@ -117,11 +117,11 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma storage_options.options.ttl = kiwi::PConfig::GetInstance().rocksdb_ttl_second; storage_options.options.periodic_compaction_seconds = kiwi::PConfig::GetInstance().rocksdb_periodic_second; if (kiwi::PConfig::GetInstance().use_raft) { - storage_options.append_log_function = [&r = PRAFT](const Binlog& log, std::promise&& promise) { + storage_options.append_log_function = [&r = RAFT_INST](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; storage_options.do_snapshot_function = - std::bind(&kiwi::PRaft::DoSnapshot, &kiwi::PRAFT, std::placeholders::_1, std::placeholders::_2); + std::bind(&kiwi::KRaft::DoSnapshot, &kiwi::RAFT_INST, std::placeholders::_1, std::placeholders::_2); } if (auto s = storage_->Open(storage_options, db_path_); !s.ok()) { diff --git a/src/kiwi.cc b/src/kiwi.cc index ddaab8a7..848dd9d4 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -27,7 +27,7 @@ #include "kiwi.h" #include "kiwi_logo.h" #include "options.h" -#include "praft/praft.h" +#include "raft/raft.h" #include "pstd/log.h" #include "pstd/pstd_util.h" #include "slow_log.h" @@ -101,11 +101,11 @@ bool KiwiDB::ParseArgs(int argc, char* argv[]) { switch (c) { case 'v': { - std::cerr << "kiwi Server version: " << Kkiwi_VERSION << " bits=" << (sizeof(void*) == 8 ? 64 : 32) + std::cerr << "kiwi Server version: " << KIWI_VERSION << " bits=" << (sizeof(void*) == 8 ? 64 : 32) << std::endl; - std::cerr << "kiwi Server Build Type: " << Kkiwi_BUILD_TYPE << std::endl; - std::cerr << "kiwi Server Build Date: " << Kkiwi_BUILD_DATE << std::endl; - std::cerr << "kiwi Server Build GIT SHA: " << Kkiwi_GIT_COMMIT_ID << std::endl; + std::cerr << "kiwi Server Build Type: " << KIWI_BUILD_TYPE << std::endl; + std::cerr << "kiwi Server Build Date: " << KIWI_BUILD_DATE << std::endl; + std::cerr << "kiwi Server Build GIT SHA: " << KIWI_GIT_COMMIT_ID << std::endl; std::exit(0); break; } @@ -246,9 +246,9 @@ void KiwiDB::Run() { } void KiwiDB::Stop() { - kiwi::PRAFT.ShutDown(); - kiwi::PRAFT.Join(); - kiwi::PRAFT.Clear(); + kiwi::RAFT_INST.ShutDown(); + kiwi::RAFT_INST.Join(); + kiwi::RAFT_INST.Clear(); cmd_threads_.Stop(); event_server_->StopServer(); } @@ -346,7 +346,7 @@ int main(int argc, char* argv[]) { if (g_kiwi->Init()) { // output logo to console char logo[1024] = ""; - snprintf(logo, sizeof logo - 1, kiwiLogo, Kkiwi_VERSION, static_cast(sizeof(void*)) * 8, + snprintf(logo, sizeof logo - 1, kiwiLogo, KIWI_VERSION, static_cast(sizeof(void*)) * 8, static_cast(kiwi::PConfig::GetInstance().port)); std::cout << logo; g_kiwi->Run(); diff --git a/src/kiwi.h b/src/kiwi.h index 499cd92d..038f5b6b 100644 --- a/src/kiwi.h +++ b/src/kiwi.h @@ -15,24 +15,24 @@ #include "net/event_server.h" #include "options.h" -#define Kkiwi_VERSION "4.0.0" +#define KIWI_VERSION "4.0.0" #ifdef BUILD_DEBUG -# define Kkiwi_BUILD_TYPE "DEBUG" +# define KIWI_BUILD_TYPE "DEBUG" #else -# define Kkiwi_BUILD_TYPE "RELEASE" +# define KIWI_BUILD_TYPE "RELEASE" #endif -#ifndef Kkiwi_GIT_COMMIT_ID -# define Kkiwi_GIT_COMMIT_ID "unknown" +#ifndef KIWI_GIT_COMMIT_ID +# define KIWI_GIT_COMMIT_ID "unknown" #endif -#ifndef Kkiwi_BUILD_DATE -# define Kkiwi_BUILD_DATE "unknown" +#ifndef KIWI_BUILD_DATE +# define KIWI_BUILD_DATE "unknown" #endif namespace kiwi { -class PRaft; +class Raft; } // namespace kiwi class KiwiDB final { @@ -74,7 +74,7 @@ class KiwiDB final { const std::function&, const net::SocketAddr&)>& onConnect, const std::function& cb); - time_t Start_time_s() { return start_time_s_; } + time_t GetStartTime() { return start_time_s_; } public: uint16_t port_{0}; diff --git a/src/praft/CMakeLists.txt b/src/raft/CMakeLists.txt similarity index 68% rename from src/praft/CMakeLists.txt rename to src/raft/CMakeLists.txt index ccfc167e..49eb275f 100644 --- a/src/praft/CMakeLists.txt +++ b/src/raft/CMakeLists.txt @@ -16,26 +16,26 @@ SET(LIBRARY_OUTPUT_PATH ${PLIB_INSTALL_DIR}) TARGET_INCLUDE_DIRECTORIES(binlog_pb PRIVATE ${PROTOBUF_INCLUDE_DIR}) ADD_CUSTOM_COMMAND( - OUTPUT "${PROTO_OUTPUT_DIR}/praft.pb.cc" + OUTPUT "${PROTO_OUTPUT_DIR}/raft.pb.cc" DEPENDS protobuf COMMAND ${PROTOBUF_PROTOC} ARGS -I ${CMAKE_CURRENT_SOURCE_DIR} --cpp_out ${PROTO_OUTPUT_DIR} - ${CMAKE_CURRENT_SOURCE_DIR}/praft.proto + ${CMAKE_CURRENT_SOURCE_DIR}/raft.proto ) -ADD_LIBRARY(praft_pb STATIC "${PROTO_OUTPUT_DIR}/praft.pb.cc") -TARGET_INCLUDE_DIRECTORIES(praft_pb PRIVATE ${PROTOBUF_INCLUDE_DIR}) +ADD_LIBRARY(raft_pb STATIC "${PROTO_OUTPUT_DIR}/raft.pb.cc") +TARGET_INCLUDE_DIRECTORIES(raft_pb PRIVATE ${PROTOBUF_INCLUDE_DIR}) -FILE(GLOB PRAFT_SRC +FILE(GLOB RAFT_INST_SRC "${CMAKE_CURRENT_SOURCE_DIR}/*.cc" ) SET(LIBRARY_OUTPUT_PATH ${PLIB_INSTALL_DIR}) -ADD_LIBRARY(praft ${PRAFT_SRC}) +ADD_LIBRARY(raft ${RAFT_INST_SRC}) -TARGET_INCLUDE_DIRECTORIES(praft +TARGET_INCLUDE_DIRECTORIES(raft PRIVATE ${PROJECT_SOURCE_DIR}/src PRIVATE ${rocksdb_SOURCE_DIR}/include PRIVATE ${BRAFT_INCLUDE_DIR} @@ -45,10 +45,10 @@ TARGET_INCLUDE_DIRECTORIES(praft ) IF (CMAKE_SYSTEM_NAME STREQUAL "Linux") - SET(PRAFT_LIB ${PRAFT_LIB} rt) + SET(RAFT_INST_LIB ${RAFT_INST_LIB} rt) ENDIF () SET(LIBRARY_OUTPUT_PATH ${PLIB_INSTALL_DIR}) -ADD_DEPENDENCIES(praft protobuf praft_pb binlog_pb) -TARGET_LINK_LIBRARIES(praft net; dl; fmt; storage; pstd braft brpc ssl crypto zlib protobuf leveldb gflags rocksdb z ${PRAFT_LIB}) -SET_TARGET_PROPERTIES(praft PROPERTIES LINKER_LANGUAGE CXX) +ADD_DEPENDENCIES(raft protobuf raft_pb binlog_pb) +TARGET_LINK_LIBRARIES(raft net; dl; fmt; storage; pstd braft brpc ssl crypto zlib protobuf leveldb gflags rocksdb z ${RAFT_INST_LIB}) +SET_TARGET_PROPERTIES(raft PROPERTIES LINKER_LANGUAGE CXX) diff --git a/src/praft/binlog.proto b/src/raft/binlog.proto similarity index 100% rename from src/praft/binlog.proto rename to src/raft/binlog.proto diff --git a/src/praft/praft.cc b/src/raft/raft.cc similarity index 85% rename from src/praft/praft.cc rename to src/raft/raft.cc index bfa77d1d..2214bf6e 100644 --- a/src/praft/praft.cc +++ b/src/raft/raft.cc @@ -5,7 +5,7 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -#include "praft.h" +#include "raft.h" #include @@ -24,8 +24,8 @@ #include "replication.h" #include "store.h" -#include "praft_service.h" -#include "psnapshot.h" +#include "raft_service.h" +#include "snapshot.h" namespace braft { DECLARE_bool(raft_enable_leader_lease); @@ -72,26 +72,26 @@ void ClusterCmdContext::ConnectTargetNode() { auto ip = PREPL.GetMasterAddr().GetIP(); auto port = PREPL.GetMasterAddr().GetPort(); if (ip == peer_ip_ && port == port_ && PREPL.GetMasterState() == kPReplStateConnected) { - PRAFT.SendNodeRequest(PREPL.GetMaster()); + RAFT_INST.SendNodeRequest(PREPL.GetMaster()); return; } // reconnect auto fail_cb = [&](const std::string& err) { INFO("Failed to connect to cluster node, err: {}", err); - PRAFT.OnClusterCmdConnectionFailed(err); + RAFT_INST.OnClusterCmdConnectionFailed(err); }; PREPL.SetFailCallback(fail_cb); PREPL.SetMasterState(kPReplStateNone); PREPL.SetMasterAddr(peer_ip_.c_str(), port_); } -PRaft& PRaft::Instance() { - static PRaft store; +Raft& Raft::Instance() { + static Raft store; return store; } -butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { +butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { if (node_ && server_) { return {0, "OK"}; } @@ -99,7 +99,7 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { server_ = std::make_unique(); auto port = kiwi::PConfig::GetInstance().port + kiwi::PConfig::GetInstance().raft_port_offset; // Add your service into RPC server - DummyServiceImpl service(&PRAFT); + DummyServiceImpl service(&RAFT_INST); if (server_->AddService(&service, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) { server_.reset(); return ERROR_LOG_AND_STATUS("Failed to add service"); @@ -122,7 +122,7 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { server_.reset(); return ERROR_LOG_AND_STATUS("Failed to start server"); } - // It's ok to start PRaft; + // It's ok to start Raft; assert(group_id.size() == RAFT_GROUPID_LEN); this->group_id_ = group_id; @@ -162,7 +162,7 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { node_options_.raft_meta_uri = prefix + "/raft_meta"; node_options_.snapshot_uri = prefix + "/snapshot"; // node_options_.disable_cli = FLAGS_disable_cli; - snapshot_adaptor_ = new PPosixFileSystemAdaptor(); + snapshot_adaptor_ = new PosixFileSystemAdaptor(); node_options_.snapshot_file_system_adaptor = &snapshot_adaptor_; node_ = std::make_unique("kiwi", braft::PeerId(addr)); // group_id @@ -178,7 +178,7 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { return {0, "OK"}; } -bool PRaft::IsLeader() const { +bool Raft::IsLeader() const { if (!node_) { ERROR("Node is not initialized"); return false; @@ -193,7 +193,7 @@ bool PRaft::IsLeader() const { return term > 0 && term == lease_status.term; } -void PRaft::GetLeaderLeaseStatus(braft::LeaderLeaseStatus* status) const { +void Raft::GetLeaderLeaseStatus(braft::LeaderLeaseStatus* status) const { if (!node_) { ERROR("Node is not initialized"); return; @@ -202,7 +202,7 @@ void PRaft::GetLeaderLeaseStatus(braft::LeaderLeaseStatus* status) const { node_->get_leader_lease_status(status); } -std::string PRaft::GetLeaderID() const { +std::string Raft::GetLeaderID() const { if (!node_) { ERROR("Node is not initialized"); return "Failed to get leader id"; @@ -210,7 +210,7 @@ std::string PRaft::GetLeaderID() const { return node_->leader_id().to_string(); } -std::string PRaft::GetLeaderAddress() const { +std::string Raft::GetLeaderAddress() const { if (!node_) { ERROR("Node is not initialized"); return "Failed to get leader id"; @@ -226,7 +226,7 @@ std::string PRaft::GetLeaderAddress() const { return addr.c_str(); } -std::string PRaft::GetNodeID() const { +std::string Raft::GetNodeID() const { if (!node_) { ERROR("Node is not initialized"); return "Failed to get node id"; @@ -234,7 +234,7 @@ std::string PRaft::GetNodeID() const { return node_->node_id().to_string(); } -std::string PRaft::GetPeerID() const { +std::string Raft::GetPeerID() const { if (!node_) { ERROR("Node is not initialized"); return "Failed to get node id"; @@ -246,7 +246,7 @@ std::string PRaft::GetPeerID() const { return peer_id; } -std::string PRaft::GetGroupID() const { +std::string Raft::GetGroupID() const { if (!node_) { ERROR("Node is not initialized"); return "Failed to get cluster id"; @@ -254,7 +254,7 @@ std::string PRaft::GetGroupID() const { return group_id_; } -braft::NodeStatus PRaft::GetNodeStatus() const { +braft::NodeStatus Raft::GetNodeStatus() const { braft::NodeStatus node_status; if (!node_) { ERROR("Node is not initialized"); @@ -265,14 +265,14 @@ braft::NodeStatus PRaft::GetNodeStatus() const { return node_status; } -butil::Status PRaft::GetListPeers(std::vector* peers) { +butil::Status Raft::GetListPeers(std::vector* peers) { if (!node_) { ERROR_LOG_AND_STATUS("Node is not initialized"); } return node_->list_peers(peers); } -storage::LogIndex PRaft::GetTerm(uint64_t log_index) { +storage::LogIndex Raft::GetTerm(uint64_t log_index) { if (!node_) { ERROR("Node is not initialized"); return 0; @@ -281,7 +281,7 @@ storage::LogIndex PRaft::GetTerm(uint64_t log_index) { return node_->get_term(log_index); } -storage::LogIndex PRaft::GetLastLogIndex(bool is_flush) { +storage::LogIndex Raft::GetLastLogIndex(bool is_flush) { if (!node_) { ERROR("Node is not initialized"); return 0; @@ -290,7 +290,7 @@ storage::LogIndex PRaft::GetLastLogIndex(bool is_flush) { return node_->get_last_log_index(is_flush); } -void PRaft::SendNodeRequest(PClient* client) { +void Raft::SendNodeRequest(PClient* client) { assert(client); auto cluster_cmd_type = cluster_cmd_ctx_.GetClusterCmdType(); @@ -308,7 +308,7 @@ void PRaft::SendNodeRequest(PClient* client) { } // Gets the cluster id, which is used to initialize node -void PRaft::SendNodeInfoRequest(PClient* client, const std::string& info_type) { +void Raft::SendNodeInfoRequest(PClient* client, const std::string& info_type) { assert(client); client->AppendArrayLen(int64_t(2)); @@ -318,7 +318,7 @@ void PRaft::SendNodeInfoRequest(PClient* client, const std::string& info_type) { // client->Clear(); } -void PRaft::SendNodeAddRequest(PClient* client) { +void Raft::SendNodeAddRequest(PClient* client) { assert(client); // Node id in braft are ip:port, the node id param in RAFT.NODE ADD cmd will be ignored. @@ -335,7 +335,7 @@ void PRaft::SendNodeAddRequest(PClient* client) { // client->Clear(); } -void PRaft::SendNodeRemoveRequest(PClient* client) { +void Raft::SendNodeRemoveRequest(PClient* client) { assert(client); client->AppendArrayLen(int64_t(3)); client->AppendString("RAFT.NODE"); @@ -345,15 +345,15 @@ void PRaft::SendNodeRemoveRequest(PClient* client) { // client->Clear(); } -int PRaft::ProcessClusterCmdResponse(PClient* client, const char* start, int len) { +int Raft::ProcessClusterCmdResponse(PClient* client, const char* start, int len) { auto cluster_cmd_type = cluster_cmd_ctx_.GetClusterCmdType(); int ret = 0; switch (cluster_cmd_type) { case ClusterCmdType::kJoin: - ret = PRAFT.ProcessClusterJoinCmdResponse(client, start, len); + ret = RAFT_INST.ProcessClusterJoinCmdResponse(client, start, len); break; case ClusterCmdType::kRemove: - ret = PRAFT.ProcessClusterRemoveCmdResponse(client, start, len); + ret = RAFT_INST.ProcessClusterRemoveCmdResponse(client, start, len); break; default: client->SetRes(CmdRes::kErrOther, "RAFT.CLUSTER response supports JOIN/REMOVE only"); @@ -363,7 +363,7 @@ int PRaft::ProcessClusterCmdResponse(PClient* client, const char* start, int len return ret; } -void PRaft::CheckRocksDBConfiguration(PClient* client, PClient* join_client, const std::string& reply) { +void Raft::CheckRocksDBConfiguration(PClient* client, PClient* join_client, const std::string& reply) { int databases_num = 0; int rocksdb_num = 0; std::string rockdb_version; @@ -409,7 +409,7 @@ void PRaft::CheckRocksDBConfiguration(PClient* client, PClient* join_client, con } } -void PRaft::LeaderRedirection(PClient* join_client, const std::string& reply) { +void Raft::LeaderRedirection(PClient* join_client, const std::string& reply) { // Resolve the ip address of the leader pstd::StringTrimLeft(reply, WRONG_LEADER); pstd::StringTrim(reply); @@ -420,20 +420,20 @@ void PRaft::LeaderRedirection(PClient* join_client, const std::string& reply) { // Reset the target of the connection cluster_cmd_ctx_.Clear(); - auto ret = PRAFT.GetClusterCmdCtx().Set(ClusterCmdType::kJoin, join_client, std::move(peer_ip), port); + auto ret = RAFT_INST.GetClusterCmdCtx().Set(ClusterCmdType::kJoin, join_client, std::move(peer_ip), port); if (!ret) { // other clients have joined join_client->SetRes(CmdRes::kErrOther, "Other clients have joined"); join_client->SendPacket(); // join_client->Clear(); return; } - PRAFT.GetClusterCmdCtx().ConnectTargetNode(); + RAFT_INST.GetClusterCmdCtx().ConnectTargetNode(); // Not reply any message here, we will reply after the connection is established. // join_client->Clear(); } -void PRaft::InitializeNodeBeforeAdd(PClient* client, PClient* join_client, const std::string& reply) { +void Raft::InitializeNodeBeforeAdd(PClient* client, PClient* join_client, const std::string& reply) { std::string prefix = RAFT_GROUP_ID; std::string::size_type prefix_length = prefix.length(); std::string::size_type group_id_start = reply.find(prefix); @@ -442,7 +442,7 @@ void PRaft::InitializeNodeBeforeAdd(PClient* client, PClient* join_client, const if (group_id_end != std::string::npos) { std::string raft_group_id = reply.substr(group_id_start, group_id_end - group_id_start); // initialize the slave node - auto s = PRAFT.Init(raft_group_id, true); + auto s = RAFT_INST.Init(raft_group_id, true); if (!s.ok()) { join_client->SetRes(CmdRes::kErrOther, s.error_str()); join_client->SendPacket(); @@ -452,7 +452,7 @@ void PRaft::InitializeNodeBeforeAdd(PClient* client, PClient* join_client, const return; } - PRAFT.SendNodeAddRequest(client); + RAFT_INST.SendNodeAddRequest(client); } else { ERROR("Joined Raft cluster fail, because of invalid raft_group_id"); join_client->SetRes(CmdRes::kErrOther, "Invalid raft_group_id"); @@ -463,7 +463,7 @@ void PRaft::InitializeNodeBeforeAdd(PClient* client, PClient* join_client, const } } -int PRaft::ProcessClusterJoinCmdResponse(PClient* client, const char* start, int len) { +int Raft::ProcessClusterJoinCmdResponse(PClient* client, const char* start, int len) { assert(start); auto join_client = cluster_cmd_ctx_.GetClient(); if (!join_client) { @@ -473,7 +473,7 @@ int PRaft::ProcessClusterJoinCmdResponse(PClient* client, const char* start, int std::string reply(start, len); if (reply.find(OK_STR) != std::string::npos) { - INFO("Joined Raft cluster, node id: {}, group_id: {}", PRAFT.GetNodeID(), PRAFT.group_id_); + INFO("Joined Raft cluster, node id: {}, group_id: {}", RAFT_INST.GetNodeID(), RAFT_INST.group_id_); join_client->SetRes(CmdRes::kOK); join_client->SendPacket(); // join_client->Clear(); @@ -497,7 +497,7 @@ int PRaft::ProcessClusterJoinCmdResponse(PClient* client, const char* start, int return len; } -int PRaft::ProcessClusterRemoveCmdResponse(PClient* client, const char* start, int len) { +int Raft::ProcessClusterRemoveCmdResponse(PClient* client, const char* start, int len) { assert(start); auto remove_client = cluster_cmd_ctx_.GetClient(); if (!remove_client) { @@ -507,7 +507,7 @@ int PRaft::ProcessClusterRemoveCmdResponse(PClient* client, const char* start, i std::string reply(start, len); if (reply.find(OK_STR) != std::string::npos) { - INFO("Removed Raft cluster, node id: {}, group_id: {}", PRAFT.GetNodeID(), PRAFT.group_id_); + INFO("Removed Raft cluster, node id: {}, group_id: {}", RAFT_INST.GetNodeID(), RAFT_INST.group_id_); ShutDown(); Join(); Clear(); @@ -531,7 +531,7 @@ int PRaft::ProcessClusterRemoveCmdResponse(PClient* client, const char* start, i return len; } -butil::Status PRaft::AddPeer(const std::string& peer) { +butil::Status Raft::AddPeer(const std::string& peer) { if (!node_) { return ERROR_LOG_AND_STATUS("Node is not initialized"); } @@ -550,7 +550,7 @@ butil::Status PRaft::AddPeer(const std::string& peer) { return {0, "OK"}; } -butil::Status PRaft::RemovePeer(const std::string& peer) { +butil::Status Raft::RemovePeer(const std::string& peer) { if (!node_) { return ERROR_LOG_AND_STATUS("Node is not initialized"); } @@ -570,7 +570,7 @@ butil::Status PRaft::RemovePeer(const std::string& peer) { return {0, "OK"}; } -butil::Status PRaft::DoSnapshot(int64_t self_snapshot_index, bool is_sync) { +butil::Status Raft::DoSnapshot(int64_t self_snapshot_index, bool is_sync) { if (!node_) { return ERROR_LOG_AND_STATUS("Node is not initialized"); } @@ -586,7 +586,7 @@ butil::Status PRaft::DoSnapshot(int64_t self_snapshot_index, bool is_sync) { } } -void PRaft::OnClusterCmdConnectionFailed(const std::string& err) { +void Raft::OnClusterCmdConnectionFailed(const std::string& err) { auto cli = cluster_cmd_ctx_.GetClient(); if (cli) { cli->SetRes(CmdRes::kErrOther, "Failed to connect to cluster for join or remove, please check logs " + err); @@ -599,7 +599,7 @@ void PRaft::OnClusterCmdConnectionFailed(const std::string& err) { } // Shut this node and server down. -void PRaft::ShutDown() { +void Raft::ShutDown() { if (node_) { node_->shutdown(nullptr); } @@ -610,7 +610,7 @@ void PRaft::ShutDown() { } // Blocking this thread until the node is eventually down. -void PRaft::Join() { +void Raft::Join() { if (node_) { node_->join(); } @@ -620,7 +620,7 @@ void PRaft::Join() { } } -void PRaft::AppendLog(const Binlog& log, std::promise&& promise) { +void Raft::AppendLog(const Binlog& log, std::promise&& promise) { assert(node_); assert(node_->is_leader()); butil::IOBuf data; @@ -639,7 +639,7 @@ void PRaft::AppendLog(const Binlog& log, std::promise&& promise } // @braft::StateMachine -void PRaft::Clear() { +void Raft::Clear() { if (node_) { node_.reset(); } @@ -649,7 +649,7 @@ void PRaft::Clear() { } } -void PRaft::on_apply(braft::Iterator& iter) { +void Raft::on_apply(braft::Iterator& iter) { // A batch of tasks are committed, which must be processed through for (; iter.valid(); iter.next()) { auto done = iter.done(); @@ -679,12 +679,12 @@ void PRaft::on_apply(braft::Iterator& iter) { } } -void PRaft::on_snapshot_save(braft::SnapshotWriter* writer, braft::Closure* done) { +void Raft::on_snapshot_save(braft::SnapshotWriter* writer, braft::Closure* done) { assert(writer); brpc::ClosureGuard done_guard(done); } -int PRaft::on_snapshot_load(braft::SnapshotReader* reader) { +int Raft::on_snapshot_load(braft::SnapshotReader* reader) { CHECK(!IsLeader()) << "Leader is not supposed to load snapshot"; assert(reader); @@ -721,26 +721,26 @@ int PRaft::on_snapshot_load(braft::SnapshotReader* reader) { return 0; } -void PRaft::on_leader_start(int64_t term) { +void Raft::on_leader_start(int64_t term) { leader_term_.store(term, butil::memory_order_release); LOG(INFO) << "Node becomes leader, term : " << term; } -void PRaft::on_leader_stop(const butil::Status& status) { +void Raft::on_leader_stop(const butil::Status& status) { leader_term_.store(-1, butil::memory_order_release); LOG(INFO) << "Node stepped down : " << status; } -void PRaft::on_shutdown() { LOG(INFO) << "This node is down"; } +void Raft::on_shutdown() { LOG(INFO) << "This node is down"; } -void PRaft::on_error(const ::braft::Error& e) { LOG(ERROR) << "Met raft error " << e; } +void Raft::on_error(const ::braft::Error& e) { LOG(ERROR) << "Met raft error " << e; } -void PRaft::on_configuration_committed(const ::braft::Configuration& conf) { +void Raft::on_configuration_committed(const ::braft::Configuration& conf) { LOG(INFO) << "Configuration of this group is " << conf; } -void PRaft::on_stop_following(const ::braft::LeaderChangeContext& ctx) { LOG(INFO) << "Node stops following " << ctx; } +void Raft::on_stop_following(const ::braft::LeaderChangeContext& ctx) { LOG(INFO) << "Node stops following " << ctx; } -void PRaft::on_start_following(const ::braft::LeaderChangeContext& ctx) { LOG(INFO) << "Node start following " << ctx; } +void Raft::on_start_following(const ::braft::LeaderChangeContext& ctx) { LOG(INFO) << "Node start following " << ctx; } } // namespace kiwi diff --git a/src/praft/praft.h b/src/raft/raft.h similarity index 96% rename from src/praft/praft.h rename to src/raft/raft.h index c9f7bff3..3ba8392c 100644 --- a/src/praft/praft.h +++ b/src/raft/raft.h @@ -34,7 +34,7 @@ namespace kiwi { #define RAFT_GROUP_ID "raft_group_id:" #define NOT_LEADER "Not leader" -#define PRAFT PRaft::Instance() +#define RAFT_INST KRaft::Instance() // class EventLoop; class Binlog; @@ -46,7 +46,7 @@ enum ClusterCmdType { }; class ClusterCmdContext { - friend class PRaft; + friend class KRaft; public: ClusterCmdContext() = default; @@ -86,18 +86,17 @@ class PRaftWriteDoneClosure : public braft::Closure { delete this; } void SetStatus(rocksdb::Status status) { result_ = std::move(status); } - private: std::promise promise_; rocksdb::Status result_{rocksdb::Status::Aborted("Unknown error")}; }; -class PRaft : public braft::StateMachine { +class KRaft : public braft::StateMachine { public: - PRaft() = default; - ~PRaft() override = default; + KRaft() = default; + ~KRaft() override = default; - static PRaft& Instance(); + static KRaft& Instance(); //===--------------------------------------------------------------------===// // Braft API diff --git a/src/praft/praft.proto b/src/raft/raft.proto similarity index 100% rename from src/praft/praft.proto rename to src/raft/raft.proto diff --git a/src/praft/praft_service.h b/src/raft/raft_service.h similarity index 86% rename from src/praft/praft_service.h rename to src/raft/raft_service.h index fa776fcf..ccacbc8a 100644 --- a/src/praft/praft_service.h +++ b/src/raft/raft_service.h @@ -11,16 +11,16 @@ namespace kiwi { -class PRaft; +class Raft; class DummyServiceImpl : public DummyService { public: - explicit DummyServiceImpl(PRaft* praft) : praft_(praft) {} + explicit DummyServiceImpl(Raft* praft) : praft_(praft) {} void DummyMethod(::google::protobuf::RpcController* controller, const ::kiwi::DummyRequest* request, ::kiwi::DummyResponse* response, ::google::protobuf::Closure* done) override {} private: - PRaft* praft_ = nullptr; + Raft* praft_ = nullptr; }; } // namespace kiwi diff --git a/src/praft/psnapshot.cc b/src/raft/snapshot.cc similarity index 91% rename from src/praft/psnapshot.cc rename to src/raft/snapshot.cc index c8ee4f3d..3d832b62 100644 --- a/src/praft/psnapshot.cc +++ b/src/raft/snapshot.cc @@ -6,9 +6,9 @@ */ // -// psnapshot.cc +// snapshot.cc -#include "psnapshot.h" +#include "snapshot.h" #include "braft/local_file_meta.pb.h" #include "braft/snapshot.h" @@ -18,12 +18,12 @@ #include "pstd/pstd_string.h" #include "config.h" -#include "praft.h" +#include "raft.h" #include "store.h" namespace kiwi { -braft::FileAdaptor* PPosixFileSystemAdaptor::open(const std::string& path, int oflag, +braft::FileAdaptor* PosixFileSystemAdaptor::open(const std::string& path, int oflag, const ::google::protobuf::Message* file_meta, butil::File::Error* e) { if ((oflag & IS_RDONLY) == 0) { // This is a read operation bool snapshots_exists = false; @@ -55,7 +55,7 @@ braft::FileAdaptor* PPosixFileSystemAdaptor::open(const std::string& path, int o for (const auto& entry : std::filesystem::directory_iterator(snapshot_path)) { std::string filename = entry.path().filename().string(); if (entry.is_regular_file() || entry.is_directory()) { - if (filename != "." && filename != ".." && filename.find(PRAFT_SNAPSHOT_META_FILE) == std::string::npos) { + if (filename != "." && filename != ".." && filename.find(RAFT_INST_SNAPSHOT_META_FILE) == std::string::npos) { // If the path directory contains files other than raft_snapshot_meta, snapshots have been generated snapshots_exists = true; break; @@ -69,7 +69,7 @@ braft::FileAdaptor* PPosixFileSystemAdaptor::open(const std::string& path, int o assert(db_id >= 0); braft::LocalSnapshotMetaTable snapshot_meta_memtable; - std::string meta_path = snapshot_path + "/" PRAFT_SNAPSHOT_META_FILE; + std::string meta_path = snapshot_path + "/" RAFT_INST_SNAPSHOT_META_FILE; INFO("start to generate snapshot in path {}", snapshot_path); braft::FileSystemAdaptor* fs = braft::default_file_system(); assert(fs); @@ -83,7 +83,7 @@ braft::FileAdaptor* PPosixFileSystemAdaptor::open(const std::string& path, int o auto& new_meta = const_cast(snapshot_meta_memtable.meta()); auto last_log_index = PSTORE.GetBackend(db_id)->GetStorage()->GetSmallestFlushedLogIndex(); new_meta.set_last_included_index(last_log_index); - auto last_log_term = PRAFT.GetTerm(last_log_index); + auto last_log_term = RAFT_INST.GetTerm(last_log_index); new_meta.set_last_included_term(last_log_term); INFO("Succeed to fix db_{} snapshot meta: {}, {}", db_id, last_log_index, last_log_term); @@ -100,7 +100,7 @@ braft::FileAdaptor* PPosixFileSystemAdaptor::open(const std::string& path, int o return braft::PosixFileSystemAdaptor::open(path, oflag, file_meta, e); } -void PPosixFileSystemAdaptor::AddAllFiles(const std::filesystem::path& dir, +void PosixFileSystemAdaptor::AddAllFiles(const std::filesystem::path& dir, braft::LocalSnapshotMetaTable* snapshot_meta_memtable, const std::string& path) { assert(snapshot_meta_memtable); diff --git a/src/praft/psnapshot.h b/src/raft/snapshot.h similarity index 78% rename from src/praft/psnapshot.h rename to src/raft/snapshot.h index e96e2c3e..2ccc4509 100644 --- a/src/praft/psnapshot.h +++ b/src/raft/snapshot.h @@ -13,16 +13,16 @@ #include "braft/macros.h" #include "braft/snapshot.h" -#define PRAFT_SNAPSHOT_META_FILE "__raft_snapshot_meta" -#define PRAFT_SNAPSHOT_PATH "snapshot/snapshot_" +#define RAFT_INST_SNAPSHOT_META_FILE "__raft_snapshot_meta" +#define RAFT_INST_SNAPSHOT_PATH "snapshot/snapshot_" #define IS_RDONLY 0x01 namespace kiwi { -class PPosixFileSystemAdaptor : public braft::PosixFileSystemAdaptor { +class PosixFileSystemAdaptor : public braft::PosixFileSystemAdaptor { public: - PPosixFileSystemAdaptor() {} - ~PPosixFileSystemAdaptor() {} + PosixFileSystemAdaptor() {} + ~PosixFileSystemAdaptor() {} braft::FileAdaptor* open(const std::string& path, int oflag, const ::google::protobuf::Message* file_meta, butil::File::Error* e) override; diff --git a/src/storage/src/storage.cc b/src/storage/src/storage.cc index 4c50d144..db1b9d6e 100644 --- a/src/storage/src/storage.cc +++ b/src/storage/src/storage.cc @@ -27,7 +27,7 @@ #include "storage/storage.h" #include "storage/util.h" -#define PRAFT_SNAPSHOT_META_FILE "__raft_snapshot_meta" +#define RAFT_INST_SNAPSHOT_META_FILE "__raft_snapshot_meta" #define SST_FILE_EXTENSION ".sst" namespace storage { @@ -105,7 +105,7 @@ static std::string AppendSubDirectory(const std::string& db_path, int index) { static int RecursiveLinkAndCopy(const std::filesystem::path& source, const std::filesystem::path& destination) { if (std::filesystem::is_regular_file(source)) { - if (source.filename() == PRAFT_SNAPSHOT_META_FILE) { + if (source.filename() == RAFT_INST_SNAPSHOT_META_FILE) { return 0; } else if (source.extension() == SST_FILE_EXTENSION) { // Create a hard link From 192d741aabfa63d926f22b9b5b3b75983fd3c387 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Mon, 6 Jan 2025 18:07:27 +0800 Subject: [PATCH 05/35] pstd -> kstd --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 10 +- src/client.cc | 4 +- src/cmd_admin.cc | 20 +-- src/cmd_hash.cc | 14 +- src/cmd_keys.cc | 10 +- src/cmd_kv.cc | 58 +++---- src/cmd_list.cc | 20 +-- src/cmd_raft.cc | 16 +- src/cmd_set.cc | 10 +- src/cmd_table_manager.cc | 4 +- src/cmd_thread_pool.cc | 6 +- src/cmd_thread_pool.h | 4 +- src/cmd_thread_pool_worker.cc | 4 +- src/cmd_zset.cc | 62 ++++---- src/config.cc | 12 +- src/config.h | 2 +- src/config_parser.cc | 2 +- src/db.cc | 12 +- src/db.h | 4 +- src/kiwi.cc | 6 +- src/net/CMakeLists.txt | 2 +- src/pstd/tests/pstd_string_test.cc | 149 ------------------ src/raft/CMakeLists.txt | 2 +- src/raft/raft.cc | 22 +-- src/raft/raft.h | 16 +- src/raft/raft_service.h | 6 +- src/raft/snapshot.cc | 6 +- src/replication.cc | 8 +- src/replication.h | 4 +- src/resp/CMakeLists.txt | 4 +- src/resp/resp2_parse.cc | 8 +- src/resp/resp_encode.cc | 2 +- src/resp/resp_encode.h | 2 +- src/resp/tests/CMakeLists.txt | 4 +- src/{pstd => std}/CMakeLists.txt | 12 +- src/{pstd => std}/env.cc | 4 +- src/{pstd => std}/env.h | 14 +- src/{pstd => std}/kiwi_slot.cc | 0 src/{pstd => std}/kiwi_slot.h | 0 src/{pstd => std}/lock_free_ring_buffer.h | 0 src/{pstd => std}/lock_mgr.cc | 4 +- src/{pstd => std}/lock_mgr.h | 6 +- src/{pstd => std}/log.h | 0 src/{pstd => std}/memory_file.cc | 4 +- src/{pstd => std}/memory_file.h | 4 +- src/{pstd => std}/mutex.h | 8 +- src/{pstd => std}/mutex_impl.cc | 4 +- src/{pstd => std}/mutex_impl.h | 4 +- src/{pstd => std}/noncopyable.h | 4 +- src/{pstd => std}/scope_record_lock.cc | 4 +- src/{pstd => std}/scope_record_lock.h | 8 +- .../pstd_coding.cc => std/std_coding.cc} | 8 +- src/{pstd/pstd_coding.h => std/std_coding.h} | 6 +- src/{pstd/pstd_defer.h => std/std_defer.h} | 0 src/{pstd/pstd_hash.cc => std/std_hash.cc} | 6 +- src/{pstd/pstd_hash.h => std/std_hash.h} | 4 +- src/{pstd/pstd_mutex.h => std/std_mutex.h} | 10 +- src/{pstd/pstd_slice.h => std/std_slice.h} | 4 +- .../pstd_status.cc => std/std_status.cc} | 6 +- src/{pstd/pstd_status.h => std/std_status.h} | 6 +- .../pstd_string.cc => std/std_string.cc} | 10 +- src/{pstd/pstd_string.h => std/std_string.h} | 4 +- src/{pstd/pstd_util.cc => std/std_util.cc} | 6 +- src/{pstd/pstd_util.h => std/std_util.h} | 4 +- src/{pstd => std}/tests/CMakeLists.txt | 18 +-- .../tests/lock_free_ring_buffer_test.cc | 0 src/std/tests/std_string_test.cc | 149 ++++++++++++++++++ .../tests/std_util_test.cc} | 24 +-- src/{pstd => std}/thread_pool.cc | 4 +- src/{pstd => std}/thread_pool.h | 4 +- src/storage/CMakeLists.txt | 4 +- src/storage/include/storage/storage.h | 8 +- src/storage/src/base_data_key_format.h | 6 +- src/storage/src/base_data_value_format.h | 2 +- src/storage/src/base_filter.h | 2 +- src/storage/src/base_key_format.h | 2 +- src/storage/src/base_value_format.h | 4 +- src/storage/src/lists_data_key_format.h | 6 +- src/storage/src/lock_mgr.h | 4 +- src/storage/src/log_index.cc | 2 +- src/storage/src/lru_cache.h | 4 +- src/storage/src/mutex.h | 8 +- src/storage/src/mutex_impl.h | 4 +- src/storage/src/redis.cc | 2 +- src/storage/src/redis.h | 8 +- src/storage/src/redis_hashes.cc | 2 +- src/storage/src/redis_lists.cc | 2 +- src/storage/src/redis_sets.cc | 8 +- src/storage/src/redis_strings.cc | 2 +- src/storage/src/redis_zsets.cc | 2 +- src/storage/src/scope_record_lock.h | 6 +- src/storage/src/scope_snapshot.h | 4 +- src/storage/src/storage.cc | 30 ++-- src/storage/src/util.cc | 10 +- src/storage/tests/CMakeLists.txt | 2 +- src/storage/tests/flush_oldest_cf_test.cc | 8 +- src/storage/tests/hashes_test.cc | 10 +- src/storage/tests/keys_test.cc | 10 +- src/storage/tests/lists_test.cc | 10 +- src/storage/tests/log_index_test.cc | 8 +- src/storage/tests/sets_test.cc | 10 +- src/storage/tests/strings_test.cc | 10 +- src/storage/tests/zsets_test.cc | 10 +- src/store.cc | 8 +- 105 files changed, 544 insertions(+), 544 deletions(-) delete mode 100644 src/pstd/tests/pstd_string_test.cc rename src/{pstd => std}/CMakeLists.txt (67%) rename src/{pstd => std}/env.cc (99%) rename src/{pstd => std}/env.h (94%) rename src/{pstd => std}/kiwi_slot.cc (100%) rename src/{pstd => std}/kiwi_slot.h (100%) rename src/{pstd => std}/lock_free_ring_buffer.h (100%) rename src/{pstd => std}/lock_mgr.cc (99%) rename src/{pstd => std}/lock_mgr.h (94%) rename src/{pstd => std}/log.h (100%) rename src/{pstd => std}/memory_file.cc (99%) rename src/{pstd => std}/memory_file.h (99%) rename src/{pstd => std}/mutex.h (96%) rename src/{pstd => std}/mutex_impl.cc (98%) rename src/{pstd => std}/mutex_impl.h (91%) rename src/{pstd => std}/noncopyable.h (93%) rename src/{pstd => std}/scope_record_lock.cc (97%) rename src/{pstd => std}/scope_record_lock.h (89%) rename src/{pstd/pstd_coding.cc => std/std_coding.cc} (98%) rename src/{pstd/pstd_coding.h => std/std_coding.h} (98%) rename src/{pstd/pstd_defer.h => std/std_defer.h} (100%) rename src/{pstd/pstd_hash.cc => std/std_hash.cc} (99%) rename src/{pstd/pstd_hash.h => std/std_hash.h} (98%) rename src/{pstd/pstd_mutex.h => std/std_mutex.h} (90%) rename src/{pstd/pstd_slice.h => std/std_slice.h} (98%) rename src/{pstd/pstd_status.cc => std/std_status.cc} (97%) rename src/{pstd/pstd_status.h => std/std_status.h} (98%) rename src/{pstd/pstd_string.cc => std/std_string.cc} (99%) rename src/{pstd/pstd_string.h => std/std_string.h} (99%) rename src/{pstd/pstd_util.cc => std/std_util.cc} (91%) rename src/{pstd/pstd_util.h => std/std_util.h} (98%) rename src/{pstd => std}/tests/CMakeLists.txt (60%) rename src/{pstd => std}/tests/lock_free_ring_buffer_test.cc (100%) create mode 100644 src/std/tests/std_string_test.cc rename src/{pstd/tests/pstd_util_test.cc => std/tests/std_util_test.cc} (74%) rename src/{pstd => std}/thread_pool.cc (98%) rename src/{pstd => std}/thread_pool.h (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2eb2703c..5448d1c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -205,7 +205,7 @@ FILE(MAKE_DIRECTORY "${PROTO_OUTPUT_DIR}") #INCLUDE(cmake/protogen.cmake) -ADD_SUBDIRECTORY(src/pstd) +ADD_SUBDIRECTORY(src/std) ADD_SUBDIRECTORY(src/net) ADD_SUBDIRECTORY(src/raft) ADD_SUBDIRECTORY(src/storage) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8ed9dd30..f7383124 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,7 +26,7 @@ SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/) TARGET_INCLUDE_DIRECTORIES(kiwi PRIVATE ${PROJECT_SOURCE_DIR}/src - PRIVATE ${PROJECT_SOURCE_DIR}/src/pstd + PRIVATE ${PROJECT_SOURCE_DIR}/src/std PRIVATE ${PROJECT_SOURCE_DIR}/src/net PRIVATE ${PROJECT_SOURCE_DIR}/src/storage/include PRIVATE ${rocksdb_SOURCE_DIR}/ @@ -45,7 +45,7 @@ ADD_DEPENDENCIES(kiwi zlib rocksdb protobuf - pstd + kstd braft brpc storage @@ -63,7 +63,7 @@ TARGET_LINK_LIBRARIES(kiwi storage gflags spdlog - pstd + kstd braft brpc ssl @@ -72,8 +72,8 @@ TARGET_LINK_LIBRARIES(kiwi protobuf leveldb z - praft - praft_pb + raft + raft_pb binlog_pb resp "${LIB}" diff --git a/src/client.cc b/src/client.cc index 57eb5964..cf8b3620 100644 --- a/src/client.cc +++ b/src/client.cc @@ -17,9 +17,9 @@ #include "env.h" #include "kiwi.h" #include "raft/raft.h" -#include "pstd/log.h" -#include "pstd/pstd_string.h" #include "slow_log.h" +#include "std/log.h" +#include "std/std_string.h" namespace kiwi { diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 45f77ed4..b92ea2fe 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -31,13 +31,13 @@ #include "braft/raft.h" #include "log.h" -#include "pstd_string.h" +#include "std_string.h" #include "resp_encode.h" #include "rocksdb/version.h" #include "kiwi.h" #include "raft/raft.h" -#include "pstd/env.h" +#include "std/env.h" #include "client_map.h" #include "slow_log.h" @@ -90,14 +90,14 @@ void FlushdbCmd::DoCmd(PClient* client) { std::string db_path = kiwi::PConfig::GetInstance().db_path + std::to_string(currentDBIndex); std::string path_temp = db_path; path_temp.append("_deleting/"); - pstd::RenameFile(db_path, path_temp); + kstd::RenameFile(db_path, path_temp); auto s = PSTORE.GetBackend(currentDBIndex)->Open(); if (!s.ok()) { client->SetRes(CmdRes::kErrOther, "flushdb failed"); return; } - auto f = std::async(std::launch::async, [&path_temp]() { pstd::DeleteDir(path_temp); }); + auto f = std::async(std::launch::async, [&path_temp]() { kstd::DeleteDir(path_temp); }); client->SetRes(CmdRes::kOK); } @@ -113,11 +113,11 @@ void FlushallCmd::DoCmd(PClient* client) { std::string db_path = kiwi::PConfig::GetInstance().db_path + std::to_string(i); std::string path_temp = db_path; path_temp.append("_deleting/"); - pstd::RenameFile(db_path, path_temp); + kstd::RenameFile(db_path, path_temp); auto s = PSTORE.GetBackend(i)->Open(); assert(s.ok()); - auto f = std::async(std::launch::async, [&path_temp]() { pstd::DeleteDir(path_temp); }); + auto f = std::async(std::launch::async, [&path_temp]() { kstd::DeleteDir(path_temp); }); PSTORE.GetBackend(i).get()->UnLock(); } client->SetRes(CmdRes::kOK); @@ -200,7 +200,7 @@ bool HelloCmd::DoInitial(PClient* client) { } if (argc > 1) { - if (pstd::String2int(client->argv_[1].data(), client->argv_[1].size(), &resp_version) == 0) { + if (kstd::String2int(client->argv_[1].data(), client->argv_[1].size(), &resp_version) == 0) { client->SetRes(CmdRes::kErrOther, "Protocol version is not an integer or out of range"); return false; } @@ -587,8 +587,8 @@ bool SortCmd::DoInitial(PClient* client) { } else if (strcasecmp(client->argv_[i].data(), "alpha") == 0) { alpha_ = 1; } else if (strcasecmp(client->argv_[i].data(), "limit") == 0 && leftargs >= 2) { - if (pstd::String2int(client->argv_[i + 1], &offset_) == 0 || - pstd::String2int(client->argv_[i + 2], &count_) == 0) { + if (kstd::String2int(client->argv_[i + 1], &offset_) == 0 || + kstd::String2int(client->argv_[i + 2], &count_) == 0) { client->SetRes(CmdRes::kSyntaxErr); return false; } @@ -667,7 +667,7 @@ void SortCmd::DoCmd(PClient* client) { sort_ret[i].u = byval; } else { double double_byval; - if (pstd::String2d(byval, &double_byval)) { + if (kstd::String2d(byval, &double_byval)) { sort_ret[i].u = double_byval; } else { client->SetRes(CmdRes::kErrOther, "One or more scores can't be converted into double"); diff --git a/src/cmd_hash.cc b/src/cmd_hash.cc index 9e760e37..261803f8 100644 --- a/src/cmd_hash.cc +++ b/src/cmd_hash.cc @@ -11,8 +11,8 @@ #include -#include "pstd/pstd_string.h" #include "resp/resp_encode.h" +#include "std/std_string.h" #include "store.h" namespace kiwi { @@ -296,15 +296,15 @@ void HScanCmd::DoCmd(PClient* client) { int64_t cursor{}; int64_t count{10}; std::string pattern{"*"}; - if (pstd::String2int(argv[2], &cursor) == 0) { + if (kstd::String2int(argv[2], &cursor) == 0) { client->SetRes(CmdRes::kInvalidCursor, kCmdNameHScan); return; } for (size_t i = 3; i < argv.size(); i += 2) { - if (auto lower = pstd::StringToLower(argv[i]); kMatchSymbol == lower) { + if (auto lower = kstd::StringToLower(argv[i]); kMatchSymbol == lower) { pattern = argv[i + 1]; } else if (kCountSymbol == lower) { - if (pstd::String2int(argv[i + 1], &count) == 0) { + if (kstd::String2int(argv[i + 1], &count) == 0) { client->SetRes(CmdRes::kInvalidInt, kCmdNameHScan); return; } @@ -434,7 +434,7 @@ bool HIncrbyCmd::DoInitial(PClient* client) { void HIncrbyCmd::DoCmd(PClient* client) { int64_t int_by = 0; - if (!pstd::String2int(client->argv_[3].data(), client->argv_[3].size(), &int_by)) { + if (!kstd::String2int(client->argv_[3].data(), client->argv_[3].size(), &int_by)) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -471,7 +471,7 @@ void HRandFieldCmd::DoCmd(PClient* client) { bool with_values{false}; if (argv.size() > 2) { // redis checks the integer argument first and then the number of parameters - if (pstd::String2int(argv[2], &count) == 0) { + if (kstd::String2int(argv[2], &count) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -480,7 +480,7 @@ void HRandFieldCmd::DoCmd(PClient* client) { return; } if (argv.size() > 3) { - if (kWithValueString != pstd::StringToLower(argv[3])) { + if (kWithValueString != kstd::StringToLower(argv[3])) { client->SetRes(CmdRes::kSyntaxErr); return; } diff --git a/src/cmd_keys.cc b/src/cmd_keys.cc index cbc6b884..197b41b4 100644 --- a/src/cmd_keys.cc +++ b/src/cmd_keys.cc @@ -10,7 +10,7 @@ #include "cmd_keys.h" -#include "pstd_string.h" +#include "std_string.h" #include "store.h" @@ -85,7 +85,7 @@ bool ExpireCmd::DoInitial(PClient* client) { void ExpireCmd::DoCmd(PClient* client) { uint64_t sec = 0; - if (pstd::String2int(client->argv_[2], &sec) == 0) { + if (kstd::String2int(client->argv_[2], &sec) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -124,7 +124,7 @@ bool PExpireCmd::DoInitial(PClient* client) { void PExpireCmd::DoCmd(PClient* client) { int64_t msec = 0; - if (pstd::String2int(client->argv_[2], &msec) == 0) { + if (kstd::String2int(client->argv_[2], &msec) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -146,7 +146,7 @@ bool ExpireatCmd::DoInitial(PClient* client) { void ExpireatCmd::DoCmd(PClient* client) { int64_t time_stamp = 0; - if (pstd::String2int(client->argv_[2], &time_stamp) == 0) { + if (kstd::String2int(client->argv_[2], &time_stamp) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -169,7 +169,7 @@ bool PExpireatCmd::DoInitial(PClient* client) { // PExpireatCmd actually invoke Expireat void PExpireatCmd::DoCmd(PClient* client) { int64_t time_stamp_ms = 0; - if (pstd::String2int(client->argv_[2], &time_stamp_ms) == 0) { + if (kstd::String2int(client->argv_[2], &time_stamp_ms) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 993a3eac..29c4bdab 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -13,8 +13,8 @@ #include #include "common.h" #include "config.h" -#include "pstd_string.h" -#include "pstd_util.h" +#include "std_string.h" +#include "std_util.h" #include "store.h" namespace kiwi { @@ -68,7 +68,7 @@ bool SetCmd::DoInitial(PClient* client) { client->SetRes(CmdRes::kSyntaxErr); return false; } - if (pstd::String2int(argv_[index].data(), argv_[index].size(), &sec_) == 0) { + if (kstd::String2int(argv_[index].data(), argv_[index].size(), &sec_) == 0) { client->SetRes(CmdRes::kInvalidInt); return false; } @@ -239,8 +239,8 @@ void BitCountCmd::DoCmd(PClient* client) { } else { int64_t start_offset = 0; int64_t end_offset = 0; - if (pstd::String2int(client->argv_[2], &start_offset) == 0 || - pstd::String2int(client->argv_[3], &end_offset) == 0) { + if (kstd::String2int(client->argv_[2], &start_offset) == 0 || + kstd::String2int(client->argv_[3], &end_offset) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -307,10 +307,10 @@ BitOpCmd::BitOpCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryString) {} bool BitOpCmd::DoInitial(PClient* client) { - if (!(pstd::StringEqualCaseInsensitive(client->argv_[1], "and") || - pstd::StringEqualCaseInsensitive(client->argv_[1], "or") || - pstd::StringEqualCaseInsensitive(client->argv_[1], "not") || - pstd::StringEqualCaseInsensitive(client->argv_[1], "xor"))) { + if (!(kstd::StringEqualCaseInsensitive(client->argv_[1], "and") || + kstd::StringEqualCaseInsensitive(client->argv_[1], "or") || + kstd::StringEqualCaseInsensitive(client->argv_[1], "not") || + kstd::StringEqualCaseInsensitive(client->argv_[1], "xor"))) { client->SetRes(CmdRes::kSyntaxErr, "operation error"); return false; } @@ -328,16 +328,16 @@ void BitOpCmd::DoCmd(PClient* client) { storage::BitOpType op = storage::kBitOpDefault; if (!keys.empty()) { - if (pstd::StringEqualCaseInsensitive(client->argv_[1], "or")) { + if (kstd::StringEqualCaseInsensitive(client->argv_[1], "or")) { err = kPErrorOK; op = storage::kBitOpOr; - } else if (pstd::StringEqualCaseInsensitive(client->argv_[1], "xor")) { + } else if (kstd::StringEqualCaseInsensitive(client->argv_[1], "xor")) { err = kPErrorOK; op = storage::kBitOpXor; - } else if (pstd::StringEqualCaseInsensitive(client->argv_[1], "and")) { + } else if (kstd::StringEqualCaseInsensitive(client->argv_[1], "and")) { err = kPErrorOK; op = storage::kBitOpAnd; - } else if (pstd::StringEqualCaseInsensitive(client->argv_[1], "not")) { + } else if (kstd::StringEqualCaseInsensitive(client->argv_[1], "not")) { if (keys.size() == 1) { err = kPErrorOK; op = storage::kBitOpNot; @@ -389,7 +389,7 @@ SetExCmd::SetExCmd(const std::string& name, int16_t arity) bool SetExCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); int64_t sec = 0; - if (pstd::String2int(client->argv_[2], &sec) == 0) { + if (kstd::String2int(client->argv_[2], &sec) == 0) { client->SetRes(CmdRes::kInvalidInt); return false; } @@ -398,7 +398,7 @@ bool SetExCmd::DoInitial(PClient* client) { void SetExCmd::DoCmd(PClient* client) { int64_t sec = 0; - pstd::String2int(client->argv_[2], &sec); + kstd::String2int(client->argv_[2], &sec); storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Setex(client->Key(), client->argv_[3], sec); if (s.ok()) { @@ -416,7 +416,7 @@ PSetExCmd::PSetExCmd(const std::string& name, int16_t arity) bool PSetExCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); int64_t msec = 0; - if (pstd::String2int(client->argv_[2], &msec) == 0) { + if (kstd::String2int(client->argv_[2], &msec) == 0) { client->SetRes(CmdRes::kInvalidInt); return false; } @@ -425,7 +425,7 @@ bool PSetExCmd::DoInitial(PClient* client) { void PSetExCmd::DoCmd(PClient* client) { int64_t msec = 0; - pstd::String2int(client->argv_[2], &msec); + kstd::String2int(client->argv_[2], &msec); storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) ->GetStorage() ->Setex(client->Key(), client->argv_[3], static_cast(msec / 1000)); @@ -443,7 +443,7 @@ IncrbyCmd::IncrbyCmd(const std::string& name, int16_t arity) bool IncrbyCmd::DoInitial(PClient* client) { int64_t by_ = 0; - if (!(pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by_))) { + if (!(kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by_))) { client->SetRes(CmdRes::kInvalidInt); return false; } @@ -454,7 +454,7 @@ bool IncrbyCmd::DoInitial(PClient* client) { void IncrbyCmd::DoCmd(PClient* client) { int64_t ret = 0; int64_t by = 0; - pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by); + kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by); storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrby(client->Key(), by, &ret); if (s.ok()) { client->AppendInteger(ret); @@ -475,7 +475,7 @@ DecrbyCmd::DecrbyCmd(const std::string& name, int16_t arity) bool DecrbyCmd::DoInitial(PClient* client) { int64_t by = 0; - if (!(pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by))) { + if (!(kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by))) { client->SetRes(CmdRes::kInvalidInt); return false; } @@ -486,7 +486,7 @@ bool DecrbyCmd::DoInitial(PClient* client) { void DecrbyCmd::DoCmd(PClient* client) { int64_t ret = 0; int64_t by = 0; - if (pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by) == 0) { + if (kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -568,7 +568,7 @@ bool GetBitCmd::DoInitial(PClient* client) { void GetBitCmd::DoCmd(PClient* client) { int32_t bit_val = 0; long offset = 0; - if (!pstd::String2int(client->argv_[2].c_str(), client->argv_[2].size(), &offset)) { + if (!kstd::String2int(client->argv_[2].c_str(), client->argv_[2].size(), &offset)) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -590,8 +590,8 @@ bool GetRangeCmd::DoInitial(PClient* client) { int64_t start = 0; int64_t end = 0; // ERR value is not an integer or out of range - if (!(pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &start)) || - !(pstd::String2int(client->argv_[3].data(), client->argv_[3].size(), &end))) { + if (!(kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &start)) || + !(kstd::String2int(client->argv_[3].data(), client->argv_[3].size(), &end))) { client->SetRes(CmdRes::kInvalidInt); return false; } @@ -603,8 +603,8 @@ void GetRangeCmd::DoCmd(PClient* client) { PString ret; int64_t start = 0; int64_t end = 0; - pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &start); - pstd::String2int(client->argv_[3].data(), client->argv_[3].size(), &end); + kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &start); + kstd::String2int(client->argv_[3].data(), client->argv_[3].size(), &end); auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Getrange(client->Key(), start, end, &ret); if (!s.ok()) { if (s.IsNotFound()) { @@ -630,11 +630,11 @@ bool SetBitCmd::DoInitial(PClient* client) { void SetBitCmd::DoCmd(PClient* client) { long offset = 0; long on = 0; - if (pstd::String2int(client->argv_[2].c_str(), client->argv_[2].size(), &offset) == 0) { + if (kstd::String2int(client->argv_[2].c_str(), client->argv_[2].size(), &offset) == 0) { client->SetRes(CmdRes::kInvalidBitOffsetInt); return; } - if (pstd::String2int(client->argv_[3].c_str(), client->argv_[3].size(), &on) == 0) { + if (kstd::String2int(client->argv_[3].c_str(), client->argv_[3].size(), &on) == 0) { client->SetRes(CmdRes::kInvalidBitInt); return; } @@ -675,7 +675,7 @@ bool SetRangeCmd::DoInitial(PClient* client) { void SetRangeCmd::DoCmd(PClient* client) { int64_t offset = 0; - if (!(pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &offset))) { + if (!(kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &offset))) { client->SetRes(CmdRes::kInvalidInt); return; } diff --git a/src/cmd_list.cc b/src/cmd_list.cc index e1bd78c3..ed6cde24 100644 --- a/src/cmd_list.cc +++ b/src/cmd_list.cc @@ -8,7 +8,7 @@ */ #include "cmd_list.h" -#include "pstd_string.h" +#include "std_string.h" #include "store.h" namespace kiwi { @@ -183,7 +183,7 @@ void LRangeCmd::DoCmd(PClient* client) { std::vector ret; int64_t start_index = 0; int64_t end_index = 0; - if (pstd::String2int(client->argv_[2], &start_index) == 0 || pstd::String2int(client->argv_[3], &end_index) == 0) { + if (kstd::String2int(client->argv_[2], &start_index) == 0 || kstd::String2int(client->argv_[3], &end_index) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -211,7 +211,7 @@ bool LRemCmd::DoInitial(PClient* client) { void LRemCmd::DoCmd(PClient* client) { int64_t freq_ = 0; std::string count = client->argv_[2]; - if (pstd::String2int(count, &freq_) == 0) { + if (kstd::String2int(count, &freq_) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -240,7 +240,7 @@ void LTrimCmd::DoCmd(PClient* client) { int64_t start_index = 0; int64_t end_index = 0; - if (pstd::String2int(client->argv_[2], &start_index) == 0 || pstd::String2int(client->argv_[3], &end_index) == 0) { + if (kstd::String2int(client->argv_[2], &start_index) == 0 || kstd::String2int(client->argv_[3], &end_index) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -268,9 +268,9 @@ void LSetCmd::DoCmd(PClient* client) { // while strtol ensures that the string is within the range of long type const std::string index_str = client->argv_[2]; - if (pstd::IsValidNumber(index_str)) { + if (kstd::IsValidNumber(index_str)) { int64_t val = 0; - if (1 != pstd::String2int(index_str, &val)) { + if (1 != kstd::String2int(index_str, &val)) { client->SetRes(CmdRes::kErrOther, "lset cmd error"); // this will not happend in normal case return; } @@ -296,8 +296,8 @@ LInsertCmd::LInsertCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} bool LInsertCmd::DoInitial(PClient* client) { - if (!pstd::StringEqualCaseInsensitive(client->argv_[2], "BEFORE") && - !pstd::StringEqualCaseInsensitive(client->argv_[2], "AFTER")) { + if (!kstd::StringEqualCaseInsensitive(client->argv_[2], "BEFORE") && + !kstd::StringEqualCaseInsensitive(client->argv_[2], "AFTER")) { return false; } client->SetKey(client->argv_[1]); @@ -307,7 +307,7 @@ bool LInsertCmd::DoInitial(PClient* client) { void LInsertCmd::DoCmd(PClient* client) { int64_t ret = 0; storage ::BeforeOrAfter before_or_after = storage::Before; - if (pstd::StringEqualCaseInsensitive(client->argv_[2], "AFTER")) { + if (kstd::StringEqualCaseInsensitive(client->argv_[2], "AFTER")) { before_or_after = storage::After; } storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) @@ -335,7 +335,7 @@ bool LIndexCmd::DoInitial(PClient* client) { void LIndexCmd::DoCmd(PClient* client) { int64_t freq_ = 0; std::string count = client->argv_[2]; - if (pstd::String2int(count, &freq_) == 0) { + if (kstd::String2int(count, &freq_) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } diff --git a/src/cmd_raft.cc b/src/cmd_raft.cc index 3fee939e..165b27ff 100644 --- a/src/cmd_raft.cc +++ b/src/cmd_raft.cc @@ -18,8 +18,8 @@ // #include "net/event_loop.h" #include "raft/raft.h" -#include "pstd/log.h" -#include "pstd/pstd_string.h" +#include "std/log.h" +#include "std/std_string.h" #include "client.h" #include "config.h" @@ -33,7 +33,7 @@ RaftNodeCmd::RaftNodeCmd(const std::string& name, int16_t arity) bool RaftNodeCmd::DoInitial(PClient* client) { auto cmd = client->argv_[1]; - pstd::StringToUpper(cmd); + kstd::StringToUpper(cmd); if (cmd != kAddCmd && cmd != kRemoveCmd && cmd != kDoSnapshot) { client->SetRes(CmdRes::kErrOther, "RAFT.NODE supports ADD / REMOVE / DOSNAPSHOT only"); @@ -44,7 +44,7 @@ bool RaftNodeCmd::DoInitial(PClient* client) { void RaftNodeCmd::DoCmd(PClient* client) { auto cmd = client->argv_[1]; - pstd::StringToUpper(cmd); + kstd::StringToUpper(cmd); if (cmd == kAddCmd) { DoCmdAdd(client); } else if (cmd == kRemoveCmd) { @@ -140,7 +140,7 @@ RaftClusterCmd::RaftClusterCmd(const std::string& name, int16_t arity) bool RaftClusterCmd::DoInitial(PClient* client) { auto cmd = client->argv_[1]; - pstd::StringToUpper(cmd); + kstd::StringToUpper(cmd); if (cmd != kInitCmd && cmd != kJoinCmd) { client->SetRes(CmdRes::kErrOther, "RAFT.CLUSTER supports INIT/JOIN only"); return false; @@ -154,7 +154,7 @@ void RaftClusterCmd::DoCmd(PClient* client) { } auto cmd = client->argv_[1]; - pstd::StringToUpper(cmd); + kstd::StringToUpper(cmd); if (cmd == kInitCmd) { DoCmdInit(client); } else { @@ -175,7 +175,7 @@ void RaftClusterCmd::DoCmdInit(PClient* client) { "Cluster id must be " + std::to_string(RAFT_GROUPID_LEN) + " characters"); } } else { - cluster_id = pstd::RandomHexChars(RAFT_GROUPID_LEN); + cluster_id = kstd::RandomHexChars(RAFT_GROUPID_LEN); } auto s = RAFT_INST.Init(cluster_id, false); if (!s.ok()) { @@ -191,7 +191,7 @@ static inline std::optional> GetIpAndPortFromEnd } int32_t ret = 0; - pstd::String2int(endpoint.substr(pos + 1), &ret); + kstd::String2int(endpoint.substr(pos + 1), &ret); return {{endpoint.substr(0, pos), ret}}; } diff --git a/src/cmd_set.cc b/src/cmd_set.cc index 70bcc331..3760f343 100644 --- a/src/cmd_set.cc +++ b/src/cmd_set.cc @@ -10,7 +10,7 @@ #include "cmd_set.h" #include #include -#include "pstd/pstd_string.h" +#include "std/std_string.h" #include "store.h" namespace kiwi { @@ -294,7 +294,7 @@ void SPopCmd::DoCmd(PClient* client) { } else if ((client->argv_.size()) == 3) { std::vector delete_members; int64_t cnt = 1; - if (client->argv_[2].find(".") != std::string::npos || !pstd::String2int(client->argv_[2], &cnt)) { + if (client->argv_[2].find(".") != std::string::npos || !kstd::String2int(client->argv_[2], &cnt)) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -405,15 +405,15 @@ void SScanCmd::DoCmd(PClient* client) { int64_t cursor = 0; int64_t count = 10; std::string pattern{"*"}; - if (pstd::String2int(argv[2], &cursor) == 0) { + if (kstd::String2int(argv[2], &cursor) == 0) { client->SetRes(CmdRes::kInvalidCursor, kCmdNameSScan); return; } for (size_t i = 3; i < argv.size(); i += 2) { - if (auto lower = pstd::StringToLower(argv[i]); kMatchSymbol == lower) { + if (auto lower = kstd::StringToLower(argv[i]); kMatchSymbol == lower) { pattern = argv[i + 1]; } else if (kCountSymbol == lower) { - if (pstd::String2int(argv[i + 1], &count) == 0) { + if (kstd::String2int(argv[i + 1], &count) == 0) { client->SetRes(CmdRes::kInvalidInt, kCmdNameSScan); return; } diff --git a/src/cmd_table_manager.cc b/src/cmd_table_manager.cc index af726297..c245581a 100644 --- a/src/cmd_table_manager.cc +++ b/src/cmd_table_manager.cc @@ -19,7 +19,7 @@ #include "cmd_raft.h" #include "cmd_set.h" #include "cmd_zset.h" -#include "pstd_string.h" +#include "std_string.h" namespace kiwi { @@ -213,7 +213,7 @@ std::pair CmdTableManager::GetCommand(const std::string& cmdNa if (client->argv_.size() < 2) { return {nullptr, CmdRes::kWrongNum}; } - return {cmd->second->GetSubCmd(pstd::StringToLower(client->argv_[1])), CmdRes::kUnknownSubCmd}; + return {cmd->second->GetSubCmd(kstd::StringToLower(client->argv_[1])), CmdRes::kUnknownSubCmd}; } return {cmd->second.get(), CmdRes::kOK}; } diff --git a/src/cmd_thread_pool.cc b/src/cmd_thread_pool.cc index b2fe96f3..25318228 100644 --- a/src/cmd_thread_pool.cc +++ b/src/cmd_thread_pool.cc @@ -15,16 +15,16 @@ namespace kiwi { CmdThreadPool::CmdThreadPool(std::string name) : name_(std::move(name)) {} -pstd::Status CmdThreadPool::Init(int fast_thread, int slow_thread, std::string name) { +kstd::Status CmdThreadPool::Init(int fast_thread, int slow_thread, std::string name) { if (fast_thread <= 0) { - return pstd::Status::InvalidArgument("thread num must be positive"); + return kstd::Status::InvalidArgument("thread num must be positive"); } name_ = std::move(name); fast_thread_num_ = fast_thread; slow_thread_num_ = slow_thread; threads_.reserve(fast_thread_num_ + slow_thread_num_); workers_.reserve(fast_thread_num_ + slow_thread_num_); - return pstd::Status::OK(); + return kstd::Status::OK(); } void CmdThreadPool::Start() { diff --git a/src/cmd_thread_pool.h b/src/cmd_thread_pool.h index 1217b0ac..20281092 100644 --- a/src/cmd_thread_pool.h +++ b/src/cmd_thread_pool.h @@ -16,7 +16,7 @@ #include #include #include "base_cmd.h" -#include "pstd/pstd_status.h" +#include "std/std_status.h" namespace kiwi { @@ -56,7 +56,7 @@ class CmdThreadPool { explicit CmdThreadPool(std::string name); - pstd::Status Init(int fast_thread, int slow_thread, std::string name); + kstd::Status Init(int fast_thread, int slow_thread, std::string name); // start the thread pool void Start(); diff --git a/src/cmd_thread_pool_worker.cc b/src/cmd_thread_pool_worker.cc index 62429928..d2d057fb 100644 --- a/src/cmd_thread_pool_worker.cc +++ b/src/cmd_thread_pool_worker.cc @@ -11,7 +11,7 @@ #include "client.h" #include "kiwi.h" #include "log.h" -#include "pstd_string.h" +#include "std_string.h" namespace kiwi { @@ -27,7 +27,7 @@ void CmdWorkThreadPoolWorker::Work() { if (param.empty()) { continue; } - task->Client()->SetCmdName(pstd::StringToLower(param[0])); + task->Client()->SetCmdName(kstd::StringToLower(param[0])); task->Client()->SetArgv(param); auto [cmdPtr, ret] = cmd_table_manager_.GetCommand(param[0], task->Client().get()); diff --git a/src/cmd_zset.cc b/src/cmd_zset.cc index 523ca74f..b635fe59 100644 --- a/src/cmd_zset.cc +++ b/src/cmd_zset.cc @@ -11,7 +11,7 @@ #include -#include "pstd/pstd_string.h" +#include "std/std_string.h" #include "store.h" namespace kiwi { @@ -32,7 +32,7 @@ int32_t DoScoreStrRange(std::string begin_score, std::string end_score, bool* le *min_score = storage::ZSET_SCORE_MIN; } else if (begin_score == "inf" || begin_score == "+inf") { *min_score = storage::ZSET_SCORE_MAX; - } else if (pstd::String2d(begin_score.data(), begin_score.size(), min_score) == 0) { + } else if (kstd::String2d(begin_score.data(), begin_score.size(), min_score) == 0) { return -1; } @@ -44,7 +44,7 @@ int32_t DoScoreStrRange(std::string begin_score, std::string end_score, bool* le *max_score = storage::ZSET_SCORE_MAX; } else if (end_score == "-inf") { *max_score = storage::ZSET_SCORE_MIN; - } else if (pstd::String2d(end_score.data(), end_score.size(), max_score) == 0) { + } else if (kstd::String2d(end_score.data(), end_score.size(), max_score) == 0) { return -1; } return 0; @@ -102,7 +102,7 @@ void ZAddCmd::DoCmd(PClient* client) { double score = 0.0; size_t index = 2; for (; index < argc; index += 2) { - if (pstd::String2d(client->argv_[index].data(), client->argv_[index].size(), &score) == 0) { + if (kstd::String2d(client->argv_[index].data(), client->argv_[index].size(), &score) == 0) { client->SetRes(CmdRes::kInvalidFloat); return; } @@ -137,7 +137,7 @@ bool ZPopMinCmd::DoInitial(PClient* client) { void ZPopMinCmd::DoCmd(PClient* client) { int32_t count = 1; if (client->argv_.size() == 3) { - if (pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &count) == 0) { + if (kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &count) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -152,7 +152,7 @@ void ZPopMinCmd::DoCmd(PClient* client) { client->AppendArrayLen(static_cast(score_members.size()) * 2); for (auto& score_member : score_members) { client->AppendString(score_member.member); - len = pstd::D2string(buf, sizeof(buf), score_member.score); + len = kstd::D2string(buf, sizeof(buf), score_member.score); client->AppendString(buf, len); } } else if (s.IsInvalidArgument()) { @@ -178,7 +178,7 @@ bool ZPopMaxCmd::DoInitial(PClient* client) { void ZPopMaxCmd::DoCmd(PClient* client) { int32_t count = 1; if (client->argv_.size() == 3) { - if (pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &count) == 0) { + if (kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &count) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -193,7 +193,7 @@ void ZPopMaxCmd::DoCmd(PClient* client) { client->AppendArrayLen(static_cast(score_members.size()) * 2); for (auto& score_member : score_members) { client->AppendString(score_member.member); - len = pstd::D2string(buf, sizeof(buf), score_member.score); + len = kstd::D2string(buf, sizeof(buf), score_member.score); client->AppendString(buf, len); } } else if (s.IsInvalidArgument()) { @@ -211,7 +211,7 @@ ZsetUIstoreParentCmd::ZsetUIstoreParentCmd(const std::string& name, int16_t arit bool ZsetUIstoreParentCmd::DoInitial(PClient* client) { auto argv_ = client->argv_; dest_key_ = argv_[1]; - if (pstd::String2int(argv_[2].data(), argv_[2].size(), &num_keys_) == 0) { + if (kstd::String2int(argv_[2].data(), argv_[2].size(), &num_keys_) == 0) { client->SetRes(CmdRes::kInvalidInt); return false; } @@ -237,7 +237,7 @@ bool ZsetUIstoreParentCmd::DoInitial(PClient* client) { double weight; auto base = index; for (; index < base + num_keys_; index++) { - if (pstd::String2d(argv_[index].data(), argv_[index].size(), &weight) == 0) { + if (kstd::String2d(argv_[index].data(), argv_[index].size(), &weight) == 0) { client->SetRes(CmdRes::kErrOther, "weight value is not a float"); return false; } @@ -325,11 +325,11 @@ void ZRevrangeCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kSyntaxErr); return; } - if (pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &start) == 0) { + if (kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &start) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } - if (pstd::String2int(client->argv_[3].data(), client->argv_[3].size(), &stop) == 0) { + if (kstd::String2int(client->argv_[3].data(), client->argv_[3].size(), &stop) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -345,7 +345,7 @@ void ZRevrangeCmd::DoCmd(PClient* client) { client->AppendArrayLen(score_members.size() * 2); for (const auto& sm : score_members) { client->AppendString(sm.member); - len = pstd::D2string(buf, sizeof(buf), sm.score); + len = kstd::D2string(buf, sizeof(buf), sm.score); client->AppendString(buf, len); } } else { @@ -390,12 +390,12 @@ void ZRangebyscoreCmd::DoCmd(PClient* client) { return; } index++; - if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &offset) == 0) { + if (kstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &offset) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } index++; - if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &count) == 0) { + if (kstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &count) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -432,7 +432,7 @@ void ZRangebyscoreCmd::DoCmd(PClient* client) { client->AppendArrayLen(count * 2); for (; start < end; start++) { client->AppendString(score_members[start].member); - len = pstd::D2string(buf, sizeof(buf), score_members[start].score); + len = kstd::D2string(buf, sizeof(buf), score_members[start].score); client->AppendString(buf, len); } } else { @@ -456,11 +456,11 @@ void ZRemrangebyrankCmd::DoCmd(PClient* client) { int32_t start = 0; int32_t end = 0; - if (pstd::String2int(client->argv_[2], &start) == 0) { + if (kstd::String2int(client->argv_[2], &start) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } - if (pstd::String2int(client->argv_[3], &end) == 0) { + if (kstd::String2int(client->argv_[3], &end) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -508,12 +508,12 @@ void ZRevrangebyscoreCmd::DoCmd(PClient* client) { return; } index++; - if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &offset) == 0) { + if (kstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &offset) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } index++; - if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &count) == 0) { + if (kstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &count) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -551,7 +551,7 @@ void ZRevrangebyscoreCmd::DoCmd(PClient* client) { client->AppendArrayLen(count * 2); for (; start < end; start++) { client->AppendString(score_members[start].member); - len = pstd::D2string(buf, sizeof(buf), score_members[start].score); + len = kstd::D2string(buf, sizeof(buf), score_members[start].score); client->AppendString(buf, len); } } else { @@ -622,12 +622,12 @@ void ZRangeCmd::DoCmd(PClient* client) { return; } index++; - if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &offset) == 0) { + if (kstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &offset) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } index++; - if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &count) == 0) { + if (kstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &count) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -717,7 +717,7 @@ void ZRangeCmd::DoCmd(PClient* client) { client->AppendArrayLen(count * 2); for (; m_start < m_end; m_start++) { client->AppendString(score_members[m_start].member); - len = pstd::D2string(buf, sizeof(buf), score_members[m_start].score); + len = kstd::D2string(buf, sizeof(buf), score_members[m_start].score); client->AppendString(buf, len); } } else { @@ -744,7 +744,7 @@ void ZScoreCmd::DoCmd(PClient* client) { s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZScore(client->Key(), client->argv_[2], &score); if (s.ok() || s.IsNotFound()) { char buf[32]; - int64_t len = pstd::D2string(buf, sizeof(buf), score); + int64_t len = kstd::D2string(buf, sizeof(buf), score); client->AppendString(buf, len); } else if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -772,11 +772,11 @@ void ZRangebylexCmd::DoCmd(PClient* client) { bool left_close = true; bool right_close = true; if (argc == 7 && strcasecmp(client->argv_[4].data(), "limit") == 0) { - if (pstd::String2int(client->argv_[5].data(), client->argv_[5].size(), &offset) == 0) { + if (kstd::String2int(client->argv_[5].data(), client->argv_[5].size(), &offset) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } - if (pstd::String2int(client->argv_[6].data(), client->argv_[6].size(), &count) == 0) { + if (kstd::String2int(client->argv_[6].data(), client->argv_[6].size(), &count) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -836,11 +836,11 @@ void ZRevrangebylexCmd::DoCmd(PClient* client) { bool left_close = true; bool right_close = true; if (argc == 7 && strcasecmp(client->argv_[4].data(), "limit") == 0) { - if (pstd::String2int(client->argv_[5].data(), client->argv_[5].size(), &offset) == 0) { + if (kstd::String2int(client->argv_[5].data(), client->argv_[5].size(), &offset) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } - if (pstd::String2int(client->argv_[6].data(), client->argv_[6].size(), &count) == 0) { + if (kstd::String2int(client->argv_[6].data(), client->argv_[6].size(), &count) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } @@ -955,7 +955,7 @@ bool ZIncrbyCmd::DoInitial(PClient* client) { void ZIncrbyCmd::DoCmd(PClient* client) { double by = .0f; double score = .0f; - if (pstd::String2d(client->argv_[2].data(), client->argv_[2].size(), &by) == 0) { + if (kstd::String2d(client->argv_[2].data(), client->argv_[2].size(), &by) == 0) { client->SetRes(CmdRes::kInvalidFloat); return; } @@ -965,7 +965,7 @@ void ZIncrbyCmd::DoCmd(PClient* client) { PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZIncrby(client->Key(), member, by, &score); if (s.ok()) { char buf[32]; - int64_t len = pstd::D2string(buf, sizeof(buf), score); + int64_t len = kstd::D2string(buf, sizeof(buf), score); client->AppendString(buf, len); } else if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); diff --git a/src/config.cc b/src/config.cc index 6633953a..e600bc59 100644 --- a/src/config.cc +++ b/src/config.cc @@ -12,7 +12,7 @@ #include #include "config.h" -#include "pstd/pstd_string.h" +#include "std/std_string.h" #include "store.h" namespace kiwi { @@ -38,15 +38,15 @@ static void EraseQuotes(std::string& str) { } static Status CheckYesNo(const std::string& value) { - if (!pstd::StringEqualCaseInsensitive(value, "yes") && !pstd::StringEqualCaseInsensitive(value, "no")) { + if (!kstd::StringEqualCaseInsensitive(value, "yes") && !kstd::StringEqualCaseInsensitive(value, "no")) { return Status::InvalidArgument("The value must be yes or no."); } return Status::OK(); } static Status CheckLogLevel(const std::string& value) { - if (!pstd::StringEqualCaseInsensitive(value, "debug") && !pstd::StringEqualCaseInsensitive(value, "verbose") && - !pstd::StringEqualCaseInsensitive(value, "notice") && !pstd::StringEqualCaseInsensitive(value, "warning")) { + if (!kstd::StringEqualCaseInsensitive(value, "debug") && !kstd::StringEqualCaseInsensitive(value, "verbose") && + !kstd::StringEqualCaseInsensitive(value, "notice") && !kstd::StringEqualCaseInsensitive(value, "warning")) { return Status::InvalidArgument("The value must be debug / verbose / notice / warning."); } return Status::OK(); @@ -88,7 +88,7 @@ Status StringValueArray::SetValue(const std::string& value) { } Status BoolValue::SetValue(const std::string& value) { - if (pstd::StringEqualCaseInsensitive(value, "yes")) { + if (kstd::StringEqualCaseInsensitive(value, "yes")) { *value_ = true; } else { *value_ = false; @@ -209,7 +209,7 @@ bool PConfig::LoadFromFile(const std::string& file_name) { void PConfig::Get(const std::string& key, std::vector* values) const { values->clear(); for (const auto& [k, v] : config_map_) { - if (key == "*" || pstd::StringMatch(key.c_str(), k.c_str(), 1)) { + if (key == "*" || kstd::StringMatch(key.c_str(), k.c_str(), 1)) { values->emplace_back(k); values->emplace_back(v->Value()); } diff --git a/src/config.h b/src/config.h index aa995f86..5bfbd717 100644 --- a/src/config.h +++ b/src/config.h @@ -80,7 +80,7 @@ class StringValueArray : public BaseValue { : BaseValue(key, std::move(check_func_ptr), rewritable), values_(value_ptr_vec), delimiter_(delimiter) {} ~StringValueArray() override = default; - std::string Value() const override { return pstd::StringConcat(values_, delimiter_); }; + std::string Value() const override { return kstd::StringConcat(values_, delimiter_); }; private: Status SetValue(const std::string& value) override; diff --git a/src/config_parser.cc b/src/config_parser.cc index 4f2cd07a..b2ad55f0 100644 --- a/src/config_parser.cc +++ b/src/config_parser.cc @@ -30,7 +30,7 @@ static size_t SkipBlank(const char* data, size_t len, size_t off) { } bool ConfigParser::Load(const char* FileName) { - pstd::InputMemoryFile file; + kstd::InputMemoryFile file; if (!file.Open(FileName)) { return false; // no such file } diff --git a/src/db.cc b/src/db.cc index 67e27486..2ff0938e 100644 --- a/src/db.cc +++ b/src/db.cc @@ -13,7 +13,7 @@ #include "config.h" #include "raft/raft.h" -#include "pstd/log.h" +#include "std/log.h" namespace kiwi { @@ -66,7 +66,7 @@ rocksdb::Status DB::Open() { void DB::CreateCheckpoint(const std::string& checkpoint_path, bool sync) { auto checkpoint_sub_path = checkpoint_path + '/' + std::to_string(db_index_); - if (0 != pstd::CreatePath(checkpoint_sub_path)) { + if (0 != kstd::CreatePath(checkpoint_sub_path)) { WARN("Create dir {} fail !", checkpoint_sub_path); return; } @@ -82,12 +82,12 @@ void DB::CreateCheckpoint(const std::string& checkpoint_path, bool sync) { void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[maybe_unused]]) { auto checkpoint_sub_path = checkpoint_path + '/' + std::to_string(db_index_); - if (0 != pstd::IsDir(checkpoint_sub_path)) { + if (0 != kstd::IsDir(checkpoint_sub_path)) { WARN("Checkpoint dir {} does not exist!", checkpoint_sub_path); return; } - if (0 != pstd::IsDir(db_path_)) { - if (0 != pstd::CreateDir(db_path_)) { + if (0 != kstd::IsDir(db_path_)) { + if (0 != kstd::CreateDir(db_path_)) { WARN("Create dir {} fail !", db_path_); return; } @@ -121,7 +121,7 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma r.AppendLog(log, std::move(promise)); }; storage_options.do_snapshot_function = - std::bind(&kiwi::KRaft::DoSnapshot, &kiwi::RAFT_INST, std::placeholders::_1, std::placeholders::_2); + std::bind(&kiwi::Raft::DoSnapshot, &kiwi::RAFT_INST, std::placeholders::_1, std::placeholders::_2); } if (auto s = storage_->Open(storage_options, db_path_); !s.ok()) { diff --git a/src/db.h b/src/db.h index 58c62f31..e7762478 100644 --- a/src/db.h +++ b/src/db.h @@ -13,8 +13,8 @@ #include #include -#include "pstd/log.h" -#include "pstd/noncopyable.h" +#include "std/log.h" +#include "std/noncopyable.h" #include "storage/storage.h" namespace kiwi { diff --git a/src/kiwi.cc b/src/kiwi.cc index 848dd9d4..e2e661bb 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -28,9 +28,9 @@ #include "kiwi_logo.h" #include "options.h" #include "raft/raft.h" -#include "pstd/log.h" -#include "pstd/pstd_util.h" #include "slow_log.h" +#include "std/log.h" +#include "std/std_util.h" #include "store.h" // g_kiwi is a global abstraction of the server-side process @@ -334,7 +334,7 @@ int main(int argc, char* argv[]) { daemonize(); } - pstd::InitRandom(); + kstd::InitRandom(); SignalSetup(); InitLogs(); InitLimit(); diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt index 5cf6490a..8f1e58d0 100644 --- a/src/net/CMakeLists.txt +++ b/src/net/CMakeLists.txt @@ -18,4 +18,4 @@ TARGET_INCLUDE_DIRECTORIES(net PRIVATE ${PSTD_INCLUDE_DIR} ) -TARGET_LINK_LIBRARIES(net pstd) +TARGET_LINK_LIBRARIES(net kstd) diff --git a/src/pstd/tests/pstd_string_test.cc b/src/pstd/tests/pstd_string_test.cc deleted file mode 100644 index 715c097f..00000000 --- a/src/pstd/tests/pstd_string_test.cc +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright (c) 2015-present, Arana/Kiwi Community. All rights reserved. -// This source code is licensed under the BSD-style license found in the -// LICENSE file in the root directory of this source tree. An additional grant -// of patent rights can be found in the PATENTS file in the same directory. - -// Copyright (c) 2011 The LevelDB Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. See the AUTHORS file for names of contributors. - -#include - -#include -#include "pstd/pstd_string.h" - -class StringTest : public ::testing::Test {}; - -TEST_F(StringTest, StringTrim) { - ASSERT_EQ(pstd::StringTrim(" computer "), "computer"); - ASSERT_EQ(pstd::StringTrim(" comp uter "), "comp uter"); - ASSERT_EQ(pstd::StringTrim(" \n computer \n ", "\n "), "computer"); - ASSERT_EQ(pstd::StringTrim("\n", "\r\n "), ""); -} - -TEST_F(StringTest, StringTrimLeft) { - ASSERT_EQ(pstd::StringTrimLeft(" computer"), "computer"); - ASSERT_EQ(pstd::StringTrimLeft(" computer "), "computer "); - ASSERT_EQ(pstd::StringTrimLeft(" comp uter "), "comp uter "); - ASSERT_EQ(pstd::StringTrimLeft(" comp uter"), "comp uter"); - ASSERT_EQ(pstd::StringTrimLeft(" \n computer \n ", "\n "), "computer \n "); - ASSERT_EQ(pstd::StringTrimLeft("\n", "\r\n "), ""); -} - -TEST_F(StringTest, StringTrimRight) { - ASSERT_EQ(pstd::StringTrimRight("computer "), "computer"); - ASSERT_EQ(pstd::StringTrimRight(" computer "), " computer"); - ASSERT_EQ(pstd::StringTrimRight(" comp uter "), " comp uter"); - ASSERT_EQ(pstd::StringTrimRight("comp uter "), "comp uter"); - ASSERT_EQ(pstd::StringTrimRight(" \n computer \n ", "\n "), " \n computer"); - ASSERT_EQ(pstd::StringTrimRight("\n", "\r\n "), ""); -} - -TEST_F(StringTest, ParseIpPort) { - std::string ip; - int port; - ASSERT_TRUE(pstd::ParseIpPortString("192.168.1.1:9221", ip, port)); - ASSERT_EQ(ip, "192.168.1.1"); - ASSERT_EQ(port, 9221); -} - -TEST_F(StringTest, test_string2ll) { - char buf[32]; - long long v; - - /* May not start with +. */ - strcpy(buf, "+1"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); - - /* Leading space. */ - strcpy(buf, " 1"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); - - /* Trailing space. */ - strcpy(buf, "1 "); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - - strcpy(buf, "-1"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, -1); - - strcpy(buf, "0"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, 0); - - strcpy(buf, "1"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, 1); - - strcpy(buf, "99"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, 99); - - strcpy(buf, "-99"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, -99); - - strcpy(buf, "-9223372036854775808"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, LLONG_MIN); - - strcpy(buf, "-9223372036854775809"); /* overflow */ - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); - - strcpy(buf, "9223372036854775807"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, LLONG_MAX); - - strcpy(buf, "9223372036854775808"); /* overflow */ - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); -} - -TEST_F(StringTest, test_string2l) { - char buf[32]; - long v; - - /* May not start with +. */ - strcpy(buf, "+1"); - ASSERT_EQ(pstd::String2int(buf, &v), 0); - - strcpy(buf, "-1"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, -1); - - strcpy(buf, "0"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, 0); - - strcpy(buf, "1"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, 1); - - strcpy(buf, "99"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, 99); - - strcpy(buf, "-99"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, -99); - -#if LONG_MAX != LLONG_MAX - strcpy(buf, "-2147483648"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, LONG_MIN); - - strcpy(buf, "-2147483649"); /* overflow */ - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); - - strcpy(buf, "2147483647"); - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 1); - ASSERT_EQ(v, LONG_MAX); - - strcpy(buf, "2147483648"); /* overflow */ - ASSERT_EQ(pstd::String2int(buf, strlen(buf), &v), 0); -#endif -} - -int main(int argc, char** argv) { - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/src/raft/CMakeLists.txt b/src/raft/CMakeLists.txt index 49eb275f..01177a91 100644 --- a/src/raft/CMakeLists.txt +++ b/src/raft/CMakeLists.txt @@ -50,5 +50,5 @@ ENDIF () SET(LIBRARY_OUTPUT_PATH ${PLIB_INSTALL_DIR}) ADD_DEPENDENCIES(raft protobuf raft_pb binlog_pb) -TARGET_LINK_LIBRARIES(raft net; dl; fmt; storage; pstd braft brpc ssl crypto zlib protobuf leveldb gflags rocksdb z ${RAFT_INST_LIB}) +TARGET_LINK_LIBRARIES(raft net; dl; fmt; storage; kstd braft brpc ssl crypto zlib protobuf leveldb gflags rocksdb z ${RAFT_INST_LIB}) SET_TARGET_PROPERTIES(raft PROPERTIES LINKER_LANGUAGE CXX) diff --git a/src/raft/raft.cc b/src/raft/raft.cc index 2214bf6e..14bdfb23 100644 --- a/src/raft/raft.cc +++ b/src/raft/raft.cc @@ -15,8 +15,8 @@ #include "brpc/server.h" #include "gflags/gflags.h" -#include "pstd/log.h" -#include "pstd/pstd_string.h" +#include "std/log.h" +#include "std/std_string.h" #include "binlog.pb.h" #include "config.h" @@ -157,7 +157,7 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { node_options_.fsm = this; node_options_.node_owns_fsm = false; node_options_.snapshot_interval_s = 0; - std::string prefix = "local://" + kiwi::PConfig::GetInstance().db_path + std::to_string(db_id_) + "/_praft"; + std::string prefix = "local://" + kiwi::PConfig::GetInstance().db_path + std::to_string(db_id_) + "/_raft"; node_options_.log_uri = prefix + "/log"; node_options_.raft_meta_uri = prefix + "/raft_meta"; node_options_.snapshot_uri = prefix + "/snapshot"; @@ -376,20 +376,20 @@ void Raft::CheckRocksDBConfiguration(PClient* client, PClient* join_client, cons std::string key = line.substr(0, pos); std::string value = line.substr(pos + 1); - if (key == DATABASES_NUM && pstd::String2int(value, &databases_num) == 0) { + if (key == DATABASES_NUM && kstd::String2int(value, &databases_num) == 0) { join_client->SetRes(CmdRes::kErrOther, "Config of databases_num invalid"); join_client->SendPacket(); // join_client->Clear(); // If the join fails, clear clusterContext and set it again by using the join command cluster_cmd_ctx_.Clear(); - } else if (key == ROCKSDB_NUM && pstd::String2int(value, &rocksdb_num) == 0) { + } else if (key == ROCKSDB_NUM && kstd::String2int(value, &rocksdb_num) == 0) { join_client->SetRes(CmdRes::kErrOther, "Config of rocksdb_num invalid"); join_client->SendPacket(); // join_client->Clear(); // If the join fails, clear clusterContext and set it again by using the join command cluster_cmd_ctx_.Clear(); } else if (key == ROCKSDB_VERSION) { - rockdb_version = pstd::StringTrimRight(value, "\r"); + rockdb_version = kstd::StringTrimRight(value, "\r"); } } } @@ -411,8 +411,8 @@ void Raft::CheckRocksDBConfiguration(PClient* client, PClient* join_client, cons void Raft::LeaderRedirection(PClient* join_client, const std::string& reply) { // Resolve the ip address of the leader - pstd::StringTrimLeft(reply, WRONG_LEADER); - pstd::StringTrim(reply); + kstd::StringTrimLeft(reply, WRONG_LEADER); + kstd::StringTrim(reply); braft::PeerId peerId; peerId.parse(reply); auto peer_ip = std::string(butil::ip2str(peerId.addr.ip).c_str()); @@ -625,7 +625,7 @@ void Raft::AppendLog(const Binlog& log, std::promise&& promise) assert(node_->is_leader()); butil::IOBuf data; butil::IOBufAsZeroCopyOutputStream wrapper(&data); - auto done = new PRaftWriteDoneClosure(std::move(promise)); + auto done = new RaftWriteDoneClosure(std::move(promise)); if (!log.SerializeToZeroCopyStream(&wrapper)) { done->SetStatus(rocksdb::Status::Incomplete("Failed to serialize binlog")); done->Run(); @@ -664,7 +664,7 @@ void Raft::on_apply(braft::Iterator& iter) { static constexpr std::string_view kMsg = "Failed to parse from protobuf when on_apply"; ERROR(kMsg); if (done) { // in leader - dynamic_cast(done)->SetStatus(rocksdb::Status::Incomplete(kMsg)); + dynamic_cast(done)->SetStatus(rocksdb::Status::Incomplete(kMsg)); } braft::run_closure_in_bthread(done_guard.release()); return; @@ -672,7 +672,7 @@ void Raft::on_apply(braft::Iterator& iter) { auto s = PSTORE.GetBackend(log.db_id())->GetStorage()->OnBinlogWrite(log, iter.index()); if (done) { // in leader - dynamic_cast(done)->SetStatus(s); + dynamic_cast(done)->SetStatus(s); } // _applied_index = iter.index(); // consider to maintain a member applied_idx braft::run_closure_in_bthread(done_guard.release()); diff --git a/src/raft/raft.h b/src/raft/raft.h index 3ba8392c..9a8262f7 100644 --- a/src/raft/raft.h +++ b/src/raft/raft.h @@ -34,7 +34,7 @@ namespace kiwi { #define RAFT_GROUP_ID "raft_group_id:" #define NOT_LEADER "Not leader" -#define RAFT_INST KRaft::Instance() +#define RAFT_INST Raft::Instance() // class EventLoop; class Binlog; @@ -46,7 +46,7 @@ enum ClusterCmdType { }; class ClusterCmdContext { - friend class KRaft; + friend class Raft; public: ClusterCmdContext() = default; @@ -77,9 +77,9 @@ class ClusterCmdContext { std::string peer_id_; }; -class PRaftWriteDoneClosure : public braft::Closure { +class RaftWriteDoneClosure : public braft::Closure { public: - explicit PRaftWriteDoneClosure(std::promise&& promise) : promise_(std::move(promise)) {} + explicit RaftWriteDoneClosure(std::promise&& promise) : promise_(std::move(promise)) {} void Run() override { promise_.set_value(result_); @@ -91,12 +91,12 @@ class PRaftWriteDoneClosure : public braft::Closure { rocksdb::Status result_{rocksdb::Status::Aborted("Unknown error")}; }; -class KRaft : public braft::StateMachine { +class Raft : public braft::StateMachine { public: - KRaft() = default; - ~KRaft() override = default; + Raft() = default; + ~Raft() override = default; - static KRaft& Instance(); + static Raft& Instance(); //===--------------------------------------------------------------------===// // Braft API diff --git a/src/raft/raft_service.h b/src/raft/raft_service.h index ccacbc8a..62ead86c 100644 --- a/src/raft/raft_service.h +++ b/src/raft/raft_service.h @@ -7,7 +7,7 @@ #pragma once -#include "praft.pb.h" +#include "raft.pb.h" namespace kiwi { @@ -15,12 +15,12 @@ class Raft; class DummyServiceImpl : public DummyService { public: - explicit DummyServiceImpl(Raft* praft) : praft_(praft) {} + explicit DummyServiceImpl(Raft* raft) : raft_(raft) {} void DummyMethod(::google::protobuf::RpcController* controller, const ::kiwi::DummyRequest* request, ::kiwi::DummyResponse* response, ::google::protobuf::Closure* done) override {} private: - Raft* praft_ = nullptr; + Raft* raft_ = nullptr; }; } // namespace kiwi diff --git a/src/raft/snapshot.cc b/src/raft/snapshot.cc index 3d832b62..4e6467fc 100644 --- a/src/raft/snapshot.cc +++ b/src/raft/snapshot.cc @@ -14,8 +14,8 @@ #include "braft/snapshot.h" #include "butil/files/file_path.h" -#include "pstd/log.h" -#include "pstd/pstd_string.h" +#include "std/log.h" +#include "std/std_string.h" #include "config.h" #include "raft.h" @@ -38,7 +38,7 @@ braft::FileAdaptor* PosixFileSystemAdaptor::open(const std::string& path, int of for (const auto& component : components) { snapshot_path += component + "/"; - if (is_find_db && pstd::String2int(component, &db_id)) { + if (is_find_db && kstd::String2int(component, &db_id)) { is_find_db = false; } diff --git a/src/replication.cc b/src/replication.cc index 4d9ee87e..e7a8ee7e 100644 --- a/src/replication.cc +++ b/src/replication.cc @@ -15,8 +15,8 @@ #include "config.h" #include "kiwi.h" #include "log.h" -#include "pstd/pstd_string.h" #include "replication.h" +#include "std/std_string.h" namespace kiwi { @@ -50,7 +50,7 @@ bool PReplication::HasAnyWaitingBgsave() const { void PReplication::OnRdbSaveDone() { bgsaving_ = false; - pstd::InputMemoryFile rdb; + kstd::InputMemoryFile rdb; // send rdb to slaves that wait rdb end, set state for (auto& wptr : slaves_) { @@ -334,7 +334,7 @@ PError replconf(const std::vector& params, UnboundedBuffer* reply) { for (size_t i = 1; i < params.size(); i += 2) { if (strncasecmp(params[i].c_str(), "listening-port", 14) == 0) { long port; - if (!pstd::String2int(params[i + 1].c_str(), params[i + 1].size(), &port)) { + if (!kstd::String2int(params[i + 1].c_str(), params[i + 1].size(), &port)) { ReplyError(kPErrorParam, reply); return kPErrorParam; } @@ -425,7 +425,7 @@ PError slaveof(const std::vector& params, UnboundedBuffer* reply) { PREPL.SetMasterAddr(nullptr, 0); } else { long tmpPort = 0; - pstd::String2int(params[2].c_str(), params[2].size(), &tmpPort); + kstd::String2int(params[2].c_str(), params[2].size(), &tmpPort); uint16_t port = static_cast(tmpPort); net::SocketAddr reqMaster(params[1].c_str(), port); diff --git a/src/replication.h b/src/replication.h index a5b59ec4..33842daf 100644 --- a/src/replication.h +++ b/src/replication.h @@ -17,7 +17,7 @@ #include "common.h" #include "net/socket_addr.h" -#include "pstd/memory_file.h" +#include "std/memory_file.h" namespace kiwi { @@ -155,7 +155,7 @@ class PReplication { // slave side PMasterInfo masterInfo_; std::weak_ptr master_; - pstd::OutputMemoryFile rdb_; + kstd::OutputMemoryFile rdb_; // Callback function that failed to connect to the master node std::function on_fail_ = nullptr; diff --git a/src/resp/CMakeLists.txt b/src/resp/CMakeLists.txt index 2e0995d4..6b4b2bdd 100644 --- a/src/resp/CMakeLists.txt +++ b/src/resp/CMakeLists.txt @@ -17,7 +17,7 @@ SET(RESP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE PATH "resp include direct ADD_LIBRARY(resp ${RESP_SRC}) -TARGET_LINK_LIBRARIES(resp pstd fmt) +TARGET_LINK_LIBRARIES(resp kstd fmt) ADD_SUBDIRECTORY(tests) @@ -27,4 +27,4 @@ TARGET_INCLUDE_DIRECTORIES(resp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) -ADD_DEPENDENCIES(resp pstd fmt) +ADD_DEPENDENCIES(resp kstd fmt) diff --git a/src/resp/resp2_parse.cc b/src/resp/resp2_parse.cc index 5c01d821..5506e0d9 100644 --- a/src/resp/resp2_parse.cc +++ b/src/resp/resp2_parse.cc @@ -5,7 +5,7 @@ #include "resp2_parse.h" #include "log.h" -#include "pstd_string.h" +#include "std_string.h" RespType Resp2Parse::PetRespType(char prefix) { switch (prefix) { @@ -73,7 +73,7 @@ RespResult Resp2Parse::ParseInline() { singleParamsSize_ = 0; return RespResult::OK; } - pstd::StringSplit(line, ' ', singleParams_); + kstd::StringSplit(line, ' ', singleParams_); return RespResult::OK; } @@ -110,7 +110,7 @@ RespResult Resp2Parse::ParseBulkString() { return RespResult::WAIT; } int length; - if (!pstd::String2int(line, &length)) { + if (!kstd::String2int(line, &length)) { return RespResult::ERROR; } @@ -137,7 +137,7 @@ RespResult Resp2Parse::ParseBulkString() { RespResult Resp2Parse::ParseArray() { auto [line, result] = ReadLine(); int count; - if (!pstd::String2int(line, &count)) { + if (!kstd::String2int(line, &count)) { return RespResult::ERROR; } if (count <= 0) { // Null array diff --git a/src/resp/resp_encode.cc b/src/resp/resp_encode.cc index 7ce2962f..001dc03f 100644 --- a/src/resp/resp_encode.cc +++ b/src/resp/resp_encode.cc @@ -13,6 +13,6 @@ void RespEncode::AppendBulkString(std::string& str, const std::string& value) { void RespEncode::SetBulkStringLen(std::string& str, int64_t ori, const std::string& prefix) { str.append(prefix); - str.append(pstd::Int2string(ori)); + str.append(kstd::Int2string(ori)); str.append(CRLF); } diff --git a/src/resp/resp_encode.h b/src/resp/resp_encode.h index d5847ed9..602d1f0e 100644 --- a/src/resp/resp_encode.h +++ b/src/resp/resp_encode.h @@ -8,7 +8,7 @@ #include #include -#include "pstd_string.h" +#include "std_string.h" enum class CmdRes : std::int8_t { kNone = 0, diff --git a/src/resp/tests/CMakeLists.txt b/src/resp/tests/CMakeLists.txt index b516313a..a36cf582 100644 --- a/src/resp/tests/CMakeLists.txt +++ b/src/resp/tests/CMakeLists.txt @@ -21,9 +21,9 @@ foreach (gtest_test_source ${GTEST_TEST_SOURCE}) PRIVATE ${GTEST_INCLUDE_DIR} ) - add_dependencies(${gtest_test_name} pstd gtest) + add_dependencies(${gtest_test_name} kstd gtest) target_link_libraries(${gtest_test_name} - PUBLIC pstd + PUBLIC kstd PUBLIC gtest ) gtest_discover_tests(${gtest_test_name}) diff --git a/src/pstd/CMakeLists.txt b/src/std/CMakeLists.txt similarity index 67% rename from src/pstd/CMakeLists.txt rename to src/std/CMakeLists.txt index 802cb82d..43e287ee 100644 --- a/src/pstd/CMakeLists.txt +++ b/src/std/CMakeLists.txt @@ -5,17 +5,17 @@ AUX_SOURCE_DIRECTORY(. STD_SRC) SET(LIBRARY_OUTPUT_PATH ${PLIB_INSTALL_DIR}) -SET(PSTD_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE PATH "pstd include directory." FORCE) -ADD_LIBRARY(pstd ${STD_SRC}) +SET(PSTD_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE PATH "kstd include directory." FORCE) +ADD_LIBRARY(kstd ${STD_SRC}) ADD_SUBDIRECTORY(tests) -TARGET_INCLUDE_DIRECTORIES(pstd +TARGET_INCLUDE_DIRECTORIES(kstd PUBLIC ${ROCKSDB_SOURCES_DIR}/include PUBLIC ${LIB_INCLUDE_DIR} PUBLIC ${PSTD_INCLUDE_DIR} ) -TARGET_LINK_LIBRARIES(pstd; spdlog pthread fmt) -SET_TARGET_PROPERTIES(pstd PROPERTIES LINKER_LANGUAGE CXX) -ADD_DEPENDENCIES(pstd rocksdb spdlog) +TARGET_LINK_LIBRARIES(kstd; spdlog pthread fmt) +SET_TARGET_PROPERTIES(kstd PROPERTIES LINKER_LANGUAGE CXX) +ADD_DEPENDENCIES(kstd rocksdb spdlog) diff --git a/src/pstd/env.cc b/src/std/env.cc similarity index 99% rename from src/pstd/env.cc rename to src/std/env.cc index dbedaf86..dd10e9fc 100644 --- a/src/pstd/env.cc +++ b/src/std/env.cc @@ -33,7 +33,7 @@ namespace filesystem = std::experimental::filesystem; #include "log.h" -namespace pstd { +namespace kstd { /* * Set the resource limits of a process @@ -682,4 +682,4 @@ Status NewRandomRWFile(const std::string& fname, std::unique_ptr& return s; } -} // namespace pstd +} // namespace std diff --git a/src/pstd/env.h b/src/std/env.h similarity index 94% rename from src/pstd/env.h rename to src/std/env.h index 63ad3a49..932f42dd 100644 --- a/src/pstd/env.h +++ b/src/std/env.h @@ -13,9 +13,9 @@ #include #include "noncopyable.h" -#include "pstd_status.h" +#include "std_status.h" -namespace pstd { +namespace kstd { class WritableFile; class SequentialFile; @@ -54,7 +54,7 @@ bool DeleteFile(const std::string& fname); int RenameFile(const std::string& oldname, const std::string& newname); -class FileLock : public pstd::noncopyable { +class FileLock : public kstd::noncopyable { public: FileLock() = default; virtual ~FileLock() = default; @@ -85,7 +85,7 @@ Status NewRandomRWFile(const std::string& fname, std::unique_ptr& // A file abstraction for sequential writing. The implementation // must provide buffering since callers may append small fragments // at a time to the file. -class WritableFile : public pstd::noncopyable { +class WritableFile : public kstd::noncopyable { public: WritableFile() = default; virtual ~WritableFile(); @@ -111,7 +111,7 @@ class SequentialFile { virtual char* ReadLine(char* buf, int n) = 0; }; -class RWFile : public pstd::noncopyable { +class RWFile : public kstd::noncopyable { public: RWFile() = default; virtual ~RWFile(); @@ -119,7 +119,7 @@ class RWFile : public pstd::noncopyable { }; // A file abstraction for random reading and writing. -class RandomRWFile : public pstd::noncopyable { +class RandomRWFile : public kstd::noncopyable { public: RandomRWFile() = default; virtual ~RandomRWFile() = default; @@ -160,4 +160,4 @@ class RandomRWFile : public pstd::noncopyable { return Status::OK(); } }; -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/kiwi_slot.cc b/src/std/kiwi_slot.cc similarity index 100% rename from src/pstd/kiwi_slot.cc rename to src/std/kiwi_slot.cc diff --git a/src/pstd/kiwi_slot.h b/src/std/kiwi_slot.h similarity index 100% rename from src/pstd/kiwi_slot.h rename to src/std/kiwi_slot.h diff --git a/src/pstd/lock_free_ring_buffer.h b/src/std/lock_free_ring_buffer.h similarity index 100% rename from src/pstd/lock_free_ring_buffer.h rename to src/std/lock_free_ring_buffer.h diff --git a/src/pstd/lock_mgr.cc b/src/std/lock_mgr.cc similarity index 99% rename from src/pstd/lock_mgr.cc rename to src/std/lock_mgr.cc index 5840dc02..e1dfe90d 100644 --- a/src/pstd/lock_mgr.cc +++ b/src/std/lock_mgr.cc @@ -19,7 +19,7 @@ #include "mutex.h" -namespace pstd::lock { +namespace kstd::lock { struct LockMapStripe { explicit LockMapStripe(const std::shared_ptr& factory) { @@ -176,4 +176,4 @@ void LockMgr::UnLock(const std::string& key) { // Signal waiting threads to retry locking stripe->stripe_cv->NotifyAll(); } -} // namespace pstd::lock +} // namespace std::lock diff --git a/src/pstd/lock_mgr.h b/src/std/lock_mgr.h similarity index 94% rename from src/pstd/lock_mgr.h rename to src/std/lock_mgr.h index b9da9290..a099182d 100644 --- a/src/pstd/lock_mgr.h +++ b/src/std/lock_mgr.h @@ -13,11 +13,11 @@ #include "mutex.h" #include "noncopyable.h" -namespace pstd::lock { +namespace kstd::lock { struct LockMap; struct LockMapStripe; -class LockMgr : public pstd::noncopyable { +class LockMgr : public kstd::noncopyable { public: LockMgr(size_t default_num_stripes, int64_t max_num_locks, const std::shared_ptr& factory); @@ -50,4 +50,4 @@ class LockMgr : public pstd::noncopyable { void UnLockKey(const std::string& key, const std::shared_ptr& stripe); }; -} // namespace pstd::lock +} // namespace kstd::lock diff --git a/src/pstd/log.h b/src/std/log.h similarity index 100% rename from src/pstd/log.h rename to src/std/log.h diff --git a/src/pstd/memory_file.cc b/src/std/memory_file.cc similarity index 99% rename from src/pstd/memory_file.cc rename to src/std/memory_file.cc index 2e9c9b91..61bd0636 100644 --- a/src/pstd/memory_file.cc +++ b/src/std/memory_file.cc @@ -14,7 +14,7 @@ #include "memory_file.h" -namespace pstd { +namespace kstd { using std::size_t; @@ -211,4 +211,4 @@ void OutputMemoryFile::AssureSpace(size_t size) { ExtendFileSize(newSize); } -} // namespace pstd +} // namespace std diff --git a/src/pstd/memory_file.h b/src/std/memory_file.h similarity index 99% rename from src/pstd/memory_file.h rename to src/std/memory_file.h index 0360ae8b..9f303cac 100644 --- a/src/pstd/memory_file.h +++ b/src/std/memory_file.h @@ -9,7 +9,7 @@ #include -namespace pstd { +namespace kstd { /* * InputMemoryFile is used to abstract the @@ -216,4 +216,4 @@ inline void OutputMemoryFile::Write(const T& t) { this->Write(&t, sizeof t); } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/mutex.h b/src/std/mutex.h similarity index 96% rename from src/pstd/mutex.h rename to src/std/mutex.h index d3fe3034..dcbc7e63 100644 --- a/src/pstd/mutex.h +++ b/src/std/mutex.h @@ -9,11 +9,11 @@ #include -#include "pstd_status.h" +#include "std_status.h" -namespace pstd::lock { +namespace kstd::lock { -using Status = pstd::Status; +using Status = kstd::Status; class Mutex { public: @@ -82,4 +82,4 @@ class MutexFactory { virtual ~MutexFactory() = default; }; -} // namespace pstd::lock +} // namespace kstd::lock diff --git a/src/pstd/mutex_impl.cc b/src/std/mutex_impl.cc similarity index 98% rename from src/pstd/mutex_impl.cc rename to src/std/mutex_impl.cc index 075d27ee..fe880f7a 100644 --- a/src/pstd/mutex_impl.cc +++ b/src/std/mutex_impl.cc @@ -11,7 +11,7 @@ #include "mutex.h" #include "mutex_impl.h" -namespace pstd::lock { +namespace kstd::lock { class MutexImpl : public Mutex { public: @@ -117,4 +117,4 @@ Status CondVarImpl::WaitFor(std::shared_ptr mutex, int64_t timeout_time) // CV was signaled, or we spuriously woke up (but didn't time out) return s; } -} // namespace pstd::lock +} // namespace std::lock diff --git a/src/pstd/mutex_impl.h b/src/std/mutex_impl.h similarity index 91% rename from src/pstd/mutex_impl.h rename to src/std/mutex_impl.h index 7a8858b6..9d814f01 100644 --- a/src/pstd/mutex_impl.h +++ b/src/std/mutex_impl.h @@ -11,11 +11,11 @@ #include -namespace pstd::lock { +namespace kstd::lock { // Default implementation of MutexFactory. class MutexFactoryImpl : public MutexFactory { public: std::shared_ptr AllocateMutex() override; std::shared_ptr AllocateCondVar() override; }; -} // namespace pstd::lock +} // namespace kstd::lock diff --git a/src/pstd/noncopyable.h b/src/std/noncopyable.h similarity index 93% rename from src/pstd/noncopyable.h rename to src/std/noncopyable.h index 81ae63d8..bf2ad417 100644 --- a/src/pstd/noncopyable.h +++ b/src/std/noncopyable.h @@ -7,7 +7,7 @@ #pragma once -namespace pstd { +namespace kstd { class noncopyable { protected: @@ -19,4 +19,4 @@ class noncopyable { void operator=(const noncopyable&) = delete; }; -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/scope_record_lock.cc b/src/std/scope_record_lock.cc similarity index 97% rename from src/pstd/scope_record_lock.cc rename to src/std/scope_record_lock.cc index 0c6f4d0e..ce67f94c 100644 --- a/src/pstd/scope_record_lock.cc +++ b/src/std/scope_record_lock.cc @@ -9,7 +9,7 @@ #include "scope_record_lock.h" -namespace pstd::lock { +namespace kstd::lock { MultiScopeRecordLock::MultiScopeRecordLock(const std::shared_ptr& lock_mgr, const std::vector& keys) @@ -76,4 +76,4 @@ void MultiRecordLock::Unlock(const std::vector& keys) { } } } -} // namespace pstd::lock +} // namespace std::lock diff --git a/src/pstd/scope_record_lock.h b/src/std/scope_record_lock.h similarity index 89% rename from src/pstd/scope_record_lock.h rename to src/std/scope_record_lock.h index f82846c4..511d8a6f 100644 --- a/src/pstd/scope_record_lock.h +++ b/src/std/scope_record_lock.h @@ -16,11 +16,11 @@ #include "noncopyable.h" #include "rocksdb/slice.h" -namespace pstd::lock { +namespace kstd::lock { using Slice = rocksdb::Slice; -class ScopeRecordLock final : public pstd::noncopyable { +class ScopeRecordLock final : public kstd::noncopyable { public: ScopeRecordLock(const std::shared_ptr& lock_mgr, const Slice& key) : lock_mgr_(lock_mgr), key_(key) { lock_mgr_->TryLock(key_.ToString()); @@ -32,7 +32,7 @@ class ScopeRecordLock final : public pstd::noncopyable { Slice key_; }; -class MultiScopeRecordLock final : public pstd::noncopyable { +class MultiScopeRecordLock final : public kstd::noncopyable { public: MultiScopeRecordLock(const std::shared_ptr& lock_mgr, const std::vector& keys); ~MultiScopeRecordLock(); @@ -54,4 +54,4 @@ class MultiRecordLock : public noncopyable { std::shared_ptr const lock_mgr_; }; -} // namespace pstd::lock +} // namespace kstd::lock diff --git a/src/pstd/pstd_coding.cc b/src/std/std_coding.cc similarity index 98% rename from src/pstd/pstd_coding.cc rename to src/std/std_coding.cc index 6fc0496c..bff82474 100644 --- a/src/pstd/pstd_coding.cc +++ b/src/std/std_coding.cc @@ -2,10 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. -#include "pstd_coding.h" -#include "pstd_slice.h" +#include "std_coding.h" +#include "std_slice.h" -namespace pstd { +namespace kstd { void EncodeFixed16(char* buf, uint16_t value) { memcpy(buf, &value, sizeof(value)); } @@ -201,4 +201,4 @@ bool GetLengthPrefixedString(std::string* input, std::string* result) { } } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_coding.h b/src/std/std_coding.h similarity index 98% rename from src/pstd/pstd_coding.h rename to src/std/std_coding.h index 206fd34f..8fbf177a 100644 --- a/src/pstd/pstd_coding.h +++ b/src/std/std_coding.h @@ -13,9 +13,9 @@ #include #include -#include "pstd_slice.h" +#include "std_slice.h" -namespace pstd { +namespace kstd { // Standard Put... routines append to a string extern void PutFixed16(std::string* dst, uint16_t value); @@ -148,4 +148,4 @@ inline const char* GetVarint32Ptr(const char* p, const char* limit, uint32_t* va return GetVarint32PtrFallback(p, limit, value); } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_defer.h b/src/std/std_defer.h similarity index 100% rename from src/pstd/pstd_defer.h rename to src/std/std_defer.h diff --git a/src/pstd/pstd_hash.cc b/src/std/std_hash.cc similarity index 99% rename from src/pstd/pstd_hash.cc rename to src/std/std_hash.cc index df68c3e9..d9f13e19 100644 --- a/src/pstd/pstd_hash.cc +++ b/src/std/std_hash.cc @@ -71,13 +71,13 @@ // LICENSE file in the root directory of this source tree. An additional grant // of patent rights can be found in the PATENTS file in the same directory. -#include "pstd_hash.h" +#include "std_hash.h" #include #include #include -namespace pstd { +namespace kstd { class SHA256 { protected: @@ -631,4 +631,4 @@ std::string md5(const std::string& str, bool raw) { return md5.hexdigest(); } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_hash.h b/src/std/std_hash.h similarity index 98% rename from src/pstd/pstd_hash.h rename to src/std/std_hash.h index 9b1ad48d..71eaa9f9 100644 --- a/src/pstd/pstd_hash.h +++ b/src/std/std_hash.h @@ -78,11 +78,11 @@ documentation and/or software. #include #include -namespace pstd { +namespace kstd { std::string md5(const std::string& str, bool raw = false); std::string sha256(const std::string& input, bool raw = false); bool isSha256(const std::string& input); -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_mutex.h b/src/std/std_mutex.h similarity index 90% rename from src/pstd/pstd_mutex.h rename to src/std/std_mutex.h index 759e5768..6af72475 100644 --- a/src/pstd/pstd_mutex.h +++ b/src/std/std_mutex.h @@ -18,7 +18,7 @@ #include #include "noncopyable.h" -namespace pstd { +namespace kstd { using Mutex = std::mutex; using CondVar = std::condition_variable; @@ -31,7 +31,7 @@ void InitOnce(OnceType& once, F&& f, Args&&... args) { return std::call_once(once, std::forward(f), std::forward(args)...); } -class RefMutex : public pstd::noncopyable { +class RefMutex : public kstd::noncopyable { public: RefMutex() = default; ~RefMutex() = default; @@ -50,7 +50,7 @@ class RefMutex : public pstd::noncopyable { int refs_ = 0; }; -class RecordMutex : public pstd::noncopyable { +class RecordMutex : public kstd::noncopyable { public: RecordMutex() = default; ; @@ -67,7 +67,7 @@ class RecordMutex : public pstd::noncopyable { std::unordered_map records_; }; -class RecordLock : public pstd::noncopyable { +class RecordLock : public kstd::noncopyable { public: RecordLock(RecordMutex* mu, std::string key) : mu_(mu), key_(std::move(key)) { mu_->Lock(key_); } ~RecordLock() { mu_->Unlock(key_); } @@ -77,4 +77,4 @@ class RecordLock : public pstd::noncopyable { std::string key_; }; -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_slice.h b/src/std/std_slice.h similarity index 98% rename from src/pstd/pstd_slice.h rename to src/std/std_slice.h index 97f77c8c..a0029028 100644 --- a/src/pstd/pstd_slice.h +++ b/src/std/std_slice.h @@ -24,7 +24,7 @@ #include #include -namespace pstd { +namespace kstd { class Slice { public: @@ -108,4 +108,4 @@ inline int Slice::compare(const Slice& b) const { return r; } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_status.cc b/src/std/std_status.cc similarity index 97% rename from src/pstd/pstd_status.cc rename to src/std/std_status.cc index 7b9ed3af..f4f3d8f6 100644 --- a/src/pstd/pstd_status.cc +++ b/src/std/std_status.cc @@ -2,11 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. -#include "pstd_status.h" +#include "std_status.h" #include #include -namespace pstd { +namespace kstd { const char* Status::CopyState(const char* state) { uint32_t size; @@ -92,4 +92,4 @@ std::string Status::ToString() const { } } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_status.h b/src/std/std_status.h similarity index 98% rename from src/pstd/pstd_status.h rename to src/std/std_status.h index ebb830f2..7103bba4 100644 --- a/src/pstd/pstd_status.h +++ b/src/std/std_status.h @@ -6,9 +6,9 @@ #pragma once #include -#include "pstd_slice.h" +#include "std_slice.h" -namespace pstd { +namespace kstd { class Status { public: @@ -126,4 +126,4 @@ inline void Status::operator=(const Status& s) { } } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_string.cc b/src/std/std_string.cc similarity index 99% rename from src/pstd/pstd_string.cc rename to src/std/std_string.cc index 2c5114f0..3bc750b5 100644 --- a/src/pstd/pstd_string.cc +++ b/src/std/std_string.cc @@ -44,11 +44,11 @@ #include #include -#include "pstd_defer.h" -#include "pstd_string.h" -#include "pstd_util.h" +#include "std_defer.h" +#include "std_string.h" +#include "std_util.h" -namespace pstd { +namespace kstd { /* Glob-style pattern matching. */ int StringMatchLen(const char* pattern, int patternLen, const char* string, int stringLen, int nocase) { @@ -659,4 +659,4 @@ void TrimSlash(std::string& dirName) { } } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_string.h b/src/std/std_string.h similarity index 99% rename from src/pstd/pstd_string.h rename to src/std/std_string.h index 9b75ff2a..d8b49e0e 100644 --- a/src/pstd/pstd_string.h +++ b/src/std/std_string.h @@ -39,7 +39,7 @@ #include #include -namespace pstd { +namespace kstd { int StringMatchLen(const char* pattern, int patternLen, const char* string, int stringLen, int nocase); int StringMatch(const char* p, const char* s, int nocase); @@ -113,4 +113,4 @@ bool IsValidNumber(const std::string& str); void TrimSlash(std::string& dirName); -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_util.cc b/src/std/std_util.cc similarity index 91% rename from src/pstd/pstd_util.cc rename to src/std/std_util.cc index 3e34a20b..b2df27c6 100644 --- a/src/pstd/pstd_util.cc +++ b/src/std/std_util.cc @@ -5,9 +5,9 @@ #include -#include "pstd_util.h" +#include "std_util.h" -namespace pstd { +namespace kstd { std::mt19937 gen; @@ -28,4 +28,4 @@ double RandomDouble() { return dis(gen); } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/pstd_util.h b/src/std/std_util.h similarity index 98% rename from src/pstd/pstd_util.h rename to src/std/std_util.h index 4a3be378..d43a49da 100644 --- a/src/pstd/pstd_util.h +++ b/src/std/std_util.h @@ -9,7 +9,7 @@ #include #include -namespace pstd { +namespace kstd { extern std::mt19937 gen; @@ -63,4 +63,4 @@ inline int64_t UnixNanoTimestamp() { .count(); } -} // namespace pstd +} // namespace kstd diff --git a/src/pstd/tests/CMakeLists.txt b/src/std/tests/CMakeLists.txt similarity index 60% rename from src/pstd/tests/CMakeLists.txt rename to src/std/tests/CMakeLists.txt index 5ea3ca35..3a553d64 100644 --- a/src/pstd/tests/CMakeLists.txt +++ b/src/std/tests/CMakeLists.txt @@ -12,21 +12,21 @@ aux_source_directory(.. DIR_SRCS) file(GLOB_RECURSE PSTD_TEST_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/*.cc") -foreach (pstd_test_source ${PSTD_TEST_SOURCE}) - get_filename_component(pstd_test_filename ${pstd_test_source} NAME) - string(REPLACE ".cc" "" pstd_test_name ${pstd_test_filename}) +foreach (std_test_source ${PSTD_TEST_SOURCE}) + get_filename_component(std_test_filename ${std_test_source} NAME) + string(REPLACE ".cc" "" std_test_name ${std_test_filename}) # set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) - add_executable(${pstd_test_name} ${pstd_test_source}) - target_include_directories(${pstd_test_name} + add_executable(${std_test_name} ${std_test_source}) + target_include_directories(${std_test_name} PUBLIC ${PROJECT_SOURCE_DIR}/src PRIVATE ${GTEST_INCLUDE_DIR} ) - add_dependencies(${pstd_test_name} pstd gtest) - target_link_libraries(${pstd_test_name} - PUBLIC pstd + add_dependencies(${std_test_name} kstd gtest) + target_link_libraries(${std_test_name} + PUBLIC kstd PUBLIC gtest ) - gtest_discover_tests(${pstd_test_name}) + gtest_discover_tests(${std_test_name}) endforeach () diff --git a/src/pstd/tests/lock_free_ring_buffer_test.cc b/src/std/tests/lock_free_ring_buffer_test.cc similarity index 100% rename from src/pstd/tests/lock_free_ring_buffer_test.cc rename to src/std/tests/lock_free_ring_buffer_test.cc diff --git a/src/std/tests/std_string_test.cc b/src/std/tests/std_string_test.cc new file mode 100644 index 00000000..5daead10 --- /dev/null +++ b/src/std/tests/std_string_test.cc @@ -0,0 +1,149 @@ +// Copyright (c) 2015-present, Arana/Kiwi Community. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. See the AUTHORS file for names of contributors. + +#include + +#include +#include "std/std_string.h" + +class StringTest : public ::testing::Test {}; + +TEST_F(StringTest, StringTrim) { + ASSERT_EQ(kstd::StringTrim(" computer "), "computer"); + ASSERT_EQ(kstd::StringTrim(" comp uter "), "comp uter"); + ASSERT_EQ(kstd::StringTrim(" \n computer \n ", "\n "), "computer"); + ASSERT_EQ(kstd::StringTrim("\n", "\r\n "), ""); +} + +TEST_F(StringTest, StringTrimLeft) { + ASSERT_EQ(kstd::StringTrimLeft(" computer"), "computer"); + ASSERT_EQ(kstd::StringTrimLeft(" computer "), "computer "); + ASSERT_EQ(kstd::StringTrimLeft(" comp uter "), "comp uter "); + ASSERT_EQ(kstd::StringTrimLeft(" comp uter"), "comp uter"); + ASSERT_EQ(kstd::StringTrimLeft(" \n computer \n ", "\n "), "computer \n "); + ASSERT_EQ(kstd::StringTrimLeft("\n", "\r\n "), ""); +} + +TEST_F(StringTest, StringTrimRight) { + ASSERT_EQ(kstd::StringTrimRight("computer "), "computer"); + ASSERT_EQ(kstd::StringTrimRight(" computer "), " computer"); + ASSERT_EQ(kstd::StringTrimRight(" comp uter "), " comp uter"); + ASSERT_EQ(kstd::StringTrimRight("comp uter "), "comp uter"); + ASSERT_EQ(kstd::StringTrimRight(" \n computer \n ", "\n "), " \n computer"); + ASSERT_EQ(kstd::StringTrimRight("\n", "\r\n "), ""); +} + +TEST_F(StringTest, ParseIpPort) { + std::string ip; + int port; + ASSERT_TRUE(kstd::ParseIpPortString("192.168.1.1:9221", ip, port)); + ASSERT_EQ(ip, "192.168.1.1"); + ASSERT_EQ(port, 9221); +} + +TEST_F(StringTest, test_string2ll) { + char buf[32]; + long long v; + + /* May not start with +. */ + strcpy(buf, "+1"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 0); + + /* Leading space. */ + strcpy(buf, " 1"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 0); + + /* Trailing space. */ + strcpy(buf, "1 "); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + + strcpy(buf, "-1"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, -1); + + strcpy(buf, "0"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, 0); + + strcpy(buf, "1"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, 1); + + strcpy(buf, "99"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, 99); + + strcpy(buf, "-99"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, -99); + + strcpy(buf, "-9223372036854775808"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, LLONG_MIN); + + strcpy(buf, "-9223372036854775809"); /* overflow */ + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 0); + + strcpy(buf, "9223372036854775807"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, LLONG_MAX); + + strcpy(buf, "9223372036854775808"); /* overflow */ + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 0); +} + +TEST_F(StringTest, test_string2l) { + char buf[32]; + long v; + + /* May not start with +. */ + strcpy(buf, "+1"); + ASSERT_EQ(kstd::String2int(buf, &v), 0); + + strcpy(buf, "-1"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, -1); + + strcpy(buf, "0"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, 0); + + strcpy(buf, "1"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, 1); + + strcpy(buf, "99"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, 99); + + strcpy(buf, "-99"); + ASSERT_EQ(kstd::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, -99); + +#if LONG_MAX != LLONG_MAX + strcpy(buf, "-2147483648"); + ASSERT_EQ(std::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, LONG_MIN); + + strcpy(buf, "-2147483649"); /* overflow */ + ASSERT_EQ(std::String2int(buf, strlen(buf), &v), 0); + + strcpy(buf, "2147483647"); + ASSERT_EQ(std::String2int(buf, strlen(buf), &v), 1); + ASSERT_EQ(v, LONG_MAX); + + strcpy(buf, "2147483648"); /* overflow */ + ASSERT_EQ(std::String2int(buf, strlen(buf), &v), 0); +#endif +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/src/pstd/tests/pstd_util_test.cc b/src/std/tests/std_util_test.cc similarity index 74% rename from src/pstd/tests/pstd_util_test.cc rename to src/std/tests/std_util_test.cc index a17853db..279ee207 100644 --- a/src/pstd/tests/pstd_util_test.cc +++ b/src/std/tests/std_util_test.cc @@ -3,15 +3,15 @@ // LICENSE file in the root directory of this source tree. An additional grant // of patent rights can be found in the PATENTS file in the same directory. -#include "pstd/pstd_util.h" #include +#include "std/std_util.h" class UtilTest : public ::testing::Test {}; TEST(UtilTest, RandomInt) { int min = 0; int max = 100; - int random_int = pstd::RandomInt(min, max); + int random_int = kstd::RandomInt(min, max); ASSERT_GE(random_int, min); ASSERT_LE(random_int, max); } @@ -19,19 +19,19 @@ TEST(UtilTest, RandomInt) { TEST(UtilTest, RandomInt2) { int max = 100; for (int i = 0; i < 100; ++i) { - int random_int = pstd::RandomInt(max); + int random_int = kstd::RandomInt(max); ASSERT_LE(random_int, max); } } TEST(UtilTest, RandomDouble) { - double random_double = pstd::RandomDouble(); + double random_double = kstd::RandomDouble(); ASSERT_GE(random_double, 0.0); ASSERT_LE(random_double, 1.0); } TEST(UtilTest, RandomPerm) { - std::vector perm = pstd::RandomPerm(10); + std::vector perm = kstd::RandomPerm(10); ASSERT_EQ(perm.size(), 10); for (int i = 0; i < 10; ++i) { ASSERT_EQ(std::count(perm.begin(), perm.end(), i), 1); @@ -39,41 +39,41 @@ TEST(UtilTest, RandomPerm) { } TEST(UtilTest, RandomPermEmpty) { - std::vector perm = pstd::RandomPerm(0); + std::vector perm = kstd::RandomPerm(0); ASSERT_EQ(perm.size(), 0); } TEST(UtilTest, RandomPermNegative) { - std::vector perm = pstd::RandomPerm(-1); + std::vector perm = kstd::RandomPerm(-1); ASSERT_EQ(perm.size(), 0); } TEST(UtilTest, UnixTimestamp) { - int64_t timestamp = pstd::UnixTimestamp(); + int64_t timestamp = kstd::UnixTimestamp(); std::cout << timestamp << std::endl; ASSERT_GT(timestamp, 0); } TEST(UtilTest, UnixMilliTimestamp) { - int64_t timestamp = pstd::UnixMilliTimestamp(); + int64_t timestamp = kstd::UnixMilliTimestamp(); std::cout << timestamp << std::endl; ASSERT_GT(timestamp, 0); } TEST(UtilTest, UnixMicroTimestamp) { - int64_t timestamp = pstd::UnixMicroTimestamp(); + int64_t timestamp = kstd::UnixMicroTimestamp(); std::cout << timestamp << std::endl; ASSERT_GT(timestamp, 0); } TEST(UtilTest, UnixNanoTimestamp) { - int64_t timestamp = pstd::UnixNanoTimestamp(); + int64_t timestamp = kstd::UnixNanoTimestamp(); std::cout << timestamp << std::endl; ASSERT_GT(timestamp, 0); } int main(int argc, char** argv) { - pstd::InitRandom(); + kstd::InitRandom(); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/src/pstd/thread_pool.cc b/src/std/thread_pool.cc similarity index 98% rename from src/pstd/thread_pool.cc rename to src/std/thread_pool.cc index 81763e5a..f66140b1 100644 --- a/src/pstd/thread_pool.cc +++ b/src/std/thread_pool.cc @@ -7,7 +7,7 @@ #include "thread_pool.h" -namespace pstd { +namespace kstd { thread_local bool ThreadPool::working_ = true; @@ -107,4 +107,4 @@ void ThreadPool::MonitorRoutine() { } } -} // namespace pstd +} // namespace std diff --git a/src/pstd/thread_pool.h b/src/std/thread_pool.h similarity index 98% rename from src/pstd/thread_pool.h rename to src/std/thread_pool.h index bdc26898..be65b0a2 100644 --- a/src/pstd/thread_pool.h +++ b/src/std/thread_pool.h @@ -16,7 +16,7 @@ #include #include -namespace pstd { +namespace kstd { class ThreadPool final { public: @@ -77,4 +77,4 @@ auto ThreadPool::ExecuteTask(F&& f, Args&&... args) -> std::futureget_future(); } -} // namespace pstd +} // namespace kstd diff --git a/src/storage/CMakeLists.txt b/src/storage/CMakeLists.txt index 980117f2..3b2b36b6 100644 --- a/src/storage/CMakeLists.txt +++ b/src/storage/CMakeLists.txt @@ -23,7 +23,7 @@ TARGET_INCLUDE_DIRECTORIES(storage ) TARGET_LINK_LIBRARIES(storage - pstd + kstd braft brpc ssl @@ -41,7 +41,7 @@ SET_TARGET_PROPERTIES(storage PROPERTIES LINKER_LANGUAGE CXX) ADD_SUBDIRECTORY(tests) ADD_DEPENDENCIES(storage - pstd + kstd braft brpc ssl diff --git a/src/storage/include/storage/storage.h b/src/storage/include/storage/storage.h index 7cc9e0ec..46f4cca8 100644 --- a/src/storage/include/storage/storage.h +++ b/src/storage/include/storage/storage.h @@ -25,9 +25,9 @@ #include "rocksdb/status.h" #include "rocksdb/table.h" -#include "pstd/env.h" -#include "pstd/pstd_mutex.h" #include "src/base_data_value_format.h" +#include "std/env.h" +#include "std/std_mutex.h" #include "storage/slot_indexer.h" namespace kiwi { @@ -1116,8 +1116,8 @@ class Storage { // Storage start the background thread for compaction task pthread_t bg_tasks_thread_id_ = 0; - pstd::Mutex bg_tasks_mutex_; - pstd::CondVar bg_tasks_cond_var_; + kstd::Mutex bg_tasks_mutex_; + kstd::CondVar bg_tasks_cond_var_; std::queue bg_tasks_queue_; std::atomic current_task_type_ = kNone; diff --git a/src/storage/src/base_data_key_format.h b/src/storage/src/base_data_key_format.h index dfea89d7..81d2b49e 100644 --- a/src/storage/src/base_data_key_format.h +++ b/src/storage/src/base_data_key_format.h @@ -6,13 +6,13 @@ #ifndef SRC_BASE_DATA_KEY_FORMAT_H_ #define SRC_BASE_DATA_KEY_FORMAT_H_ -#include "pstd/pstd_coding.h" +#include "std/std_coding.h" #include "storage/storage_define.h" namespace storage { using Slice = rocksdb::Slice; -using namespace pstd; +using namespace kstd; /* * used for Hash/Set/Zset's member data key. format: * | reserve1 | key | version | data | reserve2 | @@ -129,7 +129,7 @@ class ParsedBaseDataKey { // user key ptr = DecodeUserKey(ptr, std::distance(ptr, end_ptr), &key_str_); - version_ = pstd::DecodeFixed64(ptr); + version_ = kstd::DecodeFixed64(ptr); ptr += sizeof(version_); data_ = Slice(ptr, std::distance(ptr, end_ptr)); } diff --git a/src/storage/src/base_data_value_format.h b/src/storage/src/base_data_value_format.h index 2b933afd..6ea2530c 100644 --- a/src/storage/src/base_data_value_format.h +++ b/src/storage/src/base_data_value_format.h @@ -14,7 +14,7 @@ #include "base_value_format.h" #include "coding.h" #include "mutex.h" -#include "pstd/pstd_coding.h" +#include "std/std_coding.h" #include "storage/storage_define.h" namespace storage { diff --git a/src/storage/src/base_filter.h b/src/storage/src/base_filter.h index bf1a2320..788b10fa 100644 --- a/src/storage/src/base_filter.h +++ b/src/storage/src/base_filter.h @@ -10,7 +10,6 @@ #include #include -#include "pstd/log.h" #include "rocksdb/compaction_filter.h" #include "rocksdb/db.h" #include "src/base_data_key_format.h" @@ -21,6 +20,7 @@ #include "src/lists_meta_value_format.h" #include "src/strings_value_format.h" #include "src/zsets_data_key_format.h" +#include "std/log.h" namespace storage { diff --git a/src/storage/src/base_key_format.h b/src/storage/src/base_key_format.h index e4a835e3..80135c44 100644 --- a/src/storage/src/base_key_format.h +++ b/src/storage/src/base_key_format.h @@ -8,7 +8,7 @@ #include -#include "pstd/pstd_coding.h" +#include "std/std_coding.h" #include "storage/storage_define.h" namespace storage { diff --git a/src/storage/src/base_value_format.h b/src/storage/src/base_value_format.h index 7d5353e7..68d4be21 100644 --- a/src/storage/src/base_value_format.h +++ b/src/storage/src/base_value_format.h @@ -11,9 +11,9 @@ #include "rocksdb/env.h" #include "rocksdb/slice.h" -#include "pstd/env.h" #include "src/coding.h" #include "src/mutex.h" +#include "std/env.h" namespace storage { @@ -33,7 +33,7 @@ const char DataTypeToTag(DataType type); class InternalValue { public: explicit InternalValue(DataType type, const Slice& user_value) : type_(type), user_value_(user_value) { - ctime_ = pstd::NowMicros() / 1e6; + ctime_ = kstd::NowMicros() / 1e6; } virtual ~InternalValue() { diff --git a/src/storage/src/lists_data_key_format.h b/src/storage/src/lists_data_key_format.h index 1a49f41c..c6c5ac85 100644 --- a/src/storage/src/lists_data_key_format.h +++ b/src/storage/src/lists_data_key_format.h @@ -6,7 +6,7 @@ #ifndef SRC_LISTS_DATA_KEY_FORMAT_H_ #define SRC_LISTS_DATA_KEY_FORMAT_H_ -#include "pstd/pstd_coding.h" +#include "std/std_coding.h" #include "storage/storage_define.h" namespace storage { @@ -91,9 +91,9 @@ class ParsedListsDataKey { end_ptr -= sizeof(reserve2_); ptr = DecodeUserKey(ptr, std::distance(ptr, end_ptr), &key_str_); - version_ = pstd::DecodeFixed64(ptr); + version_ = kstd::DecodeFixed64(ptr); ptr += sizeof(version_); - index_ = pstd::DecodeFixed64(ptr); + index_ = kstd::DecodeFixed64(ptr); } virtual ~ParsedListsDataKey() = default; diff --git a/src/storage/src/lock_mgr.h b/src/storage/src/lock_mgr.h index 4a930f1e..fcdfad42 100644 --- a/src/storage/src/lock_mgr.h +++ b/src/storage/src/lock_mgr.h @@ -8,12 +8,12 @@ #include #include -#include "pstd/lock_mgr.h" +#include "std/lock_mgr.h" #include "src/mutex.h" namespace storage { -using LockMgr = pstd::lock::LockMgr; +using LockMgr = kstd::lock::LockMgr; } // namespace storage diff --git a/src/storage/src/log_index.cc b/src/storage/src/log_index.cc index bba42b02..5ef596d7 100644 --- a/src/storage/src/log_index.cc +++ b/src/storage/src/log_index.cc @@ -7,7 +7,7 @@ #include "log_index.h" -#include "pstd/log.h" +#include "std/log.h" #include #include diff --git a/src/storage/src/lru_cache.h b/src/storage/src/lru_cache.h index b97e4ada..803f87d9 100644 --- a/src/storage/src/lru_cache.h +++ b/src/storage/src/lru_cache.h @@ -12,7 +12,7 @@ #include "rocksdb/status.h" -#include "pstd/pstd_mutex.h" +#include "std/std_mutex.h" namespace storage { @@ -113,7 +113,7 @@ class LRUCache { size_t usage_ = 0; size_t size_ = 0; - pstd::Mutex mutex_; + kstd::Mutex mutex_; // Dummy head of LRU list. // lru.prev is newest entry, lru.next is oldest entry. diff --git a/src/storage/src/mutex.h b/src/storage/src/mutex.h index 633f5c4e..c1ab908d 100644 --- a/src/storage/src/mutex.h +++ b/src/storage/src/mutex.h @@ -9,14 +9,14 @@ #include "rocksdb/status.h" -#include "pstd/mutex.h" +#include "std/mutex.h" namespace storage { using Status = rocksdb::Status; -using Mutex = pstd::lock::Mutex; -using CondVar = pstd::lock::CondVar; -using MutexFactory = pstd::lock::MutexFactory; +using Mutex = kstd::lock::Mutex; +using CondVar = kstd::lock::CondVar; +using MutexFactory = kstd::lock::MutexFactory; } // namespace storage diff --git a/src/storage/src/mutex_impl.h b/src/storage/src/mutex_impl.h index a9944ea6..6c6ec391 100644 --- a/src/storage/src/mutex_impl.h +++ b/src/storage/src/mutex_impl.h @@ -7,12 +7,12 @@ #include "src/mutex.h" -#include "pstd/mutex_impl.h" +#include "std/mutex_impl.h" #include namespace storage { -using MutexFactoryImpl = pstd::lock::MutexFactoryImpl; +using MutexFactoryImpl = kstd::lock::MutexFactoryImpl; } // namespace storage diff --git a/src/storage/src/redis.cc b/src/storage/src/redis.cc index 2bba2252..9b456cb1 100644 --- a/src/storage/src/redis.cc +++ b/src/storage/src/redis.cc @@ -5,8 +5,8 @@ #include -#include "pstd/log.h" #include "rocksdb/env.h" +#include "std/log.h" #include "src/base_filter.h" #include "src/lists_filter.h" diff --git a/src/storage/src/redis.h b/src/storage/src/redis.h index 0e16d773..b0e22036 100644 --- a/src/storage/src/redis.h +++ b/src/storage/src/redis.h @@ -15,14 +15,14 @@ #include "rocksdb/status.h" #include "log_index.h" -#include "pstd/env.h" -#include "pstd/log.h" #include "src/custom_comparator.h" #include "src/debug.h" #include "src/lock_mgr.h" #include "src/lru_cache.h" #include "src/mutex_impl.h" #include "src/type_iterator.h" +#include "std/env.h" +#include "std/log.h" #include "storage/storage.h" #include "storage/storage_define.h" @@ -84,9 +84,9 @@ class Redis { uint64_t start_us; DataType dtype; KeyStatisticsDurationGuard(Redis* that, const DataType type, const std::string& key) - : ctx(that), key(key), start_us(pstd::NowMicros()), dtype(type) {} + : ctx(that), key(key), start_us(kstd::NowMicros()), dtype(type) {} ~KeyStatisticsDurationGuard() { - uint64_t end_us = pstd::NowMicros(); + uint64_t end_us = kstd::NowMicros(); uint64_t duration = end_us > start_us ? end_us - start_us : 0; ctx->UpdateSpecificKeyDuration(dtype, key, duration); } diff --git a/src/storage/src/redis_hashes.cc b/src/storage/src/redis_hashes.cc index 084f0aa2..2d90f8a4 100644 --- a/src/storage/src/redis_hashes.cc +++ b/src/storage/src/redis_hashes.cc @@ -13,12 +13,12 @@ #include #include "batch.h" -#include "pstd/log.h" #include "src/base_data_key_format.h" #include "src/base_data_value_format.h" #include "src/base_filter.h" #include "src/scope_record_lock.h" #include "src/scope_snapshot.h" +#include "std/log.h" #include "storage/util.h" namespace storage { diff --git a/src/storage/src/redis_lists.cc b/src/storage/src/redis_lists.cc index 43191f5e..77060001 100644 --- a/src/storage/src/redis_lists.cc +++ b/src/storage/src/redis_lists.cc @@ -6,13 +6,13 @@ #include #include -#include "pstd/log.h" #include "src/base_data_value_format.h" #include "src/batch.h" #include "src/lists_filter.h" #include "src/redis.h" #include "src/scope_record_lock.h" #include "src/scope_snapshot.h" +#include "std/log.h" #include "storage/util.h" namespace storage { diff --git a/src/storage/src/redis_sets.cc b/src/storage/src/redis_sets.cc index 94798f33..8d31d25e 100644 --- a/src/storage/src/redis_sets.cc +++ b/src/storage/src/redis_sets.cc @@ -14,12 +14,12 @@ #include -#include "pstd/env.h" -#include "pstd/log.h" #include "src/base_data_value_format.h" #include "src/base_filter.h" #include "src/scope_record_lock.h" #include "src/scope_snapshot.h" +#include "std/env.h" +#include "std/log.h" #include "storage/util.h" namespace storage { @@ -805,7 +805,7 @@ rocksdb::Status Redis::SPop(const Slice& key, std::vector* members, auto batch = Batch::CreateBatch(this); ScopeRecordLock l(lock_mgr_, key); - uint64_t start_us = pstd::NowMicros(); + uint64_t start_us = kstd::NowMicros(); BaseMetaKey base_meta_key(key); Status s = db_->Get(default_read_options_, handles_[kMetaCF], base_meta_key.Encode(), &meta_value); @@ -899,7 +899,7 @@ rocksdb::Status Redis::SRandmember(const Slice& key, int32_t count, std::vector< } members->clear(); - auto last_seed = pstd::NowMicros(); + auto last_seed = kstd::NowMicros(); std::default_random_engine engine; std::string meta_value; diff --git a/src/storage/src/redis_strings.cc b/src/storage/src/redis_strings.cc index 537d450d..b4bfc91a 100644 --- a/src/storage/src/redis_strings.cc +++ b/src/storage/src/redis_strings.cc @@ -7,13 +7,13 @@ #include -#include "pstd/log.h" #include "src/base_key_format.h" #include "src/batch.h" #include "src/redis.h" #include "src/scope_record_lock.h" #include "src/scope_snapshot.h" #include "src/strings_filter.h" +#include "std/log.h" #include "storage/util.h" namespace storage { diff --git a/src/storage/src/redis_zsets.cc b/src/storage/src/redis_zsets.cc index 325bac60..3e878e9f 100644 --- a/src/storage/src/redis_zsets.cc +++ b/src/storage/src/redis_zsets.cc @@ -11,7 +11,6 @@ #include -#include "pstd/log.h" #include "src/base_data_value_format.h" #include "src/base_key_format.h" #include "src/batch.h" @@ -19,6 +18,7 @@ #include "src/scope_record_lock.h" #include "src/scope_snapshot.h" #include "src/zsets_filter.h" +#include "std/log.h" #include "storage/util.h" namespace storage { diff --git a/src/storage/src/scope_record_lock.h b/src/storage/src/scope_record_lock.h index 8f993ed8..0bd3f95d 100644 --- a/src/storage/src/scope_record_lock.h +++ b/src/storage/src/scope_record_lock.h @@ -10,13 +10,13 @@ #include #include -#include "pstd/scope_record_lock.h" #include "src/lock_mgr.h" +#include "std/scope_record_lock.h" #include "storage/storage.h" namespace storage { -using ScopeRecordLock = pstd::lock::ScopeRecordLock; -using MultiScopeRecordLock = pstd::lock::MultiScopeRecordLock; +using ScopeRecordLock = kstd::lock::ScopeRecordLock; +using MultiScopeRecordLock = kstd::lock::MultiScopeRecordLock; } // namespace storage diff --git a/src/storage/src/scope_snapshot.h b/src/storage/src/scope_snapshot.h index 96297064..c3065641 100644 --- a/src/storage/src/scope_snapshot.h +++ b/src/storage/src/scope_snapshot.h @@ -7,10 +7,10 @@ #include "rocksdb/db.h" -#include "pstd/noncopyable.h" +#include "std/noncopyable.h" namespace storage { -class ScopeSnapshot : public pstd::noncopyable { +class ScopeSnapshot : public kstd::noncopyable { public: ScopeSnapshot(rocksdb::DB* db, const rocksdb::Snapshot** snapshot) : db_(db), snapshot_(snapshot) { *snapshot_ = db_->GetSnapshot(); diff --git a/src/storage/src/storage.cc b/src/storage/src/storage.cc index db1b9d6e..5aad12d3 100644 --- a/src/storage/src/storage.cc +++ b/src/storage/src/storage.cc @@ -12,9 +12,6 @@ #include "binlog.pb.h" #include "config.h" -#include "pstd/kiwi_slot.h" -#include "pstd/log.h" -#include "pstd/pstd_string.h" #include "rocksdb/utilities/checkpoint.h" #include "scope_snapshot.h" #include "src/lru_cache.h" @@ -23,6 +20,9 @@ #include "src/redis.h" #include "src/redis_hyperloglog.h" #include "src/type_iterator.h" +#include "std/kiwi_slot.h" +#include "std/log.h" +#include "std/std_string.h" #include "storage/slot_indexer.h" #include "storage/storage.h" #include "storage/util.h" @@ -123,8 +123,8 @@ static int RecursiveLinkAndCopy(const std::filesystem::path& source, const std:: DEBUG("copy success! source_file = {} , destination_file = {}", source.string(), destination.string()); } } else { - if (!pstd::FileExists(destination)) { - if (pstd::CreateDir(destination) != 0) { + if (!kstd::FileExists(destination)) { + if (kstd::CreateDir(destination) != 0) { WARN("create dir {} fail", destination.string()); return -1; } @@ -182,7 +182,7 @@ Status Storage::CreateCheckpointInternal(const std::string& checkpoint_path, int auto tmp_dir = source_dir + ".tmp"; // 1) Make sure the temporary directory does not exist - if (!pstd::DeleteDirIfExist(tmp_dir)) { + if (!kstd::DeleteDirIfExist(tmp_dir)) { WARN("DB{}'s RocksDB {} delete directory fail!", db_id_, index); return Status::IOError("DeleteDirIfExist() fail! dir_name : {} ", tmp_dir); } @@ -205,19 +205,19 @@ Status Storage::CreateCheckpointInternal(const std::string& checkpoint_path, int } // 4) Make sure the source directory does not exist - if (!pstd::DeleteDirIfExist(source_dir)) { + if (!kstd::DeleteDirIfExist(source_dir)) { WARN("DB{}'s RocksDB {} delete directory {} fail!", db_id_, index, source_dir); - if (!pstd::DeleteDirIfExist(tmp_dir)) { + if (!kstd::DeleteDirIfExist(tmp_dir)) { WARN("DB{}'s RocksDB {} fail to delete the temporary directory {} ", db_id_, index, tmp_dir); } return Status::IOError("DeleteDirIfExist() fail! dir_name : {} ", source_dir); } // 5) Rename the temporary directory to source directory - if (auto status = pstd::RenameFile(tmp_dir, source_dir); status != 0) { + if (auto status = kstd::RenameFile(tmp_dir, source_dir); status != 0) { WARN("DB{}'s RocksDB {} rename temporary directory {} to source directory {} fail!", db_id_, index, tmp_dir, source_dir); - if (!pstd::DeleteDirIfExist(tmp_dir)) { + if (!kstd::DeleteDirIfExist(tmp_dir)) { WARN("DB{}'s RocksDB {} fail to delete the rename failed directory {} ", db_id_, index, tmp_dir); } return Status::IOError("Rename directory {} fail!", tmp_dir); @@ -249,21 +249,21 @@ Status Storage::LoadCheckpointInternal(const std::string& checkpoint_sub_path, c auto source_dir = AppendSubDirectory(checkpoint_sub_path, index); // 1) Rename the original db to db.tmp, and only perform the maximum possible recovery of data // when loading the checkpoint fails. - if (auto status = pstd::RenameFile(rocksdb_path, tmp_rocksdb_path); status != 0) { + if (auto status = kstd::RenameFile(rocksdb_path, tmp_rocksdb_path); status != 0) { WARN("DB{}'s RocksDB {} rename db directory {} to temporary directory {} fail!", db_id_, index, rocksdb_path, tmp_rocksdb_path); return Status::IOError("Rename directory {} fail!", rocksdb_path); } // 2) Create a db directory to save the checkpoint. - if (0 != pstd::CreatePath(rocksdb_path)) { - pstd::RenameFile(tmp_rocksdb_path, rocksdb_path); + if (0 != kstd::CreatePath(rocksdb_path)) { + kstd::RenameFile(tmp_rocksdb_path, rocksdb_path); WARN("DB{}'s RocksDB {} load a checkpoint from {} fail!", db_id_, index, checkpoint_sub_path); return Status::IOError("Create directory {} fail!", rocksdb_path); } if (RecursiveLinkAndCopy(source_dir, rocksdb_path) != 0) { - pstd::DeleteDir(rocksdb_path); - pstd::RenameFile(tmp_rocksdb_path, rocksdb_path); + kstd::DeleteDir(rocksdb_path); + kstd::RenameFile(tmp_rocksdb_path, rocksdb_path); WARN("DB{}'s RocksDB {} load a checkpoint from {} fail!", db_id_, index, source_dir); return Status::IOError("recursive link and copy directory {} fail!", rocksdb_path); } diff --git a/src/storage/src/util.cc b/src/storage/src/util.cc index e1f77e3f..a46b62a9 100644 --- a/src/storage/src/util.cc +++ b/src/storage/src/util.cc @@ -10,10 +10,10 @@ #include #include -#include "pstd/pstd_string.h" #include "src/base_data_key_format.h" #include "src/base_key_format.h" #include "src/coding.h" +#include "std/std_string.h" #include "storage/storage_define.h" #include "storage/util.h" @@ -30,21 +30,21 @@ namespace storage { * * Modified in order to handle signed integers since the original code was * designed for unsigned integers. */ -int Int64ToStr(char* dst, size_t dstlen, int64_t svalue) { return pstd::Ll2string(dst, dstlen, svalue); } +int Int64ToStr(char* dst, size_t dstlen, int64_t svalue) { return kstd::Ll2string(dst, dstlen, svalue); } /* Convert a string into a long long. Returns 1 if the string could be parsed * into a (non-overflowing) long long, 0 otherwise. The value will be set to * the parsed value when appropriate. */ -int StrToInt64(const char* s, size_t slen, int64_t* value) { return pstd::String2int(s, slen, value); } +int StrToInt64(const char* s, size_t slen, int64_t* value) { return kstd::String2int(s, slen, value); } /* Convert a string into a long long. Returns 1 if all char of string could be parsed * into a (non-overflowing) long long, 0 otherwise. The value will be set to * the parsed value when appropriate. */ -int StrToInt64Strict(const char* s, size_t slen, int64_t* value) { return pstd::String2intStrict(s, slen, value); } +int StrToInt64Strict(const char* s, size_t slen, int64_t* value) { return kstd::String2intStrict(s, slen, value); } /* Glob-style pattern matching. */ int StringMatch(const char* pattern, uint64_t pattern_len, const char* str, uint64_t string_len, int nocase) { - return pstd::StringMatchLen(pattern, static_cast(pattern_len), str, static_cast(string_len), + return kstd::StringMatchLen(pattern, static_cast(pattern_len), str, static_cast(string_len), nocase); } diff --git a/src/storage/tests/CMakeLists.txt b/src/storage/tests/CMakeLists.txt index d0de9db6..7685d632 100644 --- a/src/storage/tests/CMakeLists.txt +++ b/src/storage/tests/CMakeLists.txt @@ -30,7 +30,7 @@ FOREACH (TEST_SOURCE ${TEST_SOURCES}) PRIVATE gtest_main PRIVATE fmt PRIVATE spdlog - PRIVATE pstd + PRIVATE kstd PRIVATE rocksdb PRIVATE snappy PRIVATE lz4 diff --git a/src/storage/tests/flush_oldest_cf_test.cc b/src/storage/tests/flush_oldest_cf_test.cc index 19f8f96c..db4b4cf9 100644 --- a/src/storage/tests/flush_oldest_cf_test.cc +++ b/src/storage/tests/flush_oldest_cf_test.cc @@ -21,10 +21,10 @@ #include "rocksdb/metadata.h" #include "rocksdb/options.h" -#include "pstd/log.h" -#include "pstd/thread_pool.h" #include "src/log_index.h" #include "src/redis.h" +#include "std/log.h" +#include "std/thread_pool.h" #include "storage/storage.h" #include "storage/util.h" @@ -40,7 +40,7 @@ LogIniter log_initer; using LogIndex = int64_t; -class LogQueue : public pstd::noncopyable { +class LogQueue : public kstd::noncopyable { public: using WriteCallback = std::function; @@ -57,7 +57,7 @@ class LogQueue : public pstd::noncopyable { private: WriteCallback write_cb_ = nullptr; - pstd::ThreadPool consumer_; + kstd::ThreadPool consumer_; std::atomic next_log_idx_{1}; }; diff --git a/src/storage/tests/hashes_test.cc b/src/storage/tests/hashes_test.cc index 0547c06a..93e4e562 100644 --- a/src/storage/tests/hashes_test.cc +++ b/src/storage/tests/hashes_test.cc @@ -10,8 +10,8 @@ #include #include -#include "pstd/env.h" -#include "pstd/log.h" +#include "std/env.h" +#include "std/log.h" #include "storage/storage.h" #include "storage/util.h" @@ -33,7 +33,7 @@ class HashesTest : public ::testing::Test { ~HashesTest() override = default; void SetUp() override { - pstd::DeleteDirIfExist(db_path); + kstd::DeleteDirIfExist(db_path); mkdir(db_path.c_str(), 0755); options.options.create_if_missing = true; options.options.create_missing_column_families = true; @@ -2441,8 +2441,8 @@ TEST_F(HashesTest, PKHRScanRangeTest) { } int main(int argc, char** argv) { - if (!pstd::FileExists("./log")) { - pstd::CreatePath("./log"); + if (!kstd::FileExists("./log")) { + kstd::CreatePath("./log"); } // FLAGS_log_dir = "./log"; // FLAGS_minloglevel = 0; diff --git a/src/storage/tests/keys_test.cc b/src/storage/tests/keys_test.cc index 9e1eda4d..f082f54a 100644 --- a/src/storage/tests/keys_test.cc +++ b/src/storage/tests/keys_test.cc @@ -7,8 +7,8 @@ #include #include -#include "pstd/env.h" -#include "pstd/log.h" +#include "std/env.h" +#include "std/log.h" #include "storage/storage.h" #include "storage/util.h" @@ -33,7 +33,7 @@ class KeysTest : public ::testing::Test { ~KeysTest() override = default; void SetUp() override { - pstd::DeleteDirIfExist(db_path); + kstd::DeleteDirIfExist(db_path); mkdir(db_path.c_str(), 0755); options.options.create_if_missing = true; options.options.create_missing_column_families = true; @@ -5196,8 +5196,8 @@ TEST_F(KeysTest, TTLTest) { } int main(int argc, char** argv) { - if (!pstd::FileExists("./log")) { - pstd::CreatePath("./log"); + if (!kstd::FileExists("./log")) { + kstd::CreatePath("./log"); } // FLAGS_log_dir = "./log"; // FLAGS_minloglevel = 0; diff --git a/src/storage/tests/lists_test.cc b/src/storage/tests/lists_test.cc index 49df2e07..181fc9c9 100644 --- a/src/storage/tests/lists_test.cc +++ b/src/storage/tests/lists_test.cc @@ -7,8 +7,8 @@ #include #include -#include "pstd/env.h" -#include "pstd/log.h" +#include "std/env.h" +#include "std/log.h" #include "storage/storage.h" #include "storage/util.h" @@ -88,7 +88,7 @@ class ListsTest : public ::testing::Test { ~ListsTest() override = default; void SetUp() override { - pstd::DeleteDirIfExist(db_path); + kstd::DeleteDirIfExist(db_path); mkdir(db_path.c_str(), 0755); options.options.create_if_missing = true; options.options.create_missing_column_families = true; @@ -2711,8 +2711,8 @@ TEST_F(ListsTest, RPushxTest) { // NOLINT } int main(int argc, char** argv) { - if (!pstd::FileExists("./log")) { - pstd::CreatePath("./log"); + if (!kstd::FileExists("./log")) { + kstd::CreatePath("./log"); } // FLAGS_log_dir = "./log"; // FLAGS_minloglevel = 0; diff --git a/src/storage/tests/log_index_test.cc b/src/storage/tests/log_index_test.cc index ffc0b146..5692c581 100644 --- a/src/storage/tests/log_index_test.cc +++ b/src/storage/tests/log_index_test.cc @@ -19,10 +19,10 @@ #include "rocksdb/metadata.h" #include "rocksdb/options.h" -#include "pstd/log.h" -#include "pstd/thread_pool.h" #include "src/log_index.h" #include "src/redis.h" +#include "std/log.h" +#include "std/thread_pool.h" // #include "storage/storage.h" #include "storage/util.h" @@ -73,7 +73,7 @@ TEST(TablePropertyTest, SimpleTest) { DeleteFiles(kDbPath); } -class LogQueue : public pstd::noncopyable { +class LogQueue : public kstd::noncopyable { public: using WriteCallback = std::function; @@ -90,7 +90,7 @@ class LogQueue : public pstd::noncopyable { private: WriteCallback write_cb_ = nullptr; - pstd::ThreadPool consumer_; + kstd::ThreadPool consumer_; std::atomic next_log_idx_{1}; }; diff --git a/src/storage/tests/sets_test.cc b/src/storage/tests/sets_test.cc index c3c80702..83f54827 100644 --- a/src/storage/tests/sets_test.cc +++ b/src/storage/tests/sets_test.cc @@ -7,8 +7,8 @@ #include #include -#include "pstd/env.h" -#include "pstd/log.h" +#include "std/env.h" +#include "std/log.h" #include "storage/storage.h" #include "storage/util.h" @@ -30,7 +30,7 @@ class SetsTest : public ::testing::Test { ~SetsTest() override = default; void SetUp() override { - pstd::DeleteDirIfExist(db_path); + kstd::DeleteDirIfExist(db_path); mkdir(db_path.c_str(), 0755); options.options.create_if_missing = true; options.options.create_missing_column_families = true; @@ -2245,8 +2245,8 @@ TEST_F(SetsTest, SScanTest) { // NOLINT } int main(int argc, char** argv) { - if (!pstd::FileExists("./log")) { - pstd::CreatePath("./log"); + if (!kstd::FileExists("./log")) { + kstd::CreatePath("./log"); } // FLAGS_log_dir = "./log"; // FLAGS_minloglevel = 0; diff --git a/src/storage/tests/strings_test.cc b/src/storage/tests/strings_test.cc index c16000fb..248c92c3 100644 --- a/src/storage/tests/strings_test.cc +++ b/src/storage/tests/strings_test.cc @@ -8,8 +8,8 @@ #include #include -#include "pstd/env.h" -#include "pstd/log.h" +#include "std/env.h" +#include "std/log.h" #include "storage/storage.h" #include "storage/util.h" @@ -31,7 +31,7 @@ class StringsTest : public ::testing::Test { ~StringsTest() override = default; void SetUp() override { - pstd::DeleteDirIfExist(db_path); + kstd::DeleteDirIfExist(db_path); mkdir(db_path.c_str(), 0755); options.options.create_if_missing = true; options.options.create_missing_column_families = true; @@ -1014,8 +1014,8 @@ TEST_F(StringsTest, PKSetexAtTest) { } int main(int argc, char** argv) { - if (!pstd::FileExists("./log")) { - pstd::CreatePath("./log"); + if (!kstd::FileExists("./log")) { + kstd::CreatePath("./log"); } // FLAGS_log_dir = "./log"; // FLAGS_minloglevel = 0; diff --git a/src/storage/tests/zsets_test.cc b/src/storage/tests/zsets_test.cc index e3b7415f..b100c350 100644 --- a/src/storage/tests/zsets_test.cc +++ b/src/storage/tests/zsets_test.cc @@ -7,8 +7,8 @@ #include #include -#include "pstd/env.h" -#include "pstd/log.h" +#include "std/env.h" +#include "std/log.h" #include "storage/storage.h" #include "storage/util.h" @@ -34,7 +34,7 @@ class ZSetsTest : public ::testing::Test { ~ZSetsTest() override = default; void SetUp() override { - pstd::DeleteDirIfExist(db_path); + kstd::DeleteDirIfExist(db_path); mkdir(db_path.c_str(), 0755); options.options.create_if_missing = true; options.options.create_missing_column_families = true; @@ -5251,8 +5251,8 @@ TEST_F(ZSetsTest, ZScanTest) { // NOLINT } int main(int argc, char** argv) { - if (!pstd::FileExists("./log")) { - pstd::CreatePath("./log"); + if (!kstd::FileExists("./log")) { + kstd::CreatePath("./log"); } // FLAGS_log_dir = "./log"; // FLAGS_minloglevel = 0; diff --git a/src/store.cc b/src/store.cc index 7b449f49..037bdf47 100644 --- a/src/store.cc +++ b/src/store.cc @@ -15,8 +15,8 @@ #include "config.h" #include "db.h" -#include "pstd/log.h" -#include "pstd/pstd_string.h" +#include "std/log.h" +#include "std/std_string.h" namespace kiwi { @@ -53,7 +53,7 @@ void PStore::HandleTaskSpecificDB(const TasksVector& tasks) { return; } auto path = task.args.find(kCheckpointPath)->second; - pstd::TrimSlash(path); + kstd::TrimSlash(path); db->CreateCheckpoint(path, task.sync); break; } @@ -63,7 +63,7 @@ void PStore::HandleTaskSpecificDB(const TasksVector& tasks) { return; } auto path = task.args.find(kCheckpointPath)->second; - pstd::TrimSlash(path); + kstd::TrimSlash(path); db->LoadDBFromCheckpoint(path, task.sync); break; } From 7a5104481f284820e141607a95d9f4cd5155ded7 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Mon, 6 Jan 2025 18:34:18 +0800 Subject: [PATCH 06/35] PConfig -> Config --- .clang-tidy | 4 ++-- src/base_cmd.cc | 2 +- src/client.cc | 4 ++-- src/cmd_admin.cc | 30 +++++++++++++++--------------- src/cmd_hash.cc | 2 +- src/cmd_kv.cc | 2 +- src/cmd_raft.cc | 2 +- src/config.cc | 14 +++++++------- src/config.h | 16 ++++++++-------- src/db.cc | 28 ++++++++++++++-------------- src/kiwi.cc | 36 ++++++++++++++++++------------------ src/raft/raft.cc | 22 +++++++++++----------- src/replication.cc | 10 +++++----- src/store.cc | 2 +- 14 files changed, 87 insertions(+), 87 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index d15157f3..51b75626 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -58,8 +58,8 @@ Checks: ' # - { key: readability-identifier-naming.VariableCase, value: lower_case } WarningsAsErrors: '*' -# HeaderFilterRegex: '(|/src|/src/net|/src/pstd|/src/storage)/include' -# HeaderFilterRegex: '/src/(net|storage|pstd)/include' +# HeaderFilterRegex: '(|/src|/src/net|/src/std|/src/storage)/include' +# HeaderFilterRegex: '/src/(net|storage|std)/include' #### Disabled checks and why: ##### # diff --git a/src/base_cmd.cc b/src/base_cmd.cc index 3c57a42f..59743ca1 100644 --- a/src/base_cmd.cc +++ b/src/base_cmd.cc @@ -42,7 +42,7 @@ void BaseCmd::Execute(PClient* client) { DEBUG("execute command: {}", client->CmdName()); // read consistency (lease read) / write redirection - if (kiwi::PConfig::GetInstance().use_raft && (HasFlag(kCmdFlagsReadonly) || HasFlag(kCmdFlagsWrite))) { + if (kiwi::Config::GetInstance().use_raft && (HasFlag(kCmdFlagsReadonly) || HasFlag(kCmdFlagsWrite))) { if (!RAFT_INST.IsInitialized()) { return client->SetRes(CmdRes::kErrOther, "RAFT_INST is not initialized"); } diff --git a/src/client.cc b/src/client.cc index cf8b3620..c3790b2d 100644 --- a/src/client.cc +++ b/src/client.cc @@ -239,7 +239,7 @@ void PClient::OnConnect() { SetName("MasterConnection"); SetFlag(kClientFlagMaster); - if (kiwi::PConfig::GetInstance().master_auth.empty()) { + if (kiwi::Config::GetInstance().master_auth.empty()) { SetAuth(); } @@ -247,7 +247,7 @@ void PClient::OnConnect() { RAFT_INST.SendNodeRequest(this); } } else { - if (kiwi::PConfig::GetInstance().password.empty()) { + if (kiwi::Config::GetInstance().password.empty()) { SetAuth(); } } diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index b92ea2fe..2d86e622 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -57,7 +57,7 @@ bool CmdConfigGet::DoInitial(PClient* client) { return true; } void CmdConfigGet::DoCmd(PClient* client) { std::vector results; for (int i = 0; i < client->argv_.size() - 2; i++) { - kiwi::PConfig::GetInstance().Get(client->argv_[i + 2], &results); + kiwi::Config::GetInstance().Get(client->argv_[i + 2], &results); } client->AppendStringVector(results); } @@ -68,7 +68,7 @@ CmdConfigSet::CmdConfigSet(const std::string& name, int16_t arity) bool CmdConfigSet::DoInitial(PClient* client) { return true; } void CmdConfigSet::DoCmd(PClient* client) { - auto s = kiwi::PConfig::GetInstance().Set(client->argv_[2], client->argv_[3]); + auto s = kiwi::Config::GetInstance().Set(client->argv_[2], client->argv_[3]); if (!s.ok()) { client->SetRes(CmdRes::kInvalidParameter); } else { @@ -87,7 +87,7 @@ void FlushdbCmd::DoCmd(PClient* client) { PSTORE.GetBackend(currentDBIndex).get()->Lock(); DEFER { PSTORE.GetBackend(currentDBIndex).get()->UnLock(); }; - std::string db_path = kiwi::PConfig::GetInstance().db_path + std::to_string(currentDBIndex); + std::string db_path = kiwi::Config::GetInstance().db_path + std::to_string(currentDBIndex); std::string path_temp = db_path; path_temp.append("_deleting/"); kstd::RenameFile(db_path, path_temp); @@ -108,9 +108,9 @@ FlushallCmd::FlushallCmd(const std::string& name, int16_t arity) bool FlushallCmd::DoInitial(PClient* client) { return true; } void FlushallCmd::DoCmd(PClient* client) { - for (size_t i = 0; i < kiwi::PConfig::GetInstance().databases; ++i) { + for (size_t i = 0; i < kiwi::Config::GetInstance().databases; ++i) { PSTORE.GetBackend(i).get()->Lock(); - std::string db_path = kiwi::PConfig::GetInstance().db_path + std::to_string(i); + std::string db_path = kiwi::Config::GetInstance().db_path + std::to_string(i); std::string path_temp = db_path; path_temp.append("_deleting/"); kstd::RenameFile(db_path, path_temp); @@ -134,12 +134,12 @@ void AuthCmd::DoCmd(PClient* client) { return; } - if (kiwi::PConfig::GetInstance().password == "") { + if (kiwi::Config::GetInstance().password == "") { client->SetRes(CmdRes::kErrOther, "Client sent AUTH, but no password is set"); } std::string password = client->argv_[1]; - if (password != kiwi::PConfig::GetInstance().password) { + if (password != kiwi::Config::GetInstance().password) { client->SetRes(CmdRes::kInvalidPwd); } else { client->SetAuth(); @@ -154,7 +154,7 @@ bool SelectCmd::DoInitial(PClient* client) { return true; } void SelectCmd::DoCmd(PClient* client) { int index = atoi(client->argv_[1].c_str()); - if (index < 0 || index >= kiwi::PConfig::GetInstance().databases) { + if (index < 0 || index >= kiwi::Config::GetInstance().databases) { client->SetRes(CmdRes::kInvalidIndex, kCmdNameSelect + " DB index is out of range"); return; } @@ -168,7 +168,7 @@ ShutdownCmd::ShutdownCmd(const std::string& name, int16_t arity) bool ShutdownCmd::DoInitial(PClient* client) { // For now, only shutdown need check local if (client->PeerIP().find("127.0.0.1") == std::string::npos && - client->PeerIP().find(kiwi::PConfig::GetInstance().ip) == std::string::npos) { + client->PeerIP().find(kiwi::Config::GetInstance().ip) == std::string::npos) { client->SetRes(CmdRes::kErrOther, kCmdNameShutdown + " should be localhost"); return false; } @@ -233,7 +233,7 @@ void HelloCmd::DoCmd(PClient* client) { if (client->GetAuth()) { continue; } - if (client->argv_[next_arg + 1] != kiwi::PConfig::GetInstance().password) { + if (client->argv_[next_arg + 1] != kiwi::Config::GetInstance().password) { client->SetRes(CmdRes::kErrOther, "invalid password"); return; } else { @@ -269,7 +269,7 @@ void HelloCmd::Hello(PClient* client) { client->AppendInteger(static_cast(client->GetUniqueID())); client->AppendString("mode"); - if (!kiwi::PConfig::GetInstance().use_raft) { + if (!kiwi::Config::GetInstance().use_raft) { client->AppendString("standalone"); } else { client->AppendString("cluster"); @@ -453,8 +453,8 @@ void InfoCmd::InfoServer(std::string& info) { tmp_stream << "os:" << host_info.sysname << " " << host_info.release << " " << host_info.machine << "\r\n"; tmp_stream << "arch_bits:" << (reinterpret_cast(&host_info.machine) + strlen(host_info.machine) - 2) << "\r\n"; tmp_stream << "process_id:" << getpid() << "\r\n"; - tmp_stream << "run_id:" << static_cast(kiwi::PConfig::GetInstance().run_id) << "\r\n"; - tmp_stream << "tcp_port:" << kiwi::PConfig::GetInstance().port << "\r\n"; + tmp_stream << "run_id:" << static_cast(kiwi::Config::GetInstance().run_id) << "\r\n"; + tmp_stream << "tcp_port:" << kiwi::Config::GetInstance().port << "\r\n"; tmp_stream << "uptime_in_seconds:" << (current_time_s - g_kiwi->GetStartTime()) << "\r\n"; tmp_stream << "uptime_in_days:" << (current_time_s / (24 * 3600) - g_kiwi->GetStartTime() / (24 * 3600) + 1) << "\r\n"; @@ -497,8 +497,8 @@ void InfoCmd::InfoCPU(std::string& info) { } void InfoCmd::InfoData(std::string& message) { - message += DATABASES_NUM + std::string(":") + std::to_string(kiwi::PConfig::GetInstance().databases) + "\r\n"; - message += ROCKSDB_NUM + std::string(":") + std::to_string(kiwi::PConfig::GetInstance().db_instance_num) + "\r\n"; + message += DATABASES_NUM + std::string(":") + std::to_string(kiwi::Config::GetInstance().databases) + "\r\n"; + message += ROCKSDB_NUM + std::string(":") + std::to_string(kiwi::Config::GetInstance().db_instance_num) + "\r\n"; message += ROCKSDB_VERSION + std::string(":") + ROCKSDB_NAMESPACE::GetRocksVersionAsString() + "\r\n"; } diff --git a/src/cmd_hash.cc b/src/cmd_hash.cc index 261803f8..097883bc 100644 --- a/src/cmd_hash.cc +++ b/src/cmd_hash.cc @@ -172,7 +172,7 @@ void HGetAllCmd::DoCmd(PClient* client) { int64_t total_fv = 0; int64_t cursor = 0; int64_t next_cursor = 0; - size_t raw_limit = kiwi::PConfig::GetInstance().max_client_response_size; + size_t raw_limit = kiwi::Config::GetInstance().max_client_response_size; std::string raw; std::vector fvs; storage::Status s; diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 29c4bdab..9895d645 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -680,7 +680,7 @@ void SetRangeCmd::DoCmd(PClient* client) { return; } // Ref: https://redis.io/docs/latest/commands/setrange/ - if (kiwi::PConfig::GetInstance().redis_compatible_mode && std::atoi(client->argv_[2].c_str()) > 536870911) { + if (kiwi::Config::GetInstance().redis_compatible_mode && std::atoi(client->argv_[2].c_str()) > 536870911) { client->SetRes(CmdRes::kErrOther, "When Redis compatibility mode is enabled, the offset parameter must not exceed 536870911"); return; diff --git a/src/cmd_raft.cc b/src/cmd_raft.cc index 165b27ff..1da14241 100644 --- a/src/cmd_raft.cc +++ b/src/cmd_raft.cc @@ -103,7 +103,7 @@ void RaftNodeCmd::DoCmdRemove(PClient* client) { // Connect target std::string peer_ip = butil::ip2str(leader_peer_id.addr.ip).c_str(); - auto port = leader_peer_id.addr.port - kiwi::PConfig::GetInstance().raft_port_offset; + auto port = leader_peer_id.addr.port - kiwi::Config::GetInstance().raft_port_offset; auto peer_id = client->argv_[2]; auto ret = RAFT_INST.GetClusterCmdCtx().Set(ClusterCmdType::kRemove, client, std::move(peer_ip), port, std::move(peer_id)); diff --git a/src/config.cc b/src/config.cc index e600bc59..3bf2c797 100644 --- a/src/config.cc +++ b/src/config.cc @@ -23,7 +23,7 @@ constexpr int DBNUMBER_MAX = 16; constexpr int THREAD_MAX = 129; constexpr int ROCKSDB_INSTANCE_NUMBER_MAX = 10; -PConfig& g_config = kiwi::PConfig::GetInstance(); +Config& g_config = kiwi::Config::GetInstance(); // preprocess func static void EraseQuotes(std::string& str) { @@ -130,7 +130,7 @@ Status MemorySize::SetValue(const std::string& value) { return Status::OK(); } -PConfig::PConfig() { +Config::Config() { AddBool("redis-compatible-mode", &CheckYesNo, true, &redis_compatible_mode); AddBool("daemonize", &CheckYesNo, false, &daemonize); AddString("ip", false, &ip); @@ -170,7 +170,7 @@ PConfig::PConfig() { AddNumber("rocksdb-level0-slowdown-writes-trigger", false, &rocksdb_level0_slowdown_writes_trigger); } -bool PConfig::LoadFromFile(const std::string& file_name) { +bool Config::LoadFromFile(const std::string& file_name) { config_file_name_ = file_name; if (!parser_.Load(file_name.c_str())) { return false; @@ -206,7 +206,7 @@ bool PConfig::LoadFromFile(const std::string& file_name) { return true; } -void PConfig::Get(const std::string& key, std::vector* values) const { +void Config::Get(const std::string& key, std::vector* values) const { values->clear(); for (const auto& [k, v] : config_map_) { if (key == "*" || kstd::StringMatch(key.c_str(), k.c_str(), 1)) { @@ -216,7 +216,7 @@ void PConfig::Get(const std::string& key, std::vector* values) cons } } -Status PConfig::Set(std::string key, const std::string& value, bool init_stage) { +Status Config::Set(std::string key, const std::string& value, bool init_stage) { std::transform(key.begin(), key.end(), key.begin(), ::tolower); auto iter = config_map_.find(key); if (iter == config_map_.end()) { @@ -225,7 +225,7 @@ Status PConfig::Set(std::string key, const std::string& value, bool init_stage) return iter->second->Set(value, init_stage); } -rocksdb::Options PConfig::GetRocksDBOptions() { +rocksdb::Options Config::GetRocksDBOptions() { rocksdb::Options options; options.create_if_missing = true; options.create_missing_column_families = true; @@ -242,7 +242,7 @@ rocksdb::Options PConfig::GetRocksDBOptions() { return options; } -rocksdb::BlockBasedTableOptions PConfig::GetRocksDBBlockBasedTableOptions() { +rocksdb::BlockBasedTableOptions Config::GetRocksDBBlockBasedTableOptions() { rocksdb::BlockBasedTableOptions options; return options; } diff --git a/src/config.h b/src/config.h index 5bfbd717..f1b60f00 100644 --- a/src/config.h +++ b/src/config.h @@ -29,7 +29,7 @@ namespace kiwi { using Status = rocksdb::Status; using CheckFunc = std::function; -class PConfig; +class Config; class BaseValue { public: @@ -146,7 +146,7 @@ using ConfigMap = std::unordered_map; * PConfig holds information about kiwi * server-side runtime information. */ -class PConfig { +class Config { public: /* Some important, globally relevant public interfaces. */ @@ -154,18 +154,18 @@ class PConfig { * PConfig() * Initialize kiwi's config & RocksDB's config. */ - static PConfig& GetInstance() { - static PConfig instance; + static Config& GetInstance() { + static Config instance; return instance; } - PConfig(const PConfig&) = delete; - PConfig& operator=(const PConfig&) = delete; + Config(const Config&) = delete; + Config& operator=(const Config&) = delete; /*------------------------ * ~PConfig() * Destroy a kiwi's config instance. */ - ~PConfig() = default; + ~Config() = default; /*------------------------ * LoadFromFile(const std::string& file_name) @@ -495,6 +495,6 @@ class PConfig { // The file name of the config std::string config_file_name_; - PConfig(); + Config(); }; } // namespace kiwi diff --git a/src/db.cc b/src/db.cc index 2ff0938e..f7510ddb 100644 --- a/src/db.cc +++ b/src/db.cc @@ -24,17 +24,17 @@ DB::~DB() { INFO("DB{} is closing...", db_index_); } rocksdb::Status DB::Open() { storage::StorageOptions storage_options; - storage_options.options = kiwi::PConfig::GetInstance().GetRocksDBOptions(); - storage_options.table_options = kiwi::PConfig::GetInstance().GetRocksDBBlockBasedTableOptions(); + storage_options.options = kiwi::Config::GetInstance().GetRocksDBOptions(); + storage_options.table_options = kiwi::Config::GetInstance().GetRocksDBBlockBasedTableOptions(); - storage_options.options.ttl = kiwi::PConfig::GetInstance().rocksdb_ttl_second; - storage_options.options.periodic_compaction_seconds = kiwi::PConfig::GetInstance().rocksdb_periodic_second; + storage_options.options.ttl = kiwi::Config::GetInstance().rocksdb_ttl_second; + storage_options.options.periodic_compaction_seconds = kiwi::Config::GetInstance().rocksdb_periodic_second; - storage_options.small_compaction_threshold = kiwi::PConfig::GetInstance().small_compaction_threshold; + storage_options.small_compaction_threshold = kiwi::Config::GetInstance().small_compaction_threshold; storage_options.small_compaction_duration_threshold = - kiwi::PConfig::GetInstance().small_compaction_duration_threshold; + kiwi::Config::GetInstance().small_compaction_duration_threshold; - if (kiwi::PConfig::GetInstance().use_raft) { + if (kiwi::Config::GetInstance().use_raft) { storage_options.append_log_function = [&r = RAFT_INST](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; @@ -44,7 +44,7 @@ rocksdb::Status DB::Open() { }; } - storage_options.db_instance_num = kiwi::PConfig::GetInstance().db_instance_num; + storage_options.db_instance_num = kiwi::Config::GetInstance().db_instance_num; storage_options.db_id = db_index_; std::unique_ptr old_storage = std::move(storage_); @@ -109,14 +109,14 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma } storage::StorageOptions storage_options; - storage_options.options = kiwi::PConfig::GetInstance().GetRocksDBOptions(); - storage_options.db_instance_num = kiwi::PConfig::GetInstance().db_instance_num; + storage_options.options = kiwi::Config::GetInstance().GetRocksDBOptions(); + storage_options.db_instance_num = kiwi::Config::GetInstance().db_instance_num; storage_options.db_id = db_index_; // options for CF - storage_options.options.ttl = kiwi::PConfig::GetInstance().rocksdb_ttl_second; - storage_options.options.periodic_compaction_seconds = kiwi::PConfig::GetInstance().rocksdb_periodic_second; - if (kiwi::PConfig::GetInstance().use_raft) { + storage_options.options.ttl = kiwi::Config::GetInstance().rocksdb_ttl_second; + storage_options.options.periodic_compaction_seconds = kiwi::Config::GetInstance().rocksdb_periodic_second; + if (kiwi::Config::GetInstance().use_raft) { storage_options.append_log_function = [&r = RAFT_INST](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; @@ -130,7 +130,7 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma } // in single-mode, kiwi will enable wal - if (!kiwi::PConfig::GetInstance().use_raft) { + if (!kiwi::Config::GetInstance().use_raft) { storage_->DisableWal(false); } diff --git a/src/kiwi.cc b/src/kiwi.cc index e2e661bb..dd03a058 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -163,46 +163,46 @@ void KiwiDB::OnNewConnection(uint64_t connId, std::shared_ptr& cl bool KiwiDB::Init() { char runid[kRunidSize + 1] = ""; getRandomHexChars(runid, kRunidSize); - kiwi::PConfig::GetInstance().Set("runid", {runid, kRunidSize}, true); + kiwi::Config::GetInstance().Set("runid", {runid, kRunidSize}, true); if (port_ != 0) { - kiwi::PConfig::GetInstance().Set("port", std::to_string(port_), true); + kiwi::Config::GetInstance().Set("port", std::to_string(port_), true); } if (!options_.GetLogLevel().empty()) { - kiwi::PConfig::GetInstance().Set("log-level", options_.GetLogLevel(), true); + kiwi::Config::GetInstance().Set("log-level", options_.GetLogLevel(), true); } if (options_.GetRedisCompatibleMode()) { - kiwi::PConfig::GetInstance().Set("redis_compatible_mode", std::to_string(options_.GetRedisCompatibleMode()), true); + kiwi::Config::GetInstance().Set("redis_compatible_mode", std::to_string(options_.GetRedisCompatibleMode()), true); } - auto num = kiwi::PConfig::GetInstance().worker_threads_num + kiwi::PConfig::GetInstance().slave_threads_num; + auto num = kiwi::Config::GetInstance().worker_threads_num + kiwi::Config::GetInstance().slave_threads_num; options_.SetThreadNum(num); // now we only use fast cmd thread pool - auto status = cmd_threads_.Init(kiwi::PConfig::GetInstance().fast_cmd_threads_num, 1, "kiwi-cmd"); + auto status = cmd_threads_.Init(kiwi::Config::GetInstance().fast_cmd_threads_num, 1, "kiwi-cmd"); if (!status.ok()) { ERROR("init cmd thread pool failed: {}", status.ToString()); return false; } - PSTORE.Init(kiwi::PConfig::GetInstance().databases); + PSTORE.Init(kiwi::Config::GetInstance().databases); - PSlowLog::Instance().SetThreshold(kiwi::PConfig::GetInstance().slow_log_time); - PSlowLog::Instance().SetLogLimit(static_cast(kiwi::PConfig::GetInstance().slow_log_max_len)); + PSlowLog::Instance().SetThreshold(kiwi::Config::GetInstance().slow_log_time); + PSlowLog::Instance().SetLogLimit(static_cast(kiwi::Config::GetInstance().slow_log_max_len)); // master ip - if (!kiwi::PConfig::GetInstance().master_ip.empty()) { - PREPL.SetMasterAddr(kiwi::PConfig::GetInstance().master_ip.c_str(), kiwi::PConfig::GetInstance().master_port); + if (!kiwi::Config::GetInstance().master_ip.empty()) { + PREPL.SetMasterAddr(kiwi::Config::GetInstance().master_ip.c_str(), kiwi::Config::GetInstance().master_port); } options_.SetRwSeparation(true); event_server_ = std::make_unique>>(options_); - net::SocketAddr addr(kiwi::PConfig::GetInstance().ip, kiwi::PConfig::GetInstance().port); - INFO("Add listen addr:{}, port:{}", kiwi::PConfig::GetInstance().ip, kiwi::PConfig::GetInstance().port); + net::SocketAddr addr(kiwi::Config::GetInstance().ip, kiwi::Config::GetInstance().port); + INFO("Add listen addr:{}, port:{}", kiwi::Config::GetInstance().ip, kiwi::Config::GetInstance().port); event_server_->AddListenAddr(addr); event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); @@ -273,7 +273,7 @@ static void InitLogs() { static int InitLimit() { rlimit limit; - rlim_t maxfiles = kiwi::PConfig::GetInstance().max_clients; + rlim_t maxfiles = kiwi::Config::GetInstance().max_clients; if (getrlimit(RLIMIT_NOFILE, &limit) == -1) { WARN("getrlimit error: {}", strerror(errno)); } else if (limit.rlim_cur < maxfiles) { @@ -324,13 +324,13 @@ int main(int argc, char* argv[]) { } if (!g_kiwi->GetConfigName().empty()) { - if (!kiwi::PConfig::GetInstance().LoadFromFile(g_kiwi->GetConfigName())) { + if (!kiwi::Config::GetInstance().LoadFromFile(g_kiwi->GetConfigName())) { std::cerr << "Load config file [" << g_kiwi->GetConfigName() << "] failed!\n"; return -1; } } - if (kiwi::PConfig::GetInstance().daemonize) { + if (kiwi::Config::GetInstance().daemonize) { daemonize(); } @@ -339,7 +339,7 @@ int main(int argc, char* argv[]) { InitLogs(); InitLimit(); - if (kiwi::PConfig::GetInstance().daemonize) { + if (kiwi::Config::GetInstance().daemonize) { closeStd(); } @@ -347,7 +347,7 @@ int main(int argc, char* argv[]) { // output logo to console char logo[1024] = ""; snprintf(logo, sizeof logo - 1, kiwiLogo, KIWI_VERSION, static_cast(sizeof(void*)) * 8, - static_cast(kiwi::PConfig::GetInstance().port)); + static_cast(kiwi::Config::GetInstance().port)); std::cout << logo; g_kiwi->Run(); } diff --git a/src/raft/raft.cc b/src/raft/raft.cc index 14bdfb23..9c69779b 100644 --- a/src/raft/raft.cc +++ b/src/raft/raft.cc @@ -97,7 +97,7 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { } server_ = std::make_unique(); - auto port = kiwi::PConfig::GetInstance().port + kiwi::PConfig::GetInstance().raft_port_offset; + auto port = kiwi::Config::GetInstance().port + kiwi::Config::GetInstance().raft_port_offset; // Add your service into RPC server DummyServiceImpl service(&RAFT_INST); if (server_->AddService(&service, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) { @@ -126,10 +126,10 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { assert(group_id.size() == RAFT_GROUPID_LEN); this->group_id_ = group_id; - // FIXME: kiwi::PConfig::GetInstance().ip is default to 127.0.0.0, which may not work in cluster. - raw_addr_ = kiwi::PConfig::GetInstance().ip + ":" + std::to_string(port); + // FIXME: kiwi::Config::GetInstance().ip is default to 127.0.0.0, which may not work in cluster. + raw_addr_ = kiwi::Config::GetInstance().ip + ":" + std::to_string(port); butil::ip_t ip; - auto ret = butil::str2ip(kiwi::PConfig::GetInstance().ip.c_str(), &ip); + auto ret = butil::str2ip(kiwi::Config::GetInstance().ip.c_str(), &ip); if (ret != 0) { server_.reset(); return ERROR_LOG_AND_STATUS("Failed to convert str_ip to butil::ip_t"); @@ -157,7 +157,7 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { node_options_.fsm = this; node_options_.node_owns_fsm = false; node_options_.snapshot_interval_s = 0; - std::string prefix = "local://" + kiwi::PConfig::GetInstance().db_path + std::to_string(db_id_) + "/_raft"; + std::string prefix = "local://" + kiwi::Config::GetInstance().db_path + std::to_string(db_id_) + "/_raft"; node_options_.log_uri = prefix + "/log"; node_options_.raft_meta_uri = prefix + "/raft_meta"; node_options_.snapshot_uri = prefix + "/snapshot"; @@ -221,7 +221,7 @@ std::string Raft::GetLeaderAddress() const { return std::string(); } - id.addr.port -= kiwi::PConfig::GetInstance().raft_port_offset; + id.addr.port -= kiwi::Config::GetInstance().raft_port_offset; auto addr = butil::endpoint2str(id.addr); return addr.c_str(); } @@ -323,8 +323,8 @@ void Raft::SendNodeAddRequest(PClient* client) { // Node id in braft are ip:port, the node id param in RAFT.NODE ADD cmd will be ignored. int unused_node_id = 0; - auto port = kiwi::PConfig::GetInstance().port + kiwi::PConfig::GetInstance().raft_port_offset; - auto raw_addr = kiwi::PConfig::GetInstance().ip + ":" + std::to_string(port); + auto port = kiwi::Config::GetInstance().port + kiwi::Config::GetInstance().raft_port_offset; + auto raw_addr = kiwi::Config::GetInstance().ip + ":" + std::to_string(port); client->AppendArrayLen(int64_t(4)); client->AppendString("RAFT.NODE"); @@ -394,8 +394,8 @@ void Raft::CheckRocksDBConfiguration(PClient* client, PClient* join_client, cons } } - int current_databases_num = kiwi::PConfig::GetInstance().databases; - int current_rocksdb_num = kiwi::PConfig::GetInstance().db_instance_num; + int current_databases_num = kiwi::Config::GetInstance().databases; + int current_rocksdb_num = kiwi::Config::GetInstance().db_instance_num; std::string current_rocksdb_version = ROCKSDB_NAMESPACE::GetRocksVersionAsString(); if (current_databases_num != databases_num || current_rocksdb_num != rocksdb_num || current_rocksdb_version != rockdb_version) { @@ -714,7 +714,7 @@ int Raft::on_snapshot_load(braft::SnapshotReader* reader) { // 3. When a snapshot is installed on a node, you do not need to set a playback point. auto reader_path = reader->get_path(); // xx/snapshot_0000001 - auto path = kiwi::PConfig::GetInstance().db_path + std::to_string(db_id_); // db/db_id + auto path = kiwi::Config::GetInstance().db_path + std::to_string(db_id_); // db/db_id TasksVector tasks(1, {TaskType::kLoadDBFromCheckpoint, db_id_, {{TaskArg::kCheckpointPath, reader_path}}, true}); PSTORE.HandleTaskSpecificDB(tasks); INFO("load snapshot success!"); diff --git a/src/replication.cc b/src/replication.cc index e7a8ee7e..dec51994 100644 --- a/src/replication.cc +++ b/src/replication.cc @@ -98,7 +98,7 @@ void PReplication::TryBgsave() { // if (ret == 0) { // { // PDBSaver qdb; - // qdb.Save(kiwi::PConfig::GetInstance().rdbfullname.c_str()); + // qdb.Save(kiwi::Config::GetInstance().rdbfullname.c_str()); // DEBUG("PReplication save rdb done, exiting child"); // } // _exit(0); @@ -177,8 +177,8 @@ void PReplication::Cron() { if (masterInfo_.addr.IsValid()) { switch (masterInfo_.state) { case kPReplStateNone: { - if (masterInfo_.addr.GetIP() == kiwi::PConfig::GetInstance().ip && - masterInfo_.addr.GetPort() == kiwi::PConfig::GetInstance().port) { + if (masterInfo_.addr.GetIP() == kiwi::Config::GetInstance().ip && + masterInfo_.addr.GetPort() == kiwi::Config::GetInstance().port) { ERROR("Fix config, master addr is self addr!"); assert(!!!"wrong config for master addr"); } @@ -226,12 +226,12 @@ void PReplication::Cron() { // send replconf char req[128]; auto len = - snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", kiwi::PConfig::GetInstance().port); + snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", kiwi::Config::GetInstance().port); std::string info(req, len); master->SendPacket(std::move(info)); masterInfo_.state = kPReplStateWaitReplconf; - INFO("Send replconf listening-port {}", kiwi::PConfig::GetInstance().port); + INFO("Send replconf listening-port {}", kiwi::Config::GetInstance().port); } else { WARN("Haven't auth to master yet, or check masterauth password"); } diff --git a/src/store.cc b/src/store.cc index 037bdf47..caa5ffa2 100644 --- a/src/store.cc +++ b/src/store.cc @@ -31,7 +31,7 @@ void PStore::Init(int db_number) { db_number_ = db_number; backends_.reserve(db_number_); for (int i = 0; i < db_number_; i++) { - auto db = std::make_unique(i, kiwi::PConfig::GetInstance().db_path); + auto db = std::make_unique(i, kiwi::Config::GetInstance().db_path); db->Open(); backends_.push_back(std::move(db)); INFO("Open DB_{} success!", i); From ec794dc16469f4669114799fc7d442695aaca296 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Mon, 6 Jan 2025 20:21:38 +0800 Subject: [PATCH 07/35] recover g_config --- src/base_cmd.cc | 2 +- src/client.cc | 4 ++-- src/cmd_admin.cc | 30 +++++++++++++++--------------- src/cmd_hash.cc | 2 +- src/cmd_kv.cc | 2 +- src/config.cc | 2 +- src/config.h | 3 ++- src/db.cc | 31 ++++++++++++++++--------------- src/kiwi.cc | 36 ++++++++++++++++++------------------ src/raft/raft.cc | 24 ++++++++++++------------ src/replication.cc | 10 ++++------ src/store.cc | 2 +- 12 files changed, 74 insertions(+), 74 deletions(-) diff --git a/src/base_cmd.cc b/src/base_cmd.cc index 59743ca1..09685da6 100644 --- a/src/base_cmd.cc +++ b/src/base_cmd.cc @@ -42,7 +42,7 @@ void BaseCmd::Execute(PClient* client) { DEBUG("execute command: {}", client->CmdName()); // read consistency (lease read) / write redirection - if (kiwi::Config::GetInstance().use_raft && (HasFlag(kCmdFlagsReadonly) || HasFlag(kCmdFlagsWrite))) { + if (g_config.use_raft && (HasFlag(kCmdFlagsReadonly) || HasFlag(kCmdFlagsWrite))) { if (!RAFT_INST.IsInitialized()) { return client->SetRes(CmdRes::kErrOther, "RAFT_INST is not initialized"); } diff --git a/src/client.cc b/src/client.cc index c3790b2d..b5a8e09e 100644 --- a/src/client.cc +++ b/src/client.cc @@ -239,7 +239,7 @@ void PClient::OnConnect() { SetName("MasterConnection"); SetFlag(kClientFlagMaster); - if (kiwi::Config::GetInstance().master_auth.empty()) { + if (g_config.master_auth.empty()) { SetAuth(); } @@ -247,7 +247,7 @@ void PClient::OnConnect() { RAFT_INST.SendNodeRequest(this); } } else { - if (kiwi::Config::GetInstance().password.empty()) { + if (g_config.password.empty()) { SetAuth(); } } diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 2d86e622..4d3ac8f3 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -57,7 +57,7 @@ bool CmdConfigGet::DoInitial(PClient* client) { return true; } void CmdConfigGet::DoCmd(PClient* client) { std::vector results; for (int i = 0; i < client->argv_.size() - 2; i++) { - kiwi::Config::GetInstance().Get(client->argv_[i + 2], &results); + g_config.Get(client->argv_[i + 2], &results); } client->AppendStringVector(results); } @@ -68,7 +68,7 @@ CmdConfigSet::CmdConfigSet(const std::string& name, int16_t arity) bool CmdConfigSet::DoInitial(PClient* client) { return true; } void CmdConfigSet::DoCmd(PClient* client) { - auto s = kiwi::Config::GetInstance().Set(client->argv_[2], client->argv_[3]); + auto s = g_config.Set(client->argv_[2], client->argv_[3]); if (!s.ok()) { client->SetRes(CmdRes::kInvalidParameter); } else { @@ -87,7 +87,7 @@ void FlushdbCmd::DoCmd(PClient* client) { PSTORE.GetBackend(currentDBIndex).get()->Lock(); DEFER { PSTORE.GetBackend(currentDBIndex).get()->UnLock(); }; - std::string db_path = kiwi::Config::GetInstance().db_path + std::to_string(currentDBIndex); + std::string db_path = g_config.db_path + std::to_string(currentDBIndex); std::string path_temp = db_path; path_temp.append("_deleting/"); kstd::RenameFile(db_path, path_temp); @@ -108,9 +108,9 @@ FlushallCmd::FlushallCmd(const std::string& name, int16_t arity) bool FlushallCmd::DoInitial(PClient* client) { return true; } void FlushallCmd::DoCmd(PClient* client) { - for (size_t i = 0; i < kiwi::Config::GetInstance().databases; ++i) { + for (size_t i = 0; i < g_config.databases; ++i) { PSTORE.GetBackend(i).get()->Lock(); - std::string db_path = kiwi::Config::GetInstance().db_path + std::to_string(i); + std::string db_path = g_config.db_path + std::to_string(i); std::string path_temp = db_path; path_temp.append("_deleting/"); kstd::RenameFile(db_path, path_temp); @@ -134,12 +134,12 @@ void AuthCmd::DoCmd(PClient* client) { return; } - if (kiwi::Config::GetInstance().password == "") { + if (g_config.password == "") { client->SetRes(CmdRes::kErrOther, "Client sent AUTH, but no password is set"); } std::string password = client->argv_[1]; - if (password != kiwi::Config::GetInstance().password) { + if (password != g_config.password) { client->SetRes(CmdRes::kInvalidPwd); } else { client->SetAuth(); @@ -154,7 +154,7 @@ bool SelectCmd::DoInitial(PClient* client) { return true; } void SelectCmd::DoCmd(PClient* client) { int index = atoi(client->argv_[1].c_str()); - if (index < 0 || index >= kiwi::Config::GetInstance().databases) { + if (index < 0 || index >= g_config.databases) { client->SetRes(CmdRes::kInvalidIndex, kCmdNameSelect + " DB index is out of range"); return; } @@ -168,7 +168,7 @@ ShutdownCmd::ShutdownCmd(const std::string& name, int16_t arity) bool ShutdownCmd::DoInitial(PClient* client) { // For now, only shutdown need check local if (client->PeerIP().find("127.0.0.1") == std::string::npos && - client->PeerIP().find(kiwi::Config::GetInstance().ip) == std::string::npos) { + client->PeerIP().find(g_config.ip) == std::string::npos) { client->SetRes(CmdRes::kErrOther, kCmdNameShutdown + " should be localhost"); return false; } @@ -233,7 +233,7 @@ void HelloCmd::DoCmd(PClient* client) { if (client->GetAuth()) { continue; } - if (client->argv_[next_arg + 1] != kiwi::Config::GetInstance().password) { + if (client->argv_[next_arg + 1] != g_config.password) { client->SetRes(CmdRes::kErrOther, "invalid password"); return; } else { @@ -269,7 +269,7 @@ void HelloCmd::Hello(PClient* client) { client->AppendInteger(static_cast(client->GetUniqueID())); client->AppendString("mode"); - if (!kiwi::Config::GetInstance().use_raft) { + if (!g_config.use_raft) { client->AppendString("standalone"); } else { client->AppendString("cluster"); @@ -453,8 +453,8 @@ void InfoCmd::InfoServer(std::string& info) { tmp_stream << "os:" << host_info.sysname << " " << host_info.release << " " << host_info.machine << "\r\n"; tmp_stream << "arch_bits:" << (reinterpret_cast(&host_info.machine) + strlen(host_info.machine) - 2) << "\r\n"; tmp_stream << "process_id:" << getpid() << "\r\n"; - tmp_stream << "run_id:" << static_cast(kiwi::Config::GetInstance().run_id) << "\r\n"; - tmp_stream << "tcp_port:" << kiwi::Config::GetInstance().port << "\r\n"; + tmp_stream << "run_id:" << static_cast(g_config.run_id) << "\r\n"; + tmp_stream << "tcp_port:" << g_config.port << "\r\n"; tmp_stream << "uptime_in_seconds:" << (current_time_s - g_kiwi->GetStartTime()) << "\r\n"; tmp_stream << "uptime_in_days:" << (current_time_s / (24 * 3600) - g_kiwi->GetStartTime() / (24 * 3600) + 1) << "\r\n"; @@ -497,8 +497,8 @@ void InfoCmd::InfoCPU(std::string& info) { } void InfoCmd::InfoData(std::string& message) { - message += DATABASES_NUM + std::string(":") + std::to_string(kiwi::Config::GetInstance().databases) + "\r\n"; - message += ROCKSDB_NUM + std::string(":") + std::to_string(kiwi::Config::GetInstance().db_instance_num) + "\r\n"; + message += DATABASES_NUM + std::string(":") + std::to_string(kiwi::g_config.databases) + "\r\n"; + message += ROCKSDB_NUM + std::string(":") + std::to_string(kiwi::g_config.db_instance_num) + "\r\n"; message += ROCKSDB_VERSION + std::string(":") + ROCKSDB_NAMESPACE::GetRocksVersionAsString() + "\r\n"; } diff --git a/src/cmd_hash.cc b/src/cmd_hash.cc index 097883bc..4c96bf44 100644 --- a/src/cmd_hash.cc +++ b/src/cmd_hash.cc @@ -172,7 +172,7 @@ void HGetAllCmd::DoCmd(PClient* client) { int64_t total_fv = 0; int64_t cursor = 0; int64_t next_cursor = 0; - size_t raw_limit = kiwi::Config::GetInstance().max_client_response_size; + size_t raw_limit = g_config.max_client_response_size; std::string raw; std::vector fvs; storage::Status s; diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 9895d645..f5cffc36 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -680,7 +680,7 @@ void SetRangeCmd::DoCmd(PClient* client) { return; } // Ref: https://redis.io/docs/latest/commands/setrange/ - if (kiwi::Config::GetInstance().redis_compatible_mode && std::atoi(client->argv_[2].c_str()) > 536870911) { + if (g_config.redis_compatible_mode && std::atoi(client->argv_[2].c_str()) > 536870911) { client->SetRes(CmdRes::kErrOther, "When Redis compatibility mode is enabled, the offset parameter must not exceed 536870911"); return; diff --git a/src/config.cc b/src/config.cc index 3bf2c797..a6bec781 100644 --- a/src/config.cc +++ b/src/config.cc @@ -23,7 +23,7 @@ constexpr int DBNUMBER_MAX = 16; constexpr int THREAD_MAX = 129; constexpr int ROCKSDB_INSTANCE_NUMBER_MAX = 10; -Config& g_config = kiwi::Config::GetInstance(); +Config g_config; // preprocess func static void EraseQuotes(std::string& str) { diff --git a/src/config.h b/src/config.h index f1b60f00..6495eed0 100644 --- a/src/config.h +++ b/src/config.h @@ -31,6 +31,8 @@ using Status = rocksdb::Status; using CheckFunc = std::function; class Config; +extern Config g_config; + class BaseValue { public: BaseValue(std::string key, CheckFunc check_func_ptr, bool rewritable = false) @@ -495,6 +497,5 @@ class Config { // The file name of the config std::string config_file_name_; - Config(); }; } // namespace kiwi diff --git a/src/db.cc b/src/db.cc index f7510ddb..be3f440c 100644 --- a/src/db.cc +++ b/src/db.cc @@ -15,6 +15,8 @@ #include "raft/raft.h" #include "std/log.h" +extern kiwi::Config g_config; + namespace kiwi { DB::DB(int db_index, const std::string& db_path) @@ -24,17 +26,16 @@ DB::~DB() { INFO("DB{} is closing...", db_index_); } rocksdb::Status DB::Open() { storage::StorageOptions storage_options; - storage_options.options = kiwi::Config::GetInstance().GetRocksDBOptions(); - storage_options.table_options = kiwi::Config::GetInstance().GetRocksDBBlockBasedTableOptions(); + storage_options.options = g_config.GetRocksDBOptions(); + storage_options.table_options = g_config.GetRocksDBBlockBasedTableOptions(); - storage_options.options.ttl = kiwi::Config::GetInstance().rocksdb_ttl_second; - storage_options.options.periodic_compaction_seconds = kiwi::Config::GetInstance().rocksdb_periodic_second; + storage_options.options.ttl = g_config.rocksdb_ttl_second; + storage_options.options.periodic_compaction_seconds = g_config.rocksdb_periodic_second; - storage_options.small_compaction_threshold = kiwi::Config::GetInstance().small_compaction_threshold; - storage_options.small_compaction_duration_threshold = - kiwi::Config::GetInstance().small_compaction_duration_threshold; + storage_options.small_compaction_threshold = g_config.small_compaction_threshold; + storage_options.small_compaction_duration_threshold = g_config.small_compaction_duration_threshold; - if (kiwi::Config::GetInstance().use_raft) { + if (g_config.use_raft) { storage_options.append_log_function = [&r = RAFT_INST](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; @@ -44,7 +45,7 @@ rocksdb::Status DB::Open() { }; } - storage_options.db_instance_num = kiwi::Config::GetInstance().db_instance_num; + storage_options.db_instance_num = g_config.db_instance_num; storage_options.db_id = db_index_; std::unique_ptr old_storage = std::move(storage_); @@ -109,14 +110,14 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma } storage::StorageOptions storage_options; - storage_options.options = kiwi::Config::GetInstance().GetRocksDBOptions(); - storage_options.db_instance_num = kiwi::Config::GetInstance().db_instance_num; + storage_options.options = g_config.GetRocksDBOptions(); + storage_options.db_instance_num = g_config.db_instance_num; storage_options.db_id = db_index_; // options for CF - storage_options.options.ttl = kiwi::Config::GetInstance().rocksdb_ttl_second; - storage_options.options.periodic_compaction_seconds = kiwi::Config::GetInstance().rocksdb_periodic_second; - if (kiwi::Config::GetInstance().use_raft) { + storage_options.options.ttl = g_config.rocksdb_ttl_second; + storage_options.options.periodic_compaction_seconds = g_config.rocksdb_periodic_second; + if (g_config.use_raft) { storage_options.append_log_function = [&r = RAFT_INST](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; @@ -130,7 +131,7 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma } // in single-mode, kiwi will enable wal - if (!kiwi::Config::GetInstance().use_raft) { + if (!g_config.use_raft) { storage_->DisableWal(false); } diff --git a/src/kiwi.cc b/src/kiwi.cc index dd03a058..90404db0 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -163,46 +163,46 @@ void KiwiDB::OnNewConnection(uint64_t connId, std::shared_ptr& cl bool KiwiDB::Init() { char runid[kRunidSize + 1] = ""; getRandomHexChars(runid, kRunidSize); - kiwi::Config::GetInstance().Set("runid", {runid, kRunidSize}, true); + g_config.Set("runid", {runid, kRunidSize}, true); if (port_ != 0) { - kiwi::Config::GetInstance().Set("port", std::to_string(port_), true); + g_config.Set("port", std::to_string(port_), true); } if (!options_.GetLogLevel().empty()) { - kiwi::Config::GetInstance().Set("log-level", options_.GetLogLevel(), true); + g_config.Set("log-level", options_.GetLogLevel(), true); } if (options_.GetRedisCompatibleMode()) { - kiwi::Config::GetInstance().Set("redis_compatible_mode", std::to_string(options_.GetRedisCompatibleMode()), true); + g_config.Set("redis_compatible_mode", std::to_string(options_.GetRedisCompatibleMode()), true); } - auto num = kiwi::Config::GetInstance().worker_threads_num + kiwi::Config::GetInstance().slave_threads_num; + auto num = g_config.worker_threads_num + g_config.slave_threads_num; options_.SetThreadNum(num); // now we only use fast cmd thread pool - auto status = cmd_threads_.Init(kiwi::Config::GetInstance().fast_cmd_threads_num, 1, "kiwi-cmd"); + auto status = cmd_threads_.Init(g_config.fast_cmd_threads_num, 1, "kiwi-cmd"); if (!status.ok()) { ERROR("init cmd thread pool failed: {}", status.ToString()); return false; } - PSTORE.Init(kiwi::Config::GetInstance().databases); + PSTORE.Init(g_config.databases); - PSlowLog::Instance().SetThreshold(kiwi::Config::GetInstance().slow_log_time); - PSlowLog::Instance().SetLogLimit(static_cast(kiwi::Config::GetInstance().slow_log_max_len)); + PSlowLog::Instance().SetThreshold(g_config.slow_log_time); + PSlowLog::Instance().SetLogLimit(static_cast(g_config.slow_log_max_len)); // master ip - if (!kiwi::Config::GetInstance().master_ip.empty()) { - PREPL.SetMasterAddr(kiwi::Config::GetInstance().master_ip.c_str(), kiwi::Config::GetInstance().master_port); + if (!g_config.master_ip.empty()) { + PREPL.SetMasterAddr(g_config.master_ip.c_str(), g_config.master_port); } options_.SetRwSeparation(true); event_server_ = std::make_unique>>(options_); - net::SocketAddr addr(kiwi::Config::GetInstance().ip, kiwi::Config::GetInstance().port); - INFO("Add listen addr:{}, port:{}", kiwi::Config::GetInstance().ip, kiwi::Config::GetInstance().port); + net::SocketAddr addr(g_config.ip, g_config.port); + INFO("Add listen addr:{}, port:{}", g_config.ip, g_config.port); event_server_->AddListenAddr(addr); event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); @@ -273,7 +273,7 @@ static void InitLogs() { static int InitLimit() { rlimit limit; - rlim_t maxfiles = kiwi::Config::GetInstance().max_clients; + rlim_t maxfiles = g_config.max_clients; if (getrlimit(RLIMIT_NOFILE, &limit) == -1) { WARN("getrlimit error: {}", strerror(errno)); } else if (limit.rlim_cur < maxfiles) { @@ -324,13 +324,13 @@ int main(int argc, char* argv[]) { } if (!g_kiwi->GetConfigName().empty()) { - if (!kiwi::Config::GetInstance().LoadFromFile(g_kiwi->GetConfigName())) { + if (!g_config.LoadFromFile(g_kiwi->GetConfigName())) { std::cerr << "Load config file [" << g_kiwi->GetConfigName() << "] failed!\n"; return -1; } } - if (kiwi::Config::GetInstance().daemonize) { + if (g_config.daemonize) { daemonize(); } @@ -339,7 +339,7 @@ int main(int argc, char* argv[]) { InitLogs(); InitLimit(); - if (kiwi::Config::GetInstance().daemonize) { + if (g_config.daemonize) { closeStd(); } @@ -347,7 +347,7 @@ int main(int argc, char* argv[]) { // output logo to console char logo[1024] = ""; snprintf(logo, sizeof logo - 1, kiwiLogo, KIWI_VERSION, static_cast(sizeof(void*)) * 8, - static_cast(kiwi::Config::GetInstance().port)); + static_cast(g_config.port)); std::cout << logo; g_kiwi->Run(); } diff --git a/src/raft/raft.cc b/src/raft/raft.cc index 9c69779b..a5a71afa 100644 --- a/src/raft/raft.cc +++ b/src/raft/raft.cc @@ -97,7 +97,7 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { } server_ = std::make_unique(); - auto port = kiwi::Config::GetInstance().port + kiwi::Config::GetInstance().raft_port_offset; + auto port = g_config.port + kiwi::g_config.raft_port_offset; // Add your service into RPC server DummyServiceImpl service(&RAFT_INST); if (server_->AddService(&service, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) { @@ -126,10 +126,10 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { assert(group_id.size() == RAFT_GROUPID_LEN); this->group_id_ = group_id; - // FIXME: kiwi::Config::GetInstance().ip is default to 127.0.0.0, which may not work in cluster. - raw_addr_ = kiwi::Config::GetInstance().ip + ":" + std::to_string(port); + // FIXME: g_config.ip is default to 127.0.0.0, which may not work in cluster. + raw_addr_ = g_config.ip + ":" + std::to_string(port); butil::ip_t ip; - auto ret = butil::str2ip(kiwi::Config::GetInstance().ip.c_str(), &ip); + auto ret = butil::str2ip(g_config.ip.c_str(), &ip); if (ret != 0) { server_.reset(); return ERROR_LOG_AND_STATUS("Failed to convert str_ip to butil::ip_t"); @@ -157,7 +157,7 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { node_options_.fsm = this; node_options_.node_owns_fsm = false; node_options_.snapshot_interval_s = 0; - std::string prefix = "local://" + kiwi::Config::GetInstance().db_path + std::to_string(db_id_) + "/_raft"; + std::string prefix = "local://" + g_config.db_path + std::to_string(db_id_) + "/_praft"; node_options_.log_uri = prefix + "/log"; node_options_.raft_meta_uri = prefix + "/raft_meta"; node_options_.snapshot_uri = prefix + "/snapshot"; @@ -221,7 +221,7 @@ std::string Raft::GetLeaderAddress() const { return std::string(); } - id.addr.port -= kiwi::Config::GetInstance().raft_port_offset; + id.addr.port -= g_config.raft_port_offset; auto addr = butil::endpoint2str(id.addr); return addr.c_str(); } @@ -323,8 +323,8 @@ void Raft::SendNodeAddRequest(PClient* client) { // Node id in braft are ip:port, the node id param in RAFT.NODE ADD cmd will be ignored. int unused_node_id = 0; - auto port = kiwi::Config::GetInstance().port + kiwi::Config::GetInstance().raft_port_offset; - auto raw_addr = kiwi::Config::GetInstance().ip + ":" + std::to_string(port); + auto port = g_config.port + kiwi::g_config.raft_port_offset; + auto raw_addr = g_config.ip + ":" + std::to_string(port); client->AppendArrayLen(int64_t(4)); client->AppendString("RAFT.NODE"); @@ -394,8 +394,8 @@ void Raft::CheckRocksDBConfiguration(PClient* client, PClient* join_client, cons } } - int current_databases_num = kiwi::Config::GetInstance().databases; - int current_rocksdb_num = kiwi::Config::GetInstance().db_instance_num; + int current_databases_num = kiwi::g_config.databases; + int current_rocksdb_num = kiwi::g_config.db_instance_num; std::string current_rocksdb_version = ROCKSDB_NAMESPACE::GetRocksVersionAsString(); if (current_databases_num != databases_num || current_rocksdb_num != rocksdb_num || current_rocksdb_version != rockdb_version) { @@ -713,8 +713,8 @@ int Raft::on_snapshot_load(braft::SnapshotReader* reader) { } // 3. When a snapshot is installed on a node, you do not need to set a playback point. - auto reader_path = reader->get_path(); // xx/snapshot_0000001 - auto path = kiwi::Config::GetInstance().db_path + std::to_string(db_id_); // db/db_id + auto reader_path = reader->get_path(); // xx/snapshot_0000001 + auto path = g_config.db_path + std::to_string(db_id_); // db/db_id TasksVector tasks(1, {TaskType::kLoadDBFromCheckpoint, db_id_, {{TaskArg::kCheckpointPath, reader_path}}, true}); PSTORE.HandleTaskSpecificDB(tasks); INFO("load snapshot success!"); diff --git a/src/replication.cc b/src/replication.cc index dec51994..76db3bd0 100644 --- a/src/replication.cc +++ b/src/replication.cc @@ -98,7 +98,7 @@ void PReplication::TryBgsave() { // if (ret == 0) { // { // PDBSaver qdb; - // qdb.Save(kiwi::Config::GetInstance().rdbfullname.c_str()); + // qdb.Save(g_config.rdbfullname.c_str()); // DEBUG("PReplication save rdb done, exiting child"); // } // _exit(0); @@ -177,8 +177,7 @@ void PReplication::Cron() { if (masterInfo_.addr.IsValid()) { switch (masterInfo_.state) { case kPReplStateNone: { - if (masterInfo_.addr.GetIP() == kiwi::Config::GetInstance().ip && - masterInfo_.addr.GetPort() == kiwi::Config::GetInstance().port) { + if (masterInfo_.addr.GetIP() == g_config.ip && masterInfo_.addr.GetPort() == g_config.port) { ERROR("Fix config, master addr is self addr!"); assert(!!!"wrong config for master addr"); } @@ -225,13 +224,12 @@ void PReplication::Cron() { } else if (master->GetAuth()) { // send replconf char req[128]; - auto len = - snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", kiwi::Config::GetInstance().port); + auto len = snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", g_config.port); std::string info(req, len); master->SendPacket(std::move(info)); masterInfo_.state = kPReplStateWaitReplconf; - INFO("Send replconf listening-port {}", kiwi::Config::GetInstance().port); + INFO("Send replconf listening-port {}", g_config.port); } else { WARN("Haven't auth to master yet, or check masterauth password"); } diff --git a/src/store.cc b/src/store.cc index caa5ffa2..9c014728 100644 --- a/src/store.cc +++ b/src/store.cc @@ -31,7 +31,7 @@ void PStore::Init(int db_number) { db_number_ = db_number; backends_.reserve(db_number_); for (int i = 0; i < db_number_; i++) { - auto db = std::make_unique(i, kiwi::Config::GetInstance().db_path); + auto db = std::make_unique(i, g_config.db_path); db->Open(); backends_.push_back(std::move(db)); INFO("Open DB_{} success!", i); From 21d6eafb0d151844dcb21389ec4511a59222f031 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Mon, 6 Jan 2025 20:53:18 +0800 Subject: [PATCH 08/35] change kiwidb.yml ci --- .github/workflows/{pikiwidb.yml => kiwidb.yml} | 14 ++++++++++++++ 1 file changed, 14 insertions(+) rename .github/workflows/{pikiwidb.yml => kiwidb.yml} (86%) diff --git a/.github/workflows/pikiwidb.yml b/.github/workflows/kiwidb.yml similarity index 86% rename from .github/workflows/pikiwidb.yml rename to .github/workflows/kiwidb.yml index 2c5118fe..b94924fc 100644 --- a/.github/workflows/pikiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -19,6 +19,20 @@ jobs: - name: Build run: bash ./etc/script/ci/build.sh + - name: Add LLVM apt repository + run: | + wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - + echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-13 main" | sudo tee /etc/apt/sources.list.d/llvm.list + sudo apt-get update + + - name: Install clang-format-16 + run: | + sudo apt-get install -y clang-format-16 + sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 100 + + - name: Verify clang-format version + run: clang-format --version + - name: Check Format working-directory: ${{ github.workspace }}/build run: make check-format From df0402bd41cadc910efacee13eb2c51152449bc5 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Mon, 6 Jan 2025 20:21:38 +0800 Subject: [PATCH 09/35] recover g_config --- src/base_cmd.cc | 2 +- src/client.cc | 4 ++-- src/cmd_admin.cc | 30 +++++++++++++++--------------- src/cmd_hash.cc | 2 +- src/cmd_kv.cc | 2 +- src/cmd_raft.cc | 2 +- src/config.cc | 2 +- src/config.h | 11 +++++------ src/db.cc | 31 ++++++++++++++++--------------- src/kiwi.cc | 36 ++++++++++++++++++------------------ src/raft/raft.cc | 24 ++++++++++++------------ src/replication.cc | 10 ++++------ src/store.cc | 2 +- 13 files changed, 78 insertions(+), 80 deletions(-) diff --git a/src/base_cmd.cc b/src/base_cmd.cc index 59743ca1..09685da6 100644 --- a/src/base_cmd.cc +++ b/src/base_cmd.cc @@ -42,7 +42,7 @@ void BaseCmd::Execute(PClient* client) { DEBUG("execute command: {}", client->CmdName()); // read consistency (lease read) / write redirection - if (kiwi::Config::GetInstance().use_raft && (HasFlag(kCmdFlagsReadonly) || HasFlag(kCmdFlagsWrite))) { + if (g_config.use_raft && (HasFlag(kCmdFlagsReadonly) || HasFlag(kCmdFlagsWrite))) { if (!RAFT_INST.IsInitialized()) { return client->SetRes(CmdRes::kErrOther, "RAFT_INST is not initialized"); } diff --git a/src/client.cc b/src/client.cc index c3790b2d..b5a8e09e 100644 --- a/src/client.cc +++ b/src/client.cc @@ -239,7 +239,7 @@ void PClient::OnConnect() { SetName("MasterConnection"); SetFlag(kClientFlagMaster); - if (kiwi::Config::GetInstance().master_auth.empty()) { + if (g_config.master_auth.empty()) { SetAuth(); } @@ -247,7 +247,7 @@ void PClient::OnConnect() { RAFT_INST.SendNodeRequest(this); } } else { - if (kiwi::Config::GetInstance().password.empty()) { + if (g_config.password.empty()) { SetAuth(); } } diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 2d86e622..4d3ac8f3 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -57,7 +57,7 @@ bool CmdConfigGet::DoInitial(PClient* client) { return true; } void CmdConfigGet::DoCmd(PClient* client) { std::vector results; for (int i = 0; i < client->argv_.size() - 2; i++) { - kiwi::Config::GetInstance().Get(client->argv_[i + 2], &results); + g_config.Get(client->argv_[i + 2], &results); } client->AppendStringVector(results); } @@ -68,7 +68,7 @@ CmdConfigSet::CmdConfigSet(const std::string& name, int16_t arity) bool CmdConfigSet::DoInitial(PClient* client) { return true; } void CmdConfigSet::DoCmd(PClient* client) { - auto s = kiwi::Config::GetInstance().Set(client->argv_[2], client->argv_[3]); + auto s = g_config.Set(client->argv_[2], client->argv_[3]); if (!s.ok()) { client->SetRes(CmdRes::kInvalidParameter); } else { @@ -87,7 +87,7 @@ void FlushdbCmd::DoCmd(PClient* client) { PSTORE.GetBackend(currentDBIndex).get()->Lock(); DEFER { PSTORE.GetBackend(currentDBIndex).get()->UnLock(); }; - std::string db_path = kiwi::Config::GetInstance().db_path + std::to_string(currentDBIndex); + std::string db_path = g_config.db_path + std::to_string(currentDBIndex); std::string path_temp = db_path; path_temp.append("_deleting/"); kstd::RenameFile(db_path, path_temp); @@ -108,9 +108,9 @@ FlushallCmd::FlushallCmd(const std::string& name, int16_t arity) bool FlushallCmd::DoInitial(PClient* client) { return true; } void FlushallCmd::DoCmd(PClient* client) { - for (size_t i = 0; i < kiwi::Config::GetInstance().databases; ++i) { + for (size_t i = 0; i < g_config.databases; ++i) { PSTORE.GetBackend(i).get()->Lock(); - std::string db_path = kiwi::Config::GetInstance().db_path + std::to_string(i); + std::string db_path = g_config.db_path + std::to_string(i); std::string path_temp = db_path; path_temp.append("_deleting/"); kstd::RenameFile(db_path, path_temp); @@ -134,12 +134,12 @@ void AuthCmd::DoCmd(PClient* client) { return; } - if (kiwi::Config::GetInstance().password == "") { + if (g_config.password == "") { client->SetRes(CmdRes::kErrOther, "Client sent AUTH, but no password is set"); } std::string password = client->argv_[1]; - if (password != kiwi::Config::GetInstance().password) { + if (password != g_config.password) { client->SetRes(CmdRes::kInvalidPwd); } else { client->SetAuth(); @@ -154,7 +154,7 @@ bool SelectCmd::DoInitial(PClient* client) { return true; } void SelectCmd::DoCmd(PClient* client) { int index = atoi(client->argv_[1].c_str()); - if (index < 0 || index >= kiwi::Config::GetInstance().databases) { + if (index < 0 || index >= g_config.databases) { client->SetRes(CmdRes::kInvalidIndex, kCmdNameSelect + " DB index is out of range"); return; } @@ -168,7 +168,7 @@ ShutdownCmd::ShutdownCmd(const std::string& name, int16_t arity) bool ShutdownCmd::DoInitial(PClient* client) { // For now, only shutdown need check local if (client->PeerIP().find("127.0.0.1") == std::string::npos && - client->PeerIP().find(kiwi::Config::GetInstance().ip) == std::string::npos) { + client->PeerIP().find(g_config.ip) == std::string::npos) { client->SetRes(CmdRes::kErrOther, kCmdNameShutdown + " should be localhost"); return false; } @@ -233,7 +233,7 @@ void HelloCmd::DoCmd(PClient* client) { if (client->GetAuth()) { continue; } - if (client->argv_[next_arg + 1] != kiwi::Config::GetInstance().password) { + if (client->argv_[next_arg + 1] != g_config.password) { client->SetRes(CmdRes::kErrOther, "invalid password"); return; } else { @@ -269,7 +269,7 @@ void HelloCmd::Hello(PClient* client) { client->AppendInteger(static_cast(client->GetUniqueID())); client->AppendString("mode"); - if (!kiwi::Config::GetInstance().use_raft) { + if (!g_config.use_raft) { client->AppendString("standalone"); } else { client->AppendString("cluster"); @@ -453,8 +453,8 @@ void InfoCmd::InfoServer(std::string& info) { tmp_stream << "os:" << host_info.sysname << " " << host_info.release << " " << host_info.machine << "\r\n"; tmp_stream << "arch_bits:" << (reinterpret_cast(&host_info.machine) + strlen(host_info.machine) - 2) << "\r\n"; tmp_stream << "process_id:" << getpid() << "\r\n"; - tmp_stream << "run_id:" << static_cast(kiwi::Config::GetInstance().run_id) << "\r\n"; - tmp_stream << "tcp_port:" << kiwi::Config::GetInstance().port << "\r\n"; + tmp_stream << "run_id:" << static_cast(g_config.run_id) << "\r\n"; + tmp_stream << "tcp_port:" << g_config.port << "\r\n"; tmp_stream << "uptime_in_seconds:" << (current_time_s - g_kiwi->GetStartTime()) << "\r\n"; tmp_stream << "uptime_in_days:" << (current_time_s / (24 * 3600) - g_kiwi->GetStartTime() / (24 * 3600) + 1) << "\r\n"; @@ -497,8 +497,8 @@ void InfoCmd::InfoCPU(std::string& info) { } void InfoCmd::InfoData(std::string& message) { - message += DATABASES_NUM + std::string(":") + std::to_string(kiwi::Config::GetInstance().databases) + "\r\n"; - message += ROCKSDB_NUM + std::string(":") + std::to_string(kiwi::Config::GetInstance().db_instance_num) + "\r\n"; + message += DATABASES_NUM + std::string(":") + std::to_string(kiwi::g_config.databases) + "\r\n"; + message += ROCKSDB_NUM + std::string(":") + std::to_string(kiwi::g_config.db_instance_num) + "\r\n"; message += ROCKSDB_VERSION + std::string(":") + ROCKSDB_NAMESPACE::GetRocksVersionAsString() + "\r\n"; } diff --git a/src/cmd_hash.cc b/src/cmd_hash.cc index 097883bc..4c96bf44 100644 --- a/src/cmd_hash.cc +++ b/src/cmd_hash.cc @@ -172,7 +172,7 @@ void HGetAllCmd::DoCmd(PClient* client) { int64_t total_fv = 0; int64_t cursor = 0; int64_t next_cursor = 0; - size_t raw_limit = kiwi::Config::GetInstance().max_client_response_size; + size_t raw_limit = g_config.max_client_response_size; std::string raw; std::vector fvs; storage::Status s; diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 9895d645..f5cffc36 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -680,7 +680,7 @@ void SetRangeCmd::DoCmd(PClient* client) { return; } // Ref: https://redis.io/docs/latest/commands/setrange/ - if (kiwi::Config::GetInstance().redis_compatible_mode && std::atoi(client->argv_[2].c_str()) > 536870911) { + if (g_config.redis_compatible_mode && std::atoi(client->argv_[2].c_str()) > 536870911) { client->SetRes(CmdRes::kErrOther, "When Redis compatibility mode is enabled, the offset parameter must not exceed 536870911"); return; diff --git a/src/cmd_raft.cc b/src/cmd_raft.cc index 1da14241..4f8ff53e 100644 --- a/src/cmd_raft.cc +++ b/src/cmd_raft.cc @@ -103,7 +103,7 @@ void RaftNodeCmd::DoCmdRemove(PClient* client) { // Connect target std::string peer_ip = butil::ip2str(leader_peer_id.addr.ip).c_str(); - auto port = leader_peer_id.addr.port - kiwi::Config::GetInstance().raft_port_offset; + auto port = leader_peer_id.addr.port - g_config.raft_port_offset; auto peer_id = client->argv_[2]; auto ret = RAFT_INST.GetClusterCmdCtx().Set(ClusterCmdType::kRemove, client, std::move(peer_ip), port, std::move(peer_id)); diff --git a/src/config.cc b/src/config.cc index 3bf2c797..a6bec781 100644 --- a/src/config.cc +++ b/src/config.cc @@ -23,7 +23,7 @@ constexpr int DBNUMBER_MAX = 16; constexpr int THREAD_MAX = 129; constexpr int ROCKSDB_INSTANCE_NUMBER_MAX = 10; -Config& g_config = kiwi::Config::GetInstance(); +Config g_config; // preprocess func static void EraseQuotes(std::string& str) { diff --git a/src/config.h b/src/config.h index f1b60f00..2d355c3a 100644 --- a/src/config.h +++ b/src/config.h @@ -31,6 +31,8 @@ using Status = rocksdb::Status; using CheckFunc = std::function; class Config; +extern Config g_config; + class BaseValue { public: BaseValue(std::string key, CheckFunc check_func_ptr, bool rewritable = false) @@ -149,15 +151,13 @@ using ConfigMap = std::unordered_map; class Config { public: /* Some important, globally relevant public interfaces. */ - /*------------------------ * PConfig() * Initialize kiwi's config & RocksDB's config. */ - static Config& GetInstance() { - static Config instance; - return instance; - } + Config(); + /*------------------------ + * ~PConfig() * Destroy a kiwi's config instance. */ Config(const Config&) = delete; Config& operator=(const Config&) = delete; @@ -495,6 +495,5 @@ class Config { // The file name of the config std::string config_file_name_; - Config(); }; } // namespace kiwi diff --git a/src/db.cc b/src/db.cc index f7510ddb..be3f440c 100644 --- a/src/db.cc +++ b/src/db.cc @@ -15,6 +15,8 @@ #include "raft/raft.h" #include "std/log.h" +extern kiwi::Config g_config; + namespace kiwi { DB::DB(int db_index, const std::string& db_path) @@ -24,17 +26,16 @@ DB::~DB() { INFO("DB{} is closing...", db_index_); } rocksdb::Status DB::Open() { storage::StorageOptions storage_options; - storage_options.options = kiwi::Config::GetInstance().GetRocksDBOptions(); - storage_options.table_options = kiwi::Config::GetInstance().GetRocksDBBlockBasedTableOptions(); + storage_options.options = g_config.GetRocksDBOptions(); + storage_options.table_options = g_config.GetRocksDBBlockBasedTableOptions(); - storage_options.options.ttl = kiwi::Config::GetInstance().rocksdb_ttl_second; - storage_options.options.periodic_compaction_seconds = kiwi::Config::GetInstance().rocksdb_periodic_second; + storage_options.options.ttl = g_config.rocksdb_ttl_second; + storage_options.options.periodic_compaction_seconds = g_config.rocksdb_periodic_second; - storage_options.small_compaction_threshold = kiwi::Config::GetInstance().small_compaction_threshold; - storage_options.small_compaction_duration_threshold = - kiwi::Config::GetInstance().small_compaction_duration_threshold; + storage_options.small_compaction_threshold = g_config.small_compaction_threshold; + storage_options.small_compaction_duration_threshold = g_config.small_compaction_duration_threshold; - if (kiwi::Config::GetInstance().use_raft) { + if (g_config.use_raft) { storage_options.append_log_function = [&r = RAFT_INST](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; @@ -44,7 +45,7 @@ rocksdb::Status DB::Open() { }; } - storage_options.db_instance_num = kiwi::Config::GetInstance().db_instance_num; + storage_options.db_instance_num = g_config.db_instance_num; storage_options.db_id = db_index_; std::unique_ptr old_storage = std::move(storage_); @@ -109,14 +110,14 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma } storage::StorageOptions storage_options; - storage_options.options = kiwi::Config::GetInstance().GetRocksDBOptions(); - storage_options.db_instance_num = kiwi::Config::GetInstance().db_instance_num; + storage_options.options = g_config.GetRocksDBOptions(); + storage_options.db_instance_num = g_config.db_instance_num; storage_options.db_id = db_index_; // options for CF - storage_options.options.ttl = kiwi::Config::GetInstance().rocksdb_ttl_second; - storage_options.options.periodic_compaction_seconds = kiwi::Config::GetInstance().rocksdb_periodic_second; - if (kiwi::Config::GetInstance().use_raft) { + storage_options.options.ttl = g_config.rocksdb_ttl_second; + storage_options.options.periodic_compaction_seconds = g_config.rocksdb_periodic_second; + if (g_config.use_raft) { storage_options.append_log_function = [&r = RAFT_INST](const Binlog& log, std::promise&& promise) { r.AppendLog(log, std::move(promise)); }; @@ -130,7 +131,7 @@ void DB::LoadDBFromCheckpoint(const std::string& checkpoint_path, bool sync [[ma } // in single-mode, kiwi will enable wal - if (!kiwi::Config::GetInstance().use_raft) { + if (!g_config.use_raft) { storage_->DisableWal(false); } diff --git a/src/kiwi.cc b/src/kiwi.cc index dd03a058..90404db0 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -163,46 +163,46 @@ void KiwiDB::OnNewConnection(uint64_t connId, std::shared_ptr& cl bool KiwiDB::Init() { char runid[kRunidSize + 1] = ""; getRandomHexChars(runid, kRunidSize); - kiwi::Config::GetInstance().Set("runid", {runid, kRunidSize}, true); + g_config.Set("runid", {runid, kRunidSize}, true); if (port_ != 0) { - kiwi::Config::GetInstance().Set("port", std::to_string(port_), true); + g_config.Set("port", std::to_string(port_), true); } if (!options_.GetLogLevel().empty()) { - kiwi::Config::GetInstance().Set("log-level", options_.GetLogLevel(), true); + g_config.Set("log-level", options_.GetLogLevel(), true); } if (options_.GetRedisCompatibleMode()) { - kiwi::Config::GetInstance().Set("redis_compatible_mode", std::to_string(options_.GetRedisCompatibleMode()), true); + g_config.Set("redis_compatible_mode", std::to_string(options_.GetRedisCompatibleMode()), true); } - auto num = kiwi::Config::GetInstance().worker_threads_num + kiwi::Config::GetInstance().slave_threads_num; + auto num = g_config.worker_threads_num + g_config.slave_threads_num; options_.SetThreadNum(num); // now we only use fast cmd thread pool - auto status = cmd_threads_.Init(kiwi::Config::GetInstance().fast_cmd_threads_num, 1, "kiwi-cmd"); + auto status = cmd_threads_.Init(g_config.fast_cmd_threads_num, 1, "kiwi-cmd"); if (!status.ok()) { ERROR("init cmd thread pool failed: {}", status.ToString()); return false; } - PSTORE.Init(kiwi::Config::GetInstance().databases); + PSTORE.Init(g_config.databases); - PSlowLog::Instance().SetThreshold(kiwi::Config::GetInstance().slow_log_time); - PSlowLog::Instance().SetLogLimit(static_cast(kiwi::Config::GetInstance().slow_log_max_len)); + PSlowLog::Instance().SetThreshold(g_config.slow_log_time); + PSlowLog::Instance().SetLogLimit(static_cast(g_config.slow_log_max_len)); // master ip - if (!kiwi::Config::GetInstance().master_ip.empty()) { - PREPL.SetMasterAddr(kiwi::Config::GetInstance().master_ip.c_str(), kiwi::Config::GetInstance().master_port); + if (!g_config.master_ip.empty()) { + PREPL.SetMasterAddr(g_config.master_ip.c_str(), g_config.master_port); } options_.SetRwSeparation(true); event_server_ = std::make_unique>>(options_); - net::SocketAddr addr(kiwi::Config::GetInstance().ip, kiwi::Config::GetInstance().port); - INFO("Add listen addr:{}, port:{}", kiwi::Config::GetInstance().ip, kiwi::Config::GetInstance().port); + net::SocketAddr addr(g_config.ip, g_config.port); + INFO("Add listen addr:{}, port:{}", g_config.ip, g_config.port); event_server_->AddListenAddr(addr); event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); @@ -273,7 +273,7 @@ static void InitLogs() { static int InitLimit() { rlimit limit; - rlim_t maxfiles = kiwi::Config::GetInstance().max_clients; + rlim_t maxfiles = g_config.max_clients; if (getrlimit(RLIMIT_NOFILE, &limit) == -1) { WARN("getrlimit error: {}", strerror(errno)); } else if (limit.rlim_cur < maxfiles) { @@ -324,13 +324,13 @@ int main(int argc, char* argv[]) { } if (!g_kiwi->GetConfigName().empty()) { - if (!kiwi::Config::GetInstance().LoadFromFile(g_kiwi->GetConfigName())) { + if (!g_config.LoadFromFile(g_kiwi->GetConfigName())) { std::cerr << "Load config file [" << g_kiwi->GetConfigName() << "] failed!\n"; return -1; } } - if (kiwi::Config::GetInstance().daemonize) { + if (g_config.daemonize) { daemonize(); } @@ -339,7 +339,7 @@ int main(int argc, char* argv[]) { InitLogs(); InitLimit(); - if (kiwi::Config::GetInstance().daemonize) { + if (g_config.daemonize) { closeStd(); } @@ -347,7 +347,7 @@ int main(int argc, char* argv[]) { // output logo to console char logo[1024] = ""; snprintf(logo, sizeof logo - 1, kiwiLogo, KIWI_VERSION, static_cast(sizeof(void*)) * 8, - static_cast(kiwi::Config::GetInstance().port)); + static_cast(g_config.port)); std::cout << logo; g_kiwi->Run(); } diff --git a/src/raft/raft.cc b/src/raft/raft.cc index 9c69779b..a5a71afa 100644 --- a/src/raft/raft.cc +++ b/src/raft/raft.cc @@ -97,7 +97,7 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { } server_ = std::make_unique(); - auto port = kiwi::Config::GetInstance().port + kiwi::Config::GetInstance().raft_port_offset; + auto port = g_config.port + kiwi::g_config.raft_port_offset; // Add your service into RPC server DummyServiceImpl service(&RAFT_INST); if (server_->AddService(&service, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) { @@ -126,10 +126,10 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { assert(group_id.size() == RAFT_GROUPID_LEN); this->group_id_ = group_id; - // FIXME: kiwi::Config::GetInstance().ip is default to 127.0.0.0, which may not work in cluster. - raw_addr_ = kiwi::Config::GetInstance().ip + ":" + std::to_string(port); + // FIXME: g_config.ip is default to 127.0.0.0, which may not work in cluster. + raw_addr_ = g_config.ip + ":" + std::to_string(port); butil::ip_t ip; - auto ret = butil::str2ip(kiwi::Config::GetInstance().ip.c_str(), &ip); + auto ret = butil::str2ip(g_config.ip.c_str(), &ip); if (ret != 0) { server_.reset(); return ERROR_LOG_AND_STATUS("Failed to convert str_ip to butil::ip_t"); @@ -157,7 +157,7 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { node_options_.fsm = this; node_options_.node_owns_fsm = false; node_options_.snapshot_interval_s = 0; - std::string prefix = "local://" + kiwi::Config::GetInstance().db_path + std::to_string(db_id_) + "/_raft"; + std::string prefix = "local://" + g_config.db_path + std::to_string(db_id_) + "/_praft"; node_options_.log_uri = prefix + "/log"; node_options_.raft_meta_uri = prefix + "/raft_meta"; node_options_.snapshot_uri = prefix + "/snapshot"; @@ -221,7 +221,7 @@ std::string Raft::GetLeaderAddress() const { return std::string(); } - id.addr.port -= kiwi::Config::GetInstance().raft_port_offset; + id.addr.port -= g_config.raft_port_offset; auto addr = butil::endpoint2str(id.addr); return addr.c_str(); } @@ -323,8 +323,8 @@ void Raft::SendNodeAddRequest(PClient* client) { // Node id in braft are ip:port, the node id param in RAFT.NODE ADD cmd will be ignored. int unused_node_id = 0; - auto port = kiwi::Config::GetInstance().port + kiwi::Config::GetInstance().raft_port_offset; - auto raw_addr = kiwi::Config::GetInstance().ip + ":" + std::to_string(port); + auto port = g_config.port + kiwi::g_config.raft_port_offset; + auto raw_addr = g_config.ip + ":" + std::to_string(port); client->AppendArrayLen(int64_t(4)); client->AppendString("RAFT.NODE"); @@ -394,8 +394,8 @@ void Raft::CheckRocksDBConfiguration(PClient* client, PClient* join_client, cons } } - int current_databases_num = kiwi::Config::GetInstance().databases; - int current_rocksdb_num = kiwi::Config::GetInstance().db_instance_num; + int current_databases_num = kiwi::g_config.databases; + int current_rocksdb_num = kiwi::g_config.db_instance_num; std::string current_rocksdb_version = ROCKSDB_NAMESPACE::GetRocksVersionAsString(); if (current_databases_num != databases_num || current_rocksdb_num != rocksdb_num || current_rocksdb_version != rockdb_version) { @@ -713,8 +713,8 @@ int Raft::on_snapshot_load(braft::SnapshotReader* reader) { } // 3. When a snapshot is installed on a node, you do not need to set a playback point. - auto reader_path = reader->get_path(); // xx/snapshot_0000001 - auto path = kiwi::Config::GetInstance().db_path + std::to_string(db_id_); // db/db_id + auto reader_path = reader->get_path(); // xx/snapshot_0000001 + auto path = g_config.db_path + std::to_string(db_id_); // db/db_id TasksVector tasks(1, {TaskType::kLoadDBFromCheckpoint, db_id_, {{TaskArg::kCheckpointPath, reader_path}}, true}); PSTORE.HandleTaskSpecificDB(tasks); INFO("load snapshot success!"); diff --git a/src/replication.cc b/src/replication.cc index dec51994..76db3bd0 100644 --- a/src/replication.cc +++ b/src/replication.cc @@ -98,7 +98,7 @@ void PReplication::TryBgsave() { // if (ret == 0) { // { // PDBSaver qdb; - // qdb.Save(kiwi::Config::GetInstance().rdbfullname.c_str()); + // qdb.Save(g_config.rdbfullname.c_str()); // DEBUG("PReplication save rdb done, exiting child"); // } // _exit(0); @@ -177,8 +177,7 @@ void PReplication::Cron() { if (masterInfo_.addr.IsValid()) { switch (masterInfo_.state) { case kPReplStateNone: { - if (masterInfo_.addr.GetIP() == kiwi::Config::GetInstance().ip && - masterInfo_.addr.GetPort() == kiwi::Config::GetInstance().port) { + if (masterInfo_.addr.GetIP() == g_config.ip && masterInfo_.addr.GetPort() == g_config.port) { ERROR("Fix config, master addr is self addr!"); assert(!!!"wrong config for master addr"); } @@ -225,13 +224,12 @@ void PReplication::Cron() { } else if (master->GetAuth()) { // send replconf char req[128]; - auto len = - snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", kiwi::Config::GetInstance().port); + auto len = snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", g_config.port); std::string info(req, len); master->SendPacket(std::move(info)); masterInfo_.state = kPReplStateWaitReplconf; - INFO("Send replconf listening-port {}", kiwi::Config::GetInstance().port); + INFO("Send replconf listening-port {}", g_config.port); } else { WARN("Haven't auth to master yet, or check masterauth password"); } diff --git a/src/store.cc b/src/store.cc index caa5ffa2..9c014728 100644 --- a/src/store.cc +++ b/src/store.cc @@ -31,7 +31,7 @@ void PStore::Init(int db_number) { db_number_ = db_number; backends_.reserve(db_number_); for (int i = 0; i < db_number_; i++) { - auto db = std::make_unique(i, kiwi::Config::GetInstance().db_path); + auto db = std::make_unique(i, g_config.db_path); db->Open(); backends_.push_back(std::move(db)); INFO("Open DB_{} success!", i); From 3494ba8592e601240e3c481746b93815cd95e60d Mon Sep 17 00:00:00 2001 From: alexstocks Date: Mon, 6 Jan 2025 21:19:50 +0800 Subject: [PATCH 10/35] PStore -> Store --- src/base_cmd.cc | 4 ++-- src/cmd_admin.cc | 28 ++++++++++++------------ src/cmd_hash.cc | 32 +++++++++++++-------------- src/cmd_keys.cc | 28 ++++++++++++------------ src/cmd_kv.cc | 52 ++++++++++++++++++++++---------------------- src/cmd_list.cc | 28 ++++++++++++------------ src/cmd_raft.cc | 2 +- src/cmd_set.cc | 32 +++++++++++++-------------- src/cmd_zset.cc | 48 ++++++++++++++++++++-------------------- src/kiwi.cc | 2 +- src/raft/raft.cc | 6 ++--- src/raft/snapshot.cc | 4 ++-- src/replication.cc | 2 +- src/store.cc | 10 ++++----- src/store.h | 14 ++++++------ 15 files changed, 146 insertions(+), 146 deletions(-) diff --git a/src/base_cmd.cc b/src/base_cmd.cc index 09685da6..b4632d89 100644 --- a/src/base_cmd.cc +++ b/src/base_cmd.cc @@ -59,11 +59,11 @@ void BaseCmd::Execute(PClient* client) { auto dbIndex = client->GetCurrentDB(); if (!HasFlag(kCmdFlagsExclusive)) { - PSTORE.GetBackend(dbIndex)->LockShared(); + STORE_INST.GetBackend(dbIndex)->LockShared(); } DEFER { if (!HasFlag(kCmdFlagsExclusive)) { - PSTORE.GetBackend(dbIndex)->UnLockShared(); + STORE_INST.GetBackend(dbIndex)->UnLockShared(); } }; diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 4d3ac8f3..66df7875 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -84,15 +84,15 @@ bool FlushdbCmd::DoInitial(PClient* client) { return true; } void FlushdbCmd::DoCmd(PClient* client) { int currentDBIndex = client->GetCurrentDB(); - PSTORE.GetBackend(currentDBIndex).get()->Lock(); - DEFER { PSTORE.GetBackend(currentDBIndex).get()->UnLock(); }; + STORE_INST.GetBackend(currentDBIndex).get()->Lock(); + DEFER { STORE_INST.GetBackend(currentDBIndex).get()->UnLock(); }; std::string db_path = g_config.db_path + std::to_string(currentDBIndex); std::string path_temp = db_path; path_temp.append("_deleting/"); kstd::RenameFile(db_path, path_temp); - auto s = PSTORE.GetBackend(currentDBIndex)->Open(); + auto s = STORE_INST.GetBackend(currentDBIndex)->Open(); if (!s.ok()) { client->SetRes(CmdRes::kErrOther, "flushdb failed"); return; @@ -109,16 +109,16 @@ bool FlushallCmd::DoInitial(PClient* client) { return true; } void FlushallCmd::DoCmd(PClient* client) { for (size_t i = 0; i < g_config.databases; ++i) { - PSTORE.GetBackend(i).get()->Lock(); + STORE_INST.GetBackend(i).get()->Lock(); std::string db_path = g_config.db_path + std::to_string(i); std::string path_temp = db_path; path_temp.append("_deleting/"); kstd::RenameFile(db_path, path_temp); - auto s = PSTORE.GetBackend(i)->Open(); + auto s = STORE_INST.GetBackend(i)->Open(); assert(s.ok()); auto f = std::async(std::launch::async, [&path_temp]() { kstd::DeleteDir(path_temp); }); - PSTORE.GetBackend(i).get()->UnLock(); + STORE_INST.GetBackend(i).get()->UnLock(); } client->SetRes(CmdRes::kOK); } @@ -176,9 +176,9 @@ bool ShutdownCmd::DoInitial(PClient* client) { } void ShutdownCmd::DoCmd(PClient* client) { - PSTORE.GetBackend(client->GetCurrentDB())->UnLockShared(); + STORE_INST.GetBackend(client->GetCurrentDB())->UnLockShared(); g_kiwi->Stop(); - PSTORE.GetBackend(client->GetCurrentDB())->LockShared(); + STORE_INST.GetBackend(client->GetCurrentDB())->LockShared(); client->SetRes(CmdRes::kNone); } @@ -612,7 +612,7 @@ bool SortCmd::DoInitial(PClient* client) { } Status s; - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRange(client->Key(), 0, -1, &ret_); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LRange(client->Key(), 0, -1, &ret_); if (s.ok()) { return true; } else if (!s.IsNotFound()) { @@ -620,7 +620,7 @@ bool SortCmd::DoInitial(PClient* client) { return false; } - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SMembers(client->Key(), &ret_); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SMembers(client->Key(), &ret_); if (s.ok()) { return true; } else if (!s.IsNotFound()) { @@ -629,7 +629,7 @@ bool SortCmd::DoInitial(PClient* client) { } std::vector score_members; - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRange(client->Key(), 0, -1, &score_members); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZRange(client->Key(), 0, -1, &score_members); if (s.ok()) { for (auto& c : score_members) { ret_.emplace_back(c.member); @@ -718,7 +718,7 @@ void SortCmd::DoCmd(PClient* client) { client->AppendStringVector(ret_); } else { uint64_t reply_num = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(store_key_, ret_, &reply_num); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(store_key_, ret_, &reply_num); if (s.ok()) { client->AppendInteger(reply_num); } else { @@ -750,9 +750,9 @@ std::optional SortCmd::lookupKeyByPattern(PClient* client, const st std::string value; storage::Status s; if (!field.empty()) { - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HGet(key, field, &value); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HGet(key, field, &value); } else { - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Get(key, &value); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Get(key, &value); } if (!s.ok()) { diff --git a/src/cmd_hash.cc b/src/cmd_hash.cc index 4c96bf44..1193f1b5 100644 --- a/src/cmd_hash.cc +++ b/src/cmd_hash.cc @@ -40,7 +40,7 @@ void HSetCmd::DoCmd(PClient* client) { auto value = client->argv_[i + 1]; int32_t temp = 0; // TODO(century): current bw doesn't support multiple fvs, fix it when necessary - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HSet(client->Key(), field, value, &temp); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HSet(client->Key(), field, value, &temp); if (s.ok()) { ret += temp; } else if (s.IsInvalidArgument()) { @@ -66,7 +66,7 @@ bool HGetCmd::DoInitial(PClient* client) { void HGetCmd::DoCmd(PClient* client) { PString value; auto field = client->argv_[2]; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HGet(client->Key(), field, &value); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HGet(client->Key(), field, &value); if (s.ok()) { client->AppendString(value); } else if (s.IsNotFound()) { @@ -89,7 +89,7 @@ bool HDelCmd::DoInitial(PClient* client) { void HDelCmd::DoCmd(PClient* client) { int32_t res{}; std::vector fields(client->argv_.begin() + 2, client->argv_.end()); - auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HDel(client->Key(), fields, &res); + auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HDel(client->Key(), fields, &res); if (!s.ok() && !s.IsNotFound()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -119,7 +119,7 @@ bool HMSetCmd::DoInitial(PClient* client) { } void HMSetCmd::DoCmd(PClient* client) { - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HMSet(client->Key(), client->Fvs()); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HMSet(client->Key(), client->Fvs()); if (s.ok()) { client->SetRes(CmdRes::kOK); } else if (s.IsInvalidArgument()) { @@ -143,7 +143,7 @@ bool HMGetCmd::DoInitial(PClient* client) { void HMGetCmd::DoCmd(PClient* client) { std::vector vss; - auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HMGet(client->Key(), client->Fields(), &vss); + auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HMGet(client->Key(), client->Fields(), &vss); if (s.ok() || s.IsNotFound()) { client->AppendArrayLen(vss.size()); for (auto& vs : vss) { @@ -179,7 +179,7 @@ void HGetAllCmd::DoCmd(PClient* client) { do { fvs.clear(); - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->HScan(client->Key(), cursor, "*", kiwi_SCAN_STEP_LENGTH, &fvs, &next_cursor); if (!s.ok()) { @@ -223,7 +223,7 @@ bool HKeysCmd::DoInitial(PClient* client) { void HKeysCmd::DoCmd(PClient* client) { std::vector fields; - auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HKeys(client->Key(), &fields); + auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HKeys(client->Key(), &fields); if (s.ok() || s.IsNotFound()) { client->AppendArrayLen(fields.size()); for (const auto& field : fields) { @@ -248,7 +248,7 @@ bool HLenCmd::DoInitial(PClient* client) { void HLenCmd::DoCmd(PClient* client) { int32_t len = 0; - auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HLen(client->Key(), &len); + auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HLen(client->Key(), &len); if (s.ok() || s.IsNotFound()) { client->AppendInteger(len); } else if (s.IsInvalidArgument()) { @@ -268,7 +268,7 @@ bool HStrLenCmd::DoInitial(PClient* client) { void HStrLenCmd::DoCmd(PClient* client) { int32_t len = 0; - auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HStrlen(client->Key(), client->argv_[2], &len); + auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HStrlen(client->Key(), client->argv_[2], &len); if (s.ok() || s.IsNotFound()) { client->AppendInteger(len); } else if (s.IsInvalidArgument()) { @@ -321,7 +321,7 @@ void HScanCmd::DoCmd(PClient* client) { // execute command std::vector fvs; int64_t next_cursor{}; - auto status = PSTORE.GetBackend(client->GetCurrentDB()) + auto status = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->HScan(client->Key(), cursor, pattern, count, &fvs, &next_cursor); if (!status.ok() && !status.IsNotFound()) { @@ -353,7 +353,7 @@ bool HValsCmd::DoInitial(PClient* client) { void HValsCmd::DoCmd(PClient* client) { std::vector valueVec; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HVals(client->Key(), &valueVec); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HVals(client->Key(), &valueVec); if (s.ok() || s.IsNotFound()) { client->AppendStringVector(valueVec); } else if (s.IsInvalidArgument()) { @@ -383,7 +383,7 @@ void HIncrbyFloatCmd::DoCmd(PClient* client) { return; } std::string newValue; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->HIncrbyfloat(client->Key(), client->argv_[2], client->argv_[3], &newValue); if (s.ok() || s.IsNotFound()) { @@ -411,7 +411,7 @@ bool HSetNXCmd::DoInitial(PClient* client) { void HSetNXCmd::DoCmd(PClient* client) { int32_t temp = 0; storage::Status s; - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->HSetnx(client->Key(), client->argv_[2], client->argv_[3], &temp); if (s.ok()) { @@ -441,7 +441,7 @@ void HIncrbyCmd::DoCmd(PClient* client) { int64_t temp = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HIncrby(client->Key(), client->argv_[2], int_by, &temp); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HIncrby(client->Key(), client->argv_[2], int_by, &temp); if (s.ok() || s.IsNotFound()) { client->AppendInteger(temp); } else if (s.IsInvalidArgument() && @@ -490,7 +490,7 @@ void HRandFieldCmd::DoCmd(PClient* client) { // execute command std::vector res; - auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HRandField(client->Key(), count, with_values, &res); + auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HRandField(client->Key(), count, with_values, &res); if (s.IsNotFound()) { client->AppendString(""); return; @@ -522,7 +522,7 @@ void HExistsCmd::DoCmd(PClient* client) { // execute command std::vector res; - auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HExists(client->Key(), field); + auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HExists(client->Key(), field); if (!s.ok() && !s.IsNotFound()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); diff --git a/src/cmd_keys.cc b/src/cmd_keys.cc index 197b41b4..af58b288 100644 --- a/src/cmd_keys.cc +++ b/src/cmd_keys.cc @@ -26,7 +26,7 @@ bool DelCmd::DoInitial(PClient* client) { } void DelCmd::DoCmd(PClient* client) { - int64_t count = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Del(client->Keys()); + int64_t count = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Del(client->Keys()); if (count >= 0) { client->AppendInteger(count); } else { @@ -44,10 +44,10 @@ bool ExistsCmd::DoInitial(PClient* client) { } void ExistsCmd::DoCmd(PClient* client) { - int64_t count = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Exists(client->Keys()); + int64_t count = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Exists(client->Keys()); if (count >= 0) { client->AppendInteger(count); - // if (PSTORE.ExistsKey(client->Key())) { + // if (STORE_INST.ExistsKey(client->Key())) { // client->AppendInteger(1); // } else { // client->SetRes(RespRet::kErrOther, "exists internal error"); @@ -67,7 +67,7 @@ bool TypeCmd::DoInitial(PClient* client) { void TypeCmd::DoCmd(PClient* client) { storage::DataType type = storage::DataType::kNones; - rocksdb::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetType(client->Key(), type); + rocksdb::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->GetType(client->Key(), type); if (s.ok()) { client->AppendSimpleString(std::string(storage::DataTypeToString(type))); } else { @@ -89,7 +89,7 @@ void ExpireCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kInvalidInt); return; } - auto res = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Expire(client->Key(), sec); + auto res = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Expire(client->Key(), sec); if (res != -1) { client->AppendInteger(res); } else { @@ -106,7 +106,7 @@ bool TtlCmd::DoInitial(PClient* client) { } void TtlCmd::DoCmd(PClient* client) { - auto timestamp = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->TTL(client->Key()); + auto timestamp = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->TTL(client->Key()); if (timestamp == -3) { client->SetRes(CmdRes::kErrOther, "ttl internal error"); } else { @@ -128,7 +128,7 @@ void PExpireCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kInvalidInt); return; } - auto res = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Expire(client->Key(), msec / 1000); + auto res = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Expire(client->Key(), msec / 1000); if (res != -1) { client->AppendInteger(res); } else { @@ -150,7 +150,7 @@ void ExpireatCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kInvalidInt); return; } - auto res = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Expireat(client->Key(), time_stamp); + auto res = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Expireat(client->Key(), time_stamp); if (res != -1) { client->AppendInteger(res); } else { @@ -173,7 +173,7 @@ void PExpireatCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kInvalidInt); return; } - auto res = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Expireat(client->Key(), time_stamp_ms / 1000); + auto res = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Expireat(client->Key(), time_stamp_ms / 1000); if (res != -1) { client->AppendInteger(res); } else { @@ -190,7 +190,7 @@ bool PersistCmd::DoInitial(PClient* client) { } void PersistCmd::DoCmd(PClient* client) { - auto res = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Persist(client->Key()); + auto res = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Persist(client->Key()); if (res != -1) { client->AppendInteger(res); } else { @@ -208,7 +208,7 @@ bool KeysCmd::DoInitial(PClient* client) { void KeysCmd::DoCmd(PClient* client) { std::vector keys; - auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Keys(storage::DataType::kAll, client->Key(), &keys); + auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Keys(storage::DataType::kAll, client->Key(), &keys); if (s.ok()) { client->AppendArrayLen(keys.size()); for (const auto& k : keys) { @@ -229,7 +229,7 @@ bool PttlCmd::DoInitial(PClient* client) { // like Blackwidow , Floyd still possible has same key in different data structure void PttlCmd::DoCmd(PClient* client) { - auto timestamp = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->TTL(client->Key()); + auto timestamp = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->TTL(client->Key()); // mean operation exception errors happen in database if (timestamp == -3) { client->SetRes(CmdRes::kErrOther, "ttl internal error"); @@ -247,7 +247,7 @@ bool RenameCmd::DoInitial(PClient* client) { } void RenameCmd::DoCmd(PClient* client) { - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Rename(client->Key(), client->argv_[2]); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Rename(client->Key(), client->argv_[2]); if (s.ok()) { client->SetRes(CmdRes::kOK); } else if (s.IsNotFound()) { @@ -267,7 +267,7 @@ bool RenameNXCmd::DoInitial(PClient* client) { void RenameNXCmd::DoCmd(PClient* client) { storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Renamenx(client->Key(), client->argv_[2]); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Renamenx(client->Key(), client->argv_[2]); if (s.ok()) { client->SetRes(CmdRes::kOK); } else if (s.IsNotFound()) { diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index f5cffc36..e821aaa2 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -30,7 +30,7 @@ bool GetCmd::DoInitial(PClient* client) { void GetCmd::DoCmd(PClient* client) { PString value; int64_t ttl = -1; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetWithTTL(client->Key(), &value, &ttl); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->GetWithTTL(client->Key(), &value, &ttl); if (s.ok()) { client->AppendString(value); } else if (s.IsNotFound()) { @@ -92,16 +92,16 @@ void SetCmd::DoCmd(PClient* client) { auto key_ = client->Key(); switch (condition_) { case SetCmd::kXX: - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Setxx(key_, value_, &res, sec_); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Setxx(key_, value_, &res, sec_); break; case SetCmd::kNX: - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Setnx(key_, value_, &res, sec_); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Setnx(key_, value_, &res, sec_); break; case SetCmd::kEXORPX: - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Setex(key_, value_, sec_); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Setex(key_, value_, sec_); break; default: - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Set(key_, value_); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Set(key_, value_); break; } @@ -129,7 +129,7 @@ bool AppendCmd::DoInitial(PClient* client) { void AppendCmd::DoCmd(PClient* client) { int32_t new_len = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Append(client->Key(), client->argv_[2], &new_len); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Append(client->Key(), client->argv_[2], &new_len); if (s.ok() || s.IsNotFound()) { client->AppendInteger(new_len); } else { @@ -148,7 +148,7 @@ bool GetSetCmd::DoInitial(PClient* client) { void GetSetCmd::DoCmd(PClient* client) { std::string old_value; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetSet(client->Key(), client->argv_[2], &old_value); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->GetSet(client->Key(), client->argv_[2], &old_value); if (s.ok()) { if (old_value.empty()) { client->AppendString(""); @@ -173,7 +173,7 @@ bool MGetCmd::DoInitial(PClient* client) { void MGetCmd::DoCmd(PClient* client) { std::vector db_value_status_array; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->MGet(client->Keys(), &db_value_status_array); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->MGet(client->Keys(), &db_value_status_array); if (s.ok()) { client->AppendArrayLen(db_value_status_array.size()); for (const auto& vs : db_value_status_array) { @@ -210,7 +210,7 @@ void MSetCmd::DoCmd(PClient* client) { for (size_t index = 1; index != client->argv_.size(); index += 2) { kvs.push_back({client->argv_[index], client->argv_[index + 1]}); } - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->MSet(kvs); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->MSet(kvs); if (s.ok()) { client->SetRes(CmdRes::kOK); } else { @@ -235,7 +235,7 @@ void BitCountCmd::DoCmd(PClient* client) { storage::Status s; int32_t count = 0; if (client->argv_.size() == 2) { - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->BitCount(client->Key(), 0, 0, &count, false); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->BitCount(client->Key(), 0, 0, &count, false); } else { int64_t start_offset = 0; int64_t end_offset = 0; @@ -245,7 +245,7 @@ void BitCountCmd::DoCmd(PClient* client) { return; } - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->BitCount(client->Key(), start_offset, end_offset, &count, true); } @@ -269,7 +269,7 @@ bool DecrCmd::DoInitial(kiwi::PClient* client) { void DecrCmd::DoCmd(kiwi::PClient* client) { int64_t ret = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Decrby(client->Key(), 1, &ret); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Decrby(client->Key(), 1, &ret); if (s.ok()) { client->AppendInteger(ret); } else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a integer") { @@ -291,7 +291,7 @@ bool IncrCmd::DoInitial(kiwi::PClient* client) { void IncrCmd::DoCmd(kiwi::PClient* client) { int64_t ret = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrby(client->Key(), 1, &ret); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Incrby(client->Key(), 1, &ret); if (s.ok()) { client->AppendInteger(ret); } else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a integer") { @@ -350,7 +350,7 @@ void BitOpCmd::DoCmd(PClient* client) { } else { PString value; int64_t result_length = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->BitOp(op, client->argv_[2], keys, value, &result_length); if (s.ok()) { @@ -373,7 +373,7 @@ bool StrlenCmd::DoInitial(PClient* client) { void StrlenCmd::DoCmd(PClient* client) { int32_t len = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Strlen(client->Key(), &len); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Strlen(client->Key(), &len); if (s.ok() || s.IsNotFound()) { client->AppendInteger(len); } else if (s.IsInvalidArgument()) { @@ -400,7 +400,7 @@ void SetExCmd::DoCmd(PClient* client) { int64_t sec = 0; kstd::String2int(client->argv_[2], &sec); storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Setex(client->Key(), client->argv_[3], sec); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Setex(client->Key(), client->argv_[3], sec); if (s.ok()) { client->SetRes(CmdRes::kOK); } else if (s.IsInvalidArgument()) { @@ -426,7 +426,7 @@ bool PSetExCmd::DoInitial(PClient* client) { void PSetExCmd::DoCmd(PClient* client) { int64_t msec = 0; kstd::String2int(client->argv_[2], &msec); - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->Setex(client->Key(), client->argv_[3], static_cast(msec / 1000)); if (s.ok()) { @@ -455,7 +455,7 @@ void IncrbyCmd::DoCmd(PClient* client) { int64_t ret = 0; int64_t by = 0; kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by); - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrby(client->Key(), by, &ret); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Incrby(client->Key(), by, &ret); if (s.ok()) { client->AppendInteger(ret); } else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a integer") { @@ -490,7 +490,7 @@ void DecrbyCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kInvalidInt); return; } - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Decrby(client->Key(), by, &ret); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Decrby(client->Key(), by, &ret); if (s.ok()) { client->AppendInteger(ret); } else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a integer") { @@ -521,7 +521,7 @@ bool IncrbyFloatCmd::DoInitial(PClient* client) { void IncrbyFloatCmd::DoCmd(PClient* client) { PString ret; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrbyfloat(client->Key(), client->argv_[2], &ret); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Incrbyfloat(client->Key(), client->argv_[2], &ret); if (s.ok()) { client->AppendString(ret); } else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a valid float") { @@ -547,7 +547,7 @@ bool SetNXCmd::DoInitial(PClient* client) { void SetNXCmd::DoCmd(PClient* client) { int32_t success = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Setnx(client->Key(), client->argv_[2], &success); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Setnx(client->Key(), client->argv_[2], &success); if (s.ok()) { client->AppendInteger(success); } else if (s.IsInvalidArgument()) { @@ -572,7 +572,7 @@ void GetBitCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kInvalidInt); return; } - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetBit(client->Key(), offset, &bit_val); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->GetBit(client->Key(), offset, &bit_val); if (s.ok()) { client->AppendInteger(bit_val); } else if (s.IsInvalidArgument()) { @@ -605,7 +605,7 @@ void GetRangeCmd::DoCmd(PClient* client) { int64_t end = 0; kstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &start); kstd::String2int(client->argv_[3].data(), client->argv_[3].size(), &end); - auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Getrange(client->Key(), start, end, &ret); + auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Getrange(client->Key(), start, end, &ret); if (!s.ok()) { if (s.IsNotFound()) { client->AppendString(""); @@ -651,7 +651,7 @@ void SetBitCmd::DoCmd(PClient* client) { PString value; int32_t bit_val = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->SetBit(client->Key(), offset, static_cast(on), &bit_val); if (s.ok()) { @@ -687,7 +687,7 @@ void SetRangeCmd::DoCmd(PClient* client) { } int32_t ret = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Setrange(client->Key(), offset, client->argv_[3], &ret); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Setrange(client->Key(), offset, client->argv_[3], &ret); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -722,7 +722,7 @@ void MSetnxCmd::DoCmd(PClient* client) { for (size_t index = 1; index != client->argv_.size(); index += 2) { kvs.push_back({client->argv_[index], client->argv_[index + 1]}); } - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->MSetnx(kvs, &success); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->MSetnx(kvs, &success); if (s.ok()) { client->AppendInteger(success); } else if (s.IsInvalidArgument()) { diff --git a/src/cmd_list.cc b/src/cmd_list.cc index ed6cde24..b7bd0ea8 100644 --- a/src/cmd_list.cc +++ b/src/cmd_list.cc @@ -24,7 +24,7 @@ void LPushCmd::DoCmd(PClient* client) { std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); uint64_t reply_num = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPush(client->Key(), list_values, &reply_num); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LPush(client->Key(), list_values, &reply_num); if (s.ok()) { client->AppendInteger(reply_num); } else if (s.IsInvalidArgument()) { @@ -46,7 +46,7 @@ void LPushxCmd::DoCmd(PClient* client) { std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); uint64_t reply_num = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPushx(client->Key(), list_values, &reply_num); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LPushx(client->Key(), list_values, &reply_num); if (s.ok() || s.IsNotFound()) { client->AppendInteger(reply_num); } else if (s.IsInvalidArgument()) { @@ -71,7 +71,7 @@ bool RPoplpushCmd::DoInitial(PClient* client) { void RPoplpushCmd::DoCmd(PClient* client) { std::string value; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPoplpush(source_, receiver_, &value); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->RPoplpush(source_, receiver_, &value); if (s.ok()) { client->AppendString(value); } else if (s.IsNotFound()) { @@ -95,7 +95,7 @@ void RPushCmd::DoCmd(PClient* client) { std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); uint64_t reply_num = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(client->Key(), list_values, &reply_num); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(client->Key(), list_values, &reply_num); if (s.ok()) { client->AppendInteger(reply_num); } else if (s.IsInvalidArgument()) { @@ -117,7 +117,7 @@ void RPushxCmd::DoCmd(PClient* client) { std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); uint64_t reply_num = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPushx(client->Key(), list_values, &reply_num); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->RPushx(client->Key(), list_values, &reply_num); if (s.ok() || s.IsNotFound()) { client->AppendInteger(reply_num); } else if (s.IsInvalidArgument()) { @@ -137,7 +137,7 @@ bool LPopCmd::DoInitial(PClient* client) { void LPopCmd::DoCmd(PClient* client) { std::vector elements; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPop(client->Key(), 1, &elements); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LPop(client->Key(), 1, &elements); if (s.ok()) { client->AppendString(elements[0]); } else if (s.IsNotFound()) { @@ -159,7 +159,7 @@ bool RPopCmd::DoInitial(PClient* client) { void RPopCmd::DoCmd(PClient* client) { std::vector elements; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPop(client->Key(), 1, &elements); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->RPop(client->Key(), 1, &elements); if (s.ok()) { client->AppendString(elements[0]); } else if (s.IsNotFound()) { @@ -188,7 +188,7 @@ void LRangeCmd::DoCmd(PClient* client) { return; } storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRange(client->Key(), start_index, end_index, &ret); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LRange(client->Key(), start_index, end_index, &ret); if (!s.ok() && !s.IsNotFound()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -218,7 +218,7 @@ void LRemCmd::DoCmd(PClient* client) { uint64_t reply_num = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRem(client->Key(), freq_, client->argv_[3], &reply_num); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LRem(client->Key(), freq_, client->argv_[3], &reply_num); if (s.ok() || s.IsNotFound()) { client->AppendInteger(reply_num); } else if (s.IsInvalidArgument()) { @@ -245,7 +245,7 @@ void LTrimCmd::DoCmd(PClient* client) { return; } storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LTrim(client->Key(), start_index, end_index); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LTrim(client->Key(), start_index, end_index); if (s.ok() || s.IsNotFound()) { client->SetRes(CmdRes::kOK); } else if (s.IsInvalidArgument()) { @@ -275,7 +275,7 @@ void LSetCmd::DoCmd(PClient* client) { return; } storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LSet(client->Key(), val, client->argv_[3]); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LSet(client->Key(), val, client->argv_[3]); if (s.ok()) { client->SetRes(CmdRes::kOK); } else if (s.IsNotFound()) { @@ -310,7 +310,7 @@ void LInsertCmd::DoCmd(PClient* client) { if (kstd::StringEqualCaseInsensitive(client->argv_[2], "AFTER")) { before_or_after = storage::After; } - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->LInsert(client->Key(), before_or_after, client->argv_[3], client->argv_[4], &ret); if (!s.ok() && !s.IsNotFound()) { @@ -341,7 +341,7 @@ void LIndexCmd::DoCmd(PClient* client) { } std::string value; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LIndex(client->Key(), freq_, &value); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LIndex(client->Key(), freq_, &value); if (s.ok()) { client->AppendString(value); } else if (s.IsNotFound()) { @@ -363,7 +363,7 @@ bool LLenCmd::DoInitial(PClient* client) { void LLenCmd::DoCmd(PClient* client) { uint64_t llen = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LLen(client->Key(), &llen); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LLen(client->Key(), &llen); if (s.ok() || s.IsNotFound()) { client->AppendInteger(static_cast(llen)); } else if (s.IsInvalidArgument()) { diff --git a/src/cmd_raft.cc b/src/cmd_raft.cc index 1da14241..516d6965 100644 --- a/src/cmd_raft.cc +++ b/src/cmd_raft.cc @@ -127,7 +127,7 @@ void RaftNodeCmd::DoCmdRemove(PClient* client) { } void RaftNodeCmd::DoCmdSnapshot(PClient* client) { - auto self_snapshot_index = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetSmallestFlushedLogIndex(); + auto self_snapshot_index = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->GetSmallestFlushedLogIndex(); INFO("DoCmdSnapshot self_snapshot_index:{}", self_snapshot_index); auto s = RAFT_INST.DoSnapshot(self_snapshot_index); if (s.ok()) { diff --git a/src/cmd_set.cc b/src/cmd_set.cc index 3760f343..2acc3faa 100644 --- a/src/cmd_set.cc +++ b/src/cmd_set.cc @@ -25,7 +25,7 @@ bool SIsMemberCmd::DoInitial(PClient* client) { void SIsMemberCmd::DoCmd(PClient* client) { int32_t reply_Num = 0; // only change to 1 if ismember . key not exist it is 0 auto s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SIsmember(client->Key(), client->argv_[2], &reply_Num); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SIsmember(client->Key(), client->argv_[2], &reply_Num); if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); return; @@ -45,7 +45,7 @@ bool SAddCmd::DoInitial(PClient* client) { void SAddCmd::DoCmd(PClient* client) { const std::vector members(client->argv_.begin() + 2, client->argv_.end()); int32_t ret = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SAdd(client->Key(), members, &ret); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SAdd(client->Key(), members, &ret); if (s.ok()) { client->AppendInteger(ret); } else if (s.IsInvalidArgument()) { @@ -68,7 +68,7 @@ void SUnionStoreCmd::DoCmd(PClient* client) { std::vector keys(client->Keys().begin() + 1, client->Keys().end()); std::vector value_to_dest; int32_t ret = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->SUnionstore(client->Keys().at(0), keys, value_to_dest, &ret); if (!s.ok()) { @@ -92,7 +92,7 @@ bool SInterCmd::DoInitial(PClient* client) { void SInterCmd::DoCmd(PClient* client) { std::vector res_vt; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SInter(client->Keys(), &res_vt); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SInter(client->Keys(), &res_vt); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -116,7 +116,7 @@ void SRemCmd::DoCmd(PClient* client) { std::vector to_delete_members(client->argv_.begin() + 2, client->argv_.end()); int32_t reply_num = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SRem(client->Key(), to_delete_members, &reply_num); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SRem(client->Key(), to_delete_members, &reply_num); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -139,7 +139,7 @@ bool SUnionCmd::DoInitial(PClient* client) { void SUnionCmd::DoCmd(PClient* client) { std::vector res_vt; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SUnion(client->Keys(), &res_vt); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SUnion(client->Keys(), &res_vt); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -164,7 +164,7 @@ void SInterStoreCmd::DoCmd(PClient* client) { int32_t reply_num = 0; std::vector inter_keys(client->argv_.begin() + 2, client->argv_.end()); - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->SInterstore(client->Key(), inter_keys, value_to_dest, &reply_num); if (!s.ok()) { @@ -187,7 +187,7 @@ bool SCardCmd::DoInitial(PClient* client) { } void SCardCmd::DoCmd(PClient* client) { int32_t reply_Num = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SCard(client->Key(), &reply_Num); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SCard(client->Key(), &reply_Num); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -210,7 +210,7 @@ bool SMoveCmd::DoInitial(PClient* client) { return true; } void SMoveCmd::DoCmd(PClient* client) { int32_t reply_num = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->SMove(client->argv_[1], client->argv_[2], client->argv_[3], &reply_num); if (s.ok() || s.IsNotFound()) { @@ -246,7 +246,7 @@ bool SRandMemberCmd::DoInitial(PClient* client) { void SRandMemberCmd::DoCmd(PClient* client) { std::vector vec_ret; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SRandmember(client->argv_[1], this->num_rand, &vec_ret); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SRandmember(client->argv_[1], this->num_rand, &vec_ret); if (s.ok()) { if (client->argv_.size() == 3) { client->AppendStringVector(vec_ret); @@ -280,7 +280,7 @@ void SPopCmd::DoCmd(PClient* client) { int64_t cnt = 1; std::vector delete_member; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SPop(client->Key(), &delete_member, cnt); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SPop(client->Key(), &delete_member, cnt); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -299,7 +299,7 @@ void SPopCmd::DoCmd(PClient* client) { return; } storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SPop(client->Key(), &delete_members, cnt); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SPop(client->Key(), &delete_members, cnt); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -326,7 +326,7 @@ bool SMembersCmd::DoInitial(PClient* client) { void SMembersCmd::DoCmd(PClient* client) { std::vector delete_members; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SMembers(client->Key(), &delete_members); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SMembers(client->Key(), &delete_members); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -349,7 +349,7 @@ bool SDiffCmd::DoInitial(PClient* client) { void SDiffCmd::DoCmd(PClient* client) { std::vector diff_members; std::vector diff_keys(client->argv_.begin() + 1, client->argv_.end()); - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SDiff(diff_keys, &diff_members); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SDiff(diff_keys, &diff_members); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); @@ -373,7 +373,7 @@ void SDiffstoreCmd::DoCmd(PClient* client) { std::vector value_to_dest; int32_t reply_num = 0; std::vector diffstore_keys(client->argv_.begin() + 2, client->argv_.end()); - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->SDiffstore(client->Key(), diffstore_keys, value_to_dest, &reply_num); if (!s.ok()) { @@ -430,7 +430,7 @@ void SScanCmd::DoCmd(PClient* client) { // execute command std::vector members; int64_t next_cursor{}; - auto status = PSTORE.GetBackend(client->GetCurrentDB()) + auto status = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->SScan(client->Key(), cursor, pattern, count, &members, &next_cursor); if (!status.ok() && !status.IsNotFound()) { diff --git a/src/cmd_zset.cc b/src/cmd_zset.cc index b635fe59..8d23d52c 100644 --- a/src/cmd_zset.cc +++ b/src/cmd_zset.cc @@ -111,7 +111,7 @@ void ZAddCmd::DoCmd(PClient* client) { client->SetKey(client->argv_[1]); int32_t count = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZAdd(client->Key(), score_members_, &count); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZAdd(client->Key(), score_members_, &count); if (s.ok()) { client->AppendInteger(count); } else if (s.IsInvalidArgument()) { @@ -145,7 +145,7 @@ void ZPopMinCmd::DoCmd(PClient* client) { std::vector score_members; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZPopMin(client->Key(), count, &score_members); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZPopMin(client->Key(), count, &score_members); if (s.ok()) { char buf[32]; int64_t len = 0; @@ -186,7 +186,7 @@ void ZPopMaxCmd::DoCmd(PClient* client) { std::vector score_members; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZPopMax(client->Key(), count, &score_members); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZPopMax(client->Key(), count, &score_members); if (s.ok()) { char buf[32]; int64_t len = 0; @@ -275,7 +275,7 @@ bool ZInterstoreCmd::DoInitial(PClient* client) { return ZsetUIstoreParentCmd::D void ZInterstoreCmd::DoCmd(PClient* client) { int32_t count = 0; std::vector value_to_dest_; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZInterstore(dest_key_, keys_, weights_, aggregate_, value_to_dest_, &count); if (s.ok()) { @@ -294,7 +294,7 @@ bool ZUnionstoreCmd::DoInitial(PClient* client) { return ZsetUIstoreParentCmd::D void ZUnionstoreCmd::DoCmd(PClient* client) { int32_t count = 0; std::map value_to_dest; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZUnionstore(dest_key_, keys_, weights_, aggregate_, value_to_dest, &count); if (s.ok()) { @@ -335,7 +335,7 @@ void ZRevrangeCmd::DoCmd(PClient* client) { } std::vector score_members; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB()) + STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRevrange(client->Key(), static_cast(start), static_cast(stop), &score_members); if (s.ok() || s.IsNotFound()) { @@ -412,7 +412,7 @@ void ZRangebyscoreCmd::DoCmd(PClient* client) { return; } std::vector score_members; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRangebyscore(client->Key(), min_score, max_score, left_close, right_close, &score_members); if (!s.ok() && !s.IsNotFound()) { @@ -466,7 +466,7 @@ void ZRemrangebyrankCmd::DoCmd(PClient* client) { } storage::Status s; - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRemrangebyrank(client->Key(), start, end, &ret); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZRemrangebyrank(client->Key(), start, end, &ret); if (s.ok() || s.IsNotFound()) { client->AppendInteger(ret); } else if (s.IsInvalidArgument()) { @@ -531,7 +531,7 @@ void ZRevrangebyscoreCmd::DoCmd(PClient* client) { } std::vector score_members; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB()) + STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRevrangebyscore(client->Key(), min_score, max_score, left_close, right_close, &score_members); if (!s.ok() && !s.IsNotFound()) { @@ -572,7 +572,7 @@ bool ZCardCmd::DoInitial(PClient* client) { void ZCardCmd::DoCmd(PClient* client) { int32_t reply_Num = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZCard(client->Key(), &reply_Num); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZCard(client->Key(), &reply_Num); if (s.ok() || s.IsNotFound()) { client->AppendInteger(reply_Num); } else { @@ -665,27 +665,27 @@ void ZRangeCmd::DoCmd(PClient* client) { storage::Status s; if (!is_rev) { if (by_score) { - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRangebyscore(client->Key(), start, stop, left_close, right_close, &score_members); } else if (by_lex) { - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRangebylex(client->Key(), lex_min, lex_max, left_close, right_close, &lex_members); } else { - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRange(client->Key(), start, stop, &score_members); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZRange(client->Key(), start, stop, &score_members); } } else { if (by_score) { - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRevrangebyscore(client->Key(), start, stop, left_close, right_close, &score_members); } else if (by_lex) { - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRangebylex(client->Key(), lex_min, lex_max, left_close, right_close, &lex_members); } else { - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRevrange(client->Key(), start, stop, &score_members); } @@ -741,7 +741,7 @@ void ZScoreCmd::DoCmd(PClient* client) { double score = 0; storage::Status s; - s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZScore(client->Key(), client->argv_[2], &score); + s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZScore(client->Key(), client->argv_[2], &score); if (s.ok() || s.IsNotFound()) { char buf[32]; int64_t len = kstd::D2string(buf, sizeof(buf), score); @@ -795,7 +795,7 @@ void ZRangebylexCmd::DoCmd(PClient* client) { } std::vector members; storage::Status s; - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRangebylex(client->Key(), min_member, max_member, left_close, right_close, &members); if (!s.ok() && !s.IsNotFound()) { @@ -855,7 +855,7 @@ void ZRevrangebylexCmd::DoCmd(PClient* client) { int32_t ret = DoMemberRange(client->argv_[2], client->argv_[3], &left_close, &right_close, &min_member, &max_member); std::vector members; storage::Status s; - s = PSTORE.GetBackend(client->GetCurrentDB()) + s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRangebylex(client->Key(), min_member, max_member, left_close, right_close, &members); if (!s.ok() && !s.IsNotFound()) { @@ -887,7 +887,7 @@ bool ZRankCmd::DoInitial(PClient* client) { void ZRankCmd::DoCmd(PClient* client) { int32_t rank = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRank(client->Key(), client->argv_[2], &rank); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZRank(client->Key(), client->argv_[2], &rank); if (s.ok()) { client->AppendInteger(rank); } else if (s.IsNotFound()) { @@ -910,7 +910,7 @@ bool ZRevrankCmd::DoInitial(PClient* client) { void ZRevrankCmd::DoCmd(PClient* client) { int32_t revrank = 0; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRevrank(client->Key(), client->argv_[2], &revrank); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZRevrank(client->Key(), client->argv_[2], &revrank); if (s.ok()) { client->AppendInteger(revrank); } else if (s.IsNotFound()) { @@ -934,7 +934,7 @@ void ZRemCmd::DoCmd(PClient* client) { auto iter = client->argv_.begin() + 2; std::vector members(iter, client->argv_.end()); int32_t deleted = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRem(client->Key(), members, &deleted); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZRem(client->Key(), members, &deleted); if (s.ok() || s.IsNotFound()) { client->AppendInteger(deleted); } else if (s.IsInvalidArgument()) { @@ -962,7 +962,7 @@ void ZIncrbyCmd::DoCmd(PClient* client) { std::string member = client->argv_[3]; storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZIncrby(client->Key(), member, by, &score); + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZIncrby(client->Key(), member, by, &score); if (s.ok()) { char buf[32]; int64_t len = kstd::D2string(buf, sizeof(buf), score); @@ -994,7 +994,7 @@ void ZRemrangebyscoreCmd::DoCmd(PClient* client) { } int32_t s_ret = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) ->GetStorage() ->ZRemrangebyscore(client->Key(), min_score, max_score, left_close, right_close, &s_ret); if (s.ok()) { diff --git a/src/kiwi.cc b/src/kiwi.cc index 90404db0..2d31211d 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -187,7 +187,7 @@ bool KiwiDB::Init() { return false; } - PSTORE.Init(g_config.databases); + STORE_INST.Init(g_config.databases); PSlowLog::Instance().SetThreshold(g_config.slow_log_time); PSlowLog::Instance().SetLogLimit(static_cast(g_config.slow_log_max_len)); diff --git a/src/raft/raft.cc b/src/raft/raft.cc index a5a71afa..7006f22f 100644 --- a/src/raft/raft.cc +++ b/src/raft/raft.cc @@ -670,7 +670,7 @@ void Raft::on_apply(braft::Iterator& iter) { return; } - auto s = PSTORE.GetBackend(log.db_id())->GetStorage()->OnBinlogWrite(log, iter.index()); + auto s = STORE_INST.GetBackend(log.db_id())->GetStorage()->OnBinlogWrite(log, iter.index()); if (done) { // in leader dynamic_cast(done)->SetStatus(s); } @@ -697,7 +697,7 @@ int Raft::on_snapshot_load(braft::SnapshotReader* reader) { 2. When a node is improperly shut down and restarted, the minimum flush-index should be obtained as the starting point for fault recovery. */ - uint64_t replay_point = PSTORE.GetBackend(db_id_)->GetStorage()->GetSmallestFlushedLogIndex(); + uint64_t replay_point = STORE_INST.GetBackend(db_id_)->GetStorage()->GetSmallestFlushedLogIndex(); node_->set_last_applied_index_and_term(replay_point); is_node_first_start_up_ = false; INFO("set replay_point: {}", replay_point); @@ -716,7 +716,7 @@ int Raft::on_snapshot_load(braft::SnapshotReader* reader) { auto reader_path = reader->get_path(); // xx/snapshot_0000001 auto path = g_config.db_path + std::to_string(db_id_); // db/db_id TasksVector tasks(1, {TaskType::kLoadDBFromCheckpoint, db_id_, {{TaskArg::kCheckpointPath, reader_path}}, true}); - PSTORE.HandleTaskSpecificDB(tasks); + STORE_INST.HandleTaskSpecificDB(tasks); INFO("load snapshot success!"); return 0; } diff --git a/src/raft/snapshot.cc b/src/raft/snapshot.cc index 4e6467fc..0bb7fc0a 100644 --- a/src/raft/snapshot.cc +++ b/src/raft/snapshot.cc @@ -76,12 +76,12 @@ braft::FileAdaptor* PosixFileSystemAdaptor::open(const std::string& path, int of snapshot_meta_memtable.load_from_file(fs, meta_path); TasksVector tasks(1, {TaskType::kCheckpoint, 0, {{TaskArg::kCheckpointPath, snapshot_path}}, true}); - PSTORE.HandleTaskSpecificDB(tasks); + STORE_INST.HandleTaskSpecificDB(tasks); AddAllFiles(snapshot_path, &snapshot_meta_memtable, snapshot_path); // update snapshot last log index and last_log_term auto& new_meta = const_cast(snapshot_meta_memtable.meta()); - auto last_log_index = PSTORE.GetBackend(db_id)->GetStorage()->GetSmallestFlushedLogIndex(); + auto last_log_index = STORE_INST.GetBackend(db_id)->GetStorage()->GetSmallestFlushedLogIndex(); new_meta.set_last_included_index(last_log_index); auto last_log_term = RAFT_INST.GetTerm(last_log_index); new_meta.set_last_included_term(last_log_term); diff --git a/src/replication.cc b/src/replication.cc index 76db3bd0..a4f729b4 100644 --- a/src/replication.cc +++ b/src/replication.cc @@ -293,7 +293,7 @@ void PReplication::SaveTmpRdb(const char* data, std::size_t& len) { // if (masterInfo_.rdbRecved == masterInfo_.rdbSize) { // INFO("Rdb recv complete, bytes {}", masterInfo_.rdbSize); // - // //PSTORE.ResetDB(); + // //STORE_INST.ResetDB(); // // PDBLoader loader; // loader.Load(slaveRdbFile); diff --git a/src/store.cc b/src/store.cc index 9c014728..495337c9 100644 --- a/src/store.cc +++ b/src/store.cc @@ -20,14 +20,14 @@ namespace kiwi { -PStore::~PStore() { INFO("STORE is closing..."); } +Store::~Store() { INFO("STORE is closing..."); } -PStore& PStore::Instance() { - static PStore store; +Store& Store::Instance() { + static Store store; return store; } -void PStore::Init(int db_number) { +void Store::Init(int db_number) { db_number_ = db_number; backends_.reserve(db_number_); for (int i = 0; i < db_number_; i++) { @@ -39,7 +39,7 @@ void PStore::Init(int db_number) { INFO("STORE Init success!"); } -void PStore::HandleTaskSpecificDB(const TasksVector& tasks) { +void Store::HandleTaskSpecificDB(const TasksVector& tasks) { std::for_each(tasks.begin(), tasks.end(), [this](const auto& task) { if (task.db < 0 || task.db >= db_number_) { WARN("The database index is out of range."); diff --git a/src/store.h b/src/store.h index ed29d19f..b86fd5be 100644 --- a/src/store.h +++ b/src/store.h @@ -44,13 +44,13 @@ struct TaskContext { using TasksVector = std::vector; -class PStore { +class Store { public: - static PStore& Instance(); + static Store& Instance(); - PStore(const PStore&) = delete; - void operator=(const PStore&) = delete; - ~PStore(); + Store(const Store&) = delete; + void operator=(const Store&) = delete; + ~Store(); void Init(int db_number); @@ -61,11 +61,11 @@ class PStore { int GetDBNumber() const { return db_number_; } private: - PStore() = default; + Store() = default; int db_number_ = 0; std::vector> backends_; }; -#define PSTORE PStore::Instance() +#define STORE_INST Store::Instance() } // namespace kiwi From be90c589453093203c3e11b1c47da282c43660fd Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Mon, 6 Jan 2025 21:56:14 +0800 Subject: [PATCH 11/35] make format --- src/cmd_admin.cc | 2 +- src/db.cc | 3 +-- src/kiwi.cc | 3 +-- src/raft/raft.cc | 2 +- src/raft/raft.h | 1 + src/raft/snapshot.cc | 6 +++--- src/replication.cc | 3 +-- src/std/env.cc | 2 +- src/std/lock_mgr.cc | 2 +- src/std/memory_file.cc | 2 +- src/std/mutex_impl.cc | 2 +- src/std/scope_record_lock.cc | 2 +- src/std/tests/std_util_test.cc | 2 +- src/std/thread_pool.cc | 2 +- 14 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 2d86e622..ef7aab91 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -31,9 +31,9 @@ #include "braft/raft.h" #include "log.h" -#include "std_string.h" #include "resp_encode.h" #include "rocksdb/version.h" +#include "std_string.h" #include "kiwi.h" #include "raft/raft.h" diff --git a/src/db.cc b/src/db.cc index f7510ddb..eea526c3 100644 --- a/src/db.cc +++ b/src/db.cc @@ -31,8 +31,7 @@ rocksdb::Status DB::Open() { storage_options.options.periodic_compaction_seconds = kiwi::Config::GetInstance().rocksdb_periodic_second; storage_options.small_compaction_threshold = kiwi::Config::GetInstance().small_compaction_threshold; - storage_options.small_compaction_duration_threshold = - kiwi::Config::GetInstance().small_compaction_duration_threshold; + storage_options.small_compaction_duration_threshold = kiwi::Config::GetInstance().small_compaction_duration_threshold; if (kiwi::Config::GetInstance().use_raft) { storage_options.append_log_function = [&r = RAFT_INST](const Binlog& log, std::promise&& promise) { diff --git a/src/kiwi.cc b/src/kiwi.cc index dd03a058..f6dc3532 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -101,8 +101,7 @@ bool KiwiDB::ParseArgs(int argc, char* argv[]) { switch (c) { case 'v': { - std::cerr << "kiwi Server version: " << KIWI_VERSION << " bits=" << (sizeof(void*) == 8 ? 64 : 32) - << std::endl; + std::cerr << "kiwi Server version: " << KIWI_VERSION << " bits=" << (sizeof(void*) == 8 ? 64 : 32) << std::endl; std::cerr << "kiwi Server Build Type: " << KIWI_BUILD_TYPE << std::endl; std::cerr << "kiwi Server Build Date: " << KIWI_BUILD_DATE << std::endl; std::cerr << "kiwi Server Build GIT SHA: " << KIWI_GIT_COMMIT_ID << std::endl; diff --git a/src/raft/raft.cc b/src/raft/raft.cc index 9c69779b..307f3632 100644 --- a/src/raft/raft.cc +++ b/src/raft/raft.cc @@ -713,7 +713,7 @@ int Raft::on_snapshot_load(braft::SnapshotReader* reader) { } // 3. When a snapshot is installed on a node, you do not need to set a playback point. - auto reader_path = reader->get_path(); // xx/snapshot_0000001 + auto reader_path = reader->get_path(); // xx/snapshot_0000001 auto path = kiwi::Config::GetInstance().db_path + std::to_string(db_id_); // db/db_id TasksVector tasks(1, {TaskType::kLoadDBFromCheckpoint, db_id_, {{TaskArg::kCheckpointPath, reader_path}}, true}); PSTORE.HandleTaskSpecificDB(tasks); diff --git a/src/raft/raft.h b/src/raft/raft.h index 9a8262f7..a5110f15 100644 --- a/src/raft/raft.h +++ b/src/raft/raft.h @@ -86,6 +86,7 @@ class RaftWriteDoneClosure : public braft::Closure { delete this; } void SetStatus(rocksdb::Status status) { result_ = std::move(status); } + private: std::promise promise_; rocksdb::Status result_{rocksdb::Status::Aborted("Unknown error")}; diff --git a/src/raft/snapshot.cc b/src/raft/snapshot.cc index 4e6467fc..096f8a73 100644 --- a/src/raft/snapshot.cc +++ b/src/raft/snapshot.cc @@ -24,7 +24,7 @@ namespace kiwi { braft::FileAdaptor* PosixFileSystemAdaptor::open(const std::string& path, int oflag, - const ::google::protobuf::Message* file_meta, butil::File::Error* e) { + const ::google::protobuf::Message* file_meta, butil::File::Error* e) { if ((oflag & IS_RDONLY) == 0) { // This is a read operation bool snapshots_exists = false; std::string snapshot_path; @@ -101,8 +101,8 @@ braft::FileAdaptor* PosixFileSystemAdaptor::open(const std::string& path, int of } void PosixFileSystemAdaptor::AddAllFiles(const std::filesystem::path& dir, - braft::LocalSnapshotMetaTable* snapshot_meta_memtable, - const std::string& path) { + braft::LocalSnapshotMetaTable* snapshot_meta_memtable, + const std::string& path) { assert(snapshot_meta_memtable); for (const auto& entry : std::filesystem::directory_iterator(dir)) { if (entry.is_directory()) { diff --git a/src/replication.cc b/src/replication.cc index dec51994..3e9a6b75 100644 --- a/src/replication.cc +++ b/src/replication.cc @@ -225,8 +225,7 @@ void PReplication::Cron() { } else if (master->GetAuth()) { // send replconf char req[128]; - auto len = - snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", kiwi::Config::GetInstance().port); + auto len = snprintf(req, sizeof req - 1, "replconf listening-port %hu\r\n", kiwi::Config::GetInstance().port); std::string info(req, len); master->SendPacket(std::move(info)); masterInfo_.state = kPReplStateWaitReplconf; diff --git a/src/std/env.cc b/src/std/env.cc index dd10e9fc..ce5c1a9a 100644 --- a/src/std/env.cc +++ b/src/std/env.cc @@ -682,4 +682,4 @@ Status NewRandomRWFile(const std::string& fname, std::unique_ptr& return s; } -} // namespace std +} // namespace kstd diff --git a/src/std/lock_mgr.cc b/src/std/lock_mgr.cc index e1dfe90d..a441a25a 100644 --- a/src/std/lock_mgr.cc +++ b/src/std/lock_mgr.cc @@ -176,4 +176,4 @@ void LockMgr::UnLock(const std::string& key) { // Signal waiting threads to retry locking stripe->stripe_cv->NotifyAll(); } -} // namespace std::lock +} // namespace kstd::lock diff --git a/src/std/memory_file.cc b/src/std/memory_file.cc index 61bd0636..c7294d1a 100644 --- a/src/std/memory_file.cc +++ b/src/std/memory_file.cc @@ -211,4 +211,4 @@ void OutputMemoryFile::AssureSpace(size_t size) { ExtendFileSize(newSize); } -} // namespace std +} // namespace kstd diff --git a/src/std/mutex_impl.cc b/src/std/mutex_impl.cc index fe880f7a..051e84b1 100644 --- a/src/std/mutex_impl.cc +++ b/src/std/mutex_impl.cc @@ -117,4 +117,4 @@ Status CondVarImpl::WaitFor(std::shared_ptr mutex, int64_t timeout_time) // CV was signaled, or we spuriously woke up (but didn't time out) return s; } -} // namespace std::lock +} // namespace kstd::lock diff --git a/src/std/scope_record_lock.cc b/src/std/scope_record_lock.cc index ce67f94c..7a472535 100644 --- a/src/std/scope_record_lock.cc +++ b/src/std/scope_record_lock.cc @@ -76,4 +76,4 @@ void MultiRecordLock::Unlock(const std::vector& keys) { } } } -} // namespace std::lock +} // namespace kstd::lock diff --git a/src/std/tests/std_util_test.cc b/src/std/tests/std_util_test.cc index 279ee207..be561f64 100644 --- a/src/std/tests/std_util_test.cc +++ b/src/std/tests/std_util_test.cc @@ -3,8 +3,8 @@ // LICENSE file in the root directory of this source tree. An additional grant // of patent rights can be found in the PATENTS file in the same directory. -#include #include "std/std_util.h" +#include class UtilTest : public ::testing::Test {}; diff --git a/src/std/thread_pool.cc b/src/std/thread_pool.cc index f66140b1..83bb59aa 100644 --- a/src/std/thread_pool.cc +++ b/src/std/thread_pool.cc @@ -107,4 +107,4 @@ void ThreadPool::MonitorRoutine() { } } -} // namespace std +} // namespace kstd From c6aaa7c4c06ded99fd38a48fc7aa6b6486fad9c3 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Mon, 6 Jan 2025 22:16:23 +0800 Subject: [PATCH 12/35] fix clang format --- .github/workflows/kiwidb.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index b94924fc..219ff660 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -22,7 +22,7 @@ jobs: - name: Add LLVM apt repository run: | wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - - echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-13 main" | sudo tee /etc/apt/sources.list.d/llvm.list + echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main" | sudo tee /etc/apt/sources.list.d/llvm.list sudo apt-get update - name: Install clang-format-16 From 666e1c0cba3fac2635eb2e3527bfc2361b550f11 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 7 Jan 2025 00:00:19 +0800 Subject: [PATCH 13/35] clang-format --- .github/workflows/kiwidb.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index 219ff660..2264c608 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -31,7 +31,7 @@ jobs: sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 100 - name: Verify clang-format version - run: clang-format --version + run: clang-format-16 --version - name: Check Format working-directory: ${{ github.workspace }}/build From 909bbb338f68f9d36ac0ab80be560604065eb5a7 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 7 Jan 2025 00:05:41 +0800 Subject: [PATCH 14/35] format test --- .github/workflows/kiwidb.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index 2264c608..219ff660 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -31,7 +31,7 @@ jobs: sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 100 - name: Verify clang-format version - run: clang-format-16 --version + run: clang-format --version - name: Check Format working-directory: ${{ github.workspace }}/build From b61d489cdac24ab0644d908cb4481b327cb883a7 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 7 Jan 2025 00:09:46 +0800 Subject: [PATCH 15/35] update --- .github/workflows/kiwidb.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index 219ff660..683dbae1 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -31,7 +31,9 @@ jobs: sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 100 - name: Verify clang-format version - run: clang-format --version + run: | + clang-format --version + ls -l /usr/bin/clang-format* - name: Check Format working-directory: ${{ github.workspace }}/build From 97e258b06f3ea553af82ac168e0b82993f71eaee Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 7 Jan 2025 00:12:19 +0800 Subject: [PATCH 16/35] update --- .github/workflows/kiwidb.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index 683dbae1..3560b16e 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -34,6 +34,7 @@ jobs: run: | clang-format --version ls -l /usr/bin/clang-format* + update-alternatives --display clang-format - name: Check Format working-directory: ${{ github.workspace }}/build From b9ee3dc7ee85d7e7a9bcea869498b57b6baffb94 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 7 Jan 2025 00:16:05 +0800 Subject: [PATCH 17/35] update --- .github/workflows/kiwidb.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index 3560b16e..78dac800 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -29,6 +29,7 @@ jobs: run: | sudo apt-get install -y clang-format-16 sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 100 + sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-14 50 - name: Verify clang-format version run: | From b18402a511cd813a9069aa1cdc7ef2072fcdd734 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 7 Jan 2025 00:24:27 +0800 Subject: [PATCH 18/35] finish --- .github/workflows/kiwidb.yml | 5 +---- src/client.h | 3 ++- src/cmd_admin.cc | 3 ++- src/cmd_hash.cc | 8 +++++--- src/cmd_keys.cc | 6 ++++-- src/cmd_kv.cc | 11 +++++++---- src/cmd_list.cc | 8 +++++--- src/cmd_set.cc | 13 ++++++++----- src/cmd_zset.cc | 7 +++++-- src/net/callback_function.h | 3 +-- src/net/event_server.h | 6 ++++-- src/net/thread_manager.h | 13 +++++++++---- 12 files changed, 53 insertions(+), 33 deletions(-) diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index 78dac800..89543ee9 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -32,10 +32,7 @@ jobs: sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-14 50 - name: Verify clang-format version - run: | - clang-format --version - ls -l /usr/bin/clang-format* - update-alternatives --display clang-format + run: clang-format --version - name: Check Format working-directory: ${{ github.workspace }}/build diff --git a/src/client.h b/src/client.h index 9b2bdd3c..cde61f39 100644 --- a/src/client.h +++ b/src/client.h @@ -137,7 +137,8 @@ class PClient : public std::enable_shared_from_this { // If T is of type string, then the contents of string must be numbers template - requires(std::integral || std::same_as) void AppendArrayLen(T value) { + requires(std::integral || std::same_as) + void AppendArrayLen(T value) { AppendStringRaw(fmt::format("*{}\r\n", value)); } diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index f3f1629f..4a679a0a 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -718,7 +718,8 @@ void SortCmd::DoCmd(PClient* client) { client->AppendStringVector(ret_); } else { uint64_t reply_num = 0; - storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(store_key_, ret_, &reply_num); + storage::Status s = + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(store_key_, ret_, &reply_num); if (s.ok()) { client->AppendInteger(reply_num); } else { diff --git a/src/cmd_hash.cc b/src/cmd_hash.cc index 1193f1b5..3f11fa11 100644 --- a/src/cmd_hash.cc +++ b/src/cmd_hash.cc @@ -440,8 +440,9 @@ void HIncrbyCmd::DoCmd(PClient* client) { } int64_t temp = 0; - storage::Status s = - STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HIncrby(client->Key(), client->argv_[2], int_by, &temp); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) + ->GetStorage() + ->HIncrby(client->Key(), client->argv_[2], int_by, &temp); if (s.ok() || s.IsNotFound()) { client->AppendInteger(temp); } else if (s.IsInvalidArgument() && @@ -490,7 +491,8 @@ void HRandFieldCmd::DoCmd(PClient* client) { // execute command std::vector res; - auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HRandField(client->Key(), count, with_values, &res); + auto s = + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->HRandField(client->Key(), count, with_values, &res); if (s.IsNotFound()) { client->AppendString(""); return; diff --git a/src/cmd_keys.cc b/src/cmd_keys.cc index af58b288..a213b0cf 100644 --- a/src/cmd_keys.cc +++ b/src/cmd_keys.cc @@ -208,7 +208,8 @@ bool KeysCmd::DoInitial(PClient* client) { void KeysCmd::DoCmd(PClient* client) { std::vector keys; - auto s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Keys(storage::DataType::kAll, client->Key(), &keys); + auto s = + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Keys(storage::DataType::kAll, client->Key(), &keys); if (s.ok()) { client->AppendArrayLen(keys.size()); for (const auto& k : keys) { @@ -247,7 +248,8 @@ bool RenameCmd::DoInitial(PClient* client) { } void RenameCmd::DoCmd(PClient* client) { - storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Rename(client->Key(), client->argv_[2]); + storage::Status s = + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Rename(client->Key(), client->argv_[2]); if (s.ok()) { client->SetRes(CmdRes::kOK); } else if (s.IsNotFound()) { diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index e821aaa2..1b77b7b8 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -30,7 +30,8 @@ bool GetCmd::DoInitial(PClient* client) { void GetCmd::DoCmd(PClient* client) { PString value; int64_t ttl = -1; - storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->GetWithTTL(client->Key(), &value, &ttl); + storage::Status s = + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->GetWithTTL(client->Key(), &value, &ttl); if (s.ok()) { client->AppendString(value); } else if (s.IsNotFound()) { @@ -572,7 +573,8 @@ void GetBitCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kInvalidInt); return; } - storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->GetBit(client->Key(), offset, &bit_val); + storage::Status s = + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->GetBit(client->Key(), offset, &bit_val); if (s.ok()) { client->AppendInteger(bit_val); } else if (s.IsInvalidArgument()) { @@ -686,8 +688,9 @@ void SetRangeCmd::DoCmd(PClient* client) { return; } int32_t ret = 0; - storage::Status s = - STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Setrange(client->Key(), offset, client->argv_[3], &ret); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) + ->GetStorage() + ->Setrange(client->Key(), offset, client->argv_[3], &ret); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); diff --git a/src/cmd_list.cc b/src/cmd_list.cc index b7bd0ea8..1f785b51 100644 --- a/src/cmd_list.cc +++ b/src/cmd_list.cc @@ -71,7 +71,8 @@ bool RPoplpushCmd::DoInitial(PClient* client) { void RPoplpushCmd::DoCmd(PClient* client) { std::string value; - storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->RPoplpush(source_, receiver_, &value); + storage::Status s = + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->RPoplpush(source_, receiver_, &value); if (s.ok()) { client->AppendString(value); } else if (s.IsNotFound()) { @@ -217,8 +218,9 @@ void LRemCmd::DoCmd(PClient* client) { } uint64_t reply_num = 0; - storage::Status s = - STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->LRem(client->Key(), freq_, client->argv_[3], &reply_num); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) + ->GetStorage() + ->LRem(client->Key(), freq_, client->argv_[3], &reply_num); if (s.ok() || s.IsNotFound()) { client->AppendInteger(reply_num); } else if (s.IsInvalidArgument()) { diff --git a/src/cmd_set.cc b/src/cmd_set.cc index 2acc3faa..36671a06 100644 --- a/src/cmd_set.cc +++ b/src/cmd_set.cc @@ -24,8 +24,9 @@ bool SIsMemberCmd::DoInitial(PClient* client) { } void SIsMemberCmd::DoCmd(PClient* client) { int32_t reply_Num = 0; // only change to 1 if ismember . key not exist it is 0 - auto s = - STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SIsmember(client->Key(), client->argv_[2], &reply_Num); + auto s = STORE_INST.GetBackend(client->GetCurrentDB()) + ->GetStorage() + ->SIsmember(client->Key(), client->argv_[2], &reply_Num); if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); return; @@ -245,8 +246,9 @@ bool SRandMemberCmd::DoInitial(PClient* client) { void SRandMemberCmd::DoCmd(PClient* client) { std::vector vec_ret; - storage::Status s = - STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SRandmember(client->argv_[1], this->num_rand, &vec_ret); + storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB()) + ->GetStorage() + ->SRandmember(client->argv_[1], this->num_rand, &vec_ret); if (s.ok()) { if (client->argv_.size() == 3) { client->AppendStringVector(vec_ret); @@ -326,7 +328,8 @@ bool SMembersCmd::DoInitial(PClient* client) { void SMembersCmd::DoCmd(PClient* client) { std::vector delete_members; - storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SMembers(client->Key(), &delete_members); + storage::Status s = + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->SMembers(client->Key(), &delete_members); if (!s.ok()) { if (s.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); diff --git a/src/cmd_zset.cc b/src/cmd_zset.cc index 8d23d52c..7f4e3590 100644 --- a/src/cmd_zset.cc +++ b/src/cmd_zset.cc @@ -673,7 +673,9 @@ void ZRangeCmd::DoCmd(PClient* client) { ->GetStorage() ->ZRangebylex(client->Key(), lex_min, lex_max, left_close, right_close, &lex_members); } else { - s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZRange(client->Key(), start, stop, &score_members); + s = STORE_INST.GetBackend(client->GetCurrentDB()) + ->GetStorage() + ->ZRange(client->Key(), start, stop, &score_members); } } else { if (by_score) { @@ -934,7 +936,8 @@ void ZRemCmd::DoCmd(PClient* client) { auto iter = client->argv_.begin() + 2; std::vector members(iter, client->argv_.end()); int32_t deleted = 0; - storage::Status s = STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZRem(client->Key(), members, &deleted); + storage::Status s = + STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->ZRem(client->Key(), members, &deleted); if (s.ok() || s.IsNotFound()) { client->AppendInteger(deleted); } else if (s.IsInvalidArgument()) { diff --git a/src/net/callback_function.h b/src/net/callback_function.h index 10385c50..1ceb1c59 100644 --- a/src/net/callback_function.h +++ b/src/net/callback_function.h @@ -35,8 +35,7 @@ concept HasSetFdFunction = requires(T t, uint64_t id, int8_t index) { { (*t).GetConnId() } -> std::same_as; // GetFd return type is int { (*t).SetThreadIndex(index) } -> std::same_as; // SetThreadIndex return type is void { (*t).GetThreadIndex() } -> std::same_as; // GetThreadIndex return type is int8_t -} -|| std::is_class_v; // If T is an ordinary class, the member function is called directly +} || std::is_class_v; // If T is an ordinary class, the member function is called directly template requires HasSetFdFunction diff --git a/src/net/event_server.h b/src/net/event_server.h index 63906e94..77428424 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -101,7 +101,8 @@ class EventServer final { }; template -requires HasSetFdFunction std::pair EventServer::StartServer(int64_t interval) { +requires HasSetFdFunction +std::pair EventServer::StartServer(int64_t interval) { if (opt_.GetThreadNum() <= 0) { return std::pair(false, "thread num must be greater than 0"); } @@ -140,7 +141,8 @@ requires HasSetFdFunction std::pair EventServer::StartS } template -requires HasSetFdFunction std::pair EventServer::StartClientServer() { +requires HasSetFdFunction +std::pair EventServer::StartClientServer() { if (opt_.GetThreadNum() <= 0) { return std::pair(false, "thread num must be greater than 0"); } diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 66591cce..9fc1d4c7 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -116,7 +116,10 @@ class ThreadManager { }; template -requires HasSetFdFunction ThreadManager::~ThreadManager() { Stop(); } +requires HasSetFdFunction +ThreadManager::~ThreadManager() { + Stop(); +} template requires HasSetFdFunction @@ -206,7 +209,9 @@ void ThreadManager::OnNetEventClose(uint64_t connId, std::string &&err) { template requires HasSetFdFunction -void ThreadManager::CloseConnection(uint64_t connId) { OnNetEventClose(connId, ""); } +void ThreadManager::CloseConnection(uint64_t connId) { + OnNetEventClose(connId, ""); +} template requires HasSetFdFunction @@ -330,8 +335,8 @@ bool ThreadManager::CreateWriteThread() { } template -requires HasSetFdFunction uint64_t ThreadManager::DoTCPConnect(T &t, int fd, - const std::shared_ptr &conn) { +requires HasSetFdFunction +uint64_t ThreadManager::DoTCPConnect(T &t, int fd, const std::shared_ptr &conn) { auto connId = getConnId(); if constexpr (IsPointer_v) { t->SetConnId(connId); From f7a647308bd8853d6592ae6a5709e54916dc2dec Mon Sep 17 00:00:00 2001 From: alexstocks Date: Tue, 7 Jan 2025 16:47:21 +0800 Subject: [PATCH 19/35] fix clang warning --- src/common.cc | 19 ++++++------------- src/kiwi.cc | 2 +- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/common.cc b/src/common.cc index 7e04a015..84ceaa80 100644 --- a/src/common.cc +++ b/src/common.cc @@ -9,6 +9,8 @@ #include "common.h" #include +#include + #include #include #include @@ -17,6 +19,7 @@ #include #include #include + #include "unbounded_buffer.h" namespace kiwi { @@ -69,12 +72,8 @@ size_t FormatInt(long value, UnboundedBuffer* reply) { return 0; } - char val[32]; - int len = snprintf(val, sizeof val, "%ld", CRLF, value); - size_t oldSize = reply->ReadableSize(); - reply->PushData(":"); - reply->PushData(val, len); + reply->PushData(fmt::format(":{}{}", value, CRLF)); return reply->ReadableSize() - oldSize; } @@ -87,10 +86,7 @@ size_t FormatBulk(const char* str, size_t len, UnboundedBuffer* reply) { size_t oldSize = reply->ReadableSize(); reply->PushData("$"); - char val[32]; - int tmp = snprintf(val, sizeof val - 1, "%lu", CRLF, len); - reply->PushData(val, tmp); - + reply->PushData(fmt::format("{}{}", len, CRLF)); if (str && len > 0) { reply->PushData(str, len); } @@ -109,10 +105,7 @@ size_t PreFormatMultiBulk(size_t nBulk, UnboundedBuffer* reply) { size_t oldSize = reply->ReadableSize(); reply->PushData("*"); - - char val[32]; - int tmp = snprintf(val, sizeof val - 1, "%lu", CRLF, nBulk); - reply->PushData(val, tmp); + reply->PushData(fmt::format("{}{}", nBulk, CRLF)); return reply->ReadableSize() - oldSize; } diff --git a/src/kiwi.cc b/src/kiwi.cc index f63cc2ed..0a525d60 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -125,7 +125,7 @@ bool KiwiDB::ParseArgs(int argc, char* argv[]) { unsigned int optarg_long = static_cast(strlen(optarg)); char* str = (char*)calloc(optarg_long, sizeof(char*)); if (str) { - if (sscanf(optarg, "%s:%d", str, &master_port_) != 2) { + if (sscanf(optarg, "%s:%hu", str, &master_port_) != 2) { ERROR("Invalid slaveof format."); free(str); return false; From d7b919cc35ac1df169c3b3c60e857f903906ee6c Mon Sep 17 00:00:00 2001 From: alexstocks Date: Tue, 7 Jan 2025 18:33:15 +0800 Subject: [PATCH 20/35] using std::ignore --- src/common.cc | 7 ++----- src/net/base_event.h | 2 +- src/std/memory_file.cc | 8 +++----- src/storage/src/util.cc | 7 +++++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/common.cc b/src/common.cc index 84ceaa80..956838d6 100644 --- a/src/common.cc +++ b/src/common.cc @@ -84,9 +84,7 @@ size_t FormatBulk(const char* str, size_t len, UnboundedBuffer* reply) { } size_t oldSize = reply->ReadableSize(); - reply->PushData("$"); - - reply->PushData(fmt::format("{}{}", len, CRLF)); + reply->PushData(fmt::format("${}{}", len, CRLF)); if (str && len > 0) { reply->PushData(str, len); } @@ -104,8 +102,7 @@ size_t PreFormatMultiBulk(size_t nBulk, UnboundedBuffer* reply) { } size_t oldSize = reply->ReadableSize(); - reply->PushData("*"); - reply->PushData(fmt::format("{}{}", nBulk, CRLF)); + reply->PushData(fmt::format("*{}{}", nBulk, CRLF)); return reply->ReadableSize() - oldSize; } diff --git a/src/net/base_event.h b/src/net/base_event.h index 16a336e0..536d99c4 100644 --- a/src/net/base_event.h +++ b/src/net/base_event.h @@ -74,7 +74,7 @@ class BaseEvent : public std::enable_shared_from_this { bool run = true; if (running_.compare_exchange_strong(run, false)) { char signal_byte = 'X'; - ::write(pipeFd_[1], &signal_byte, sizeof(signal_byte)); // send signal to pipe,end poll loop + std::ignore = ::write(pipeFd_[1], &signal_byte, sizeof(signal_byte)); // send signal to pipe,end poll loop close(EvFd()); } } diff --git a/src/std/memory_file.cc b/src/std/memory_file.cc index c7294d1a..2434e9bb 100644 --- a/src/std/memory_file.cc +++ b/src/std/memory_file.cc @@ -45,8 +45,6 @@ bool InputMemoryFile::Open(const char* file) { file_ = ::open(file, O_RDONLY); if (file_ == kInvalidFile) { - char err[128]; - snprintf(err, sizeof(err - 1), "OpenForRead %s failed\n", file); return false; } @@ -122,14 +120,14 @@ bool OutputMemoryFile::Open(const char* file, bool bAppend) { offset_ = 0; } - ::ftruncate(file_, size_); + std::ignore = ::ftruncate(file_, size_); return MapWriteOnly(); } void OutputMemoryFile::Close() { if (file_ != kInvalidFile) { ::munmap(pMemory_, size_); - ::ftruncate(file_, offset_); + std::ignore = ::ftruncate(file_, offset_); ::close(file_); file_ = kInvalidFile; @@ -170,7 +168,7 @@ void OutputMemoryFile::Truncate(std::size_t size) { } size_ = size; - ::ftruncate(file_, size); + std::ignore = ::ftruncate(file_, size); if (offset_ > size_) { offset_ = size_; diff --git a/src/storage/src/util.cc b/src/storage/src/util.cc index a46b62a9..16ee8a9e 100644 --- a/src/storage/src/util.cc +++ b/src/storage/src/util.cc @@ -10,6 +10,8 @@ #include #include +#include + #include "src/base_data_key_format.h" #include "src/base_key_format.h" #include "src/coding.h" @@ -161,6 +163,7 @@ int delete_dir(const char* dirname) { if (nullptr == dir) { return -1; } + std::string tmp; while ((ptr = readdir(dir)) != nullptr) { ret = strcmp(ptr->d_name, "."); if (0 == ret) { @@ -170,8 +173,8 @@ int delete_dir(const char* dirname) { if (0 == ret) { continue; } - snprintf(chBuf, sizeof(chBuf), "%s/%s", dirname, ptr->d_name); - ret = is_dir(chBuf); + tmp = fmt::format("{}/{}", dirname, ptr->d_name); + ret = is_dir(tmp.data()); if (0 == ret) { // is dir ret = delete_dir(chBuf); From e093dc4262d54db17e0aae9765c1a2b2b3a97429 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Fri, 10 Jan 2025 13:27:56 +0800 Subject: [PATCH 21/35] update src/CMakeLists.txt by cursor --- src/CMakeLists.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f7383124..7026dc1e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,8 +16,8 @@ IF (CMAKE_BUILD_TYPE STREQUAL "Release") MESSAGE(STATUS "Git commit id: ${GIT_COMMIT_ID}") TARGET_COMPILE_DEFINITIONS(kiwi - PRIVATE Kkiwi_GIT_COMMIT_ID="${GIT_COMMIT_HASH}" - PRIVATE Kkiwi_BUILD_DATE="${BUILD_DATE}" + PRIVATE Kkiwi_GIT_COMMIT_ID="${GIT_COMMIT_ID}" + PRIVATE Kkiwi_BUILD_DATE="${BUILD_TIMESTAMP}" ) ENDIF () @@ -50,6 +50,10 @@ ADD_DEPENDENCIES(kiwi brpc storage resp + leveldb + raft + raft_pb + binlog_pb ) TARGET_LINK_LIBRARIES(kiwi @@ -76,6 +80,7 @@ TARGET_LINK_LIBRARIES(kiwi raft_pb binlog_pb resp + event "${LIB}" ) From b8d69af0edf002ac228a722b431317aeead287f1 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Fri, 10 Jan 2025 14:17:40 +0800 Subject: [PATCH 22/35] clang format --- src/cmd_admin.cc | 9 +++------ src/cmd_admin.h | 6 ++---- src/common.cc | 2 +- src/net/base_socket.h | 2 +- src/storage/src/base_data_value_format.h | 2 +- src/storage/src/redis.cc | 3 +-- src/storage/src/storage_murmur3.h | 3 ++- 7 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 4a679a0a..5ded6dec 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -465,8 +465,7 @@ void InfoCmd::InfoServer(std::string& info) { void InfoCmd::InfoStats(std::string& info) { std::stringstream tmp_stream; - tmp_stream << "# Stats" - << "\r\n"; + tmp_stream << "# Stats" << "\r\n"; tmp_stream << "is_bgsaving:" << (PREPL.IsBgsaving() ? "Yes" : "No") << "\r\n"; tmp_stream << "slow_logs_count:" << PSlowLog::Instance().GetLogsCount() << "\r\n"; @@ -479,8 +478,7 @@ void InfoCmd::InfoCPU(std::string& info) { getrusage(RUSAGE_SELF, &self_ru); getrusage(RUSAGE_CHILDREN, &c_ru); std::stringstream tmp_stream; - tmp_stream << "# CPU" - << "\r\n"; + tmp_stream << "# CPU" << "\r\n"; tmp_stream << "used_cpu_sys:" << std::setiosflags(std::ios::fixed) << std::setprecision(2) << static_cast(self_ru.ru_stime.tv_sec) + static_cast(self_ru.ru_stime.tv_usec) / 1000000 << "\r\n"; @@ -514,8 +512,7 @@ void InfoCmd::InfoCommandStats(PClient* client, std::string& info) { std::stringstream tmp_stream; tmp_stream.precision(2); tmp_stream.setf(std::ios::fixed); - tmp_stream << "# Commandstats" - << "\r\n"; + tmp_stream << "# Commandstats" << "\r\n"; auto cmdstat_map = client->GetCommandStatMap(); for (auto iter : *cmdstat_map) { if (iter.second.cmd_count_ != 0) { diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 853b6935..5fa0b57f 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -37,9 +37,7 @@ class CmdConfig : public BaseCmdGroup { bool DoInitial(PClient* client) override { return true; }; private: - // std::vector subCmd_; - - void DoCmd(PClient* client) override{}; + void DoCmd(PClient* client) override {}; }; class CmdConfigGet : public BaseCmd { @@ -296,7 +294,7 @@ class CmdDebug : public BaseCmdGroup { bool DoInitial(PClient* client) override { return true; }; private: - void DoCmd(PClient* client) override{}; + void DoCmd(PClient* client) override {}; }; class CmdDebugHelp : public BaseCmd { diff --git a/src/common.cc b/src/common.cc index 956838d6..3a9baccc 100644 --- a/src/common.cc +++ b/src/common.cc @@ -8,8 +8,8 @@ */ #include "common.h" -#include #include +#include #include #include diff --git a/src/net/base_socket.h b/src/net/base_socket.h index 5161cf2f..62195798 100644 --- a/src/net/base_socket.h +++ b/src/net/base_socket.h @@ -34,7 +34,7 @@ class BaseSocket : public NetEvent { ~BaseSocket() override = default; - void OnError() override{}; + void OnError() override {}; void Close() override; diff --git a/src/storage/src/base_data_value_format.h b/src/storage/src/base_data_value_format.h index 6ea2530c..f8c5266e 100644 --- a/src/storage/src/base_data_value_format.h +++ b/src/storage/src/base_data_value_format.h @@ -97,7 +97,7 @@ class ParsedBaseDataValue : public ParsedInternalValue { } protected: - virtual void SetVersionToValue() override{}; + virtual void SetVersionToValue() override {}; private: const size_t kBaseDataValueSuffixLength = kSuffixReserveLength + kTimestampLength; diff --git a/src/storage/src/redis.cc b/src/storage/src/redis.cc index 9b456cb1..1fa1efe0 100644 --- a/src/storage/src/redis.cc +++ b/src/storage/src/redis.cc @@ -303,8 +303,7 @@ Status Redis::SetOptions(const OptionType& option_type, const std::unordered_map void Redis::GetRocksDBInfo(std::string& info, const char* prefix) { std::ostringstream string_stream; - string_stream << "#" << prefix << "RocksDB" - << "\r\n"; + string_stream << "#" << prefix << "RocksDB" << "\r\n"; auto write_stream_key_value = [&](const Slice& property, const char* metric) { uint64_t value; diff --git a/src/storage/src/storage_murmur3.h b/src/storage/src/storage_murmur3.h index 958c5dbf..f3b468ae 100644 --- a/src/storage/src/storage_murmur3.h +++ b/src/storage/src/storage_murmur3.h @@ -58,7 +58,8 @@ inline uint32_t rotl32(uint32_t x, uint8_t r) { return (x << r) | (x >> (32 - r) #endif // defined(__GNUC__) || defined(__clang__) /* last resort (big-endian w/o __builtin_bswap) */ #ifndef BYTESWAP -# define BYTESWAP(x) ((((x)&0xFF) << 24) | (((x) >> 24) & 0xFF) | (((x)&0x0000FF00) << 8) | (((x)&0x00FF0000) >> 8)) +# define BYTESWAP(x) \ + ((((x) & 0xFF) << 24) | (((x) >> 24) & 0xFF) | (((x) & 0x0000FF00) << 8) | (((x) & 0x00FF0000) >> 8)) #endif //----------------------------------------------------------------------------- From d3afca5b98755709cfe83399babde2247858dbbe Mon Sep 17 00:00:00 2001 From: alexstocks Date: Fri, 10 Jan 2025 21:02:13 +0800 Subject: [PATCH 23/35] add clang-format before commit --- .github/workflows/kiwidb.yml | 9 +++--- .gitignore | 3 ++ CMakeLists.txt | 11 ++++++++ etc/script/check_format.sh | 54 ++++++++++++++++++++++++++++++++++++ src/net/base_event.h | 2 +- src/net/client_socket.h | 2 +- src/net/io_thread.h | 2 +- src/net/kqueue_event.h | 2 +- 8 files changed, 77 insertions(+), 8 deletions(-) create mode 100755 etc/script/check_format.sh diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index 89543ee9..bdaa922b 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -23,11 +23,12 @@ jobs: run: | wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main" | sudo tee /etc/apt/sources.list.d/llvm.list - sudo apt-get update + sudo apt update - - name: Install clang-format-16 + - name: Install clang-format-18 run: | - sudo apt-get install -y clang-format-16 + sudo apt install -y clang-format-18 + sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-18 200 sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 100 sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-14 50 @@ -109,7 +110,7 @@ jobs: - uses: actions/checkout@v4 - name: Install dependencies - run: sudo apt-get install -y ccache + run: sudo apt install -y ccache - name: Configure ccache run: | diff --git a/.gitignore b/.gitignore index bc85f55c..493f58ac 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,9 @@ download/ CMakeFiles/ CMakeCache.txt +# clang-format +temp.txt + # Precompiled Headers *.gch *.pch diff --git a/CMakeLists.txt b/CMakeLists.txt index 5448d1c8..4916a254 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -239,6 +239,17 @@ ADD_CUSTOM_TARGET(check-format --source_dirs ${FORMAT_DIRS} ) +FIND_PROGRAM(CLANG_FORMAT "clang-format") +IF(CLANG_FORMAT) + ADD_CUSTOM_TARGET( + format-check + COMMAND ${CMAKE_SOURCE_DIR}/etc/script/check-format.sh + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "Checking code format..." + VERBATIM + ) +ENDIF() + ADD_CUSTOM_TARGET(clang-tidy COMMAND ${BUILD_SUPPORT_DIR}/run_clang_tidy.py -clang-tidy-binary ${CLANG_TIDY_BIN} diff --git a/etc/script/check_format.sh b/etc/script/check_format.sh new file mode 100755 index 00000000..e43454bb --- /dev/null +++ b/etc/script/check_format.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# Set color output +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +# Counters +error_count=0 +checked_count=0 + +# Check file +check_file() { + local file=$1 + # Create a temporary file with formatted content + clang-format $file > temp.txt + + # Compare the original file with the formatted file + if ! diff -u "$file" temp.txt > /dev/null; then + echo -e "${RED}Needs formatting: $file${NC}" + # Show specific differences + diff -u "$file" temp.txt | grep -E "^[\+\-]" | head -n 10 + echo "..." + ((error_count++)) + else + echo -e "${GREEN}Correctly formatted: $file${NC}" + fi + ((checked_count++)) + + # Clean up temporary file + rm temp.txt +} + +# Main function +main() { + echo "Starting code format check..." + + # Find all C/C++ source files + while IFS= read -r -d '' file; do + check_file "$file" + done < <(find ./src -type f \( -name "*.cpp" -o -name "*.hpp" -o -name "*.c" -o -name "*.h" -o -name "*.cc" \) -print0) + + # Output summary + echo "----------------------------------------" + echo "Check completed!" + echo "Total files checked: $checked_count" + echo "Files needing formatting: $error_count" + + # Return non-zero if there are errors + [ "$error_count" -gt 0 ] && exit 1 || exit 0 +} + +# Run the main function +main diff --git a/src/net/base_event.h b/src/net/base_event.h index 536d99c4..22c097c5 100644 --- a/src/net/base_event.h +++ b/src/net/base_event.h @@ -46,7 +46,7 @@ class BaseEvent : public std::enable_shared_from_this { const static int EVENT_NULL; BaseEvent(const std::shared_ptr &listen, int8_t mode, int8_t type) - : listen_(listen), mode_(mode), type_(type){}; + : listen_(listen), mode_(mode), type_(type) {}; virtual ~BaseEvent() = default; diff --git a/src/net/client_socket.h b/src/net/client_socket.h index 6eb51641..3117c993 100644 --- a/src/net/client_socket.h +++ b/src/net/client_socket.h @@ -13,7 +13,7 @@ namespace net { class ClientSocket : public StreamSocket { public: - explicit ClientSocket(const SocketAddr& addr) : StreamSocket(0, SOCKET_TCP), addr_(addr){}; + explicit ClientSocket(const SocketAddr& addr) : StreamSocket(0, SOCKET_TCP), addr_(addr) {}; ~ClientSocket() override = default; diff --git a/src/net/io_thread.h b/src/net/io_thread.h index 56169558..00d22770 100644 --- a/src/net/io_thread.h +++ b/src/net/io_thread.h @@ -16,7 +16,7 @@ namespace net { class IOThread { public: - explicit IOThread(const std::shared_ptr &event) : baseEvent_(event){}; + explicit IOThread(const std::shared_ptr &event) : baseEvent_(event) {}; ~IOThread() = default; diff --git a/src/net/kqueue_event.h b/src/net/kqueue_event.h index e013d19f..d21b4026 100644 --- a/src/net/kqueue_event.h +++ b/src/net/kqueue_event.h @@ -24,7 +24,7 @@ namespace net { class KqueueEvent : public BaseEvent { public: explicit KqueueEvent(std::shared_ptr listen, int8_t mode) - : BaseEvent(std::move(listen), mode, BaseEvent::EVENT_TYPE_KQUEUE){}; + : BaseEvent(std::move(listen), mode, BaseEvent::EVENT_TYPE_KQUEUE) {}; ~KqueueEvent() override { Close(); } From 517161768386f6e03ac6c9125fe4bc5f3c7dc1db Mon Sep 17 00:00:00 2001 From: alexstocks Date: Fri, 10 Jan 2025 23:53:18 +0800 Subject: [PATCH 24/35] merge upstream --- etc/script/check_format.sh | 7 +++++++ src/cmd_admin.h | 8 ++++---- src/net/epoll_event.h | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/etc/script/check_format.sh b/etc/script/check_format.sh index e43454bb..675f673a 100755 --- a/etc/script/check_format.sh +++ b/etc/script/check_format.sh @@ -1,5 +1,12 @@ #!/bin/bash +# Check if clang-format is installed +if ! command -v clang-format &> /dev/null; then + echo "Error: clang-format is not installed" + echo "Please install clang-format first" + exit 1 +fi + # Set color output RED='\033[0;31m' GREEN='\033[0;32m' diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 5fa0b57f..4dfb59c6 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -297,15 +297,15 @@ class CmdDebug : public BaseCmdGroup { void DoCmd(PClient* client) override {}; }; -class CmdDebugHelp : public BaseCmd { +class CmdDebugHelp : public BaseCmd{ public: - CmdDebugHelp(const std::string& name, int16_t arity); + CmdDebugHelp(const std::string& name,int16_t arity); protected: - bool DoInitial(PClient* client) override; + bool DoInitial(PClient* client)override; private: - void DoCmd(PClient* client) override; + void DoCmd(PClient* client)override; }; class CmdDebugOOM : public BaseCmd { diff --git a/src/net/epoll_event.h b/src/net/epoll_event.h index d0efeb14..5f2d3ff2 100644 --- a/src/net/epoll_event.h +++ b/src/net/epoll_event.h @@ -23,7 +23,7 @@ namespace net { class EpollEvent : public BaseEvent { public: explicit EpollEvent(const std::shared_ptr &listen, int8_t mode) - : BaseEvent(listen, mode, BaseEvent::EVENT_TYPE_EPOLL){}; + : BaseEvent(listen, mode, BaseEvent::EVENT_TYPE_EPOLL) {}; ~EpollEvent() override { Close(); } From df7f2425411f34a5ffeffdb8175965c1c43cba86 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Fri, 10 Jan 2025 23:57:22 +0800 Subject: [PATCH 25/35] merge upstream --- src/cmd_admin.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 4dfb59c6..5fa0b57f 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -297,15 +297,15 @@ class CmdDebug : public BaseCmdGroup { void DoCmd(PClient* client) override {}; }; -class CmdDebugHelp : public BaseCmd{ +class CmdDebugHelp : public BaseCmd { public: - CmdDebugHelp(const std::string& name,int16_t arity); + CmdDebugHelp(const std::string& name, int16_t arity); protected: - bool DoInitial(PClient* client)override; + bool DoInitial(PClient* client) override; private: - void DoCmd(PClient* client)override; + void DoCmd(PClient* client) override; }; class CmdDebugOOM : public BaseCmd { From 880bce19c5ac4d37dcc119a5d7e394e760c64ecf Mon Sep 17 00:00:00 2001 From: alexstocks Date: Fri, 10 Jan 2025 23:46:03 +0800 Subject: [PATCH 26/35] test --- src/cmd_admin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 5fa0b57f..018b7388 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -291,7 +291,7 @@ class CmdDebug : public BaseCmdGroup { bool HasSubCommand() const override; protected: - bool DoInitial(PClient* client) override { return true; }; + bool DoInitial(PClient* client) override {return true; }; private: void DoCmd(PClient* client) override {}; From e229f38d7f415b3d9e7d3d2bbcee1933471d7e65 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Fri, 10 Jan 2025 23:48:09 +0800 Subject: [PATCH 27/35] test --- src/cmd_admin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 018b7388..5fa0b57f 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -291,7 +291,7 @@ class CmdDebug : public BaseCmdGroup { bool HasSubCommand() const override; protected: - bool DoInitial(PClient* client) override {return true; }; + bool DoInitial(PClient* client) override { return true; }; private: void DoCmd(PClient* client) override {}; From c523f82d9ca9c3290e11b4a9b7f5ec111919dce2 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sat, 11 Jan 2025 01:59:45 +0800 Subject: [PATCH 28/35] add precommit --- etc/script/build.sh | 13 +++++++++++++ etc/script/pre_commit.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100755 etc/script/pre_commit.sh diff --git a/etc/script/build.sh b/etc/script/build.sh index f91f0ea0..3af6886c 100755 --- a/etc/script/build.sh +++ b/etc/script/build.sh @@ -16,7 +16,18 @@ PWD=`pwd` PROJECT_HOME="${PWD}/" CONF="${PROJECT_HOME}/etc/conf/kiwi.conf" +function precommit() { + # Copy pre-commit hook to the .git/hooks directory + + if [ ! -f "./git/hooks/pre-commit" ];then + cp etc/script/pre_commit.sh .git/hooks/pre-commit + chmod +x .git/hooks/pre-commit + echo "Git hooks installed." + fi +} + function build() { + precommit if [ ! -f "/proc/cpuinfo" ];then CPU_CORE=$(sysctl -n hw.ncpu) else @@ -104,6 +115,7 @@ while true; do --kiwi) MAKE_FLAGS="${MAKE_FLAGS} kiwi" ;; + -h|--help) show_help ;; @@ -133,3 +145,4 @@ while true; do done build + diff --git a/etc/script/pre_commit.sh b/etc/script/pre_commit.sh new file mode 100755 index 00000000..96c8ee5b --- /dev/null +++ b/etc/script/pre_commit.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Path to the check_format.sh script +SCRIPT_PATH="etc/script/check_format.sh" + +# Check if the script exists +if [ ! -f "$SCRIPT_PATH" ]; then + echo "Error: $SCRIPT_PATH not found!" + exit 1 +fi + +# Make sure the script is executable +chmod +x "$SCRIPT_PATH" + +# Run the script on all staged files +#STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|cc|h|hpp|c)$') + +# if [ -n "$STAGED_FILES" ]; then +# for file in $STAGED_FILES; do +# "$SCRIPT_PATH" "$file" +# if [ $? -ne 0 ]; then +# echo "Commit aborted due to formatting issues." +# exit 1 +# fi +# done +# fi + +sh $SCRIPT_PATH + +exit 0 # Path to the check_format.sh script + From ad2acc88b692a3ff18f331e8eaa0cc95b545a0da Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sat, 11 Jan 2025 02:05:25 +0800 Subject: [PATCH 29/35] remark format-check --- CMakeLists.txt | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4916a254..4ba40a34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -239,16 +239,16 @@ ADD_CUSTOM_TARGET(check-format --source_dirs ${FORMAT_DIRS} ) -FIND_PROGRAM(CLANG_FORMAT "clang-format") -IF(CLANG_FORMAT) - ADD_CUSTOM_TARGET( - format-check - COMMAND ${CMAKE_SOURCE_DIR}/etc/script/check-format.sh - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - COMMENT "Checking code format..." - VERBATIM - ) -ENDIF() +# FIND_PROGRAM(CLANG_FORMAT "clang-format") +# IF(CLANG_FORMAT) +# ADD_CUSTOM_TARGET( +# format-check +# COMMAND ${CMAKE_SOURCE_DIR}/etc/script/check-format.sh +# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} +# COMMENT "Checking code format..." +# VERBATIM +# ) +# ENDIF() ADD_CUSTOM_TARGET(clang-tidy COMMAND ${BUILD_SUPPORT_DIR}/run_clang_tidy.py From 41f76211d97b2aedeabd3c2618e6aef5f062c945 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sat, 11 Jan 2025 12:06:11 +0800 Subject: [PATCH 30/35] add precommit in cmake files --- CMakeLists.txt | 21 ++++++++++----------- cmake/precommit.cmake | 19 +++++++++++++++++++ etc/script/build.sh | 11 ----------- etc/script/{pre_commit.sh => precommit.sh} | 0 src/CMakeLists.txt | 4 ++-- 5 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 cmake/precommit.cmake rename etc/script/{pre_commit.sh => precommit.sh} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ba40a34..0de858a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,6 +184,7 @@ ENDIF() SET(LIB ${LIB} CACHE INTERNAL "libs which should be linked for executable target") INCLUDE(ExternalProject) +INCLUDE(cmake/precommit.cmake) INCLUDE(cmake/findTools.cmake) INCLUDE(cmake/openssl.cmake) INCLUDE(cmake/gflags.cmake) @@ -223,6 +224,15 @@ FOREACH(IGNORE_FILE ${CLANG_FORMAT_IGNORE_FILES}) FILE(APPEND ${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt "${IGNORE_FILE}\n") ENDFOREACH() +# install git hooks when configure +EXECUTE_PROCESS( + COMMAND ${CMAKE_COMMAND} -E make_directory ${GIT_HOOKS_DIR} + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${HOOKS_SOURCE_DIR}/precommit.sh + ${GIT_HOOKS_DIR}/pre-commit + COMMAND ${CMAKE_COMMAND} -E chmod 755 ${GIT_HOOKS_DIR}/pre-commit +) + STRING(CONCAT FORMAT_DIRS "${PROJECT_SOURCE_DIR}/src,") ADD_CUSTOM_TARGET(format COMMAND ${BUILD_SUPPORT_DIR}/run_clang_format.py @@ -239,17 +249,6 @@ ADD_CUSTOM_TARGET(check-format --source_dirs ${FORMAT_DIRS} ) -# FIND_PROGRAM(CLANG_FORMAT "clang-format") -# IF(CLANG_FORMAT) -# ADD_CUSTOM_TARGET( -# format-check -# COMMAND ${CMAKE_SOURCE_DIR}/etc/script/check-format.sh -# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} -# COMMENT "Checking code format..." -# VERBATIM -# ) -# ENDIF() - ADD_CUSTOM_TARGET(clang-tidy COMMAND ${BUILD_SUPPORT_DIR}/run_clang_tidy.py -clang-tidy-binary ${CLANG_TIDY_BIN} diff --git a/cmake/precommit.cmake b/cmake/precommit.cmake new file mode 100644 index 00000000..79dfdda5 --- /dev/null +++ b/cmake/precommit.cmake @@ -0,0 +1,19 @@ +# Copyright (c) 2025-present, Arana/Kiwi Community. All rights reserved. +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. An additional grant +# of patent rights can be found in the PATENTS file in the same directory. + + +# define hooks source and target path +SET(HOOKS_SOURCE_DIR "${CMAKE_SOURCE_DIR}/etc/script") +set(GIT_HOOKS_DIR "${CMAKE_SOURCE_DIR}/.git/hooks") + +# ensure pre-commit hook is installed +ADD_CUSTOM_TARGET(install_git_hooks + COMMAND ${CMAKE_COMMAND} -E make_directory ${GIT_HOOKS_DIR} + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${HOOKS_SOURCE_DIR}/precommit.sh + ${GIT_HOOKS_DIR}/pre-commit + COMMAND ${CMAKE_COMMAND} -E chmod 755 ${GIT_HOOKS_DIR}/pre-commit + COMMENT "Installing git hooks..." +) diff --git a/etc/script/build.sh b/etc/script/build.sh index 3af6886c..fdb0e800 100755 --- a/etc/script/build.sh +++ b/etc/script/build.sh @@ -16,18 +16,7 @@ PWD=`pwd` PROJECT_HOME="${PWD}/" CONF="${PROJECT_HOME}/etc/conf/kiwi.conf" -function precommit() { - # Copy pre-commit hook to the .git/hooks directory - - if [ ! -f "./git/hooks/pre-commit" ];then - cp etc/script/pre_commit.sh .git/hooks/pre-commit - chmod +x .git/hooks/pre-commit - echo "Git hooks installed." - fi -} - function build() { - precommit if [ ! -f "/proc/cpuinfo" ];then CPU_CORE=$(sysctl -n hw.ncpu) else diff --git a/etc/script/pre_commit.sh b/etc/script/precommit.sh similarity index 100% rename from etc/script/pre_commit.sh rename to etc/script/precommit.sh diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7026dc1e..ac61f4f8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,9 +3,9 @@ # LICENSE file in the root directory of this source tree. An additional grant # of patent rights can be found in the PATENTS file in the same directory. -AUX_SOURCE_DIRECTORY(. kiwi_SRC) +AUX_SOURCE_DIRECTORY(. KIWI_SRC) -ADD_EXECUTABLE(kiwi ${kiwi_SRC}) +ADD_EXECUTABLE(kiwi ${KIWI_SRC}) IF (CMAKE_BUILD_TYPE STREQUAL "Release") # get current date and time and git commit id From 408e1b5ed346aa7467d79c3da54e136b14567d29 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sat, 11 Jan 2025 23:45:41 +0800 Subject: [PATCH 31/35] fix bugs --- .github/workflows/kiwidb.yml | 4 ++-- cmake/precommit.cmake | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index bdaa922b..667e1c54 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -4,7 +4,7 @@ on: push: branches: [ "unstable" ] pull_request: - branches: [ "unstable" ] + branches: [ "*" ] env: BUILD_DIR: cmake-build-release @@ -22,7 +22,7 @@ jobs: - name: Add LLVM apt repository run: | wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - - echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main" | sudo tee /etc/apt/sources.list.d/llvm.list + echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main" | sudo tee /etc/apt/sources.list.d/llvm.list sudo apt update - name: Install clang-format-18 diff --git a/cmake/precommit.cmake b/cmake/precommit.cmake index 79dfdda5..e1716a69 100644 --- a/cmake/precommit.cmake +++ b/cmake/precommit.cmake @@ -8,12 +8,12 @@ SET(HOOKS_SOURCE_DIR "${CMAKE_SOURCE_DIR}/etc/script") set(GIT_HOOKS_DIR "${CMAKE_SOURCE_DIR}/.git/hooks") -# ensure pre-commit hook is installed -ADD_CUSTOM_TARGET(install_git_hooks - COMMAND ${CMAKE_COMMAND} -E make_directory ${GIT_HOOKS_DIR} - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${HOOKS_SOURCE_DIR}/precommit.sh - ${GIT_HOOKS_DIR}/pre-commit - COMMAND ${CMAKE_COMMAND} -E chmod 755 ${GIT_HOOKS_DIR}/pre-commit - COMMENT "Installing git hooks..." -) +# # ensure pre-commit hook is installed +# ADD_CUSTOM_TARGET(install_git_hooks +# COMMAND ${CMAKE_COMMAND} -E make_directory ${GIT_HOOKS_DIR} +# COMMAND ${CMAKE_COMMAND} -E copy_if_different +# ${HOOKS_SOURCE_DIR}/precommit.sh +# ${GIT_HOOKS_DIR}/pre-commit +# COMMAND ${CMAKE_COMMAND} -E chmod 755 ${GIT_HOOKS_DIR}/pre-commit +# COMMENT "Installing git hooks..." +# ) From 7ad81b9e9cbe16f98c03ae229c6ace79113cfc32 Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sun, 12 Jan 2025 06:23:51 +0800 Subject: [PATCH 32/35] update cmake --- src/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7026dc1e..aa09ca48 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -80,8 +80,7 @@ TARGET_LINK_LIBRARIES(kiwi raft_pb binlog_pb resp - event "${LIB}" ) -SET_TARGET_PROPERTIES(kiwi PROPERTIES LINKER_LANGUAGE CXX) \ No newline at end of file +SET_TARGET_PROPERTIES(kiwi PROPERTIES LINKER_LANGUAGE CXX) From db1b621951ff77974d9c3d6083cec8e8001ab9ae Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sun, 12 Jan 2025 07:36:38 +0800 Subject: [PATCH 33/35] fix ci failure --- .github/workflows/kiwidb.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/kiwidb.yml b/.github/workflows/kiwidb.yml index 667e1c54..4d6d1db6 100644 --- a/.github/workflows/kiwidb.yml +++ b/.github/workflows/kiwidb.yml @@ -29,8 +29,6 @@ jobs: run: | sudo apt install -y clang-format-18 sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-18 200 - sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 100 - sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-14 50 - name: Verify clang-format version run: clang-format --version From 869324681f5cbee4600aeb0dbf5c14b77c2a13fc Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sun, 12 Jan 2025 09:38:24 +0800 Subject: [PATCH 34/35] add std::ignore header file --- src/std/memory_file.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/std/memory_file.cc b/src/std/memory_file.cc index 2434e9bb..b42936d4 100644 --- a/src/std/memory_file.cc +++ b/src/std/memory_file.cc @@ -9,8 +9,10 @@ #include #include #include + #include #include +#include #include "memory_file.h" From d14f52fbfbe2f3bc2a6ea60be1e9ab37fe291b0b Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sun, 12 Jan 2025 16:24:37 +0800 Subject: [PATCH 35/35] format --- src/storage/src/base_filter.h | 2 +- src/storage/src/lists_filter.h | 2 +- src/storage/src/strings_filter.h | 2 +- src/storage/src/zsets_filter.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/storage/src/base_filter.h b/src/storage/src/base_filter.h index 788b10fa..52ffb3bd 100644 --- a/src/storage/src/base_filter.h +++ b/src/storage/src/base_filter.h @@ -112,7 +112,7 @@ class BaseDataFilter : public rocksdb::CompactionFilter { UNUSED(new_value); UNUSED(value_changed); ParsedBaseDataKey parsed_base_data_key(key); - TRACE("[DataFilter], key: %s, data = %s, version = %llu", parsed_base_data_key.Key().ToString().c_str(), + TRACE("[DataFilter], key: %s, data = %s, version = %lu", parsed_base_data_key.Key().ToString().c_str(), parsed_base_data_key.Data().ToString().c_str(), parsed_base_data_key.Version()); const char* ptr = key.data(); diff --git a/src/storage/src/lists_filter.h b/src/storage/src/lists_filter.h index 723a0402..c5ea045e 100644 --- a/src/storage/src/lists_filter.h +++ b/src/storage/src/lists_filter.h @@ -31,7 +31,7 @@ class ListsDataFilter : public rocksdb::CompactionFilter { UNUSED(value_changed); ParsedListsDataKey parsed_lists_data_key(key); TRACE("==========================START=========================="); - TRACE("[DataFilter], key: %s, index = %llu, data = %s, version = %llu", + TRACE("[DataFilter], key: %s, index = %lu, data = %s, version = %lu", parsed_lists_data_key.key().ToString().c_str(), parsed_lists_data_key.index(), value.ToString().c_str(), parsed_lists_data_key.Version()); diff --git a/src/storage/src/strings_filter.h b/src/storage/src/strings_filter.h index 75a70f04..adcf5e70 100644 --- a/src/storage/src/strings_filter.h +++ b/src/storage/src/strings_filter.h @@ -25,7 +25,7 @@ class StringsFilter : public rocksdb::CompactionFilter { auto cur_time = static_cast(unix_time); ParsedStringsValue parsed_strings_value(value); TRACE("==========================START=========================="); - TRACE("[StringsFilter], key: %s, value = %s, timestamp: %llu, cur_time: %d", key.ToString().c_str(), + TRACE("[StringsFilter], key: %s, value = %s, timestamp: %lu, cur_time: %d", key.ToString().c_str(), parsed_strings_value.UserValue().ToString().c_str(), parsed_strings_value.Etime(), cur_time); if (parsed_strings_value.Etime() != 0 && parsed_strings_value.Etime() < cur_time) { diff --git a/src/storage/src/zsets_filter.h b/src/storage/src/zsets_filter.h index 0de17653..c5098a78 100644 --- a/src/storage/src/zsets_filter.h +++ b/src/storage/src/zsets_filter.h @@ -31,7 +31,7 @@ class ZSetsScoreFilter : public rocksdb::CompactionFilter { UNUSED(value_changed); ParsedZSetsScoreKey parsed_zsets_score_key(key); TRACE("==========================START=========================="); - TRACE("[ScoreFilter], key: %s, score = %lf, member = %s, version = %lld", + TRACE("[ScoreFilter], key: %s, score = %lf, member = %s, version = %ld", parsed_zsets_score_key.key().ToString().c_str(), parsed_zsets_score_key.score(), parsed_zsets_score_key.member().ToString().c_str(), parsed_zsets_score_key.Version());