8000 Cluster: EXEC during slot migration: partial ASK replies, no-writes scripts refused, stale fast-path check · Issue #339 · kividbio/kividb · GitHub
[go: up one dir, main page]

Skip to content

Cluster: EXEC during slot migration: partial ASK replies, no-writes scripts refused, stale fast-path check #339

Closed

Description

@murtazayusuf

How EXEC behaves while slots move, after #335:

  1. Queued commands are routed one by one inside the gate. Redis refuses the whole transaction when its keys are mid-migration. Since Fix lost writes when a write or EXEC races a slot move (#331), and a spurious TRYAGAIN for hashes (#332) #335, KiviDB refuses the whole EXEC only when the "routes moved" count has changed. Otherwise a partial reply is still possible, with some queued commands answered -ASK/-MOVED and others run.
  2. writes() disagrees with pause::holds / may_replicate (src/cluster/routing.rs ~243). writes() counts every EVAL/EVALSHA/FCALL as a write. holds() lets a no-writes script through, as Redis does. So in the lua/full variant, MULTI; EVAL <no-writes script>; EXEC is refused with TRYAGAIN on every retry for the whole handoff. Fix: reuse may_replicate.
  3. Stale fast-path check (src/server.rs ~1436, src/server_uring.rs ~1626). The old || crate::pause::handoff() in the SET-only transaction fast path is now unreachable for real racers. When it does fire, it answers a WATCH-abort null even to an unwatched transaction. Remove it, or make it answer TRYAGAIN.
  4. Option (design): instead of refusing, an EXEC that meets a handoff could drop its gates, wait the handoff out and route its queue again, as single writes do. That is closer to Redis/Valkey atomic slot migration. It's only worth doing if clients have trouble with TRYAGAIN on EXEC.

The CHANGELOG line "refused with TRYAGAIN if it writes" should be updated along with (2).

Tests are missing for: a read-only EXEC during a handoff not being refused, and a transaction with a no-writes script.

Found in the reviews of #335.

Activity

  1. added
    redis-compat
    cluster
    after-1.2.0
    on Nov 4, 2026
  2. added 2 commits that reference this issue on Nov 5, 2026
  3. murtazayusuf commented on Nov 16, 2026

    @murtazayusuf
    ContributorAuthor

    Two concrete cases for item 1 (queued commands are routed again, one at a time, inside EXEC), run on a two-master cluster while slot 405 was moving. Both leave a transaction half applied; nothing acknowledged is lost.

    • Importing node, {t}a and {t}b both already arrived: ASKING; MULTI; DEL {t}b; RENAME {t}a {t}b; EXEC → both QUEUED, then EXEC replies [1, TRYAGAIN Multiple keys request during rehashing of slot]. The DEL ran (ASKING; GET {t}b → nil), the RENAME did not. Redis routes the whole queue once at EXEC and runs both. Reachable since ASKING before MULTI covers the transaction up to its EXEC #746, which keeps ASKING for the transaction.
    • Migrating source, {t}a present: MULTI; DEL {t}a; SET {t}a v; EXEC → [1, ASK 405 <dest>]. The DEL ran, the SET got ASK. This one was there before.

    The cause in both: the commands run inside EXEC are routed with the keys as they are at that moment, so an earlier command in the transaction changes how a later one routes. The fix is to route the queue once, before any of it runs, and not again per command.

  4. magic-peach commented on Nov 17, 2026

    @magic-peach
    Collaborator

    @murtazayusuf since you pointed me to this in #770 can I take item 1?

    My plan is to route the whole queue once when EXEC starts, using the keys as they are before anything runs. If any command would get MOVED, ASK or TRYAGAIN, the whole EXEC is refused, as Redis does. Otherwise the queued commands run without being routed again, so an earlier command can't change how a later one routes. ASKING keeps its current meaning for the queue, from #746.

    I'd add both of your cases as cluster tests: DEL + RENAME on the importing node, and DEL + SET on the migrating source. Both should then run whole, or be refused whole, never half applied.

  5. murtazayusuf commented on Nov 17, 2026

    @murtazayusuf
    ContributorAuthor

    Yes, please take item 1, @magic-peach, and that is the right plan: route the whole queue once when EXEC starts, with the keys as they are before anything runs; refuse the whole EXEC if any command would be redirected; then run the queue without routing again. Notes so the PR goes straight through:

    • What a refused EXEC answers. Match Redis: the transaction is discarded and the reply is the redirection itself for the whole EXEC (-MOVED / -ASK / -TRYAGAIN), not an array with errors inside, and -CROSSSLOT stays as route_queue gives it today. Check a real Redis 8 cluster for the exact replies in your two cases and one where only the second command would be redirected, and put them in the PR.
    • Keys the queue itself creates or deletes. Routing "as before anything runs" is what Redis does, so DEL {t}b; RENAME {t}a {t}b on the importing node is judged on {t}a and {t}b as they are at EXEC. Say in the PR what happens when the first command creates a key the second needs in a migrating slot: Redis's answer, and ours.
    • WATCH, and EXEC held by a pause (exec_held_in_gate, redispatched): the queue must be routed again on the retry, not reuse a decision from before the pause, since slots can move while it is held.
    • The fast path. EXEC of plain SETs goes through exec_fast_apply_sets in both loops (src/server.rs, src/server_uring.rs) and already never routes per command; keep it that way, and outside cluster mode nothing may be added to EXEC at all (it is all behind cluster.enabled today). If you touch run_command, the per-command path for ordinary commands must stay as it is.
    • Tests on both I/O models (the io_models() helper from ASKING before MULTI covers the transaction up to its EXEC #746 is in tests/cluster.rs now), the two cases from my comment above running whole or refused whole, and one for the paused-EXEC retry if you can make it deterministic.

    On #850: one thing at a time, and that one needs a diagnosis from our side first (it may be how the script is classified rather than something to implement), so I'd rather not hand it over yet. I'll write what we find there.

  6. added 2 commits that reference this issue on Nov 17, 2026
  7. magic-peach commented on Nov 19, 2026

    @magic-peach
    Collaborator

    @murtazayusuf can I take item 2?

  8. murtazayusuf commented on Nov 19, 2026

    @murtazayusuf
    ContributorAuthor

    @magic-peach yes, and take item 3 with it in the same PR; they are both small and touch the same path.

    • Item 2: check after EXEC routes its queue once, as one command, and does not route its commands again (#339) #866 where writes() is still consulted for a queued EXEC (src/cluster/routing.rs); make it agree with pause::holds by reusing may_replicate, so MULTI; EVAL <no-writes script>; EXEC and FCALL_RO/EVAL_RO in a queue are not refused during a handoff, while a queue with any command that may replicate still is. Flags are per script (the shebang no-writes flag, and function flags), so the answer has to come from the loaded script, not the command name; a script that is not loaded yet (EVALSHA miss) counts as a write.
    • Item 3: the || crate::pause::handoff() in the SET-only transaction fast path (both loops). Prefer removing it if you can show nothing reaches it; if something can, it must answer TRYAGAIN for the whole EXEC, never the WATCH-abort null for a transaction that watched nothing.
    • Tests (cluster, lua build for the script ones): a read-only EXEC during a handoff is not refused; a queue with a no-writes script is not refused; the same queue plus one write is refused with TRYAGAIN as a whole; and the fast-path case. Update the CHANGELOG line "refused with TRYAGAIN if it writes" to say what decides it.
    • One thing to watch: two open PRs (A command waiting for a script's hash tag no longer holds up its thread (part of #882) #924, KEYS and SCAN no longer stop every connection with parallel scripts on (#772) #925) change how scripts take their locks in src/commands/handler.rs and src/tag_locks.rs. Yours should not need to touch those files; if it does, say so on the PR and I'll sequence the merges.
  9. murtazayusuf commented on Nov 19, 2026

    @murtazayusuf
    ContributorAuthor

    Items 1 to 3 are done: #866 (EXEC routes its queue once and is refused as a whole), and the script-flag and fast-path changes with their tests in #954. Item 4 (waiting a handoff out instead of answering TRYAGAIN) was only worth doing if clients had trouble with TRYAGAIN on EXEC; nobody has reported any, so I'm closing this. If that changes it gets its own issue. Thanks @magic-peach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

after-1.2.0clusterredis-compat

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0