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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Network fanout acknowledgement policy and result types.
//!
//! Implements Gun.js ask-pattern semantics for multi-peer replication: a
//! requester sends a [`Message::Put`](crate::message::Message::Put) to the
//! router, the router registers the put as a quorum-tracked write via
//! [`Message::RegisterQuorum`](crate::message::Message::RegisterQuorum),
//! the router fans the put out to peers, and tracks the per-peer acks
//! until an [`AckPolicy`] quorum is satisfied.
//!
//! # Wire format (Gun.js compatible)
//!
//! Peer acks reuse the existing `Put { in_response_to: Some(put_id), .. }`
//! wire — the `@` field in serialized JSON. Quorum completion is signalled
//! by a **`__quorum_met__` sentinel** in the reply's `updated_nodes`, with
//! the ack count as the value. This mirrors the existing `_ack`/`_err`
//! sentinel convention used by [`Node::put`](crate::Node::put) and
//! [`Node::batch_put`](crate::Node::batch_put), so callers can use the
//! same drain plumbing.
//!
//! # Reserved sentinel prefix
//!
//! `__beam__` is the reserved prefix for wire-level sentinels. Existing:
//! - `__beam_replay_complete__` — emitted by `map()` replay to signal drain complete
//!
//! New:
//! - `__quorum_met__` — emitted by Router when quorum threshold is reached
//!
//! The `__` prefix is filtered during normal data iteration (see
//! `Node::handle_put` reserved-prefix handling), so these sentinels never
//! collide with user data.
//!
//! # Lifecycle
//!
//! ```text
//! Node::put_quorum(value, policy)
//! ├── build Put, register oneshot in pending_puts
//! ├── send Message::RegisterQuorum { put_id, requester, policy } to Router
//! ├── send Message::Put(put) to Router
//! │ ↓
//! │ Router creates internal QuorumEntry for put_id
//! │ Router::handle_put_relay fans out to peers (same as fire-and-forget)
//! │ ↓
//! │ Each peer eventually replies with Put { @: put_id, .. }
//! │ ↓
//! │ Router::handle_put sees Put.@, finds QuorumEntry, increments counter
//! │ When counter >= policy.quorum → Router sends reply back to requester:
//! │ Put { @: put_id, updated_nodes: { "__quorum_met__": ack_count } }
//! └── requester's oneshot resolves with ReplicationStatus
//! ```
//!
//! # Why a sentinel, not a new Result variant
//!
//! The codebase has converged on sentinel-drain as the canonical ack
//! pattern (see `feat/beam-redux-async-ack-and-drain` branch):
//!
//! - `_ack`/`_err` sentinels for storage commit confirmation
//! - `__beam_replay_complete__` sentinel for replay drain
//!
//! Adding `__quorum_met__` keeps the drain plumbing DRY — the same
//! `pending_puts: Arc<RwLock<HashMap<String, oneshot::Sender<...>>>>` map
//! and `crate::tokio_time::timeout` envelope that [`Node::put`](crate::Node::put)
//! uses are reused for [`Node::put_quorum`](crate::Node::put_quorum).
//! Only the *decoder* differs: instead of looking for `_ack`/`_err`,
//! `put_quorum` looks for `__quorum_met__`.
//!
//! # Quorum policies
//!
//! - [`AckPolicy::any`] — first ack wins (Gun.js default, fastest)
//! - [`AckPolicy::for_peer_count`] — ⌈N/2⌉ majority (Raft/Dynamo style)
//! - [`AckPolicy::all`] — every fan-out target must ack
//!
//! # Timeout
//!
//! Default timeout matches Gun.js `lack = 9000ms`. Configurable via
//! [`AckPolicy::with_timeout`]. On timeout the requester's oneshot resolves
//! with `Err("put_quorum timed out")` and the Router's internal
//! `QuorumEntry` is reaped lazily (removed on next access by another put
//! for the same id, or by the Router's periodic cleanup if added later).
//!
//! # Design constraints
//!
//! - **No new Message variant for the ack wire** — reuses
//! `Put { in_response_to, .. }` for ack routing
//! - **One new Message variant: `RegisterQuorum`** — minimal struct with
//! `(put_id, requester_addr, policy)`; used by the Router to create the
//! `QuorumEntry` before fan-out
//! - **Sentinel-driven completion** — `__quorum_met__` in `updated_nodes`,
//! matching the `_ack`/`_err` convention
//! - **No new dependency** — uses existing `std`, `tokio`
use Duration;
/// Reserved sentinel key emitted by the Router when quorum threshold is met.
///
/// Stored in the reply Put's `updated_nodes` map with the ack count as the
/// value, e.g. `{"__quorum_met__": 3}` means 3 peers acked.
///
/// The `__` prefix is filtered during normal data iteration so this
/// sentinel never collides with user data.
pub const QUORUM_MET_SENTINEL: &str = "__quorum_met__";
/// Default timeout for quorum requests, matching Gun.js `lack = 9000ms`.
///
/// Gun.js's original ask pattern uses a 9-second lack to bound how long a
/// requester waits for an ack. We adopt the same default for wire-level
/// compatibility, but callers can override via
/// [`AckPolicy::with_timeout`].
pub const DEFAULT_QUORUM_TIMEOUT: Duration = from_millis;
/// Policy controlling how many peer acks satisfy a `put_quorum` request.
///
/// Construct via the associated functions ([`any`](Self::any),
/// [`for_peer_count`](Self::for_peer_count), [`all`](Self::all)) which
/// pick sensible default timeouts. Override the timeout via
/// [`with_timeout`](Self::with_timeout).
///
/// # Examples
///
/// ```ignore
/// // Gun.js default — first ack wins, 9s timeout
/// let p = AckPolicy::any();
///
/// // Majority of 5 peers — 3 acks needed, 9s timeout
/// let p = AckPolicy::for_peer_count(5);
///
/// // All fanned-out peers must ack
/// let p = AckPolicy::all();
///
/// // Any ack, but with a tighter 2s deadline
/// let p = AckPolicy::any().with_timeout(Duration::from_secs(2));
/// ```
/// Result of a successful `put_quorum` request.
///
/// Reports how many peers acked, whether the policy was satisfied, and
/// how long the request took to resolve. Returned in the `Ok` arm of
/// `put_quorum`'s `Result`.