Clean-ups to types.
[invirt/third/libt4.git] / paxos.h
1 #ifndef paxos_h
2 #define paxos_h
3
4 #include "types.h"
5 #include "rpc/rpc.h"
6 #include "paxos_protocol.h"
7 #include "log.h"
8
9 using prepareres = paxos_protocol::prepareres;
10
11 using node_t = string;
12 using nodes_t = vector<node_t>;
13 using value_t = string;
14
15 class paxos_change {
16     public:
17         virtual void paxos_commit(unsigned instance, const value_t & v) = 0;
18         virtual ~paxos_change() {}
19 };
20
21 extern bool isamember(const node_t & m, const nodes_t & nodes);
22 extern bool majority(const nodes_t & l1, const nodes_t & l2);
23
24 class proposer_acceptor {
25     private:
26         mutex proposer_mutex;
27         mutex acceptor_mutex;
28
29         paxos_change *delegate;
30         node_t me;
31
32         rpcs pxs{(in_port_t)stoi(me)};
33
34         bool break1 = false;
35         bool break2 = false;
36
37         // Proposer state
38         bool stable = true;
39         prop_t proposal = {0, me};  // number of the last proposal used in this instance
40
41         // Acceptor state
42         prop_t promise = {0, me};   // number of the highest proposal seen in a prepare
43         prop_t accepted = {0, me};  // number of highest proposal accepted
44         value_t accepted_value;     // value of highest proposal accepted
45         unsigned instance_h = 0;    // number of the highest instance we have decided
46         map<unsigned,value_t> values;   // vals of each instance
47
48         friend class log;
49         log l = {this, me};
50
51         void commit(unsigned instance, const value_t & v);
52         void commit(unsigned instance, const value_t & v, lock & pxs_mutex_lock);
53
54         paxos_protocol::status preparereq(prepareres & r, const node_t & src, unsigned instance, prop_t n);
55         paxos_protocol::status acceptreq(bool & r, const node_t & src, unsigned instance, prop_t n, const value_t & v);
56         paxos_protocol::status decidereq(int & r, const node_t & src, unsigned instance, const value_t & v);
57
58         bool prepare(unsigned instance, nodes_t & accepts, const nodes_t & nodes, value_t & v);
59         void accept(unsigned instance, nodes_t & accepts, const nodes_t & nodes, const value_t & v);
60         void decide(unsigned instance, const nodes_t & accepts, const value_t & v);
61
62         void breakpoint1();
63         void breakpoint2();
64
65     public:
66         proposer_acceptor(paxos_change *delegate, bool _first, const node_t & _me, const value_t & _value);
67         unsigned instance() { return instance_h; }
68         const value_t & value(unsigned instance) { return values[instance]; }
69         string dump() { return l.dump(); }
70         void restore(const string &s) { l.restore(s); l.logread(); }
71         rpcs *get_rpcs() { return &pxs; }
72
73         bool run(unsigned instance, const nodes_t & cnodes, const value_t & v);
74         bool isrunning() { lock ml(proposer_mutex); return !stable; }
75         void breakpoint(int b);
76 };
77
78 #endif