1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: BUSL-1.1
//! Leader-aware forwarding for in-transaction staging control ops.
//!
//! The per-transaction staging overlay lives on the Data-Plane core that owns
//! the target vShard — i.e. on that vShard's current Raft leader. When this
//! node is NOT the leader for a vShard a transaction stages a write to, the
//! `MetaOp::StageWrite` (and, at COMMIT / ROLLBACK, the `MetaOp::DropTxnOverlay`)
//! must execute on the leader, keyed purely by `txn_id` — the Data-Plane
//! handlers are session-less, so a wire-carried `txn_id` is all the leader needs
//! to stage into / drop the correct overlay.
//!
//! INVARIANT: for a given vShard, the stage, every read-your-own-writes read,
//! and the overlay drop all resolve to the SAME node (the vShard's leader at the
//! time each runs). A leader change mid-transaction strands the staged overlay
//! on the former leader — the same failure surface Calvin already tolerates for
//! staged cross-shard state. A stranded overlay is bounded (one transaction's
//! staging), invisible (its `txn_id` comes from a monotonic counter and is never
//! reused or committed), and observable (the `active_txn_overlays` gauge plus the
//! ERROR the commit/rollback teardown logs when a drop-forward fails); a former
//! leader that crashes clears it with its memory. There is no automatic
//! time-based reclaim of a still-running former leader's stranded overlay today —
//! it is cleared on that node's next restart.
//!
//! Both choke points (`staging_gate::stage_write`, `commit::drop_txn_overlay`)
//! resolve the leader themselves and keep their existing LOCAL dispatch
//! byte-identical; only the REMOTE arm routes through [`forward_to_leader`],
//! which fails CLOSED — a `LeaderUnknown` resolution surfaces as
//! `Error::NotLeader` via the gateway dispatcher. `commit::drop_txn_overlay`'s
//! remote arm wraps this in a bounded retry (`retry_not_leader`) so a
//! transient leader election does not strand the overlay after a single
//! attempt; `staging_gate::stage_write`'s remote arm surfaces the error
//! directly to the statement layer. Neither ever falls back to local staging
//! on a non-leader.
use crate;
use crate;
use crate;
use crate;
use cratebare_ok_response;
use crateSharedState;
use crate;
use PhysicalTask;
/// Resolve the current leader for `task`'s target vShard against live Raft
/// leadership (falling back to the routing-table hint), so a staging choke point
/// can branch between its existing local dispatch and a remote forward.
pub
/// Forward an already-wrapped staging control op (`MetaOp::StageWrite` /
/// `MetaOp::DropTxnOverlay`) in `forward_task` to a REMOTE leader, adapting the
/// remote outcome into the same `crate::Result<Response>` a local dispatch of
/// the op yields.
///
/// `decision` is the non-`Local` resolution from [`resolve_leader`]; `Local` is
/// handled by the caller's own dispatch path and never reaches here. A
/// `LeaderUnknown` (or the unreachable `Broadcast`) resolution is passed
/// straight to the gateway dispatcher, which maps it to `Error::NotLeader` /
/// an internal error — fail-closed, never a local staging fallback.
///
/// `version_plan` is the plan used to compute the descriptor version set the
/// leader validates for OCC: for a `StageWrite` it is the INNER (un-wrapped)
/// write, so the forwarded stage carries the same descriptor versions a normal
/// remote write would and is not spuriously rejected; a `DropTxnOverlay` touches
/// no user collection, so its version set is empty.
pub async
/// Descriptor version set for `plan`, looked up against this node's catalog —
/// the same `(collection, descriptor_version)` payload the gateway attaches to a
/// normal remote write so the leader's OCC descriptor check accepts it.