Diffinery

homeflare/celldpublic

celld/crates/logic/src/routing.rs
90 lines · 3.7 KBRaw History
1//! Whether a failed peer dispatch may be re-sent.
2//!
3//! A cell runs on exactly one node, so every other node forwards to the
4//! owner. When that forward fails, celld must decide between two bad
5//! options: fail the caller, or re-send and risk running the request twice.
6//! The deciding fact is how far the attempt got. A connection that was never
7//! established carried no request bytes, so the owner's cell is untouched and
8//! a fresh attempt cannot double-apply. Every other failure — a timeout after
9//! the request was written, a truncated body, a decode error — leaves the
10//! outcome ambiguous: the owner may have run it and lost the reply. Re-sending
11//! those would break at-most-once execution.
12//!
13//! The lab found the case this exists for: under a burst of cold activations,
14//! 57 forwards died with `connect=true timeout=true` — TCP handshakes stalling
15//! past the client's 3s connect timeout. Those never reached the owner, and
16//! surfacing them to the caller was a self-inflicted error.
17
18/// How far a dispatch attempt got before it failed.
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub enum Attempt {
21 /// The peer answered that it no longer owns the cell. Nothing ran.
22 NotOwner,
23 /// The connection was never established. No bytes reached the owner.
24 NeverConnected,
25 /// The request was on the wire when the attempt failed. The owner may or
26 /// may not have run it.
27 Ambiguous,
28}
29
30/// Redispatches allowed per failure class, counted separately so a route that
31/// is both stale and unreachable still terminates. The unreachable budget is
32/// deliberately one: every attempt of that class costs a full connect timeout,
33/// so each retry is also the latency a caller pays when the owner is genuinely
34/// down, not just briefly congested.
35pub const MAX_NOT_OWNER: u32 = 1;
36pub const MAX_NEVER_CONNECTED: u32 = 1;
37
38/// May a request that failed with `attempt` be dispatched again, given how
39/// many redispatches of that class it has already spent?
40pub fn may_redispatch(attempt: Attempt, spent: u32) -> bool {
41 match attempt {
42 Attempt::Ambiguous => false,
43 Attempt::NotOwner => spent < MAX_NOT_OWNER,
44 Attempt::NeverConnected => spent < MAX_NEVER_CONNECTED,
45 }
46}
47
48/// What the router does after a failed attempt.
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub enum Next {
51 /// Resolve the owner again and re-send. `forget_node` additionally drops
52 /// the cached node record, whose address is otherwise trusted until the
53 /// node's lease expires — re-resolving without it returns the identical
54 /// failed route, so the redispatch is wasted.
55 Redispatch { forget_node: bool },
56 /// Give up and answer the caller.
57 Fail,
58}
59
60/// Per-request redispatch state. Budgets are counted per failure class so a
61/// route that is both stale and unreachable still terminates.
62#[derive(Clone, Copy, Debug, Default)]
63pub struct Dispatcher {
64 spent_not_owner: u32,
65 spent_never_connected: u32,
66}
67
68impl Dispatcher {
69 /// Record a failed attempt and decide what happens next.
70 pub fn on_failure(&mut self, attempt: Attempt) -> Next {
71 let spent = match attempt {
72 Attempt::NotOwner => self.spent_not_owner,
73 _ => self.spent_never_connected,
74 };
75 if !may_redispatch(attempt, spent) {
76 return Next::Fail;
77 }
78 match attempt {
79 Attempt::NotOwner => {
80 self.spent_not_owner += 1;
81 // Ownership moved; the address is still good.
82 Next::Redispatch { forget_node: false }
83 }
84 _ => {
85 self.spent_never_connected += 1;
86 Next::Redispatch { forget_node: true }
87 }
88 }
89 }
90}