Skip to content

Commit

Permalink
fix some ut error by commands filter and return logic
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbt-hz committed Jun 19, 2024
1 parent c12d2e9 commit 06e739d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ enum CmdFlags {
kCmdFlagsFast = (1 << 21),
kCmdFlagsSlow = (1 << 22),
kCmdTransaction = (1 << 23),
kCmdNoReadBeforeQueue = (1 << 24),
};

void inline RedisAppendContent(std::string& str, const std::string& value);
Expand Down
26 changes: 13 additions & 13 deletions src/pika_client_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,12 @@ void PikaClientConn::ProcessRedisCmds(const std::vector<net::RedisCmdArgsType>&
std::string opt = argvs[0][0];
pstd::StringToLower(opt);
bool is_slow_cmd = g_pika_conf->is_slow_cmd(opt);
bool read_status = false;
std::shared_ptr<Cmd> c_ptr = g_pika_cmd_table_manager->GetCmd(opt);

if (PIKA_CACHE_NONE != g_pika_conf->cache_mode()){
if ( c_ptr && c_ptr->is_cacheread() ){
// read in cache
read_status = BatchReadCmdInCache(argvs);
time_stat_->process_done_ts_ = pstd::NowMicros();
auto cmdstat_map = g_pika_cmd_table_manager->GetCommandStatMap();
(*cmdstat_map)[opt].cmd_count.fetch_add(1);
(*cmdstat_map)[opt].cmd_time_consuming.fetch_add(time_stat_->total_time());
if (read_status){
if (BatchReadCmdInCache(argvs)){
return;
}
}
Expand Down Expand Up @@ -351,17 +345,23 @@ bool PikaClientConn::BatchReadCmdInCache(const std::vector<net::RedisCmdArgsType
if (!cur_keys.empty()){
record_lock.Lock(cur_keys);
}
if (!c_ptr->DoReadCommandInCache()) {
read_status = false;
}

read_status = c_ptr->DoReadCommandInCache();
time_stat_->process_done_ts_ = pstd::NowMicros();
auto cmdstat_map = g_pika_cmd_table_manager->GetCommandStatMap();
(*cmdstat_map)[argv[0]].cmd_count.fetch_add(1);
(*cmdstat_map)[argv[0]].cmd_time_consuming.fetch_add(time_stat_->total_time());
*resp_ptr = std::move(c_ptr->res().message());
resp_num--;
record_lock.Unlock(cur_keys);
}
time_stat_->process_done_ts_ = pstd::NowMicros();
TryWriteResp();

return read_status;
if(!read_status){
return read_status;
}else{
TryWriteResp();
return read_status;
}
}

void PikaClientConn::TryWriteResp() {
Expand Down
23 changes: 12 additions & 11 deletions src/pika_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -736,31 +736,31 @@ void InitCmdTable(CmdTable* cmd_table) {
// PubSub
////Publish
std::unique_ptr<Cmd> publishptr =
std::make_unique<PublishCmd>(kCmdNamePublish, 3, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsFast);
std::make_unique<PublishCmd>(kCmdNamePublish, 3, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsFast | kCmdNoReadBeforeQueue);
cmd_table->insert(std::pair<std::string, std::unique_ptr<Cmd>>(kCmdNamePublish, std::move(publishptr)));
////Subscribe
std::unique_ptr<Cmd> subscribeptr =
std::make_unique<SubscribeCmd>(kCmdNameSubscribe, -2, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow);
std::make_unique<SubscribeCmd>(kCmdNameSubscribe, -2, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow | kCmdNoReadBeforeQueue);
cmd_table->insert(std::pair<std::string, std::unique_ptr<Cmd>>(kCmdNameSubscribe, std::move(subscribeptr)));
////UnSubscribe
std::unique_ptr<Cmd> unsubscribeptr =
std::make_unique<UnSubscribeCmd>(kCmdNameUnSubscribe, -1, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow);
std::make_unique<UnSubscribeCmd>(kCmdNameUnSubscribe, -1, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow | kCmdNoReadBeforeQueue);
cmd_table->insert(std::pair<std::string, std::unique_ptr<Cmd>>(kCmdNameUnSubscribe, std::move(unsubscribeptr)));
////PSubscribe
std::unique_ptr<Cmd> psubscribeptr =
std::make_unique<PSubscribeCmd>(kCmdNamePSubscribe, -2, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow);
std::make_unique<PSubscribeCmd>(kCmdNamePSubscribe, -2, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow | kCmdNoReadBeforeQueue);
cmd_table->insert(std::pair<std::string, std::unique_ptr<Cmd>>(kCmdNamePSubscribe, std::move(psubscribeptr)));
////PUnSubscribe
std::unique_ptr<Cmd> punsubscribeptr =
std::make_unique<PUnSubscribeCmd>(kCmdNamePUnSubscribe, -1, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow);
std::make_unique<PUnSubscribeCmd>(kCmdNamePUnSubscribe, -1, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow | kCmdNoReadBeforeQueue);
cmd_table->insert(std::pair<std::string, std::unique_ptr<Cmd>>(kCmdNamePUnSubscribe, std::move(punsubscribeptr)));
////PubSub
std::unique_ptr<Cmd> pubsubptr =
std::make_unique<PubSubCmd>(kCmdNamePubSub, -2, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow);
std::make_unique<PubSubCmd>(kCmdNamePubSub, -2, kCmdFlagsRead | kCmdFlagsPubSub | kCmdFlagsSlow | kCmdNoReadBeforeQueue);
cmd_table->insert(std::pair<std::string, std::unique_ptr<Cmd>>(kCmdNamePubSub, std::move(pubsubptr)));

////ACL
std::unique_ptr<Cmd> aclptr = std::make_unique<PikaAclCmd>(KCmdNameAcl, -2, kCmdFlagsAdmin | kCmdFlagsSlow);
std::unique_ptr<Cmd> aclptr = std::make_unique<PikaAclCmd>(KCmdNameAcl, -2, kCmdFlagsAdmin | kCmdFlagsSlow | kCmdNoReadBeforeQueue);
cmd_table->insert(std::pair<std::string, std::unique_ptr<Cmd>>(KCmdNameAcl, std::move(aclptr)));

// Transaction
Expand Down Expand Up @@ -928,11 +928,12 @@ bool Cmd::DoReadCommandInCache(const HintKeys& hint_keys) {
if (IsNeedReadCache()) {
ReadCache();
}
if (is_read() && res().CacheMiss()) {
return false;
// 只有读命令且返回值为命中的情况才返回true
if (is_read() && !res().CacheMiss()) {
return true;
}
}
return true;
return false;
}


Expand Down Expand Up @@ -965,7 +966,7 @@ void Cmd::DoBinlog() {
bool Cmd::hasFlag(uint32_t flag) const { return (flag_ & flag); }
bool Cmd::is_read() const { return (flag_ & kCmdFlagsRead); }
bool Cmd::is_write() const { return (flag_ & kCmdFlagsWrite); }
bool Cmd::is_cacheread() const { return (flag_ & kCmdFlagsRead) && !(flag_ & kCmdFlagsAdmin) && !(flag_ & kCmdTransaction); }
bool Cmd::is_cacheread() const { return (flag_ & kCmdFlagsRead) && !(flag_ & kCmdFlagsAdmin) && !(flag_ & kCmdFlagsOperateKey) && !(flag_ & kCmdTransaction) && !(flag_ & kCmdNoReadBeforeQueue);}
bool Cmd::IsLocal() const { return (flag_ & kCmdFlagsLocal); }

int8_t Cmd::SubCmdIndex(const std::string& cmdName) {
Expand Down

0 comments on commit 06e739d

Please sign in to comment.