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