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

Handle SPUBLISH as a non key-based command #182

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
2 changes: 1 addition & 1 deletion redis/src/cluster_async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@ where
// (e.g., sending management command to a different node than the user asked for); instead, raise the error.
let routable_cmd = cmd.and_then(|cmd| Routable::command(&*cmd));
if routable_cmd.is_some()
&& !RoutingInfo::is_key_based_cmd(&routable_cmd.unwrap())
&& !RoutingInfo::is_key_routing_command(&routable_cmd.unwrap())
{
return Err((
ErrorKind::ClusterConnectionNotFound,
Expand Down
19 changes: 16 additions & 3 deletions redis/src/cluster_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,10 @@ impl RoutingInfo {
matches!(base_routing(cmd), RouteBy::AllNodes)
}

/// Returns true if the `cmd` is a key-based command.
pub fn is_key_based_cmd(cmd: &[u8]) -> bool {
/// Returns true if the `cmd` is a key-based command that triggers MOVED errors.
/// A key-based command is one that will be accepted only by the slot owner,
/// while other nodes will respond with a MOVED error redirecting to the relevant primary owner.
pub fn is_key_routing_command(cmd: &[u8]) -> bool {
match base_routing(cmd) {
RouteBy::FirstKey
| RouteBy::SecondArg
Expand All @@ -560,7 +562,18 @@ impl RoutingInfo {
| RouteBy::SecondArgSlot
| RouteBy::StreamsIndex
| RouteBy::MultiShardNoValues
| RouteBy::MultiShardWithValues => true,
| RouteBy::MultiShardWithValues => {
if matches!(cmd, b"SPUBLISH") {
// SPUBLISH does not return MOVED errors within the slot's shard. This means that even if READONLY wasn't sent to a replica,
// executing SPUBLISH FOO BAR on that replica will succeed. This behavior differs from true key-based commands,
// such as SET FOO BAR, where a non-readonly replica would return a MOVED error if READONLY is off.
// Consequently, SPUBLISH does not meet the requirement of being a command that triggers MOVED errors.
// TODO: remove this when PRIMARY_PREFERRED route for SPUBLISH is added
false
} else {
true
}
}
RouteBy::AllNodes | RouteBy::AllPrimaries | RouteBy::Random | RouteBy::Undefined => {
false
}
Expand Down