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(server): Add support for aliasing commands #4782

Merged
merged 7 commits into from
Mar 23, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(server): Add support for command aliasing
A command can be aliased by supplying the flag:

--command_alias=__PING=PING

to the server startup.

A map of alias is retained. When finding a command id, first we look up
into the original command map, then if not found there we lookup through
the command alias map. This results in two find operations for an
aliased command.

Signed-off-by: Abhijat Malviya <[email protected]>
abhijat committed Mar 17, 2025

Verified

This commit was signed with the committer’s verified signature.
abhijat Abhijat Malviya
commit bbdac602af9596f803dc4b5cc48bedacd8192c87
4 changes: 4 additions & 0 deletions src/server/command_registry.cc
Original file line number Diff line number Diff line change
@@ -22,6 +22,9 @@ using namespace std;
ABSL_FLAG(vector<string>, rename_command, {},
"Change the name of commands, format is: <cmd1_name>=<cmd1_new_name>, "
"<cmd2_name>=<cmd2_new_name>");
ABSL_FLAG(vector<string>, command_alias, {},
"Add an alias for given commands, format is: <alias>=<original>, "
"<alias>=<original>");
ABSL_FLAG(vector<string>, restricted_commands, {},
"Commands restricted to connections on the admin port");

@@ -171,6 +174,7 @@ optional<facade::ErrorReply> CommandId::Validate(CmdArgList tail_args) const {

CommandRegistry::CommandRegistry() {
cmd_rename_map_ = ParseCmdlineArgMap(FLAGS_rename_command);
cmd_aliases_ = ParseCmdlineArgMap(FLAGS_command_alias, true);

for (string name : GetFlag(FLAGS_restricted_commands)) {
restricted_cmds_.emplace(AsciiStrToUpper(name));
15 changes: 14 additions & 1 deletion src/server/command_registry.h
Original file line number Diff line number Diff line change
@@ -169,7 +169,15 @@ class CommandRegistry {
CommandRegistry& operator<<(CommandId cmd);

const CommandId* Find(std::string_view cmd) const {
auto it = cmd_map_.find(cmd);
const auto it = cmd_map_.find(cmd);
if (it == cmd_map_.end()) {
if (const auto alias_it = cmd_aliases_.find(cmd); alias_it != cmd_aliases_.end()) {
if (alias_it->first == alias_it->second) {
return nullptr;
}
return Find(alias_it->second);
}
}
return it == cmd_map_.end() ? nullptr : &it->second;
}

@@ -215,6 +223,11 @@ class CommandRegistry {
private:
absl::flat_hash_map<std::string, CommandId> cmd_map_;
absl::flat_hash_map<std::string, std::string> cmd_rename_map_;
// Stores a mapping from alias to original command. During the find operation, the first lookup is
// done in the cmd_map_, then in the alias map. This results in two lookups but only for commands
// which are not in original map, ie either typos or aliases. While it would be faster, we cannot
// store iterators into cmd_map_ here as they may be invalidated on rehashing.
absl::flat_hash_map<std::string, std::string> cmd_aliases_;
absl::flat_hash_set<std::string> restricted_cmds_;
absl::flat_hash_set<std::string> oomdeny_cmds_;