| 1 | // Copyright 2026 Deno Land Inc. Apache-2.0 license. |
| 2 | |
| 3 | //! When a write's durability proof gives up, sans-IO. |
| 4 | //! |
| 5 | //! A write's response waits until the node proves the write durable. The |
| 6 | //! wait has a budget, but the budget measures a stall, not a queue: a node |
| 7 | //! that commits more writes than it can upload at once keeps the excess in a |
| 8 | //! queue behind its upload slots, and a write at the back of that queue is |
| 9 | //! not failing -- it is waiting its turn while the node proves other writes. |
| 10 | //! Measured on one c3-standard-4 (2026-09-01), 4,000 cells that committed in |
| 11 | //! the same second needed about nine seconds to drain through 64 upload |
| 12 | //! slots; a fixed ten-second budget counted from each write's commit failed |
| 13 | //! the last third of them, reset those cells, and re-fired their alarms 30 to |
| 14 | //! 110 seconds late, with the store healthy throughout. |
| 15 | //! |
| 16 | //! The rule here therefore has two regimes. While the upload that covers the |
| 17 | //! write has not started, the wait extends as long as the node lands proofs, |
| 18 | //! up to [`QUEUED_BUDGETS`] budgets in total. Once that upload is in flight, |
| 19 | //! the fixed budget runs from the moment it began, exactly as before, so a |
| 20 | //! stuck upload still fails in one budget. The two together mean a stalled |
| 21 | //! store fails every waiter one budget after its last proof, a stuck cell |
| 22 | //! fails one budget after its capture, and a healthy but busy node fails |
| 23 | //! nobody. Raising the budget instead would move the same false failure to |
| 24 | //! a bigger burst. |
| 25 | |
| 26 | /// One write's wait for its proof. |
| 27 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 28 | pub struct ProofWait { |
| 29 | /// Monotonic ms when the wait began, right after the write took its |
| 30 | /// ticket. |
| 31 | pub started_ms: u64, |
| 32 | /// The durability ticket the write took. A capture whose `capture_seq` |
| 33 | /// reaches it covers this write. |
| 34 | pub ticket: u64, |
| 35 | /// The budget for one proof, in ms (`CELLD_LTX_DURABILITY_TIMEOUT_SECS`). |
| 36 | pub budget_ms: u64, |
| 37 | } |
| 38 | |
| 39 | /// What the node has done since, as the executor observes it. |
| 40 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 41 | pub struct ProofProgress { |
| 42 | /// Monotonic ms of the last proof any cell on the node landed; 0 when |
| 43 | /// none has yet. |
| 44 | pub node_proof_ms: u64, |
| 45 | /// The highest ticket a capture of this cell has begun for. |
| 46 | pub capture_seq: u64, |
| 47 | /// Monotonic ms when the capture that reached `capture_seq` began. |
| 48 | pub capture_started_ms: u64, |
| 49 | } |
| 50 | |
| 51 | /// How many budgets a queued write may wait in total while the node keeps |
| 52 | /// proving other writes. The cap bounds a client's wait under sustained |
| 53 | /// overload, where the queue never empties; six budgets is a minute at the |
| 54 | /// default, and the operation deadline usually ends the wait first. |
| 55 | pub const QUEUED_BUDGETS: u64 = 6; |
| 56 | |
| 57 | /// The monotonic ms at which this wait gives up, given what the node has done |
| 58 | /// so far. Recomputed on every wake, so a proof landing elsewhere on the node |
| 59 | /// or the cell's own capture beginning moves the deadline forward. |
| 60 | pub fn proof_deadline(wait: &ProofWait, progress: &ProofProgress) -> u64 { |
| 61 | if progress.capture_seq >= wait.ticket { |
| 62 | // In flight: the fixed budget, from the capture that covers this |
| 63 | // write. A capture cannot cover a ticket taken after it, so the |
| 64 | // `max` only guards a clock read that raced the ticket. |
| 65 | return progress |
| 66 | .capture_started_ms |
| 67 | .max(wait.started_ms) |
| 68 | .saturating_add(wait.budget_ms); |
| 69 | } |
| 70 | // Queued: one budget after the node's last proof, bounded in total. |
| 71 | let anchor = progress.node_proof_ms.max(wait.started_ms); |
| 72 | let cap = wait |
| 73 | .started_ms |
| 74 | .saturating_add(wait.budget_ms.saturating_mul(QUEUED_BUDGETS)); |
| 75 | anchor.saturating_add(wait.budget_ms).min(cap) |
| 76 | } |