More clean-ups and cool template stuff
[invirt/third/libt4.git] / paxos.h
1 #ifndef paxos_h
2 #define paxos_h
3
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include "rpc/rpc.h"
8 #include "paxos_protocol.h"
9 #include "log.h"
10 #include "lock.h"
11
12 using std::string;
13 using std::map;
14 using std::vector;
15
16 class paxos_change {
17     public:
18         virtual void paxos_commit(unsigned instance, const string & v) = 0;
19         virtual ~paxos_change() {}
20 };
21
22 class acceptor {
23     private:
24         log *l;
25         rpcs *pxs;
26         paxos_change *cfg;
27         string me;
28         mutex pxs_mutex;
29
30         // Acceptor state
31         prop_t n_h;             // number of the highest proposal seen in a prepare
32         prop_t n_a;             // number of highest proposal accepted
33         string v_a;     // value of highest proposal accepted
34         unsigned instance_h;    // number of the highest instance we have decided
35         map<unsigned,string> values;    // vals of each instance
36
37         void commit(unsigned instance, const string & v, lock & pxs_mutex_lock);
38         paxos_protocol::status preparereq(paxos_protocol::prepareres & r,
39                 const string & src, paxos_protocol::preparearg a);
40         paxos_protocol::status acceptreq(bool & r, const string & src,
41                 paxos_protocol::acceptarg a);
42         paxos_protocol::status decidereq(int & r, const string & src,
43                 paxos_protocol::decidearg a);
44
45         friend class log;
46
47     public:
48         acceptor(class paxos_change *cfg, bool _first, const string & _me,
49                 const string & _value);
50         ~acceptor() {}
51         void commit(unsigned instance, const string & v);
52         unsigned instance() { return instance_h; }
53         const string & value(unsigned instance) { return values[instance]; }
54         string dump();
55         void restore(const string &);
56         rpcs *get_rpcs() { return pxs; }
57         prop_t get_n_h() { return n_h; }
58         unsigned get_instance_h() { return instance_h; }
59 };
60
61 extern bool isamember(const string & m, const vector<string> & nodes);
62 extern string print_members(const vector<string> & nodes);
63
64 class proposer {
65     private:
66         log *l;
67         paxos_change *cfg;
68         acceptor *acc;
69         string me;
70         bool break1;
71         bool break2;
72
73         mutex pxs_mutex;
74
75         // Proposer state
76         bool stable;
77         prop_t my_n;            // number of the last proposal used in this instance
78
79         void setn();
80         bool prepare(unsigned instance, vector<string> & accepts,
81                 const vector<string> & nodes,
82                 string & v);
83         void accept(unsigned instance, vector<string> & accepts,
84                 const vector<string> & nodes, const string & v);
85         void decide(unsigned instance, const vector<string> & accepts,
86                 const string & v);
87
88         void breakpoint1();
89         void breakpoint2();
90         bool majority(const vector<string> & l1, const vector<string> & l2);
91
92         friend class log;
93     public:
94         proposer(class paxos_change *cfg, class acceptor *_acceptor, const string &_me);
95         ~proposer() {}
96         bool run(unsigned instance, const vector<string> & cnodes, const string & v);
97         bool isrunning();
98         void breakpoint(int b);
99 };
100
101
102
103 #endif /* paxos_h */