Skip to content

Commit

Permalink
fix exec_count_db data race issue (OpenAtomFoundation#2671)
Browse files Browse the repository at this point in the history
Co-authored-by: cjh <[email protected]>
  • Loading branch information
2 people authored and brother-jin committed Jul 31, 2024
1 parent 6f17ac8 commit 05706fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,17 @@ class PikaServer : public pstd::noncopyable {
*/
int64_t GetLastSave() const {return lastsave_;}
void UpdateLastSave(int64_t lastsave) {lastsave_ = lastsave;}
void InitStatistic(CmdTable *inited_cmd_table) {
// we insert all cmd name to statistic_.server_stat.exec_count_db,
// then when we can call PikaServer::UpdateQueryNumAndExecCountDB(const std::string&, const std::string&, bool) in parallel without lock
// although exec_count_db(unordered_map) is not thread-safe, but we won't trigger any insert or erase operation toward exec_count_db(unordered_map) during the running of pika
auto &exec_stat_map = statistic_.server_stat.exec_count_db;
for (auto& it : *inited_cmd_table) {
std::string cmd_name = it.first; //value copy is needed
pstd::StringToUpper(cmd_name); //cmd_name now is all uppercase
exec_stat_map.insert(std::make_pair(cmd_name, 0));
}
}
private:
/*
* TimingTask use
Expand Down
3 changes: 2 additions & 1 deletion src/pika.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ int main(int argc, char* argv[]) {
g_pika_rm = std::make_unique<PikaReplicaManager>();
g_network_statistic = std::make_unique<net::NetworkStatistic>();
g_pika_server->InitDBStruct();

//the cmd table of g_pika_cmd_table_manager must be inited before calling PikaServer::InitStatistic(CmdTable* )
g_pika_server->InitStatistic(g_pika_cmd_table_manager->GetCmdTable());
auto status = g_pika_server->InitAcl();
if (!status.ok()) {
LOG(FATAL) << status.ToString();
Expand Down

0 comments on commit 05706fd

Please sign in to comment.