Lots of clean-ups and simplifications
[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(std::string src, 
34           paxos_protocol::preparearg a,
35           paxos_protocol::prepareres &r);
36   paxos_protocol::status acceptreq(std::string src, 
37           paxos_protocol::acceptarg a, bool &r);
38   paxos_protocol::status decidereq(std::string src, 
39           paxos_protocol::decidearg a, int &r);
40
41   friend class log;
42
43  public:
44   acceptor(class paxos_change *cfg, bool _first, std::string _me, 
45         std::string _value);
46   ~acceptor() {};
47   void commit(unsigned instance, std::string v);
48   unsigned instance() { return instance_h; }
49   std::string value(unsigned instance) { return values[instance]; }
50   std::string dump();
51   void restore(std::string);
52   rpcs *get_rpcs() { return pxs; };
53   prop_t get_n_h() { return n_h; };
54   unsigned get_instance_h() { return instance_h; };
55 };
56
57 extern bool isamember(std::string m, const std::vector<std::string> &nodes);
58 extern std::string print_members(const std::vector<std::string> &nodes);
59
60 class proposer {
61  private:
62   log *l;
63   paxos_change *cfg;
64   acceptor *acc;
65   std::string me;
66   bool break1;
67   bool break2;
68
69   std::mutex pxs_mutex;
70
71   // Proposer state
72   bool stable;
73   prop_t my_n;          // number of the last proposal used in this instance
74
75   void setn();
76   bool prepare(unsigned instance, std::vector<std::string> &accepts, 
77          std::vector<std::string> nodes,
78          std::string &v);
79   void accept(unsigned instance, std::vector<std::string> &accepts, 
80         std::vector<std::string> nodes, std::string v);
81   void decide(unsigned instance, std::vector<std::string> accepts,
82         std::string v);
83
84   void breakpoint1();
85   void breakpoint2();
86   bool majority(const std::vector<std::string> &l1, const std::vector<std::string> &l2);
87
88   friend class log;
89  public:
90   proposer(class paxos_change *cfg, class acceptor *_acceptor, std::string _me);
91   ~proposer() {};
92   bool run(int instance, std::vector<std::string> cnodes, std::string v);
93   bool isrunning();
94   void breakpoint(int b);
95 };
96
97
98
99 #endif /* paxos_h */