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
use std::fmt;
use display_more::DisplayOptionExt;
use crate::RaftTypeConfig;
use crate::StorageError;
use crate::core::NotificationName;
use crate::core::sm;
use crate::progress::inflight_id::InflightId;
use crate::progress::stream_id::StreamId;
use crate::proposer::SendStamp;
use crate::raft::VoteResponse;
use crate::raft_state::IOId;
use crate::replication;
use crate::type_config::alias::CommittedVoteOf;
use crate::type_config::alias::InstantOf;
use crate::type_config::alias::LogIdOf;
use crate::type_config::alias::UncommittedVoteOf;
use crate::type_config::alias::VoteOf;
/// A message coming from the internal components.
pub(crate) enum Notification<C>
where C: RaftTypeConfig
{
VoteResponse {
target: C::NodeId,
resp: VoteResponse<C>,
/// The candidate that sent the vote request.
///
/// A vote identifies a unique server state.
candidate_vote: UncommittedVoteOf<C>,
},
/// Response to a Pre-Vote request sent by a pre-candidate.
///
/// Handling the Pre-Vote request changes no persistent state, but handling this response can.
/// A response to the current pre-vote round goes to the engine: a grant advances the tally, and
/// a quorum of grants starts a real election, which saves a new vote. A rejection that carries
/// a higher vote makes this node adopt that vote and save it as a non-committed vote. A
/// response to an earlier round is ignored. See the [Pre-Vote protocol].
///
/// [Pre-Vote protocol]: crate::docs::protocol::pre_vote
PreVoteResponse {
target: C::NodeId,
resp: VoteResponse<C>,
/// The pre-candidate vote (the hypothetical next-term vote) this response answers.
candidate_vote: UncommittedVoteOf<C>,
},
/// A Leader sees a higher `vote` when replicating.
HigherVote {
/// The ID of the target node from which the new term was observed.
target: C::NodeId,
/// The higher vote observed.
higher: VoteOf<C>,
/// The Leader that sent the replication request.
leader_vote: CommittedVoteOf<C>,
},
/// [`StorageError`] error has taken place locally(not on remote node),
/// and [`RaftCore`](`crate::core::RaftCore`) needs to shutdown.
StorageError { error: StorageError<C> },
/// Completion of an IO operation to local store.
LocalIO { io_id: IOId<C> },
/// Completion of purging log entries from storage.
LogPurged { upto: LogIdOf<C> },
/// Result of executing a command sent from network worker.
ReplicationProgress {
/// Identifies the replication stream that produced this progress.
stream_id: StreamId,
progress: replication::Progress<C>,
/// The `InflightId` of the tracked operation that this progress answers, if any.
///
/// - `Some(id)`: The progress answers an operation that the leader tracks as inflight: a
/// probe, a log stream, or a snapshot transfer. The leader acknowledges the operation, or
/// handles its conflict, only while it is still the inflight one with this `id`, so a
/// response to a superseded operation changes nothing. A request of a log stream carries
/// the stream's `id` even when it holds no entries, such as one that only advances the
/// commit index.
///
/// - `None`: The progress answers an RPC that no inflight operation tracks: a heartbeat, or
/// an update of the commit index sent on its own. It acknowledges no inflight operation
/// and matches no conflict to one. A success does not update the matching log either: the
/// target may have lost the log since it answered. But when log reversion is allowed, a
/// conflict below the known matching log still resets the matching log and drops the
/// operation in flight.
inflight_id: Option<InflightId>,
},
/// The replication task of `target` no longer reads the log for the plan the leader dropped.
///
/// It answers [`Command::ClearReplicate`] with the same `inflight_id`, and it is sent only
/// once no read for the dropped plan is running or can start.
///
/// [`Command::ClearReplicate`]: crate::engine::Command::ClearReplicate
ReplicateCleared {
stream_id: StreamId,
target: C::NodeId,
inflight_id: InflightId,
},
HeartbeatProgress {
stream_id: StreamId,
sending_time: SendStamp<InstantOf<C>>,
target: C::NodeId,
},
/// The replication task of the stream `stream_id` to `target`, which `RaftCore` closed, reads
/// the log no more.
///
/// The task sends it once its request stream can read nothing more, without waiting for the
/// transport to finish the requests in flight. The task's join sends it again once the task
/// has returned, so it may arrive twice for a stream. Until it arrives, the task may still be
/// reading, and no log is purged, see [`RaftCore::lingering_replications`].
///
/// [`RaftCore::lingering_replications`]: crate::core::RaftCore::lingering_replications
ReplicationStopped { stream_id: StreamId, target: C::NodeId },
/// Result of executing a command sent from a state machine worker.
StateMachine { command_result: sm::CommandResult<C> },
/// A leader's heartbeat timer reached the next heartbeat, waking RaftCore to send it.
Tick {
/// How many times the heartbeat timer has fired, counting from 1.
i: u64,
},
/// The earliest pending linearizable read reached its deadline.
PendingReadDeadlineReached,
/// The instant at which this follower's election may be due is reached.
ElectionDeadlineReached,
}
impl<C> Notification<C>
where C: RaftTypeConfig
{
pub(crate) fn sm(command_result: sm::CommandResult<C>) -> Self {
Self::StateMachine { command_result }
}
/// Returns the name of this notification variant.
pub(crate) fn name(&self) -> NotificationName {
match self {
Self::VoteResponse { .. } => NotificationName::VoteResponse,
Self::PreVoteResponse { .. } => NotificationName::PreVoteResponse,
Self::HigherVote { .. } => NotificationName::HigherVote,
Self::StorageError { .. } => NotificationName::StorageError,
Self::LocalIO { .. } => NotificationName::LocalIO,
Self::LogPurged { .. } => NotificationName::LogPurged,
Self::ReplicationProgress { .. } => NotificationName::ReplicationProgress,
Self::ReplicateCleared { .. } => NotificationName::ReplicateCleared,
Self::ReplicationStopped { .. } => NotificationName::ReplicationStopped,
Self::HeartbeatProgress { .. } => NotificationName::HeartbeatProgress,
Self::StateMachine { .. } => NotificationName::StateMachine,
Self::Tick { .. } => NotificationName::Tick,
Self::PendingReadDeadlineReached => NotificationName::PendingReadDeadlineReached,
Self::ElectionDeadlineReached => NotificationName::ElectionDeadlineReached,
}
}
}
impl<C> fmt::Display for Notification<C>
where C: RaftTypeConfig
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::VoteResponse {
target,
resp,
candidate_vote,
} => {
write!(
f,
"VoteResponse: from target={}, to candidate_vote: {}, {}",
target, candidate_vote, resp
)
}
Self::PreVoteResponse {
target,
resp,
candidate_vote,
} => {
write!(
f,
"PreVoteResponse: from target={}, to pre_candidate_vote: {}, {}",
target, candidate_vote, resp
)
}
Self::HigherVote {
target,
higher: new_vote,
leader_vote: vote,
} => {
write!(
f,
"Seen a higher vote: target: {}, vote: {}, server_state_vote: {}",
target, new_vote, vote
)
}
Self::StorageError { error } => write!(f, "StorageError: {}", error),
Self::LocalIO { io_id } => write!(f, "IOFlushed: {}", io_id),
Self::LogPurged { upto } => write!(f, "LogPurged: {}", upto),
Self::ReplicationProgress {
stream_id,
progress,
inflight_id,
} => {
write!(
f,
"{}, stream_id: {}, inflight_id: {}",
progress,
stream_id,
inflight_id.display()
)
}
Self::HeartbeatProgress {
stream_id,
sending_time,
target,
} => {
write!(
f,
"HeartbeatProgress: target={}, stream_id: {}, sending_time: {}",
target, stream_id, sending_time,
)
}
Self::ReplicateCleared {
stream_id,
target,
inflight_id,
} => {
write!(
f,
"ReplicateCleared: target={}, stream_id: {}, inflight_id: {}",
target, stream_id, inflight_id
)
}
Self::ReplicationStopped { stream_id, target } => {
write!(f, "ReplicationStopped: target={}, stream_id: {}", target, stream_id)
}
Self::StateMachine { command_result } => {
write!(f, "{}", command_result)
}
Self::Tick { i } => {
write!(f, "Tick {}", i)
}
Self::PendingReadDeadlineReached => {
write!(f, "PendingReadDeadlineReached")
}
Self::ElectionDeadlineReached => {
write!(f, "ElectionDeadlineReached")
}
}
}
}
#[cfg(test)]
mod tests {
use super::Notification;
use crate::engine::testing::UTConfig;
use crate::progress::stream_id::StreamId;
use crate::proposer::SendStamp;
use crate::type_config::TypeConfigExt;
/// The send time is displayed as a wall-clock time derived from the current time, so
/// formatting the same stamp twice can give two different times: only its shape is checked.
#[test]
fn test_heartbeat_progress_display_labels_the_stream_id() {
let notification = Notification::<UTConfig>::HeartbeatProgress {
stream_id: StreamId::new(2),
sending_time: SendStamp::new(UTConfig::<()>::now(), 3),
target: 1,
};
let display = notification.to_string();
let time = display
.strip_prefix("HeartbeatProgress: target=1, stream_id: StreamId(2), sending_time: ")
.and_then(|rest| rest.strip_suffix("(heartbeat_round=3)"))
.unwrap_or_else(|| panic!("unexpected display: {display}"));
let shape: String = time.chars().map(|c| if c.is_ascii_digit() { '9' } else { c }).collect();
assert_eq!(shape, "99:99:99.999999", "send time in: {display}");
}
}