-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.cc
275 lines (222 loc) · 7.79 KB
/
node.cc
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <assert.h>
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include "node.h"
#include "node_registry.h"
using ::std::atomic_int;
using ::std::chrono::seconds;
using ::std::cout;
using ::std::endl;
using ::std::lock_guard;
using ::std::make_pair;
using ::std::make_shared;
using ::std::mutex;
using ::std::shared_ptr;
using ::std::string;
using ::std::this_thread::sleep_for;
using ::std::thread;
using ::std::to_string;
using ::std::vector;
namespace paxos {
atomic_int GenerationClock::epoch_{0};
Node::Node(int node_id) {
node_id_ = node_id;
generation_clock_val_ = GenerationClock::Get();
cout << "Created Node with id: " << node_id <<
" , Generation Clock value: " << generation_clock_val_ << std::endl;
}
string Node::ToString() {
if (proposal_) {
return "{ node_id: " + to_string(node_id_) + ", generation_clock_val: "
+ to_string(generation_clock_val_) + ", proposed_value: "
+ proposal_->value + " }";
}
return "{ node_id: " + to_string(node_id_) + ", generation_clock_val: "
+ to_string(generation_clock_val_) + " }";
}
void Node::SetProposalValue(string value) {
if (!proposal_) {
proposal_ = make_shared<Proposal>();
}
proposal_->generation = GetGeneration();
proposal_->value = value;
}
void Node::RefreshProposal() {
if (proposal_) {
generation_clock_val_ = GenerationClock::Get();
proposal_->generation = GetGeneration();
}
}
void Node::Propose() {
// Endlessly loop until if have a proposal for the cluster and have not yet
// committed to a value.
while (proposal_ && !committed_value_) {
// Obtain a fresh generation before every PROPOSAL attempt.
RefreshProposal();
// Begin PROPOSE PHASE.
const auto& nodes = NodeRegistry::Get();
const int quoram = nodes.size() / 2 + 1;
vector<shared_ptr<Node>> acceptedNodes;
Generation highestAcceptedGeneration = make_pair(INT_MIN, INT_MIN);
string potentialValueToAdopt;
for (const auto& node : nodes) {
// Stub the networking layer.
const auto& promise = node->HandleProposal(shared_from_this(),
*proposal_);
cout << this->ToString() << " PROPOSED " << node->ToString() << endl;
if (!promise || !promise->accepted) {
cout << node->ToString() << " DID NOT PROMISE " << this->ToString()
<< endl;
continue;
}
cout << node->ToString() << " PROMISED " << this->ToString() << endl;
acceptedNodes.push_back(node);
if (promise->acceptReq
&& promise->acceptReq->generation > highestAcceptedGeneration) {
highestAcceptedGeneration = promise->acceptReq->generation;
potentialValueToAdopt = promise->acceptReq->value;
}
}
// Achieved quoram. Move to ACCEPT PHASE.
if (acceptedNodes.size() >= quoram) {
cout << this->ToString()
<< " achieved quoram in PROPOSE phase. Moving to ACCEPT phase..."
<< endl;
if (highestAcceptedGeneration > GetGeneration())
// Update my value to match the largest generation that was accepted.
SetProposalValue(potentialValueToAdopt);
AcceptReq acceptReq;
acceptReq.generation = GetGeneration();
acceptReq.value = proposal_->value;
SendAcceptReqs(acceptedNodes, acceptReq);
}
}
}
shared_ptr<Promise> Node::HandleProposal(shared_ptr<Node> proposer,
Proposal proposal) {
// Obtain mutex so that PROPOSAL processing is serialized.
const lock_guard<mutex> lock(handle_proposal_mutex_);
// Simulate random network delay.
sleep_for(seconds(GenerationClock::Get() % 7));
// Simulate random network partition.
if (GenerationClock::Get() % 11 == 0) {
return nullptr;
}
if (committed_value_) {
// Already committed to a value, so cannot PROMISE.
return nullptr;
}
if (!promised_generation_) {
promised_generation_ = make_shared<Generation>();
*promised_generation_ = proposal.generation;
return make_shared<Promise>(true, nullptr);
}
if (proposal.generation > *promised_generation_) {
*promised_generation_ = proposal.generation;
if (accepted_req_) {
return make_shared<Promise>(true, accepted_req_);
}
return make_shared<Promise>(true, nullptr);
}
return make_shared<Promise>(false, nullptr);
}
void Node::SendAcceptReqs(vector<shared_ptr<Node>> nodes,
AcceptReq acceptReq) {
const int quoram = nodes.size() / 2 + 1;
int acceptedCount = 0;
for (const auto& promisedNode : nodes) {
// Stub the networking layer.
const auto& acceptResponse =
promisedNode->HandleAcceptReq(shared_from_this(), acceptReq);
cout << this->ToString() << " requested ACCEPT from "
<< promisedNode->ToString() << endl;
if (acceptResponse && acceptResponse->accepted) {
cout << promisedNode->ToString() << " ACCEPTED " << this->ToString()
<< endl;
acceptedCount++;
} else {
cout << promisedNode->ToString() << " DID NOT ACCEPT "
<< this->ToString() << endl;
}
}
if (acceptedCount >= quoram) {
cout << this->ToString()
<< " achieved quoram in ACCEPT phase. Sending COMMIT to all nodes..."
<< endl;
// Send the accepted value as a COMMIT to all nodes.
for (const auto& node : NodeRegistry::Get()) {
node->Commit(acceptReq.value);
}
}
}
shared_ptr<AcceptResponse> Node::HandleAcceptReq(
shared_ptr<Node> acceptRequester, AcceptReq acceptReq) {
// Obtain mutex so that ACCEPT processing is serialized.
const lock_guard<mutex> lock(handle_accept_req_mutex_);
// Simulate random network delay.
sleep_for(seconds(GenerationClock::Get() % 7));
// Simulate random network partition.
if (GenerationClock::Get() % 11 == 0) {
return nullptr;
}
if (committed_value_) {
// Already committed to a value, so cannot ACCEPT.
return nullptr;
}
// Accept the request if its generation is >= the highest generation that
// has been promised so far. Theoretically, checking for = should be
// sufficient, since the accept request's generation cannot be higher than
// the highest promised generation.
if (!promised_generation_
|| acceptReq.generation >= *promised_generation_) {
if (!accepted_req_) {
accepted_req_ = make_shared<AcceptReq>();
}
*accepted_req_ = acceptReq;
return make_shared<AcceptResponse>(true);
}
return make_shared<AcceptResponse>(false);
}
void Node::Commit(string value) {
if (!committed_value_) {
committed_value_ = make_shared<string>(value);
} else {
*committed_value_ = value;
}
}
shared_ptr<string> Node::GetCommittedValue() {
return committed_value_;
}
}
int main() {
const auto& node1 = paxos::NodeRegistry::Register(1);
const auto& node2 = paxos::NodeRegistry::Register(2);
const auto& node3 = paxos::NodeRegistry::Register(3);
const auto& node4 = paxos::NodeRegistry::Register(4);
const auto& node5 = paxos::NodeRegistry::Register(5);
thread consensusChecker([] {
shared_ptr<string> committedValue = nullptr;
while (!committedValue) {
// Paxos consensus not achieved yet.
sleep_for(seconds(2));
committedValue = paxos::NodeRegistry::GetCommittedValue();
}
cout << "Paxos consensus achieved; value committed: " << *committedValue
<< endl;
});
thread proposer1([node1] {
node1->SetProposalValue("alice");
node1->Propose();
});
thread proposer2([node2] {
node2->SetProposalValue("bob");
node2->Propose();
});
consensusChecker.join();
proposer1.join();
proposer2.join();
return 0;
}