Skip to content

Commit

Permalink
feat(cmd): avoid to use CommandAttributes::flags directly (#2619)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Oct 24, 2024
1 parent a738122 commit c525d7a
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 110 deletions.
6 changes: 4 additions & 2 deletions src/cluster/cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,8 @@ Status Cluster::CanExecByMySelf(const redis::CommandAttributes *attributes, cons
cross_slot_ok = true;
}

uint64_t flags = attributes->GenerateFlags(cmd_tokens);

if (myself_ && myself_ == slots_nodes_[slot]) {
// We use central controller to manage the topology of the cluster.
// Server can't change the topology directly, so we record the migrated slots
Expand All @@ -880,7 +882,7 @@ Status Cluster::CanExecByMySelf(const redis::CommandAttributes *attributes, cons
}
// To keep data consistency, slot will be forbidden write while sending the last incremental data.
// During this phase, the requests of the migrating slot has to be rejected.
if ((attributes->flags & redis::kCmdWrite) && IsWriteForbiddenSlot(slot)) {
if ((flags & redis::kCmdWrite) && IsWriteForbiddenSlot(slot)) {
return {Status::RedisTryAgain, "Can't write to slot being migrated which is in write forbidden phase"};
}

Expand All @@ -903,7 +905,7 @@ Status Cluster::CanExecByMySelf(const redis::CommandAttributes *attributes, cons
return Status::OK(); // I'm serving the imported slot
}

if (myself_ && myself_->role == kClusterSlave && !(attributes->flags & redis::kCmdWrite) &&
if (myself_ && myself_->role == kClusterSlave && !(flags & redis::kCmdWrite) &&
nodes_.find(myself_->master_id) != nodes_.end() && nodes_[myself_->master_id] == slots_nodes_[slot] &&
conn->IsFlagEnabled(redis::Connection::kReadOnly)) {
return Status::OK(); // My master is serving this slot
Expand Down
10 changes: 5 additions & 5 deletions src/commands/cmd_cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ class CommandAsking : public Commander {
};

REDIS_REGISTER_COMMANDS(Cluster,
MakeCmdAttr<CommandCluster>("cluster", -2, "cluster no-script", 0, 0, 0, GenerateClusterFlag),
MakeCmdAttr<CommandClusterX>("clusterx", -2, "cluster no-script", 0, 0, 0, GenerateClusterFlag),
MakeCmdAttr<CommandReadOnly>("readonly", 1, "cluster no-multi", 0, 0, 0),
MakeCmdAttr<CommandReadWrite>("readwrite", 1, "cluster no-multi", 0, 0, 0),
MakeCmdAttr<CommandAsking>("asking", 1, "cluster", 0, 0, 0), )
MakeCmdAttr<CommandCluster>("cluster", -2, "cluster no-script", NO_KEY, GenerateClusterFlag),
MakeCmdAttr<CommandClusterX>("clusterx", -2, "cluster no-script", NO_KEY, GenerateClusterFlag),
MakeCmdAttr<CommandReadOnly>("readonly", 1, "cluster no-multi", NO_KEY),
MakeCmdAttr<CommandReadWrite>("readwrite", 1, "cluster no-multi", NO_KEY),
MakeCmdAttr<CommandAsking>("asking", 1, "cluster", NO_KEY), )

} // namespace redis
2 changes: 1 addition & 1 deletion src/commands/cmd_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ uint64_t GenerateFunctionFlags(uint64_t flags, const std::vector<std::string> &a
}

REDIS_REGISTER_COMMANDS(
Function, MakeCmdAttr<CommandFunction>("function", -2, "exclusive no-script", 0, 0, 0, GenerateFunctionFlags),
Function, MakeCmdAttr<CommandFunction>("function", -2, "exclusive no-script", NO_KEY, GenerateFunctionFlags),
MakeCmdAttr<CommandFCall<>>("fcall", -3, "exclusive write no-script", GetScriptEvalKeyRange),
MakeCmdAttr<CommandFCall<true>>("fcall_ro", -3, "read-only ro-script no-script", GetScriptEvalKeyRange));

Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_geo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class CommandGeoRadius : public CommandGeoBase {

count_ = *parse_result;
i += 2;
} else if ((attributes_->flags & kCmdWrite) &&
} else if ((attributes_->InitialFlags() & kCmdWrite) &&
(util::ToLower(args_[i]) == "store" || util::ToLower(args_[i]) == "storedist") &&
i + 1 < args_.size()) {
store_key_ = args_[i + 1];
Expand Down
18 changes: 9 additions & 9 deletions src/commands/cmd_pubsub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ class CommandPubSub : public Commander {
};

REDIS_REGISTER_COMMANDS(
Pubsub, MakeCmdAttr<CommandPublish>("publish", 3, "read-only pub-sub", 0, 0, 0),
MakeCmdAttr<CommandMPublish>("mpublish", -3, "read-only pub-sub", 0, 0, 0),
MakeCmdAttr<CommandSubscribe>("subscribe", -2, "read-only pub-sub no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandUnSubscribe>("unsubscribe", -1, "read-only pub-sub no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandPSubscribe>("psubscribe", -2, "read-only pub-sub no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandPUnSubscribe>("punsubscribe", -1, "read-only pub-sub no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandSSubscribe>("ssubscribe", -2, "read-only pub-sub no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandSUnSubscribe>("sunsubscribe", -1, "read-only pub-sub no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandPubSub>("pubsub", -2, "read-only pub-sub no-script", 0, 0, 0), )
Pubsub, MakeCmdAttr<CommandPublish>("publish", 3, "read-only pub-sub", NO_KEY),
MakeCmdAttr<CommandMPublish>("mpublish", -3, "read-only pub-sub", NO_KEY),
MakeCmdAttr<CommandSubscribe>("subscribe", -2, "read-only pub-sub no-multi no-script", NO_KEY),
MakeCmdAttr<CommandUnSubscribe>("unsubscribe", -1, "read-only pub-sub no-multi no-script", NO_KEY),
MakeCmdAttr<CommandPSubscribe>("psubscribe", -2, "read-only pub-sub no-multi no-script", NO_KEY),
MakeCmdAttr<CommandPUnSubscribe>("punsubscribe", -1, "read-only pub-sub no-multi no-script", NO_KEY),
MakeCmdAttr<CommandSSubscribe>("ssubscribe", -2, "read-only pub-sub no-multi no-script", NO_KEY),
MakeCmdAttr<CommandSUnSubscribe>("sunsubscribe", -1, "read-only pub-sub no-multi no-script", NO_KEY),
MakeCmdAttr<CommandPubSub>("pubsub", -2, "read-only pub-sub no-script", NO_KEY), )

} // namespace redis
10 changes: 5 additions & 5 deletions src/commands/cmd_replication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,10 @@ class CommandDBName : public Commander {
};

REDIS_REGISTER_COMMANDS(
Replication, MakeCmdAttr<CommandReplConf>("replconf", -3, "read-only replication no-script", 0, 0, 0),
MakeCmdAttr<CommandPSync>("psync", -2, "read-only replication no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandFetchMeta>("_fetch_meta", 1, "read-only replication no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandFetchFile>("_fetch_file", 2, "read-only replication no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandDBName>("_db_name", 1, "read-only replication no-multi", 0, 0, 0), )
Replication, MakeCmdAttr<CommandReplConf>("replconf", -3, "read-only replication no-script", NO_KEY),
MakeCmdAttr<CommandPSync>("psync", -2, "read-only replication no-multi no-script", NO_KEY),
MakeCmdAttr<CommandFetchMeta>("_fetch_meta", 1, "read-only replication no-multi no-script", NO_KEY),
MakeCmdAttr<CommandFetchFile>("_fetch_file", 2, "read-only replication no-multi no-script", NO_KEY),
MakeCmdAttr<CommandDBName>("_db_name", 1, "read-only replication no-multi", NO_KEY), )

} // namespace redis
2 changes: 1 addition & 1 deletion src/commands/cmd_script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ REDIS_REGISTER_COMMANDS(
MakeCmdAttr<CommandEvalSHA>("evalsha", -3, "exclusive write no-script", GetScriptEvalKeyRange),
MakeCmdAttr<CommandEvalRO>("eval_ro", -3, "read-only no-script ro-script", GetScriptEvalKeyRange),
MakeCmdAttr<CommandEvalSHARO>("evalsha_ro", -3, "read-only no-script ro-script", GetScriptEvalKeyRange),
MakeCmdAttr<CommandScript>("script", -2, "exclusive no-script", 0, 0, 0), )
MakeCmdAttr<CommandScript>("script", -2, "exclusive no-script", NO_KEY), )

} // namespace redis
20 changes: 10 additions & 10 deletions src/commands/cmd_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,15 @@ class CommandFTTagVals : public Commander {
};

REDIS_REGISTER_COMMANDS(Search,
MakeCmdAttr<CommandFTCreate>("ft.create", -2, "write exclusive no-multi no-script slow", 0, 0,
0),
MakeCmdAttr<CommandFTSearchSQL>("ft.searchsql", -2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandFTSearch>("ft.search", -3, "read-only", 0, 0, 0),
MakeCmdAttr<CommandFTExplainSQL>("ft.explainsql", -2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandFTExplain>("ft.explain", -3, "read-only", 0, 0, 0),
MakeCmdAttr<CommandFTInfo>("ft.info", 2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandFTList>("ft._list", 1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandFTDrop>("ft.dropindex", 2, "write exclusive no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandFTTagVals>("ft.tagvals", 3, "read-only slow", 0, 0, 0));
MakeCmdAttr<CommandFTCreate>("ft.create", -2, "write exclusive no-multi no-script slow",
NO_KEY),
MakeCmdAttr<CommandFTSearchSQL>("ft.searchsql", -2, "read-only", NO_KEY),
MakeCmdAttr<CommandFTSearch>("ft.search", -3, "read-only", NO_KEY),
MakeCmdAttr<CommandFTExplainSQL>("ft.explainsql", -2, "read-only", NO_KEY),
MakeCmdAttr<CommandFTExplain>("ft.explain", -3, "read-only", NO_KEY),
MakeCmdAttr<CommandFTInfo>("ft.info", 2, "read-only", NO_KEY),
MakeCmdAttr<CommandFTList>("ft._list", 1, "read-only", NO_KEY),
MakeCmdAttr<CommandFTDrop>("ft.dropindex", 2, "write exclusive no-multi no-script", NO_KEY),
MakeCmdAttr<CommandFTTagVals>("ft.tagvals", 3, "read-only slow", NO_KEY));

} // namespace redis
74 changes: 37 additions & 37 deletions src/commands/cmd_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1331,43 +1331,43 @@ class CommandPollUpdates : public Commander {
Format format_ = Format::Raw;
};

REDIS_REGISTER_COMMANDS(Server, MakeCmdAttr<CommandAuth>("auth", 2, "read-only ok-loading", 0, 0, 0),
MakeCmdAttr<CommandPing>("ping", -1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandSelect>("select", 2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandInfo>("info", -1, "read-only ok-loading", 0, 0, 0),
MakeCmdAttr<CommandRole>("role", 1, "read-only ok-loading", 0, 0, 0),
MakeCmdAttr<CommandConfig>("config", -2, "read-only", 0, 0, 0, GenerateConfigFlag),
MakeCmdAttr<CommandNamespace>("namespace", -3, "read-only", 0, 0, 0),
MakeCmdAttr<CommandKeys>("keys", 2, "read-only slow", 0, 0, 0),
MakeCmdAttr<CommandFlushDB>("flushdb", 1, "write no-dbsize-check", 0, 0, 0),
MakeCmdAttr<CommandFlushAll>("flushall", 1, "write no-dbsize-check", 0, 0, 0),
MakeCmdAttr<CommandDBSize>("dbsize", -1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandSlowlog>("slowlog", -2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandPerfLog>("perflog", -2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandClient>("client", -2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandMonitor>("monitor", 1, "read-only no-multi", 0, 0, 0),
MakeCmdAttr<CommandShutdown>("shutdown", 1, "read-only no-multi no-script", 0, 0, 0),
MakeCmdAttr<CommandQuit>("quit", 1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandScan>("scan", -2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandRandomKey>("randomkey", 1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandDebug>("debug", -2, "read-only exclusive", 0, 0, 0),
MakeCmdAttr<CommandCommand>("command", -1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandEcho>("echo", 2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandTime>("time", 1, "read-only ok-loading", 0, 0, 0),
MakeCmdAttr<CommandDisk>("disk", 3, "read-only", 0, 0, 0),
MakeCmdAttr<CommandMemory>("memory", 3, "read-only", 0, 0, 0),
MakeCmdAttr<CommandHello>("hello", -1, "read-only ok-loading", 0, 0, 0),
REDIS_REGISTER_COMMANDS(Server, MakeCmdAttr<CommandAuth>("auth", 2, "read-only ok-loading", NO_KEY),
MakeCmdAttr<CommandPing>("ping", -1, "read-only", NO_KEY),
MakeCmdAttr<CommandSelect>("select", 2, "read-only", NO_KEY),
MakeCmdAttr<CommandInfo>("info", -1, "read-only ok-loading", NO_KEY),
MakeCmdAttr<CommandRole>("role", 1, "read-only ok-loading", NO_KEY),
MakeCmdAttr<CommandConfig>("config", -2, "read-only", NO_KEY, GenerateConfigFlag),
MakeCmdAttr<CommandNamespace>("namespace", -3, "read-only", NO_KEY),
MakeCmdAttr<CommandKeys>("keys", 2, "read-only slow", NO_KEY),
MakeCmdAttr<CommandFlushDB>("flushdb", 1, "write no-dbsize-check", NO_KEY),
MakeCmdAttr<CommandFlushAll>("flushall", 1, "write no-dbsize-check", NO_KEY),
MakeCmdAttr<CommandDBSize>("dbsize", -1, "read-only", NO_KEY),
MakeCmdAttr<CommandSlowlog>("slowlog", -2, "read-only", NO_KEY),
MakeCmdAttr<CommandPerfLog>("perflog", -2, "read-only", NO_KEY),
MakeCmdAttr<CommandClient>("client", -2, "read-only", NO_KEY),
MakeCmdAttr<CommandMonitor>("monitor", 1, "read-only no-multi", NO_KEY),
MakeCmdAttr<CommandShutdown>("shutdown", 1, "read-only no-multi no-script", NO_KEY),
MakeCmdAttr<CommandQuit>("quit", 1, "read-only", NO_KEY),
MakeCmdAttr<CommandScan>("scan", -2, "read-only", NO_KEY),
MakeCmdAttr<CommandRandomKey>("randomkey", 1, "read-only", NO_KEY),
MakeCmdAttr<CommandDebug>("debug", -2, "read-only exclusive", NO_KEY),
MakeCmdAttr<CommandCommand>("command", -1, "read-only", NO_KEY),
MakeCmdAttr<CommandEcho>("echo", 2, "read-only", NO_KEY),
MakeCmdAttr<CommandTime>("time", 1, "read-only ok-loading", NO_KEY),
MakeCmdAttr<CommandDisk>("disk", 3, "read-only", NO_KEY),
MakeCmdAttr<CommandMemory>("memory", 3, "read-only", NO_KEY),
MakeCmdAttr<CommandHello>("hello", -1, "read-only ok-loading", NO_KEY),
MakeCmdAttr<CommandRestore>("restore", -4, "write", 1, 1, 1),

MakeCmdAttr<CommandCompact>("compact", 1, "read-only no-script", 0, 0, 0),
MakeCmdAttr<CommandBGSave>("bgsave", 1, "read-only no-script", 0, 0, 0),
MakeCmdAttr<CommandLastSave>("lastsave", 1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandFlushBackup>("flushbackup", 1, "read-only no-script", 0, 0, 0),
MakeCmdAttr<CommandSlaveOf>("slaveof", 3, "read-only exclusive no-script", 0, 0, 0),
MakeCmdAttr<CommandStats>("stats", 1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandRdb>("rdb", -3, "write exclusive", 0, 0, 0),
MakeCmdAttr<CommandReset>("reset", 1, "ok-loading multi no-script pub-sub", 0, 0, 0),
MakeCmdAttr<CommandApplyBatch>("applybatch", -2, "write no-multi", 0, 0, 0),
MakeCmdAttr<CommandDump>("dump", 2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandPollUpdates>("pollupdates", -2, "read-only", 0, 0, 0), )
MakeCmdAttr<CommandCompact>("compact", 1, "read-only no-script", NO_KEY),
MakeCmdAttr<CommandBGSave>("bgsave", 1, "read-only no-script", NO_KEY),
MakeCmdAttr<CommandLastSave>("lastsave", 1, "read-only", NO_KEY),
MakeCmdAttr<CommandFlushBackup>("flushbackup", 1, "read-only no-script", NO_KEY),
MakeCmdAttr<CommandSlaveOf>("slaveof", 3, "read-only exclusive no-script", NO_KEY),
MakeCmdAttr<CommandStats>("stats", 1, "read-only", NO_KEY),
MakeCmdAttr<CommandRdb>("rdb", -3, "write exclusive", NO_KEY),
MakeCmdAttr<CommandReset>("reset", 1, "ok-loading multi no-script pub-sub", NO_KEY),
MakeCmdAttr<CommandApplyBatch>("applybatch", -2, "write no-multi", NO_KEY),
MakeCmdAttr<CommandDump>("dump", 2, "read-only", NO_KEY),
MakeCmdAttr<CommandPollUpdates>("pollupdates", -2, "read-only", NO_KEY), )
} // namespace redis
6 changes: 3 additions & 3 deletions src/commands/cmd_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1884,12 +1884,12 @@ REDIS_REGISTER_COMMANDS(Stream, MakeCmdAttr<CommandXAck>("xack", -4, "write no-d
MakeCmdAttr<CommandAutoClaim>("xautoclaim", -6, "write", 1, 1, 1),
MakeCmdAttr<CommandXGroup>("xgroup", -4, "write", 2, 2, 1),
MakeCmdAttr<CommandXLen>("xlen", -2, "read-only", 1, 1, 1),
MakeCmdAttr<CommandXInfo>("xinfo", -2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandXInfo>("xinfo", -2, "read-only", NO_KEY),
MakeCmdAttr<CommandXPending>("xpending", -3, "read-only", 1, 1, 1),
MakeCmdAttr<CommandXRange>("xrange", -4, "read-only", 1, 1, 1),
MakeCmdAttr<CommandXRevRange>("xrevrange", -2, "read-only", 1, 1, 1),
MakeCmdAttr<CommandXRead>("xread", -4, "read-only", 0, 0, 0),
MakeCmdAttr<CommandXReadGroup>("xreadgroup", -7, "write", 0, 0, 0),
MakeCmdAttr<CommandXRead>("xread", -4, "read-only", NO_KEY),
MakeCmdAttr<CommandXReadGroup>("xreadgroup", -7, "write", NO_KEY),
MakeCmdAttr<CommandXTrim>("xtrim", -4, "write no-dbsize-check", 1, 1, 1),
MakeCmdAttr<CommandXSetId>("xsetid", -3, "write", 1, 1, 1))

Expand Down
8 changes: 4 additions & 4 deletions src/commands/cmd_txn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ class CommandUnwatch : public Commander {
}
};

REDIS_REGISTER_COMMANDS(Txn, MakeCmdAttr<CommandMulti>("multi", 1, "multi", 0, 0, 0),
MakeCmdAttr<CommandDiscard>("discard", 1, "multi", 0, 0, 0),
MakeCmdAttr<CommandExec>("exec", 1, "exclusive multi slow", 0, 0, 0),
REDIS_REGISTER_COMMANDS(Txn, MakeCmdAttr<CommandMulti>("multi", 1, "multi", NO_KEY),
MakeCmdAttr<CommandDiscard>("discard", 1, "multi", NO_KEY),
MakeCmdAttr<CommandExec>("exec", 1, "exclusive multi slow", NO_KEY),
MakeCmdAttr<CommandWatch>("watch", -2, "multi", 1, -1, 1),
MakeCmdAttr<CommandUnwatch>("unwatch", 1, "multi", 0, 0, 0), )
MakeCmdAttr<CommandUnwatch>("unwatch", 1, "multi", NO_KEY), )

} // namespace redis
2 changes: 1 addition & 1 deletion src/commands/commander.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ std::string CommandTable::GetCommandInfo(const CommandAttributes *command_attrib
command.append(redis::BulkString(command_attributes->name));
command.append(redis::Integer(command_attributes->arity));
command_flags.append(redis::MultiLen(1));
command_flags.append(redis::BulkString(command_attributes->flags & kCmdWrite ? "write" : "readonly"));
command_flags.append(redis::BulkString(command_attributes->InitialFlags() & kCmdWrite ? "write" : "readonly"));
command.append(command_flags);
command.append(redis::Integer(command_attributes->key_range.first_key));
command.append(redis::Integer(command_attributes->key_range.last_key));
Expand Down
Loading

0 comments on commit c525d7a

Please sign in to comment.