-
Notifications
You must be signed in to change notification settings - Fork 399
/
raw_node.rs
487 lines (441 loc) · 16.7 KB
/
raw_node.rs
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::mem;
use eraftpb::{
ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, MessageType,
Snapshot,
};
use protobuf::{self, RepeatedField};
use super::errors::{Error, Result};
use super::raft::{Config, Raft, SoftState, INVALID_ID};
use super::read_only::ReadState;
use super::Status;
use super::Storage;
#[derive(Debug, Default)]
pub struct Peer {
pub id: u64,
pub context: Option<Vec<u8>>,
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum SnapshotStatus {
Finish,
Failure,
}
fn is_local_msg(t: MessageType) -> bool {
match t {
MessageType::MsgHup
| MessageType::MsgBeat
| MessageType::MsgUnreachable
| MessageType::MsgSnapStatus
| MessageType::MsgCheckQuorum => true,
_ => false,
}
}
fn is_response_msg(t: MessageType) -> bool {
match t {
MessageType::MsgAppendResponse
| MessageType::MsgRequestVoteResponse
| MessageType::MsgHeartbeatResponse
| MessageType::MsgUnreachable
| MessageType::MsgRequestPreVoteResponse => true,
_ => false,
}
}
pub fn is_empty_snap(s: &Snapshot) -> bool {
s.get_metadata().get_index() == 0
}
// Ready encapsulates the entries and messages that are ready to read,
// be saved to stable storage, committed or sent to other peers.
// All fields in Ready are read-only.
#[derive(Default, Debug, PartialEq)]
pub struct Ready {
// The current volatile state of a Node.
// SoftState will be nil if there is no update.
// It is not required to consume or store SoftState.
pub ss: Option<SoftState>,
// The current state of a Node to be saved to stable storage BEFORE
// Messages are sent.
// HardState will be equal to empty state if there is no update.
pub hs: Option<HardState>,
// read_states states can be used for node to serve linearizable read requests locally
// when its applied index is greater than the index in ReadState.
// Note that the read_state will be returned when raft receives MsgReadIndex.
// The returned is only valid for the request that requested to read.
pub read_states: Vec<ReadState>,
// Entries specifies entries to be saved to stable storage BEFORE
// Messages are sent.
pub entries: Vec<Entry>,
// Snapshot specifies the snapshot to be saved to stable storage.
pub snapshot: Snapshot,
// CommittedEntries specifies entries to be committed to a
// store/state-machine. These have previously been committed to stable
// store.
pub committed_entries: Option<Vec<Entry>>,
// Messages specifies outbound messages to be sent AFTER Entries are
// committed to stable storage.
// If it contains a MsgSnap message, the application MUST report back to raft
// when the snapshot has been received or has failed by calling ReportSnapshot.
pub messages: Vec<Message>,
// MustSync indicates whether the HardState and Entries must be synchronously
// written to disk or if an asynchronous write is permissible.
pub must_sync: bool,
}
impl Ready {
fn new<T: Storage>(
raft: &mut Raft<T>,
prev_ss: &SoftState,
prev_hs: &HardState,
since_idx: Option<u64>,
) -> Ready {
let mut rd = Ready {
entries: raft.raft_log.unstable_entries().unwrap_or(&[]).to_vec(),
..Default::default()
};
if !raft.msgs.is_empty() {
mem::swap(&mut raft.msgs, &mut rd.messages);
}
rd.committed_entries = Some(
(match since_idx {
None => raft.raft_log.next_entries(),
Some(idx) => raft.raft_log.next_entries_since(idx),
}).unwrap_or_else(Vec::new),
);
let ss = raft.soft_state();
if &ss != prev_ss {
rd.ss = Some(ss);
}
let hs = raft.hard_state();
if &hs != prev_hs {
if hs.get_vote() != prev_hs.get_vote() || hs.get_term() != prev_hs.get_term() {
rd.must_sync = true;
}
rd.hs = Some(hs);
}
if raft.raft_log.get_unstable().snapshot.is_some() {
rd.snapshot = raft.raft_log.get_unstable().snapshot.clone().unwrap();
}
if !raft.read_states.is_empty() {
rd.read_states = raft.read_states.clone();
}
rd
}
}
// RawNode is a thread-unsafe Node.
// The methods of this struct correspond to the methods of Node and are described
// more fully there.
pub struct RawNode<T: Storage> {
pub raft: Raft<T>,
prev_ss: SoftState,
prev_hs: HardState,
}
impl<T: Storage> RawNode<T> {
/// Create a new RawNode given some [`Config`](../struct.Config.html) and a list of [`Peer`](raw_node/struct.Peer.html)s.
pub fn new(config: &Config, store: T, mut peers: Vec<Peer>) -> Result<RawNode<T>> {
assert_ne!(config.id, 0, "config.id must not be zero");
let r = Raft::new(config, store);
let mut rn = RawNode {
raft: r,
prev_hs: Default::default(),
prev_ss: Default::default(),
};
let last_index = rn.raft.get_store().last_index().expect("");
if last_index == 0 {
rn.raft.become_follower(1, INVALID_ID);
let mut ents = Vec::with_capacity(peers.len());
for (i, peer) in peers.iter_mut().enumerate() {
let mut cc = ConfChange::new();
cc.set_change_type(ConfChangeType::AddNode);
cc.set_node_id(peer.id);
if let Some(ctx) = peer.context.take() {
cc.set_context(ctx);
}
let data =
protobuf::Message::write_to_bytes(&cc).expect("unexpected marshal error");
let mut e = Entry::new();
e.set_entry_type(EntryType::EntryConfChange);
e.set_term(1);
e.set_index(i as u64 + 1);
e.set_data(data);
ents.push(e);
}
rn.raft.raft_log.append(&ents);
rn.raft.raft_log.committed = ents.len() as u64;
for peer in peers {
rn.raft.add_node(peer.id);
}
}
rn.prev_ss = rn.raft.soft_state();
if last_index == 0 {
rn.prev_hs = Default::default();
} else {
rn.prev_hs = rn.raft.hard_state();
}
Ok(rn)
}
fn commit_ready(&mut self, rd: Ready) {
if rd.ss.is_some() {
self.prev_ss = rd.ss.unwrap();
}
if let Some(e) = rd.hs {
if e != HardState::new() {
self.prev_hs = e;
}
}
if !rd.entries.is_empty() {
let e = rd.entries.last().unwrap();
self.raft.raft_log.stable_to(e.get_index(), e.get_term());
}
if rd.snapshot != Snapshot::new() {
self.raft
.raft_log
.stable_snap_to(rd.snapshot.get_metadata().get_index());
}
if !rd.read_states.is_empty() {
self.raft.read_states.clear();
}
}
fn commit_apply(&mut self, applied: u64) {
self.raft.raft_log.applied_to(applied);
}
// Tick advances the internal logical clock by a single tick.
//
// Returns true to indicate that there will probably be some readiness need to be handled.
pub fn tick(&mut self) -> bool {
self.raft.tick()
}
// Campaign causes this RawNode to transition to candidate state.
pub fn campaign(&mut self) -> Result<()> {
let mut m = Message::new();
m.set_msg_type(MessageType::MsgHup);
self.raft.step(m)
}
// Propose proposes data be appended to the raft log.
pub fn propose(&mut self, context: Vec<u8>, data: Vec<u8>) -> Result<()> {
let mut m = Message::new();
m.set_msg_type(MessageType::MsgPropose);
m.set_from(self.raft.id);
let mut e = Entry::new();
e.set_data(data);
e.set_context(context);
m.set_entries(RepeatedField::from_vec(vec![e]));
self.raft.step(m)
}
// ProposeConfChange proposes a config change.
#[allow(needless_pass_by_value)]
pub fn propose_conf_change(&mut self, context: Vec<u8>, cc: ConfChange) -> Result<()> {
let data = protobuf::Message::write_to_bytes(&cc)?;
let mut m = Message::new();
m.set_msg_type(MessageType::MsgPropose);
let mut e = Entry::new();
e.set_entry_type(EntryType::EntryConfChange);
e.set_data(data);
e.set_context(context);
m.set_entries(RepeatedField::from_vec(vec![e]));
self.raft.step(m)
}
pub fn apply_conf_change(&mut self, cc: &ConfChange) -> ConfState {
if cc.get_node_id() == INVALID_ID {
let mut cs = ConfState::new();
cs.set_nodes(self.raft.prs().nodes());
cs.set_learners(self.raft.prs().learner_nodes());
return cs;
}
let nid = cc.get_node_id();
match cc.get_change_type() {
ConfChangeType::AddNode => self.raft.add_node(nid),
ConfChangeType::AddLearnerNode => self.raft.add_learner(nid),
ConfChangeType::RemoveNode => self.raft.remove_node(nid),
}
let mut cs = ConfState::new();
cs.set_nodes(self.raft.prs().nodes());
cs.set_learners(self.raft.prs().learner_nodes());
cs
}
// Step advances the state machine using the given message.
pub fn step(&mut self, m: Message) -> Result<()> {
// ignore unexpected local messages receiving over network
if is_local_msg(m.get_msg_type()) {
return Err(Error::StepLocalMsg);
}
if self.raft.prs().get(m.get_from()).is_some() || !is_response_msg(m.get_msg_type()) {
return self.raft.step(m);
}
Err(Error::StepPeerNotFound)
}
pub fn ready_since(&mut self, applied_idx: u64) -> Ready {
Ready::new(
&mut self.raft,
&self.prev_ss,
&self.prev_hs,
Some(applied_idx),
)
}
// Ready returns the current point-in-time state of this RawNode.
pub fn ready(&mut self) -> Ready {
Ready::new(&mut self.raft, &self.prev_ss, &self.prev_hs, None)
}
pub fn has_ready_since(&self, applied_idx: Option<u64>) -> bool {
let raft = &self.raft;
if !raft.msgs.is_empty() || raft.raft_log.unstable_entries().is_some() {
return true;
}
if !raft.read_states.is_empty() {
return true;
}
if self.get_snap().map_or(false, |s| !is_empty_snap(s)) {
return true;
}
let has_unapplied_entries = match applied_idx {
None => raft.raft_log.has_next_entries(),
Some(idx) => raft.raft_log.has_next_entries_since(idx),
};
if has_unapplied_entries {
return true;
}
if raft.soft_state() != self.prev_ss {
return true;
}
let hs = raft.hard_state();
if hs != HardState::new() && hs != self.prev_hs {
return true;
}
false
}
// HasReady called when RawNode user need to check if any Ready pending.
// Checking logic in this method should be consistent with Ready.containsUpdates().
pub fn has_ready(&self) -> bool {
self.has_ready_since(None)
}
#[inline]
pub fn get_snap(&self) -> Option<&Snapshot> {
self.raft.get_snap()
}
// Advance notifies the RawNode that the application has applied and saved progress in the
// last Ready results.
pub fn advance(&mut self, rd: Ready) {
self.advance_append(rd);
let commit_idx = self.prev_hs.get_commit();
if commit_idx != 0 {
// In most cases, prevHardSt and rd.HardState will be the same
// because when there are new entries to apply we just sent a
// HardState with an updated Commit value. However, on initial
// startup the two are different because we don't send a HardState
// until something changes, but we do send any un-applied but
// committed entries (and previously-committed entries may be
// incorporated into the snapshot, even if rd.CommittedEntries is
// empty). Therefore we mark all committed entries as applied
// whether they were included in rd.HardState or not.
self.advance_apply(commit_idx);
}
}
pub fn advance_append(&mut self, rd: Ready) {
self.commit_ready(rd);
}
pub fn advance_apply(&mut self, applied: u64) {
self.commit_apply(applied);
}
// Status returns the current status of the given group.
pub fn status(&self) -> Status {
Status::new(&self.raft)
}
// ReportUnreachable reports the given node is not reachable for the last send.
pub fn report_unreachable(&mut self, id: u64) {
let mut m = Message::new();
m.set_msg_type(MessageType::MsgUnreachable);
m.set_from(id);
// we don't care if it is ok actually
self.raft.step(m).is_ok();
}
// ReportSnapshot reports the status of the sent snapshot.
pub fn report_snapshot(&mut self, id: u64, status: SnapshotStatus) {
let rej = status == SnapshotStatus::Failure;
let mut m = Message::new();
m.set_msg_type(MessageType::MsgSnapStatus);
m.set_from(id);
m.set_reject(rej);
// we don't care if it is ok actually
self.raft.step(m).is_ok();
}
// TransferLeader tries to transfer leadership to the given transferee.
pub fn transfer_leader(&mut self, transferee: u64) {
let mut m = Message::new();
m.set_msg_type(MessageType::MsgTransferLeader);
m.set_from(transferee);
self.raft.step(m).is_ok();
}
// ReadIndex requests a read state. The read state will be set in ready.
// Read State has a read index. Once the application advances further than the read
// index, any linearizable read requests issued before the read request can be
// processed safely. The read state will have the same rctx attched.
pub fn read_index(&mut self, rctx: Vec<u8>) {
let mut m = Message::new();
m.set_msg_type(MessageType::MsgReadIndex);
let mut e = Entry::new();
e.set_data(rctx);
m.set_entries(RepeatedField::from_vec(vec![e]));
self.raft.step(m).is_ok();
}
#[inline]
pub fn get_store(&self) -> &T {
self.raft.get_store()
}
#[inline]
pub fn mut_store(&mut self) -> &mut T {
self.raft.mut_store()
}
}
#[cfg(test)]
mod test {
use super::is_local_msg;
use eraftpb::MessageType;
#[test]
fn test_is_local_msg() {
let tests = vec![
(MessageType::MsgHup, true),
(MessageType::MsgBeat, true),
(MessageType::MsgUnreachable, true),
(MessageType::MsgSnapStatus, true),
(MessageType::MsgCheckQuorum, true),
(MessageType::MsgPropose, false),
(MessageType::MsgAppend, false),
(MessageType::MsgAppendResponse, false),
(MessageType::MsgRequestVote, false),
(MessageType::MsgRequestVoteResponse, false),
(MessageType::MsgSnapshot, false),
(MessageType::MsgHeartbeat, false),
(MessageType::MsgHeartbeatResponse, false),
(MessageType::MsgTransferLeader, false),
(MessageType::MsgTimeoutNow, false),
(MessageType::MsgReadIndex, false),
(MessageType::MsgReadIndexResp, false),
(MessageType::MsgRequestPreVote, false),
(MessageType::MsgRequestPreVoteResponse, false),
];
for (msg_type, result) in tests {
assert_eq!(is_local_msg(msg_type), result);
}
}
}