Skip to content

Commit

Permalink
add rename-command config (#2455)
Browse files Browse the repository at this point in the history
add rename-command  config
  • Loading branch information
Mixficsol authored Mar 6, 2024
1 parent ab9ed71 commit a753d90
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
15 changes: 15 additions & 0 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,18 @@ 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 Warning: To prevent
# data inconsistency caused by different configuration files, do not use the rename
# command to modify write commands on the primary and secondary servers. If necessary,
# ensure that the configuration files of the primary and secondary servers are consistent
# In addition, when using the command rename, you must not use "" to modify the command,
# for example, rename-command: FLUSHALL "360flushall" is incorrect; instead, use
# rename-command: FLUSHALL 360flushall is correct. After the rename command is executed,
# it is most appropriate to use a numeric string with uppercase or lowercase letters
# for example: rename-command : FLUSHALL joYAPNXRPmcarcR4ZDgC81TbdkSmLAzRPmcarcR
#
# Example:
#
# rename-command : FLUSHALL 360flushall
# rename-command : FLUSHDB 360flushdb
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
2 changes: 1 addition & 1 deletion include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ class PikaConf : public pstd::BaseConf {
std::vector<std::string> users_; // acl user rules

std::string aclFile_;

std::vector<std::string> cmds_;
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
12 changes: 12 additions & 0 deletions src/pika_cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ void PikaCmdTableManager::InitCmdTable(void) {
}
}

void PikaCmdTableManager::RenameCommand(const std::string before, const std::string after) {
auto it = cmds_->find(before);
if (it != cmds_->end()) {
if (after.length() > 0) {
cmds_->insert(std::pair<std::string, std::unique_ptr<Cmd>>(after, std::move(it->second)));
} else {
LOG(ERROR) << "The value of rename-command is null";
}
cmds_->erase(it);
}
}

std::unordered_map<std::string, CommandStatistics>* PikaCmdTableManager::GetCommandStatMap() {
return &cmdstat_map_;
}
Expand Down
22 changes: 20 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,23 @@ int PikaConf::Load() {
GetConfStrMulti("user", &users_);

GetConfStr("aclfile", &aclFile_);

GetConfStrMulti("rename-command", &cmds_);
for (const auto & i : cmds_) {
std::string before, after;
std::istringstream iss(i);
iss >> before;
if (iss) {
iss >> after;
pstd::StringToLower(before);
pstd::StringToLower(after);
std::shared_ptr<Cmd> c_ptr = g_pika_cmd_table_manager->GetCmd(before);
if (!c_ptr) {
LOG(ERROR) << "No such " << before << " command in pika-command";
return -1;
}
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

0 comments on commit a753d90

Please sign in to comment.