viewstamped-replication 0.4.0

A Rust-based implementation of the Viewstamped Replication consensus protocol.
Documentation
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
use std::collections::{HashMap, HashSet};
use crate::mailbox::Mailbox;
use crate::new_model::{Envelope, Message, ReplicaIdentifier, Request, View, OpNumber, Payload, Prepare, ClientIdentifier, GroupIdentifier, RequestIdentifier, PrepareOk, Address, Reply};
use crate::service::Service;

pub struct Client {
    identifier: ClientIdentifier,
    view: View,
    request: RequestIdentifier,
    group: GroupIdentifier,
}

impl Client {
    pub fn new(group: GroupIdentifier) -> Self {
        Self { identifier: Default::default(), view: Default::default(), request: Default::default(), group }
    }

    pub fn envelope(&mut self, payload: &[u8]) -> Envelope {
        Envelope {
            from: self.identifier.into(),
            to: self.group.primary(self.view).into(),
            message: Message {
                view: self.view,
                payload: Request {
                    op: Vec::from(payload),
                    c: self.identifier,
                    s: self.request.increment(),
                }.into(),
            },
        }
    }
}

#[derive(Copy, Clone, Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
pub enum Status {
    #[default]
    Normal,
    ViewChange,
    Recovering,
}

pub struct Replica<S> {
    /// The service code for processing committed client requests.
    service: S,
    identifier: ReplicaIdentifier,
    /// The current view.
    view: View,
    /// The latest op-number received by this replica.
    op_number: OpNumber,
    /// The current status, either normal, view-change, or recovering.
    status: Status,
    /// This is an array containing op-number entries.
    /// The entries contain the requests that have been received so far in their assigned order.
    log: Vec<Request>,
    /// The last operation number committed in the current view.
    committed: OpNumber,
    /// The count of operations executed in the current view.
    executed: OpNumber,
}

impl<S> Replica<S>
    where
        S: Service,
{
    pub fn new(
        service: S,
        identifier: ReplicaIdentifier,
    ) -> Self {
        Self {
            service,
            identifier,
            view: View::default(),
            op_number: OpNumber::default(),
            status: Default::default(),
            log: vec![],
            committed: Default::default(),
            executed: Default::default(),
        }
    }

    pub fn poll(&mut self, mailbox: &mut Mailbox) {
        let primary = self.identifier.primary(self.view);

        if primary == self.identifier {
            self.poll_primary(mailbox);
        } else {
            self.poll_replica(mailbox);
        }
    }

    fn poll_primary(&mut self, mailbox: &mut Mailbox) {
        match self.status {
            Status::Normal => self.process_normal_primary(mailbox),
            Status::ViewChange => self.process_view_change_primary(mailbox),
            Status::Recovering => ()
        }
    }

    fn process_normal_primary(&mut self, mailbox: &mut Mailbox) {
        let mut prepared: HashMap<OpNumber, HashSet<Address>> = HashMap::new();

        mailbox.select(|sender, envelope| {
            match envelope {
                Envelope { message: Message { payload: Payload::Request(request), .. }, .. } => {
                    self.push_request(request.clone());

                    sender.broadcast(Message::new(
                        self.view,
                        Prepare {
                            n: self.op_number,
                            m: request,
                            k: self.committed,
                        },
                    ));

                    None
                }
                ref envelope @ Envelope { from, message: Message { payload: Payload::PrepareOk(prepare_ok), .. }, .. } => {
                    if self.committed >= prepare_ok.n {
                        None
                    } else {
                        let replication = prepared.entry(prepare_ok.n).or_insert_with(HashSet::new);

                        replication.insert(from);

                        if replication.len() >= self.identifier.sub_majority() {
                            self.committed = self.committed.max(prepare_ok.n);

                            let length = OpNumber::from(self.log.len());
                            while self.committed > self.executed && self.executed < length {
                                // executed must be incremented after indexing the log to avoid panicking.
                                let request = &self.log[usize::from(self.executed)];
                                let reply = self.service.invoke(request.op.as_slice());

                                sender.send(request.c, Message::new(self.view, Reply { s: request.s, x: reply }));
                                self.executed.increment();
                            }

                            None
                        } else {
                            Some(envelope.clone())
                        }
                    }
                }
                _ => Some(envelope)
            }
        })
    }

    fn process_view_change_primary(&mut self, mailbox: &mut Mailbox) {}

    fn poll_replica(&mut self, mailbox: &mut Mailbox) {
        match self.status {
            Status::Normal => self.process_normal_replica(mailbox),
            Status::ViewChange => self.process_view_change_replica(mailbox),
            Status::Recovering => ()
        }
    }

    fn process_normal_replica(&mut self, mailbox: &mut Mailbox) {
        let next_op = self.op_number.next();

        mailbox.select(|sender, envelope| match envelope {
            Envelope { message: Message { payload: Payload::Prepare(prepare), .. }, .. } if next_op == prepare.n => {
                self.push_request(prepare.m);

                let primary = self.identifier.primary(self.view);
                sender.send(primary, Message::new(self.view, PrepareOk { n: self.op_number }));

                self.committed = self.committed.max(prepare.k);

                let length = OpNumber::from(self.log.len());
                while self.committed > self.executed && self.executed < length {
                    // executed must be incremented after indexing the log to avoid panicking.
                    let request = &self.log[usize::from(self.executed)];
                    self.service.invoke(request.op.as_slice());
                    self.executed.increment();
                }

                None
            }
            // TODO: perform state transfer if necessary to get missing information.
            _ => Some(envelope)
        })
    }

    fn process_view_change_replica(&mut self, mailbox: &mut Mailbox) {}

    // Push a request to the end of the log and increment the op-number.
    fn push_request(&mut self, request: Request) {
        self.op_number.increment();
        self.log.push(request);
    }
}


#[cfg(test)]
mod tests {
    use crate::new_model::GroupIdentifier;
    use super::*;

    #[test]
    fn request_primary() {
        let operation = b"Hi!";
        let group = GroupIdentifier::new(3);
        let replicas: Vec<ReplicaIdentifier> = group.replicas().collect();

        let mut primary = Replica::new(0, replicas[0]);
        let mut client = Client::new(group);
        let mut mailbox = simulate_request(&mut primary, &mut client, operation, 1);

        let envelopes: Vec<Envelope> = mailbox.drain_outbound().collect();

        assert_eq!(envelopes, vec![prepare_envelope(&primary, &client, operation)]);
    }

    #[test]
    fn prepare_replica() {
        let operation = b"Hi!";
        let group = GroupIdentifier::new(3);
        let replicas: Vec<ReplicaIdentifier> = group.replicas().collect();

        let mut primary = Replica::new(0, replicas[0]);
        let mut client = Client::new(group);

        let mut replica = Replica::new(0, replicas[1]);
        let mut mailbox = Mailbox::from(replicas[1]);

        simulate_request(&mut primary, &mut client, operation, 1);

        let envelope = prepare_envelope(&primary, &client, operation);

        mailbox.deliver(envelope);
        replica.poll(&mut mailbox);

        let envelopes: Vec<Envelope> = mailbox.drain_outbound().collect();

        assert_eq!(envelopes, vec![prepare_ok_envelope(&primary, &replica)]);
        assert_eq!(replica.op_number, OpNumber::from(1));
    }

    #[test]
    fn prepare_replica_committed() {
        let operation = b"Hi!";
        let group = GroupIdentifier::new(3);
        let replicas: Vec<ReplicaIdentifier> = group.replicas().collect();

        let mut primary = Replica::new(0, replicas[0]);
        let mut client = Client::new(group);

        let mut replica = Replica::new(0, replicas[1]);
        let mut mailbox = Mailbox::from(replicas[1]);

        simulate_request(&mut primary, &mut client, operation, 1);
        let envelope = prepare_envelope(&primary, &client, operation);
        mailbox.deliver(envelope);
        replica.poll(&mut mailbox);
        assert_eq!(mailbox.drain_outbound().count(), 1);

        simulate_request(&mut primary, &mut client, operation, 1);
        primary.committed.increment();
        let envelope = prepare_envelope(&primary, &client, operation);
        mailbox.deliver(envelope);
        replica.poll(&mut mailbox);

        let envelopes: Vec<Envelope> = mailbox.drain_outbound().collect();

        assert_eq!(envelopes, vec![prepare_ok_envelope(&primary, &replica)]);
        assert_eq!(replica.service, operation.len());
    }

    #[test]
    fn prepare_replica_committed_not_in_log() {
        let operation = b"Hi!";
        let group = GroupIdentifier::new(3);
        let replicas: Vec<ReplicaIdentifier> = group.replicas().collect();

        let mut primary = Replica::new(0, replicas[0]);
        let mut client = Client::new(group);

        let mut replica = Replica::new(0, replicas[1]);
        let mut mailbox = Mailbox::from(replicas[1]);

        simulate_request(&mut primary, &mut client, operation, 2);
        primary.committed.increment();

        let envelope = prepare_envelope(&primary, &client, operation);

        mailbox.deliver(envelope);
        replica.poll(&mut mailbox);

        assert_eq!(mailbox.drain_outbound().count(), 0);
        assert_eq!(replica.service, 0);
    }

    #[test]
    fn prepare_replica_buffered() {
        let operation = b"Hi!";
        let group = GroupIdentifier::new(3);
        let replicas: Vec<ReplicaIdentifier> = group.replicas().collect();

        let mut primary = Replica::new(0, replicas[0]);
        let mut client = Client::new(group);

        let mut replica = Replica::new(0, replicas[1]);
        let mut mailbox = Mailbox::from(replicas[1]);

        simulate_request(&mut primary, &mut client, operation, 2);

        let envelope = prepare_envelope(&primary, &client, operation);

        mailbox.deliver(envelope);
        replica.poll(&mut mailbox);

        let envelopes: Vec<Envelope> = mailbox.drain_outbound().collect();

        assert_eq!(envelopes, vec![]);
    }

    #[test]
    fn prepare_ok_primary() {
        let operation = b"Hi!";
        let group = GroupIdentifier::new(5);
        let replicas: Vec<ReplicaIdentifier> = group.replicas().collect();

        let mut primary = Replica::new(0, replicas[0]);
        let mut replica1 = Replica::new(0, replicas[1]);
        let mut replica2 = Replica::new(0, replicas[2]);
        let mut client = Client::new(group);
        let mut mailbox = simulate_request(&mut primary, &mut client, operation, 1);

        simulate_broadcast(&mut mailbox, vec![&mut replica1, &mut replica2]);

        let envelope1 = prepare_ok_envelope(&primary, &replica1);
        mailbox.deliver(envelope1);
        primary.poll(&mut mailbox);
        assert_eq!(mailbox.drain_outbound().count(), 0);

        let envelope2 = prepare_ok_envelope(&primary, &replica2);
        mailbox.deliver(envelope2);
        primary.poll(&mut mailbox);

        let envelopes: Vec<Envelope> = mailbox.drain_outbound().collect();

        assert_eq!(envelopes, vec![reply_envelope(&primary, &client, operation, 1)]);
        assert_eq!(primary.service, operation.len());
    }

    #[test]
    fn prepare_ok_primary_skipped() {
        let times = 2;
        let operation = b"Hi!";
        let group = GroupIdentifier::new(5);
        let replicas: Vec<ReplicaIdentifier> = group.replicas().collect();

        let mut primary = Replica::new(0, replicas[0]);
        let mut replica1 = Replica::new(0, replicas[1]);
        let mut replica2 = Replica::new(0, replicas[2]);
        let mut client = Client::new(group);
        let mut mailbox = simulate_request(&mut primary, &mut client, operation, times);

        simulate_broadcast(&mut mailbox, vec![&mut replica1, &mut replica2]);

        let envelope1 = prepare_ok_envelope(&primary, &replica1);
        let envelope2 = prepare_ok_envelope(&primary, &replica2);

        mailbox.deliver(envelope1);
        mailbox.deliver(envelope2);
        primary.poll(&mut mailbox);

        let envelopes: Vec<Envelope> = mailbox.drain_outbound().collect();

        // backtrack the request id to build the replies correctly.
        client.request = RequestIdentifier::default();
        let mut replies = Vec::with_capacity(times);

        for i in 1..=times {
            client.request.increment();
            replies.push(reply_envelope(&primary, &client, operation, i));
        }

        assert_eq!(envelopes, replies);
        assert_eq!(primary.service, operation.len() * times);
    }

    fn simulate_broadcast(source: &mut Mailbox, replicas: Vec<&mut Replica<usize>>) {
        let envelopes: Vec<Envelope> = source.drain_outbound().collect();

        for replica in replicas {
            let mut mailbox = Mailbox::from(replica.identifier);

            for envelope in envelopes.iter() {
                mailbox.deliver(envelope.clone());
                replica.poll(&mut mailbox);
            }
        }
    }

    fn simulate_request(primary: &mut Replica<usize>, client: &mut Client, operation: &[u8], times: usize) -> Mailbox {
        let mut mailbox = Mailbox::from(primary.identifier);

        for _ in 0..times {
            mailbox.deliver(client.envelope(operation));
            primary.poll(&mut mailbox);
        }

        mailbox
    }

    fn prepare_envelope<S>(replica: &Replica<S>, client: &Client, operation: &[u8]) -> Envelope {
        Envelope {
            from: replica.identifier.into(),
            to: replica.identifier.group().into(),
            message: Message {
                view: replica.view,
                payload: Prepare {
                    n: replica.op_number,
                    m: Request {
                        op: Vec::from(operation),
                        c: client.identifier,
                        s: client.request,
                    },
                    k: replica.committed,
                }.into(),
            },
        }
    }

    fn prepare_ok_envelope<S>(primary: &Replica<S>, replica: &Replica<S>) -> Envelope {
        Envelope {
            from: replica.identifier.into(),
            to: primary.identifier.into(),
            message: Message {
                view: replica.view,
                payload: PrepareOk {
                    n: replica.op_number,
                }.into(),
            },
        }
    }

    fn reply_envelope<S>(primary: &Replica<S>, client: &Client, operation: &[u8], times: usize) -> Envelope {
        Envelope {
            from: primary.identifier.into(),
            to: client.identifier.into(),
            message: Message {
                view: primary.view,
                payload: Reply {
                    x: (operation.len() * times).to_be_bytes().to_vec(),
                    s: client.request,
                }.into(),
            },
        }
    }
}