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
use d_engine_proto::common::Entry;
use d_engine_proto::common::entry_payload::Payload;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio::sync::watch;
use tonic::async_trait;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::trace;
use tracing::warn;
use super::CommitHandler;
use crate::Membership;
use crate::NewCommitData;
use crate::RaftEvent;
use crate::RaftLog;
use crate::Result;
use crate::StateMachineHandler;
use crate::TypeConfig;
use crate::alias::MOF;
use crate::alias::ROF;
use crate::alias::SMHOF;
use crate::scoped_timer::ScopedTimer;
// Dependencies container
pub struct CommitHandlerDependencies<T: TypeConfig> {
pub state_machine_handler: Arc<SMHOF<T>>,
pub raft_log: Arc<ROF<T>>,
pub membership: Arc<MOF<T>>,
pub event_tx: mpsc::Sender<RaftEvent>,
pub sm_apply_tx: mpsc::UnboundedSender<Vec<Entry>>,
pub shutdown_signal: watch::Receiver<()>,
pub max_batch_size: usize,
}
#[derive(Debug)]
pub struct DefaultCommitHandler<T>
where
T: TypeConfig,
{
my_id: u32,
my_role: i32,
my_current_term: u64,
state_machine_handler: Arc<SMHOF<T>>,
raft_log: Arc<ROF<T>>,
new_commit_rx: Option<mpsc::UnboundedReceiver<NewCommitData>>,
membership: Arc<MOF<T>>,
event_tx: mpsc::Sender<RaftEvent>, // Cloned from Raft
sm_apply_tx: mpsc::UnboundedSender<Vec<Entry>>, // Send entries to SM Worker
// Shutdown signal
shutdown_signal: watch::Receiver<()>,
// Batch size for draining commit notifications
max_batch_size: usize,
}
#[async_trait]
impl<T> CommitHandler for DefaultCommitHandler<T>
where
T: TypeConfig,
{
async fn run(&mut self) -> Result<()> {
info!("[Node-{}] Commit handler started", self.my_id);
let mut new_commit_rx =
self.new_commit_rx.take().expect("Expected a commit recv but found None");
let mut shutdown_signal = self.shutdown_signal.clone();
loop {
tokio::select! {
// P0: shutdown received;
_ = shutdown_signal.changed() => {
info!("[CommitHandler] shutdown signal received.");
return Ok(());
}
// Submit events in real time
Some(new_commit_data) = new_commit_rx.recv() => {
trace!("[Node-{}] new commit index = {:?} committed..", self.my_id, new_commit_data.new_commit_index);
self.state_machine_handler.update_pending(new_commit_data.new_commit_index);
// Keep sync my current term and role from new commit data
self.my_current_term = new_commit_data.current_term;
self.my_role = new_commit_data.role;
// Drain all pending commit notifications (max_batch_size limit)
// This batches multiple committed entries into a single process_batch() call
let mut count = 1;
while count < self.max_batch_size {
match new_commit_rx.try_recv() {
Ok(next_data) => {
self.state_machine_handler.update_pending(next_data.new_commit_index);
self.my_current_term = next_data.current_term;
self.my_role = next_data.role;
count += 1;
}
Err(_) => break,
}
}
trace!("[Node-{}] Processing batch with {} commit notifications", self.my_id, count);
if let Err(e) = self.process_batch().await {
error!("Failed to process batch: {}", e);
}
}
}
}
}
}
impl<T> DefaultCommitHandler<T>
where
T: TypeConfig,
{
pub fn new(
my_id: u32,
my_role: i32,
my_current_term: u64,
deps: CommitHandlerDependencies<T>,
new_commit_rx: mpsc::UnboundedReceiver<NewCommitData>,
) -> Self {
Self {
my_id,
my_role,
my_current_term,
state_machine_handler: deps.state_machine_handler,
raft_log: deps.raft_log,
membership: deps.membership,
new_commit_rx: Some(new_commit_rx),
event_tx: deps.event_tx,
sm_apply_tx: deps.sm_apply_tx,
shutdown_signal: deps.shutdown_signal,
max_batch_size: deps.max_batch_size,
}
}
/// Process batch logs
/// - Separates config changes from application commands
/// - Applies config changes first via membership module
/// - Then applies application commands to state machine
/// - Error handling for config changes
///
/// # Note:Sequential Integrity
/// Consider this sequence in a single batch: [ConfigRemove(A), ConfigAdd(B), EntryNormal(X)]
pub(crate) async fn process_batch(&self) -> Result<()> {
let _timer = ScopedTimer::new("CommitHandler::process_batch");
let pending_range = self.state_machine_handler.pending_range();
trace!("[Node-{}] Pending range: {:?}", self.my_id, pending_range);
let Some(range) = pending_range else {
return Ok(());
};
let entries = self.raft_log.get_entries_range(range)?;
debug!(
"[Node-{}] commit handler process batch, length = {}",
self.my_id,
entries.len()
);
// Merge consecutive normal commands
let mut command_batch = vec![];
let mut last_error = None;
for entry in entries {
// In exact log order
if let Some(ref entry_payload) = entry.payload {
match entry_payload.payload {
Some(Payload::Command(_)) => command_batch.push(entry),
Some(Payload::Config(_)) => {
command_batch.push(entry.clone());
self.send_to_sm_worker(&mut command_batch).await?;
if last_error.is_none() {
if let Err(e) = self.apply_config_change(entry).await {
last_error = Some(e);
}
}
}
Some(Payload::Noop(_)) => {
command_batch.push(entry);
self.send_to_sm_worker(&mut command_batch).await?;
}
None => unreachable!(),
}
}
}
debug!(?last_error, "flush complete");
// Finally send remaining commands to SM Worker
if let Some(e) = last_error {
return Err(e);
} else {
self.send_to_sm_worker(&mut command_batch).await?;
}
// Snapshot check moved to ApplyCompleted handler in Raft event loop.
// SM Worker applies entries asynchronously, so last_applied is stale here.
Ok(())
}
/// Check if configuration change is a self-removal
///
/// Returns true if the change is RemoveNode(my_id), indicating
/// that this node is removing itself from the cluster.
pub(crate) fn is_self_removal_config(
my_id: u32,
change: &d_engine_proto::common::MembershipChange,
) -> bool {
matches!(
&change.change,
Some(d_engine_proto::common::membership_change::Change::RemoveNode(remove))
if remove.node_id == my_id
)
}
/// Apply configuration change and detect self-removal
///
/// If the first configure been applied failed, then all the following commands will be
/// rejected. (Consistency)
///
/// Per Raft protocol: Leader can remove itself. After applying the removal,
/// leader must step down immediately.
async fn apply_config_change(
&self,
entry: Entry,
) -> Result<()> {
let _timer = ScopedTimer::new("apply_config_change");
debug!("Received config change:{:?}", &entry);
if let Some(payload) = entry.payload {
if let Some(Payload::Config(change)) = payload.payload {
// Check if this is a self-removal (check BEFORE applying)
let is_self_removal = Self::is_self_removal_config(self.my_id, &change);
// 1. Apply to membership state
if let Err(e) = self.membership.apply_config_change(change).await {
error!(
"[{}] Failed to apply config change at index {}: {:?}",
self.my_id, entry.index, e
);
// Critical error - should panic or handle carefully
return Err(e);
}
// 2. CRITICAL: Barrier point
self.membership.notify_config_applied(entry.index).await;
// 2.5. Notify leader to refresh cluster metadata cache
// This must happen AFTER membership is applied
if let Err(e) = self.event_tx.send(RaftEvent::MembershipApplied).await {
warn!("Failed to send MembershipApplied event: {:?}", e);
}
// 3. Leader self-removal: Step down immediately per Raft protocol
if is_self_removal {
warn!(
"[{}] Node removed from cluster membership, triggering step down (index {})",
self.my_id, entry.index
);
// Signal step down - error is non-fatal as removal is already committed
if let Err(e) = self.event_tx.send(RaftEvent::StepDownSelfRemoved).await {
error!(
"[{}] Failed to send StepDownSelfRemoved event: {:?}",
self.my_id, e
);
}
}
}
}
Ok(())
}
// Define flush as an async function
async fn send_to_sm_worker(
&self,
batch: &mut Vec<Entry>,
) -> Result<()> {
if !batch.is_empty() {
let entries = std::mem::take(batch);
trace!(
"[Node-{}] Sending batch to SM Worker: {} entries",
self.my_id,
entries.len()
);
// Send entries to SM Worker without waiting for apply
self.sm_apply_tx.send(entries).map_err(|e| {
error!("[Node-{}] SM Worker channel closed: {:?}", self.my_id, e);
crate::Error::Fatal(format!("SM Worker channel closed: {e:?}"))
})?;
}
Ok(())
}
}