-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.rs
466 lines (410 loc) · 17 KB
/
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
use crate::command_ext::{CommandView, NetworkCommand};
/// This module contains an implementation nodes that can run in primary or backup mode.
/// Every Set command to a primary node will be broadcasted reliably for the backup nodes to replicate it.
/// We plan to add backup promotion in case of primary failure.
use anyhow::{anyhow, Result};
use bytes::Bytes;
use lib::{
command::{ClientCommand, CommandResult},
network::SimpleSender,
store::Store,
};
use log::{error, info};
use std::{
collections::HashSet,
net::SocketAddr,
time::{self, Duration, Instant},
};
use tokio::sync::mpsc::Receiver;
use tokio::sync::oneshot::{self, Sender};
#[derive(Clone)]
/// A message handler that just forwards key/value store requests from clients to an internal rocksdb store.
pub struct Node {
pub socket_address: SocketAddr,
pub store: Store,
pub peers: Vec<SocketAddr>,
pub sender: SimpleSender,
pub view_change_delta_ms: Option<u16>,
pub timer_start: time::Instant,
pub current_view: u128,
pub command_view_lock: CommandView,
// the amount of peers which responded with "Lock"
// note: if we were to create a QC to store in the blockchain,
// we would need to store signatures from peers here
pub lock_responses: HashSet<SocketAddr>,
pub blame_messages: HashSet<SocketAddr>,
}
/// The state of a node viewed as a state-machine.
#[derive(Clone, Copy)]
pub enum State {
Primary,
Backup,
}
use State::*;
impl Node {
pub fn new(
peers: Vec<SocketAddr>,
db_path: &str,
address: SocketAddr,
view_change_delta_ms: Option<u16>,
) -> Self {
Self {
store: Store::new(db_path).unwrap(),
peers,
sender: SimpleSender::new(),
current_view: 0,
command_view_lock: CommandView::new(),
lock_responses: HashSet::new(),
blame_messages: HashSet::new(),
socket_address: address,
view_change_delta_ms,
timer_start: Instant::now(),
}
}
/// Runs a check to see if timer expired and acts accordingly
pub async fn check_timer(&mut self) {
let timer_duration = self.view_change_delta_ms.unwrap();
let delta = Duration::from_millis(timer_duration.into());
if self.timer_start.elapsed() > delta * 8 {
self.timer_start = Instant::now();
info!("{}: timer expired!", self.socket_address);
self.blame_messages.insert(self.socket_address);
// same as if we receive enough blames (TODO: this needs to check that there is no log for this view?)
self.broadcast_to_others(NetworkCommand::Blame {
socket_addr: self.socket_address,
view: self.current_view,
timer_expired: false,
})
.await;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
/// Runs the node to process network messages incoming in the given receiver
pub async fn run(
&mut self,
mut network_receiver: Receiver<(NetworkCommand, oneshot::Sender<String>)>,
mut client_receiver: Receiver<(ClientCommand, oneshot::Sender<CommandResult>)>,
) {
if self.view_change_delta_ms.is_none() {
loop {
tokio::select! {
Some((command, reply_sender)) = client_receiver.recv() => {
info!("Received client message {}", command);
self.proccess_client_msg(command, reply_sender).await;
}
Some((command, reply_sender)) = network_receiver.recv() => {
info!("Received network message {}", command);
reply_sender.send("ACK".to_string()).unwrap();
self.handle_network_msg(command.clone()).await.unwrap();
}
}
}
} else {
loop {
tokio::select! {
Some((command, reply_sender)) = client_receiver.recv() => {
info!("Received client message {}", command);
self.proccess_client_msg(command, reply_sender).await;
}
Some((command, reply_sender)) = network_receiver.recv() => {
info!("Received network message {}", command);
reply_sender.send("ACK".to_string()).unwrap();
self.handle_network_msg(command.clone()).await.unwrap();
}
_ = self.check_timer() => ()
}
}
}
}
pub async fn proccess_client_msg(
&mut self,
command: ClientCommand,
reply_sender: Sender<Result<Option<String>, String>>,
) {
let result = self
.handle_client_msg(command.clone())
.await
.map_err(|e| e.to_string());
if let Err(error) = reply_sender.send(result) {
error!("failed to send message {:?} response {:?}", command, error);
};
}
pub async fn handle_client_msg(&mut self, message: ClientCommand) -> Result<Option<String>> {
let state = self.get_state();
match (state, message) {
(_, cmd @ ClientCommand::Get { key: _ }) => self.handle_client_command(cmd).await,
(Primary, client_comand) => {
// we advance the view according to the primary and propose it
let command_view = CommandView {
command: client_comand,
view: self.current_view + 1,
};
// since we are primary, we lock the command view
self.lock_command_view(&command_view);
self.handle_lock_message(self.socket_address, command_view.clone())
.await
.unwrap();
let command = NetworkCommand::Propose {
command_view: command_view.clone(),
};
self.timer_start = Instant::now(); // for blame/view-change
info!("Received command, broadcasting Propose");
self.broadcast_to_others(command).await;
Ok(None)
}
(Backup, client_command) => {
info!("Received client command, forwarding to primary");
self.send_to_primary(NetworkCommand::Forward {
command: client_command,
})
.await;
Ok(None)
}
}
}
/// Process each messages coming from clients and foward events to the replicas
pub async fn handle_network_msg(&mut self, message: NetworkCommand) -> Result<Option<String>> {
let state = self.get_state();
match (state, message) {
// Once we proposed and we receive lock requests as a Primary, we can start counting the responses
(
Primary,
NetworkCommand::Lock {
socket_addr,
command_view,
},
) => self.handle_lock_message(socket_addr, command_view).await,
// a command has been proposed and we can lock it before sending a Lock message
// for now this happens in the Primary as well, but that functionality could be piggy-backed in the section where we receive the command
(_, NetworkCommand::Propose { command_view }) => {
// TODO: You should only lock if view number is expected?
self.lock_command_view(&command_view);
info!(
"{}: View-command locked, sending out Lock message",
self.socket_address
);
let lock_command = NetworkCommand::Lock {
socket_addr: self.socket_address,
command_view,
};
self.send_to_primary(lock_command).await;
Ok(None)
}
// for the primary, the command is committed as we reach quorum, so we can return Ok
(Primary, NetworkCommand::Commit { .. }) => Ok(None),
// the backup gets a Commit message after we reach quorum, so we can go ahead and commit
(Backup, NetworkCommand::Commit { command_view }) => {
info!("about to try commit as a response to Commit message");
match self.try_commit(command_view).await {
Ok(result) => {
info!(
"{}: Committed command, response was {:?}",
self.socket_address,
result.unwrap()
);
Ok(None)
}
_ => Err(anyhow!("Error committing command")),
}
}
// View change moves the view/primary after enough blames were emitted
(
_,
NetworkCommand::ViewChange {
socket_addr: _,
new_view,
highest_lock: _,
},
) => {
if new_view > self.current_view {
info!(
"{}: View-change performed, primary is {}",
self.socket_address,
self.get_primary(new_view)
);
self.timer_start = Instant::now();
self.trigger_view_change(new_view);
}
Ok(None)
}
// blames are emitted after timer expires or if 'f' nodes sent us a blame command
// this differentiation is so that we can make the protocol partially synchronous instead of synchronous
(
_,
NetworkCommand::Blame {
socket_addr,
view,
timer_expired,
},
) => self.handle_blame(view, socket_addr, timer_expired).await,
(_, NetworkCommand::Forward { command }) => {
self.handle_client_msg(command).await.unwrap();
Ok(None)
}
_ => {
info!("{} :unhandled command", self.socket_address);
Err(anyhow!("Unhandled command"))
}
}
}
async fn handle_lock_message(
&mut self,
socket_addr: SocketAddr,
command_view: CommandView,
) -> Result<Option<String>> {
if command_view.view <= self.current_view {
info!("Received command with an old, previously committed view, discarding");
Ok(None)
} else {
let _ = self.lock_responses.insert(socket_addr);
let response_count = self.lock_responses.len();
// the literature defines quorum as a function of the adversarial threshold we want to support
// n > f, n > 2f, or n > 3f are the alternatives; this uses n > 2f model
let quorum_count = ((self.peers.len()) / 2) + 1;
info!(
"Received lock, did we get quorum? {} responses so far vs expected quorum of {} ",
response_count, quorum_count
);
self.timer_start = Instant::now();
// broadcast commit, then try commit
if response_count >= quorum_count {
info!("Quorum achieved, commiting first and sending out Commit message!");
self.try_commit(command_view.clone())
.await
.expect("Error committing command as primary");
self.broadcast_to_others(NetworkCommand::Commit { command_view })
.await;
}
Ok(None)
}
}
async fn handle_client_command(&self, command: ClientCommand) -> Result<Option<String>> {
match command {
ClientCommand::Set { key, value } => {
self.store.write(key.into(), value.clone().into()).await?;
Ok(Some(value))
}
ClientCommand::Get { key } => {
if let Ok(Some(val)) = self.store.read(key.clone().into()).await {
let value = String::from_utf8(val)?;
return Ok(Some(value));
}
Ok(None)
}
}
}
async fn broadcast(&mut self, network_command: NetworkCommand) {
let message: Bytes = bincode::serialize(&network_command).unwrap().into();
// forward the command to all replicas and wait for them to respond
self.sender.broadcast(self.peers.clone(), message).await;
}
async fn broadcast_to_others(&mut self, network_command: NetworkCommand) {
let message: Bytes = bincode::serialize(&network_command).unwrap().into();
let other_peers: Vec<SocketAddr> = self
.peers
.iter()
.copied()
.filter(|x| *x != self.socket_address)
.collect();
// forward the command to all replicas and wait for them to respond
self.sender.broadcast(other_peers, message).await;
}
async fn send_to_primary(&mut self, cmd: NetworkCommand) {
let message: Bytes = bincode::serialize(&cmd).unwrap().into();
let primary_address = *(self.get_primary(self.current_view));
// forward the command to all replicas and wait for them to respond
self.sender.send(primary_address, message).await;
}
async fn try_commit(&mut self, command_view: CommandView) -> Result<Option<String>> {
self.timer_start = Instant::now();
// handle command, remove command lock (Primray already commits when quorum is achieved)
if self.command_view_lock != command_view {
info!(
"{}: trying to commit {:?} but we had locked {:?}",
self.socket_address, command_view, self.command_view_lock
);
// we are trying to commit something that has not been locked correctly,
// so there must have been some fault
return Err(anyhow!(
"Trying to commit a command-view that had not been previously locked"
));
}
self.current_view = command_view.view;
self.command_view_lock.view += 1;
self.lock_responses.clear();
self.clear_cmd_view_lock();
self.handle_client_command(command_view.command).await
}
fn lock_command_view(&mut self, command_view: &CommandView) {
if command_view.view != 0 {
self.command_view_lock = command_view.clone();
}
info!(
"{}: Locked command view {:?}",
self.socket_address, self.command_view_lock
);
}
pub fn get_state(&self) -> State {
if self.get_primary(self.current_view) == &self.socket_address {
return State::Primary;
}
State::Backup
}
fn trigger_view_change(&mut self, new_view: u128) {
self.current_view = new_view; // update last valid view number
self.lock_responses.clear();
self.blame_messages.clear();
self.command_view_lock = CommandView::new();
}
fn get_primary(&self, view: u128) -> &SocketAddr {
self.peers.get(view as usize % self.peers.len()).unwrap()
}
fn clear_cmd_view_lock(&mut self) {
self.command_view_lock = CommandView::new();
}
async fn handle_blame(
&mut self,
view: u128,
socket_addr: SocketAddr,
timer_expired: bool,
) -> Result<Option<String>> {
if view != self.current_view && !timer_expired {
return Ok(None);
}
let _ = self.blame_messages.insert(socket_addr);
let blame_count = self.blame_messages.len();
let highest_view_lock = self.command_view_lock.clone();
// from the docs, f is the amount of omission failures we want to tolerate
// again, f is defined from the adversarial threshold of the system
let f = self.peers.len() / 2;
let quorum_count = f + 1;
info!(
"Received blame, did we get quorum? {} responses so far vs expected quorum of {} ",
blame_count, quorum_count
);
let current_view = self.current_view;
// if we receive enough blames, even if the timer did not go off yet,
// we send out the blame message
if blame_count == f || timer_expired {
// same as if the timer expired on the node
self.broadcast_to_others(NetworkCommand::Blame {
socket_addr,
view: current_view,
timer_expired: false,
})
.await;
}
// broadcast commit, then try commit
if blame_count >= quorum_count {
info!("Enough nodes emmitted a blame response, starting view-change");
// same as if the timer expired on the node
self.broadcast(NetworkCommand::ViewChange {
socket_addr,
new_view: current_view + 1,
highest_lock: highest_view_lock,
})
.await;
}
Ok(None)
}
}