forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disptch binlog sync operation to multi thread
- Loading branch information
Showing
11 changed files
with
209 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef PIKA_BINLOG_BGWORKER_H_ | ||
#define PIKA_BINLOG_BGWORKER_H_ | ||
#include "pika_command.h" | ||
#include "bg_thread.h" | ||
|
||
class BinlogBGWorker{ | ||
public: | ||
BinlogBGWorker() { | ||
cmds_.reserve(300); | ||
InitCmdTable(&cmds_); | ||
} | ||
~BinlogBGWorker() { | ||
binlogbg_thread_.Stop(); | ||
DestoryCmdTable(cmds_); | ||
} | ||
Cmd* GetCmd(const std::string& opt) { | ||
return GetCmdFromTable(opt, cmds_); | ||
} | ||
void Schedule(PikaCmdArgsType *argv, const std::string& raw_args, uint64_t serial) { | ||
BinlogBGArg *arg = new BinlogBGArg(argv, raw_args, serial, this); | ||
binlogbg_thread_.StartIfNeed(); | ||
binlogbg_thread_.Schedule(&DoBinlogBG, static_cast<void*>(arg)); | ||
} | ||
static void DoBinlogBG(void* arg); | ||
|
||
private: | ||
std::unordered_map<std::string, Cmd*> cmds_; | ||
pink::BGThread binlogbg_thread_; | ||
|
||
struct BinlogBGArg { | ||
PikaCmdArgsType *argv; | ||
std::string raw_args; | ||
uint64_t serial; | ||
BinlogBGWorker *myself; | ||
BinlogBGArg(PikaCmdArgsType* _argv, const std::string& _raw, uint64_t _s, BinlogBGWorker* _my) : | ||
argv(_argv), raw_args(_raw), serial(_s), myself(_my) {} | ||
~BinlogBGArg() { | ||
delete argv; | ||
} | ||
}; | ||
|
||
}; | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "pika_binlog_bgworker.h" | ||
#include "pika_server.h" | ||
#include "pika_conf.h" | ||
#include "slash_string.h" | ||
|
||
extern PikaServer* g_pika_server; | ||
extern PikaConf* g_pika_conf; | ||
|
||
void BinlogBGWorker::DoBinlogBG(void* arg) { | ||
BinlogBGArg *bgarg = static_cast<BinlogBGArg*>(arg); | ||
PikaCmdArgsType argv = *(bgarg->argv); | ||
uint64_t my_serial = bgarg->serial; | ||
BinlogBGWorker *self = bgarg->myself; | ||
std::string opt = argv[0]; | ||
slash::StringToLower(opt); | ||
|
||
// Get command info | ||
const CmdInfo* const cinfo_ptr = GetCmdInfo(opt); | ||
Cmd* c_ptr = self->GetCmd(opt); | ||
if (!cinfo_ptr || !c_ptr) { | ||
LOG(ERROR) << "Error operation from binlog: " << opt; | ||
} | ||
c_ptr->res().clear(); | ||
|
||
// Initial | ||
c_ptr->Initial(argv, cinfo_ptr); | ||
if (!c_ptr->res().ok()) { | ||
LOG(ERROR) << "Fail to initial command from binlog: " << opt; | ||
} | ||
|
||
uint64_t start_us; | ||
if (g_pika_conf->slowlog_slower_than() >= 0) { | ||
start_us = slash::NowMicros(); | ||
} | ||
|
||
g_pika_server->mutex_record_.Lock(argv[1]); | ||
|
||
// Add read lock for no suspend command | ||
if (!cinfo_ptr->is_suspend()) { | ||
pthread_rwlock_rdlock(g_pika_server->rwlock()); | ||
} | ||
|
||
// Force the binlog write option to serialize | ||
if (g_pika_server->WaitTillBinlogBGSerial(my_serial)) { | ||
g_pika_server->logger_->Lock(); | ||
g_pika_server->logger_->Put(bgarg->raw_args); | ||
g_pika_server->logger_->Unlock(); | ||
g_pika_server->SignalNextBinlogBGSerial(); | ||
|
||
c_ptr->Do(); | ||
} | ||
|
||
if (!cinfo_ptr->is_suspend()) { | ||
pthread_rwlock_unlock(g_pika_server->rwlock()); | ||
} | ||
|
||
g_pika_server->mutex_record_.Unlock(argv[1]); | ||
|
||
if (g_pika_conf->slowlog_slower_than() >= 0) { | ||
int64_t duration = slash::NowMicros() - start_us; | ||
if (duration > g_pika_conf->slowlog_slower_than()) { | ||
LOG(ERROR) << "command:" << opt << ", start_time(s): " << start_us / 1000000 << ", duration(us): " << duration; | ||
} | ||
} | ||
|
||
delete bgarg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.