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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// SPDX-License-Identifier: BUSL-1.1
//! Single tick of the Raft event loop.
//!
//! Ordering (each phase uses a short-lived `MultiRaft` lock acquisition —
//! the lock is never held across an `.await`):
//!
//! 1. **Tick Raft groups**: drive election timeouts / heartbeats and pull
//! `MultiRaftReady` output (messages, vote requests, committed
//! entries, snapshot-needed peers).
//! 2. **Dispatch AppendEntries**: one background task per peer, batching
//! all messages targeting the same peer.
//! 3. **Dispatch RequestVote**: same batching strategy.
//! 4. **Apply committed entries**: feed them to the user-supplied
//! `CommitApplier`. Conf-change entries are detected and applied to
//! `MultiRaft` before the user applier sees them.
//! 5. **Install snapshots**: send `InstallSnapshot` RPCs to peers that
//! have fallen behind the leader's snapshot boundary.
//! 6. **Promote caught-up learners**: for every group where this node is
//! leader, query learners whose `match_index >= commit_index` and
//! propose `PromoteLearner` for each. Idempotent by design — after
//! the first promotion the peer has moved from `learners` to
//! `members` and won't be returned again.
use std::collections::HashMap as BatchMap;
use tracing::{debug, warn};
use nodedb_raft::transport::RaftTransport;
use crate::conf_change::{ConfChange, ConfChangeType};
use crate::forward::PlanExecutor;
use super::loop_core::{CommitApplier, RaftLoop};
impl<A: CommitApplier, P: PlanExecutor> RaftLoop<A, P> {
/// Execute a single tick: drive Raft, dispatch outbound messages,
/// apply commits, promote caught-up learners.
pub(super) fn do_tick(&self) {
// Tick under lock and extract Ready.
let ready = {
let mut mr = self.multi_raft.lock().unwrap_or_else(|p| p.into_inner());
mr.tick()
};
// Dispatch outgoing messages and persist log/HardState first (even if
// ready looks "empty" we still want to run the learner-promotion step
// each tick so a just-caught-up learner is promoted promptly).
if !ready.is_empty() {
let mut ae_batches: BatchMap<u64, Vec<(u64, nodedb_raft::AppendEntriesRequest)>> =
BatchMap::new();
let mut vote_batches: BatchMap<u64, Vec<(u64, nodedb_raft::RequestVoteRequest)>> =
BatchMap::new();
for (group_id, group_ready) in &ready.groups {
for (peer, req) in &group_ready.messages {
ae_batches
.entry(*peer)
.or_default()
.push((*group_id, req.clone()));
}
for (peer, req) in &group_ready.vote_requests {
vote_batches
.entry(*peer)
.or_default()
.push((*group_id, req.clone()));
}
}
// Dispatch batched AppendEntries — one task per peer.
//
// Each detached task subscribes to the shutdown watch
// and wraps its RPC awaits in `tokio::select!` so a
// `RaftLoop::begin_shutdown` signal (or the `run` loop
// propagating an external shutdown) cancels the
// in-flight QUIC call at the next await point. This
// is what lets graceful shutdown drop the
// `Arc<Mutex<MultiRaft>>` clone promptly and release
// per-group redb locks for an in-process restart.
for (peer, messages) in ae_batches {
let transport = self.transport.clone();
let mr = self.multi_raft.clone();
let mut shutdown_rx = self.shutdown_watch.subscribe();
tokio::spawn(async move {
if *shutdown_rx.borrow() {
return;
}
for (group_id, req) in messages {
tokio::select! {
biased;
_ = shutdown_rx.changed() => return,
rpc = transport.append_entries(peer, req) => {
match rpc {
Ok(resp) => {
let mut mr =
mr.lock().unwrap_or_else(|p| p.into_inner());
if let Err(e) = mr
.handle_append_entries_response(group_id, peer, &resp)
{
debug!(group_id, peer, error = %e, "handle ae response");
}
}
Err(e) => {
warn!(group_id, peer, error = %e, "append_entries RPC failed");
break; // Peer is down — skip remaining groups.
}
}
}
}
}
});
}
// Dispatch batched RequestVote — one task per peer.
for (peer, votes) in vote_batches {
let transport = self.transport.clone();
let mr = self.multi_raft.clone();
let mut shutdown_rx = self.shutdown_watch.subscribe();
tokio::spawn(async move {
if *shutdown_rx.borrow() {
return;
}
for (group_id, req) in votes {
tokio::select! {
biased;
_ = shutdown_rx.changed() => return,
rpc = transport.request_vote(peer, req) => {
match rpc {
Ok(resp) => {
let mut mr =
mr.lock().unwrap_or_else(|p| p.into_inner());
if let Err(e) = mr
.handle_request_vote_response(group_id, peer, &resp)
{
debug!(group_id, peer, error = %e, "handle vote response");
}
}
Err(e) => {
warn!(group_id, peer, error = %e, "request_vote RPC failed");
break;
}
}
}
}
}
});
}
// Apply committed entries and conf-changes.
for (group_id, group_ready) in ready.groups {
if !group_ready.committed_entries.is_empty() {
for entry in &group_ready.committed_entries {
if let Some(cc) = ConfChange::from_entry_data(&entry.data) {
let mut mr = self.multi_raft.lock().unwrap_or_else(|p| p.into_inner());
if let Err(e) = mr.apply_conf_change(group_id, &cc) {
warn!(group_id, error = %e, "failed to apply conf change");
}
}
}
let last_applied = if group_id == crate::metadata_group::METADATA_GROUP_ID {
// Metadata group (0): dispatch to the metadata applier.
// Raft no-op entries and conf-changes are already
// handled above; data entries carry a serialized
// `MetadataEntry` and are decoded by the applier.
let pairs: Vec<(u64, Vec<u8>)> = group_ready
.committed_entries
.iter()
.filter(|e| ConfChange::from_entry_data(&e.data).is_none())
.map(|e| (e.index, e.data.clone()))
.collect();
self.metadata_applier.apply(&pairs)
} else {
self.applier
.apply_committed(group_id, &group_ready.committed_entries)
};
if last_applied > 0 {
let mut mr = self.multi_raft.lock().unwrap_or_else(|p| p.into_inner());
if let Err(e) = mr.advance_applied(group_id, last_applied) {
warn!(group_id, error = %e, "failed to advance applied index");
} else if group_id == crate::metadata_group::METADATA_GROUP_ID {
// Metadata group: the metadata applier
// applied entries synchronously to redb
// before returning, so the apply
// watermark is data-visible at this
// point. Bump the watcher.
//
// Data groups are NOT bumped here — for
// them `applier.apply_committed` only
// enqueues entries onto the
// `DistributedApplier` channel; the
// actual data lands in storage when
// `run_apply_loop` finishes the
// SPSC round-trip to the Data Plane.
// The host crate bumps the watcher
// there, so the watermark always means
// "data visible on this node up to
// index N" regardless of which group.
//
// Snapshot-install path also bumps
// (in `super::handle_rpc`) — covers
// jump-on-snapshot for both group
// kinds.
self.group_watchers.bump(group_id, last_applied);
}
}
// Boot-time readiness: the first time the metadata
// group (0) applies any entry on this node — which
// is the leader-election no-op or a replayed entry
// — flip the ready watch. The host crate's
// `start_raft` returns the receiver; `main.rs`
// awaits it before binding client-facing
// listeners. Idempotent: subsequent ticks are a
// no-op once the latch is set.
if group_id == crate::metadata_group::METADATA_GROUP_ID
&& !*self.ready_watch.borrow()
{
let _ = self.ready_watch.send(true);
}
// Detect false→true transitions on metadata-group
// leadership and bump the cluster epoch exactly once
// per acquisition. The fence token rides on every
// outbound RPC after this point (see
// `cluster_epoch.rs`).
if group_id == crate::metadata_group::METADATA_GROUP_ID {
let is_leader = self
.multi_raft
.lock()
.unwrap_or_else(|p| p.into_inner())
.group_role_is_leader(group_id);
let was_leader = self
.prev_metadata_leader
.swap(is_leader, std::sync::atomic::Ordering::AcqRel);
if is_leader && !was_leader {
if let Some(catalog) = self.catalog.as_ref() {
match crate::cluster_epoch::bump_local_cluster_epoch(catalog) {
Ok(new_epoch) => tracing::info!(
node = self.node_id,
new_epoch,
"bumped cluster epoch on metadata-group leadership acquisition"
),
Err(e) => tracing::warn!(
node = self.node_id,
error = %e,
"failed to persist bumped cluster epoch (in-memory value advanced anyway)"
),
}
} else {
// No catalog → in-memory only (test path).
let _ = crate::cluster_epoch::observe_peer_cluster_epoch(
crate::cluster_epoch::current_local_cluster_epoch() + 1,
);
}
}
}
}
// Install-snapshot dispatch for lagging peers.
if !group_ready.snapshots_needed.is_empty() {
let snapshot_meta = {
let mr = self.multi_raft.lock().unwrap_or_else(|p| p.into_inner());
mr.snapshot_metadata(group_id).ok()
};
if let Some((term, snap_index, snap_term)) = snapshot_meta {
for peer in group_ready.snapshots_needed {
let transport = self.transport.clone();
let mr = self.multi_raft.clone();
let mut shutdown_rx = self.shutdown_watch.subscribe();
let node_id = self.node_id;
let chunk_bytes = self.snapshot_chunk_bytes;
tokio::spawn(async move {
if *shutdown_rx.borrow() {
return;
}
tokio::select! {
biased;
_ = shutdown_rx.changed() => {}
result = crate::install_snapshot::sender::send_chunked(
&transport,
crate::install_snapshot::sender::SendChunkedParams {
peer,
group_id,
term,
leader_id: node_id,
last_included_index: snap_index,
last_included_term: snap_term,
snapshot_bytes: &[], // empty until engines fill in data
chunk_bytes,
},
) => {
match result {
Ok(resp_term) => {
if resp_term > term {
let mut mr =
mr.lock().unwrap_or_else(|p| p.into_inner());
// Higher term — let the tick loop handle step-down.
let _ = mr.handle_append_entries_response(
group_id,
peer,
&nodedb_raft::AppendEntriesResponse {
term: resp_term,
success: false,
last_log_index: 0,
},
);
}
debug!(group_id, peer, "install_snapshot sent");
}
Err(e) => {
warn!(
group_id, peer, error = %e,
"install_snapshot RPC failed"
);
}
}
}
}
});
}
}
}
}
}
// Promote caught-up learners. Runs every tick so a
// just-caught-up learner is promoted within one tick interval of
// catching up. Idempotent: once promoted, the peer is in
// `members` and `ready_learners` no longer returns it.
self.promote_ready_learners();
}
/// For every group where this node is leader, propose
/// `PromoteLearner` for every learner whose `match_index` has
/// reached the current `commit_index`.
///
/// This is the automated second phase of the two-step Raft single-
/// server add. The first phase (`AddLearner`) is proposed by the
/// join flow; the second phase is proposed here, asynchronously,
/// once the learner has caught up.
fn promote_ready_learners(&self) {
// Snapshot the work list under one lock acquisition.
let promotions: Vec<(u64, u64)> = {
let mr = self.multi_raft.lock().unwrap_or_else(|p| p.into_inner());
let group_ids: Vec<u64> = mr.routing().group_ids();
group_ids
.into_iter()
.flat_map(|gid| {
mr.ready_learners(gid)
.into_iter()
.map(move |learner| (gid, learner))
})
.collect()
};
// Propose each promotion in its own lock acquisition. If any fails
// (e.g., `NotLeader` after a step-down between phases), log and
// move on — the next tick will retry any still-pending learner.
for (group_id, learner_id) in promotions {
let mut mr = self.multi_raft.lock().unwrap_or_else(|p| p.into_inner());
let change = ConfChange {
change_type: ConfChangeType::PromoteLearner,
node_id: learner_id,
};
match mr.propose_conf_change(group_id, &change) {
Ok((_gid, idx)) => {
debug!(
group_id,
learner_id,
log_index = idx,
"proposed learner promotion"
);
}
Err(e) => {
debug!(
group_id,
learner_id,
error = %e,
"learner promotion proposal deferred"
);
}
}
}
}
}