Skip to content

fix(raft): avoid replaying proposals with unknown outcomes - #1839

Merged
bplatz merged 4 commits into
mainfrom
investigate/propose-relay-retry
Sep 16, 2026
Merged

bplatz merged 4 commits into
mainfrom
investigate/propose-relay-retry

Conversation

@bplatz

@bplatz bplatz commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A follower could replay a command after losing the leader's response, even when the first attempt had already committed. In a three-node reproduction, one Add(1) applied twice and returned success with value 2.

Return ProposeError::Unknown without replay for ambiguous send failures, response-read failures, malformed success responses, unrecognized HTTP errors, and local Raft fatal errors. Keep one retry only for connection-establishment failures or a complete 503 rejection with {"error":"not_leader"}. The peer endpoint now emits this structured rejection; generic 503 responses cannot authorize replay.

The replay came entirely from this helper's own Relay loop. Reqwest's default retry policy covers only HTTP/2 and HTTP/3 protocol rejections and never applied to this plain HTTP/1.1 relay, so retry::never() keeps replay decisions in one place rather than removing a second source of them. Do not restore a reqwest policy here on the belief that it was load-bearing.

A 5 s connect timeout keeps a black-holed leader — one whose SYNs go unanswered rather than refused — in the retryable class. An elapsed connect timeout surfaces as a connect error, so it earns the one permitted retry against a freshly elected leader. Without it the attempt consumed the whole 30 s request budget and then classified as Unknown, forfeiting that retry. The safety argument is unchanged: a connect that never completed provably never sent the request.

Compatibility

  • Downstream exhaustive matches must handle the new public Unknown variant; higher-level callers must not automatically replay it.
  • During rolling upgrades, new callers treat older peers' plain-text 503 responses as unknown. Automatic rejection retries resume when peers support the structured response. Old callers retain their original behavior until upgraded.
  • Recovering a lost result still requires application-level request IDs and replicated deduplication.

Validation

  • RUSTC_WRAPPER= cargo test --locked --offline -p fluree-raft-core --all-features — 196 passed; four existing documentation examples ignored.
  • RUSTC_WRAPPER= cargo clippy --locked --offline -p fluree-raft-core --all-features --all-targets -- -D warnings — passed.
  • cargo fmt --all and git diff --check — passed.

Cluster regressions cover response-header/body loss, a real follower rejection, post-apply 500 and bare 503, and malformed success JSON. Unit tests cover timeout after receipt, connection refusal and subsequent recovery, connect timeout against an unresponsive address, and invalid/legacy rejection responses. The timeout test exercises the HTTP relay with a short deadline, not a delayed Raft commit or leader election.

The connect-timeout test reaches an unresponsive address by filling a listener's backlog and never accepting, then asserts both the Relay classification and that the wait was bounded by the connect budget. The elapsed bound is what makes it non-vacuous: on macOS the OS abandons a loopback SYN after ~8 s, which already classifies as Relay on its own, so classification alone would pass with the fix removed.

@bplatz
bplatz requested review from aaj3f and zonotope September 14, 2026 23:31
@bplatz bplatz added the bug Something isn't working as expected label Sep 14, 2026

@aaj3f aaj3f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good & makes sense, @bplatz! The classification is exactly the "could the request have reached client_write?" split I'd want, and the two guarantees it leans on both hold when you go look: hyper only hands a request back for retry if the dispatch channel refused it (so the keep-alive reuse hole is closed a layer below is_connect()), and openraft 0.9 keeps pending client-write responders on RaftCore and answers them Ok on apply even after a step-down, sending ForwardToLeader only pre-append or on conflict truncation — so the structured not_leader 503 really can't follow a commit. The byte-level fault proxy in multi_node_group.rs is the right test shape too: it asserts the leader answered 2xx before damaging the response, and a one-line mutation of the body-read arm makes it fail with a double apply. Solo's origin/main already pins a descendant of this head and maps Unknown to CoordinationUnavailable without replay, so the new public variant is already absorbed downstream.

Two small notes, neither blocking. First, the client has no connect_timeout, so a black-holed leader address burns the full 30 s and then classifies as Unknown rather than Relay — safe, but it gives up the one retry the PR permits for the never-reached case; inline suggestion at forward.rs:1125. Second, fwiw the "disable reqwest's retry policy" line in the description slightly overstates what that call does: the default policy is h2/h3 protocol-NACK-only and never fired on this plain HTTP/1.1 relay, so retry::never() is good belt-and-braces but the replay was entirely the helper's own Relay loop — maybe worth a word in the description so nobody later "restores" a reqwest policy thinking it mattered. Also minor: the fix commit 9ba6b6edb is subject-only; the PR body carries the rationale, but a body on the commit would help git log archaeology later.

Adherence to repo commitments:

  • Patterns/abstractions: ✔ Extends ProposeError and the existing single-retry loop rather than adding a parallel relay path; the rejection is a typed serde enum with deny_unknown_fields, shared between the emitting handler and the parser.
  • Performance (speed first, memory second): ✔ Relay error path only; one serde_json::from_slice attempted only on 503 bodies. No performance-degradation risk.
  • Testing: ✔ Three unit tests against a real listener plus six three-node cluster tests through a byte-level fault proxy; all nine run under cargo test -p fluree-raft-core --all-features and the truncated-body test goes red under mutation. The pooled-connection reuse case is covered by hyper's contract rather than a test here.
  • Conventions: ⚠️ fmt and clippy (--all-features --all-targets -D warnings) clean; self-describing fix(raft): title; but the fix commit has no body.

Verified locally at branch HEAD (b285e9555): cargo test -p fluree-raft-core --all-features (195 passed, 4 pre-existing ignored doc examples), cargo fmt --all -- --check, cargo clippy -p fluree-raft-core --all-features --all-targets --no-deps -- -D warnings, one classification mutation (test went red, restored), and the reqwest/hyper-util/hyper/openraft source traces above.

Approving so you can merge when ready, but maybe worth considering the connect_timeout first — and since #1844 and #1845 are stacked on this branch and solo pins #1845's head, landing this one first keeps the stack simple.

serde_json::json!({"error": "not_leader"})
);
} else if first {
assert!(status.is_success(), "leader must apply before fault");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fluree-raft-core/tests/multi_node_group.rs:125 — praise. This assertion is what makes the whole cluster suite mean "response lost after commit" rather than "some error happened": the proxy only damages a response the real leader already answered 2xx, and :179-181 then pin one increment / one apply on the leader's state machine. I mutated forward.rs:1151 back to Relay and this suite caught it with value=2, applies=2, proposals=2, so it is genuinely regression-proofing the bug and not just the error variant. Nice.

// Membership-supplied URL: never follow a redirect off it.
.redirect(reqwest::redirect::Policy::none())
.timeout(std::time::Duration::from_secs(30))
// Keep replay decisions in propose_via_leader.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fluree-raft-core/src/forward.rs:1125 — optional. This is more of a question than a suggestion. With no connect_timeout on this client, a leader address that black-holes (a hard-killed node whose SYNs go unanswered, rather than refused) runs the connect phase until the 30 s request timeout at :1135 fires, and reqwest wraps that as request(TimedOut) with no hyper-util Connect error in the source chain (async_impl/client.rs:3048), so :1141 is_connect() is false and we return Unknown. That is the safe answer — nothing was sent, nothing gets replayed — but it forfeits the one retry the helper permits for exactly the "never established" case, and after 30 s the local raft has almost certainly elected someone else. If I'm reading reqwest right, .connect_timeout(...) is applied inside the connector service (connect.rs:141-160), so an elapsed connect timeout comes back as ErrorKind::Connect → is_connect()==true → Relay → the fresh-leader retry we want. Something like:

Suggested change
// Keep replay decisions in propose_via_leader.
// A leader that black-holes should count as "never reached", not
// burn the whole request budget and then read as unknown.
.connect_timeout(Duration::from_secs(5))
// Keep replay decisions in propose_via_leader.

I don't think this changes any safety property (a connect-phase timeout provably never wrote the request), and it isn't hot-path, so it's minor and non-blocking — but if you agree it's right, I'd rather see it folded in here than lost in the backlog. Happy to be told there's a reason to prefer the single 30 s budget.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 5162099 — .connect_timeout(5s) on the relay client, plus a regression test.

One thing worth flagging: my first version of that test asserted only the Relay classification, and it passed with the fix reverted. On macOS the OS abandons a loopback SYN after ~8 s, which already reads as a connect error, so classification alone proves nothing there. The test now also bounds elapsed time by the connect budget — 5.2 s with the fix, 7.79 s without, which trips the bound. On Linux the reverted case runs to the request timeout and returns Unknown, so it fails there too.

…s retryable

A leader address that silently drops SYNs (hard-killed host, rather than
one refusing connections) ran the connect phase until the 30 s request
timeout fired. Reqwest reports that as a request timeout with no connect
error in its source chain, so the attempt classified as Unknown and
forfeited the one retry the helper permits for a leader that was never
reached — by which point the local raft has almost certainly elected
someone else.

A connect timeout is enforced inside the connector, so an elapsed connect
surfaces as a connect error and lands in the retryable class instead. No
safety property changes: a connect that never completed provably never
sent the request.

The test's elapsed-time bound is the load-bearing assertion. On macOS the
OS abandons a loopback SYN after ~8 s, which already reads as a connect
error, so classification alone would pass with the fix removed.
@bplatz

bplatz commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR description for the second note: the replay came entirely from the helper Relay loop, and the reqwest default retry policy is H2/H3-only and never applied to this HTTP/1.1 relay, with a line warning against restoring one later.

Left the subject-only commit 9ba6b6e as it is, since #1844 and #1845 are stacked on it; the new commit carries a full body. Latest main is also merged in.

@bplatz
bplatz merged commit 88d1eb3 into main Sep 16, 2026
16 checks passed
@bplatz
bplatz deleted the investigate/propose-relay-retry branch September 16, 2026 00:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants