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
#[macro_use]
extern crate serde_derive;
extern crate bincode;
extern crate serde;
extern crate sled;

use std::fmt::Debug;
use std::time::{Duration, SystemTime};

use serde::de::DeserializeOwned;
use serde::Serialize;

mod acceptor;
mod client;
mod proposer;
mod storage;

pub use acceptor::Acceptor;
pub use client::Client;
pub use proposer::Proposer;
pub use storage::{MemStorage, SledStorage};

// Reactor is a trait for building simulable systems.
pub trait Reactor: Debug + Clone {
    type Peer: std::net::ToSocketAddrs;
    type Message: Serialize + DeserializeOwned;

    fn receive(
        &mut self,
        at: SystemTime,
        from: Self::Peer,
        msg: Self::Message,
    ) -> Vec<(Self::Peer, Self::Message)>;

    fn tick(&mut self, _at: SystemTime) -> Vec<(Self::Peer, Self::Message)> {
        vec![]
    }
}

#[derive(
    Default,
    Clone,
    Debug,
    PartialOrd,
    PartialEq,
    Eq,
    Hash,
    Ord,
    Serialize,
    Deserialize,
)]
pub struct Ballot(u64);

type Key = Vec<u8>;
type Value = Vec<u8>;

#[derive(
    PartialOrd, Ord, Eq, PartialEq, Debug, Clone, Serialize, Deserialize,
)]
pub enum Req {
    Get(Key),
    Del(Key),
    Set(Key, Value),
    Cas(Key, Option<Value>, Option<Value>),
}

impl Req {
    fn key(&self) -> Key {
        match *self {
            Req::Get(ref k)
            | Req::Del(ref k)
            | Req::Set(ref k, _)
            | Req::Cas(ref k, _, _) => k.clone(),
        }
    }
}

#[derive(
    Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Serialize, Deserialize,
)]
pub enum Rpc {
    ClientRequest(u64, Req),
    ClientResponse(u64, Result<Option<Value>, Error>),
    SetAcceptAcceptors(Vec<String>),
    SetProposeAcceptors(Vec<String>),
    ProposeReq(Ballot, Key),
    ProposeRes {
        req_ballot: Ballot,
        last_accepted_ballot: Ballot,
        last_accepted_value: Option<Value>,
        res: Result<(), Error>,
    },
    AcceptReq(Ballot, Key, Option<Value>),
    AcceptRes(Ballot, Result<(), Error>),
}
use Rpc::*;

impl Rpc {
    pub fn client_req_id(&self) -> Option<u64> {
        match *self {
            ClientResponse(id, _) | ClientRequest(id, _) => Some(id),
            _ => None,
        }
    }

    pub fn client_req(self) -> Option<Req> {
        match self {
            ClientRequest(_, req) => Some(req),
            _ => None,
        }
    }
}

#[derive(
    Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Serialize, Deserialize,
)]
pub enum Error {
    ProposalRejected { last: Ballot },
    AcceptRejected { last: Ballot },
    CasFailed(Option<Value>),
    Timeout,
}

impl Error {
    pub fn is_rejected_accept(&self) -> bool {
        match *self {
            Error::AcceptRejected { .. } => true,
            _ => false,
        }
    }

    pub fn is_rejected_proposal(&self) -> bool {
        match *self {
            Error::ProposalRejected { .. } => true,
            _ => false,
        }
    }

    pub fn is_timeout(&self) -> bool {
        match *self {
            Error::Timeout => true,
            _ => false,
        }
    }

    pub fn is_failed_cas(&self) -> bool {
        match *self {
            Error::CasFailed(_) => true,
            _ => false,
        }
    }
}