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