Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Rename Command #2453

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,15 @@ cache-lfu-decay-time: 1
#
# aclfile : ../conf/users.acl

# It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
#
# Example:
#
rename-command : flushall 360flushall
rename-command : slaveof 360slaveof
# rename-command : config 360config
# rename-command : shutdown 360shutdown
# rename-command : bgsave 360bgsave
# rename-command : dumpoff 360dumpoff
# rename-command : client 360client
1 change: 1 addition & 0 deletions include/pika_cmd_table_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class PikaCmdTableManager {
PikaCmdTableManager();
virtual ~PikaCmdTableManager() = default;
void InitCmdTable(void);
void RenameCommand(const std::string before, const std::string after);
std::shared_ptr<Cmd> GetCmd(const std::string& opt);
bool CmdExist(const std::string& cmd) const;
CmdTable* GetCmdTable();
Expand Down
1 change: 1 addition & 0 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ class PikaConf : public pstd::BaseConf {
std::vector<std::string> users_; // acl user rules

std::string aclFile_;
std::vector<std::string> cmd_;

std::atomic<uint32_t> acl_pubsub_default_ = 0; // default channel pub/sub permission
std::atomic<uint32_t> acl_Log_max_len_ = 0; // default acl log max len
Expand Down
4 changes: 2 additions & 2 deletions src/pika.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ int main(int argc, char* argv[]) {
usage();
exit(-1);
}
g_pika_cmd_table_manager = std::make_unique<PikaCmdTableManager>();
g_pika_cmd_table_manager->InitCmdTable();
PikaConfInit(path);

rlimit limit;
Expand Down Expand Up @@ -205,8 +207,6 @@ int main(int argc, char* argv[]) {
PikaSignalSetup();

LOG(INFO) << "Server at: " << path;
g_pika_cmd_table_manager = std::make_unique<PikaCmdTableManager>();
g_pika_cmd_table_manager->InitCmdTable();
g_pika_server = new PikaServer();
g_pika_rm = std::make_unique<PikaReplicaManager>();
g_network_statistic = std::make_unique<net::NetworkStatistic>();
Expand Down
8 changes: 8 additions & 0 deletions src/pika_cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ void PikaCmdTableManager::InitCmdTable(void) {
}
}

void PikaCmdTableManager::RenameCommand(const std::string before, const std::string after) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

将 before 转为小写字母,再处理

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑 after 为 "" 的情况,比如在 conf 文件里面配置的 hget:
rename-command : hget
rename-command : set 360set

redis 里面会先把命令删除了:https://github.com/redis/redis/blob/unstable/src/config.c
image

另外,如果 before cmd 不存在,直接返回错误,让用户去修改配置文件

auto it = cmds_->find(before);
if (it != cmds_->end()) {
cmds_->insert(std::pair<std::string, std::unique_ptr<Cmd>>(after, std::move(it->second)));
cmds_->erase(it);
}
}

std::unordered_map<std::string, CommandStatistics>* PikaCmdTableManager::GetCommandStatMap() {
return &cmdstat_map_;
}
Expand Down
15 changes: 13 additions & 2 deletions src/pika_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

#include "cache/include/config.h"
#include "include/acl.h"
#include "include/pika_define.h"
#include "include/pika_cmd_table_manager.h"
#include "include/pika_conf.h"
#include "include/pika_define.h"

using pstd::Status;
extern std::unique_ptr<PikaCmdTableManager> g_pika_cmd_table_manager;

PikaConf::PikaConf(const std::string& path)
: pstd::BaseConf(path), conf_path_(path) {}
Expand Down Expand Up @@ -465,7 +467,16 @@ int PikaConf::Load() {
GetConfStrMulti("user", &users_);

GetConfStr("aclfile", &aclFile_);

GetConfStr("rename-command", &cmd_);
std::string before, after;
for (const auto & i : cmd_) {
std::istringstream iss(i);
iss >> before;
if (iss) {
iss >> after;
g_pika_cmd_table_manager->RenameCommand(before, after);
}
}
std::string acl_pubsub_default;
GetConfStr("acl-pubsub-default", &acl_pubsub_default);
if (acl_pubsub_default == "allchannels") {
Expand Down
2 changes: 1 addition & 1 deletion src/pstd/include/base_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BaseConf {
bool GetConfIntHuman(const std::string& name, int* value) const;
bool GetConfInt64(const std::string& name, int64_t* value) const;
bool GetConfInt64Human(const std::string& name, int64_t* value) const;

bool GetConfStr(const std::string& name, std::vector<std::string>* values) const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接使用 GetConfStrMulti 方法,直接获取一个数组

bool GetConfStr(const std::string& name, std::string* value) const;
bool GetConfBool(const std::string& name, bool* value) const;
bool GetConfStrVec(const std::string& name, std::vector<std::string>* value) const;
Expand Down
13 changes: 13 additions & 0 deletions src/pstd/src/base_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ bool BaseConf::GetConfStr(const std::string& name, std::string* val) const {
return false;
}

bool BaseConf::GetConfStr(const std::string& name, std::vector<std::string>* values) const {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法用 GetConfStrMulti 替换

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法获取的结果不符合预期

image

for (const auto& i : rep_->item) {
if (i.type == 1) {
continue;
}
if (name == i.name) {
values->push_back(i.value);
}
}

return !values->empty();
}

bool BaseConf::GetConfStrVec(const std::string& name, std::vector<std::string>* value) const {
for (auto& i : rep_->item) {
if (i.type == Rep::kComment) {
Expand Down
Loading