-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaxos.cc
More file actions
419 lines (316 loc) · 8.88 KB
/
paxos.cc
File metadata and controls
419 lines (316 loc) · 8.88 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <paxos.hh>
#include <QDebug>
// Broadcast a message, first send it to the local node
// and then send it to the router for broadcast.
Paxos::Paxos(const QList<QString>& given_participants)
{
retryTimer.stop();
assert(given_participants.count() >= 1);
me = given_participants[0];
maxSafeRound = 1;
participants = given_participants;
QString file = "pxs-commits-log-";
file += me;
file += ".txt";
my_log = new Log(file);
QList<QString> old_commands = my_log->readLog();
replayLog(old_commands);
proposer = new Proposer((given_participants.count()/2)+1, me);
acceptor = new Acceptor(me);
// Phase 1 Message -> Acceptor
connect(this, SIGNAL(phase1Message(const QVariantMap&)),
acceptor, SLOT(tryPromise(const QVariantMap&)));
// Phase 2 Message -> Acceptor
connect(this, SIGNAL(phase2Message(const QVariantMap&)),
acceptor, SLOT(tryAccept(const QVariantMap&)));
// PromiseMessage -> Proposer
connect(this, SIGNAL(promiseMessage(const QVariantMap&)),
proposer, SLOT(processPromise(const QVariantMap&)));
// Accept Message -> Proposer
connect(this, SIGNAL(acceptMessage(const QVariantMap&)),
proposer, SLOT(processAccept(const QVariantMap&)));
// Reject Message -> Proposer
connect(this, SIGNAL(rejectMessage(const QVariantMap&)),
proposer, SLOT(processFailed(const QVariantMap&)));
// Proposer -> broadcast message
connect(proposer, SIGNAL(broadcastMessage(const QVariantMap&)),
this, SLOT(broadcastMsg(const QVariantMap&)));
// Acceptor -> send p2p messages
connect(acceptor, SIGNAL(singleReceiver(const QVariantMap &, const QString &)),
this, SLOT(sendSingle(const QVariantMap&, const QString&)));
// Catchup instance.
connect(proposer, SIGNAL(catchupInstance(quint32, const QVariantMap&)),
this, SLOT(laggingRoundNumber(quint32, const QVariantMap&)));
// Timeout failures.
connect(proposer, SIGNAL(proposalTimeout()),
this, SLOT(proposerTimeoutFailure()));
connect(&retryTimer, SIGNAL(timeout()),
this, SLOT(buzz()));
}
void
Paxos::clientRequest(const QString& value)
{
QString requestId = newId();
QVariantMap valueMap;
valueMap["Id"] = requestId;
valueMap["Value"] = value;
qDebug() << "Paxos: got new request for value="<<value;
pendingRequests.append(valueMap);
assert(pendingRequests.count() > 0);
if(pendingRequests.count() == 1){
qDebug() << "Paxos: dispatching new request, with round="<<maxSafeRound;
proposer->phase1(getSafeRound(), pendingRequests[0]);
}
}
QString
Paxos::newId()
{
QUuid localId = QUuid::createUuid();
return localId.toString();
}
// Just got a request to broadcast a message.
void
Paxos::broadcastMsg(const QVariantMap& msg)
{
for(int i = 0; i < participants.count(); ++i)
emit sendP2P(msg, participants[i]);
}
// Just received a new message from the router.
void
Paxos::newMessage(const QVariantMap& msg)
{
qDebug() << "Paxos: got new message!";
PaxosCodes pc = (PaxosCodes) msg["Paxos"].toInt();
switch(pc){
case PHASE1:
qDebug() << "Paxos: got phase1 message";
processPhase1(msg);
break;
case PHASE2:
qDebug() << "Paxos: got phase2 message";
emit phase2Message(msg);
break;
case COMMIT:
qDebug() << "Paxos: got commit message";
commit(msg);
break;
case REJECT:
qDebug() << "Paxos: got reject message";
emit rejectMessage(msg);
break;
case PROMISEVALUE:
qDebug() << "Paxos: got promise message";
emit promiseMessage(msg);
break;
case PROMISENOVALUE:
qDebug() << "Paxos: got promise message with no value";
emit promiseMessage(msg);
break;
case ACCEPT:
qDebug() << "Paxos: got accept message";
emit acceptMessage(msg);
break;
default:
break;
}
}
void
Paxos::processPhase1(const QVariantMap&msg)
{
quint32 round = msg["Round"].toUInt();
QString origin = msg["Origin"].toString();
QPair<bool, QVariantMap> isCommitted = checkCommitted(round);
if (isCommitted.first){
QVariantMap response;
PaxosCodes pc = REJECT;
response["Paxos"] = (int)pc;
response["Round"] = round;
response["Value"] = isCommitted.second;
response["Proposal"] = msg["Proposal"];
sendSingle(response, origin);
}
else
emit phase1Message(msg);
}
void
Paxos::sendSingle(const QVariantMap& msg, const QString& destination)
{
emit sendP2P(msg, destination);
}
void
Paxos::proposerTimeoutFailure()
{
int sleepTime = qrand() % 10;
sleepTime *= 1000;
sleepTime += 1000;
retryTimer.start(sleepTime);
}
void
Paxos::buzz()
{
retryTimer.stop();
qDebug() << "Paxos: Retrying request with round="<<maxSafeRound;
proposer->phase1(maxSafeRound, pendingRequests[0]);
}
void
Paxos::commit(QVariantMap msg)
{
quint32 round = msg["Round"].toUInt();
QVariantMap value = msg["Value"].toMap();
storeCommit(round, value);
}
void
Paxos::laggingRoundNumber(quint32 round, const QVariantMap& value)
{
qDebug() << "In lagging round number";
assert(round == getSafeRound());
incrementSafeRound();
QString id = value["Id"].toString();
QString proposedId = pendingRequests[0]["Id"].toString();
storeCommit(round, value);
// Our value has been committed.
if (id == proposedId)
pendingRequests.removeFirst();
if (pendingRequests.count() > 0){
qDebug() << "Paxos: dispatching new request with round="<<maxSafeRound;
proposer->phase1(getSafeRound(), pendingRequests[0]);
}
}
void
Paxos::incrementSafeRound()
{
++maxSafeRound;
}
quint32
Paxos::getSafeRound()
{
return maxSafeRound;
}
void
Paxos::storeCommit(quint32 round, const QVariantMap& value)
{
if (!commits.contains(round)){
QString toLog;
QTextStream stream(&toLog);
stream << "commit" << ":" << round << ":" << value["Id"].toString() << ":" << value["Value"].toString() << ":\n";
my_log->log(toLog);
emit newValue(round, value["Value"].toString());
commits.insert(round, value);
}
}
void
Paxos::replayLog(const QList<QString> &log)
{
quint32 round;
QVariantMap value;
bool quit = false;
for(int i = 0; i < log.count(); ++i){
QStringList split = log[i].split(":");
if (split[0] == "commit" && split[4] == ""){
round = split[1].toUInt();
value.insert("Id", split[2]);
value.insert("Value", split[3]);
assert(!commits.contains(round));
commits.insert(round, value);
qDebug() << "Replay log: found commit#" << round << ", value=" << value;
}
else
assert(false);
if (quit)
break;
}
}
QPair<bool, QVariantMap>
Paxos::checkCommitted(quint32 round)
{
QVariantMap value;
bool isCommitted = false;
if (commits.contains(round)){
isCommitted = true;
value = commits[round];
}
QPair<bool, QVariantMap> ret(isCommitted, value);
return ret;
}
ProposalNumber::ProposalNumber()
{
number = 0;
name = "";
}
ProposalNumber::ProposalNumber(const ProposalNumber& other)
{
number = other.number;
name = other.name;
}
ProposalNumber::~ProposalNumber()
{
}
ProposalNumber ProposalNumber::incr(const ProposalNumber &p)
{
ProposalNumber temp;
temp.number = p.number + 1;
temp.name = p.name;
qDebug()<< "incr number="<<temp.number<<" "<<temp.name;
return temp;
}
ProposalNumber::ProposalNumber(quint64 given_number, QString given_name)
{
number = given_number;
name = given_name;
}
bool
ProposalNumber::operator< (const ProposalNumber & second) const
{
return (number < second.number) || ((number == second.number) &&
(name < second.name));
}
bool
ProposalNumber::operator<= (const ProposalNumber &second) const
{
return ((number < second.number) || ((number == second.number) && (name < second.name))) || ((number == second.number) && (name == second.name));
}
bool
ProposalNumber::operator== (const ProposalNumber &second) const
{
return ((number == second.number) && (name == second.name));
}
bool
ProposalNumber::operator> (const ProposalNumber &second) const
{
return (number > second.number) || ((number == second.number) && (name > second.name));
}
bool
ProposalNumber::operator>= (const ProposalNumber &second) const
{
return ((number > second.number) || ((number == second.number) && (name > second.name))) || ((number == second.number) && (name == second.name));
}
ProposalNumber
ProposalNumber::operator= (const ProposalNumber &second)
{
number = second.number;
name = second.name;
return *this;
}
Log::Log(const QString &given_fileName)
{
fileName = given_fileName;
}
void
Log::log(const QString& tolog)
{
std::ofstream logFile;
logFile.open(fileName.toStdString().c_str(), std::ios::out | std::ios::app);
logFile << tolog.toStdString();
logFile.close();
}
QList<QString>
Log::readLog()
{
QList<QString> ret;
std::string temp;
std::ifstream logFile;
logFile.open(fileName.toStdString().c_str());
while(std::getline(logFile, temp))
ret.append(QString(temp.c_str()));
return ret;
}