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