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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "ElectionLogic.h"
#include "include/ceph_assert.h"
#include "common/dout.h"
#define dout_subsys ceph_subsys_mon
#undef dout_prefix
#define dout_prefix _prefix(_dout, epoch, elector)
using std::cerr;
using std::cout;
using std::dec;
using std::hex;
using std::list;
using std::map;
using std::make_pair;
using std::ostream;
using std::ostringstream;
using std::pair;
using std::set;
using std::setfill;
using std::string;
using std::stringstream;
using std::to_string;
using std::vector;
using std::unique_ptr;
using ceph::bufferlist;
using ceph::decode;
using ceph::encode;
using ceph::Formatter;
using ceph::JSONFormatter;
using ceph::mono_clock;
using ceph::mono_time;
using ceph::timespan_str;
static ostream& _prefix(std::ostream *_dout, epoch_t epoch, ElectionOwner* elector) {
return *_dout << "paxos." << elector->get_my_rank()
<< ").electionLogic(" << epoch << ") ";
}
void ElectionLogic::init()
{
epoch = elector->read_persisted_epoch();
if (!epoch) {
ldout(cct, 1) << "init, first boot, initializing epoch at 1 " << dendl;
epoch = 1;
} else if (epoch % 2) {
ldout(cct, 1) << "init, last seen epoch " << epoch
<< ", mid-election, bumping" << dendl;
++epoch;
elector->persist_epoch(epoch);
} else {
ldout(cct, 1) << "init, last seen epoch " << epoch << dendl;
}
}
void ElectionLogic::bump_epoch(epoch_t e)
{
ldout(cct, 10) << __func__ << " to " << e << dendl;
ceph_assert(epoch <= e);
epoch = e;
peer_tracker->increase_epoch(e);
elector->persist_epoch(epoch);
// clear up some state
electing_me = false;
acked_me.clear();
elector->notify_bump_epoch();
}
void ElectionLogic::declare_standalone_victory()
{
assert(elector->paxos_size() == 1 && elector->get_my_rank() == 0);
init();
bump_epoch(epoch+1);
}
void ElectionLogic::clear_live_election_state()
{
leader_acked = -1;
electing_me = false;
reset_stable_tracker();
leader_peer_tracker.reset();
}
void ElectionLogic::reset_stable_tracker()
{
stable_peer_tracker.reset(new ConnectionTracker(*peer_tracker));
}
void ElectionLogic::connectivity_bump_epoch_in_election(epoch_t mepoch)
{
ldout(cct, 30) << __func__ << " to " << mepoch << dendl;
ceph_assert(mepoch > epoch);
bump_epoch(mepoch);
reset_stable_tracker();
double lscore, my_score;
my_score = connectivity_election_score(elector->get_my_rank());
lscore = connectivity_election_score(leader_acked);
if (my_score > lscore) {
leader_acked = -1;
leader_peer_tracker.reset();
}
}
void ElectionLogic::start()
{
if (!participating) {
ldout(cct, 0) << "not starting new election -- not participating" << dendl;
return;
}
ldout(cct, 5) << "start -- can i be leader?" << dendl;
acked_me.clear();
init();
// start by trying to elect me
if (epoch % 2 == 0) {
bump_epoch(epoch+1); // odd == election cycle
} else {
elector->validate_store();
}
acked_me.insert(elector->get_my_rank());
clear_live_election_state();
reset_stable_tracker();
electing_me = true;
bufferlist bl;
if (strategy == CONNECTIVITY) {
stable_peer_tracker->encode(bl);
}
elector->propose_to_peers(epoch, bl);
elector->_start();
}
void ElectionLogic::defer(int who)
{
if (strategy == CLASSIC) {
ldout(cct, 5) << "defer to " << who << dendl;
ceph_assert(who < elector->get_my_rank());
} else {
ldout(cct, 5) << "defer to " << who << ", disallowed_leaders=" << elector->get_disallowed_leaders() << dendl;
ceph_assert(!elector->get_disallowed_leaders().count(who));
}
if (electing_me) {
// drop out
acked_me.clear();
electing_me = false;
}
// ack them
leader_acked = who;
elector->_defer_to(who);
}
void ElectionLogic::end_election_period()
{
ldout(cct, 5) << "election period ended" << dendl;
// did i win?
if (electing_me &&
acked_me.size() > (elector->paxos_size() / 2)) {
// i win
declare_victory();
} else {
// whoever i deferred to didn't declare victory quickly enough.
if (elector->ever_participated())
start();
else
elector->reset_election();
}
}
void ElectionLogic::declare_victory()
{
ldout(cct, 5) << "I win! acked_me=" << acked_me << dendl;
last_election_winner = elector->get_my_rank();
last_voted_for = last_election_winner;
clear_live_election_state();
set<int> new_quorum;
new_quorum.swap(acked_me);
ceph_assert(epoch % 2 == 1); // election
bump_epoch(epoch+1); // is over!
elector->message_victory(new_quorum);
}
bool ElectionLogic::propose_classic_prefix(int from, epoch_t mepoch)
{
if (mepoch > epoch) {
bump_epoch(mepoch);
} else if (mepoch < epoch) {
// got an "old" propose,
if (epoch % 2 == 0 && // in a non-election cycle
!elector->is_current_member(from)) { // from someone outside the quorum
// a mon just started up, call a new election so they can rejoin!
ldout(cct, 5) << " got propose from old epoch, "
<< from << " must have just started" << dendl;
// we may be active; make sure we reset things in the monitor appropriately.
elector->trigger_new_election();
} else {
ldout(cct, 5) << " ignoring old propose" << dendl;
}
return true;
}
return false;
}
void ElectionLogic::receive_propose(int from, epoch_t mepoch,
const ConnectionTracker *ct)
{
ldout(cct, 20) << __func__ << " from " << from << dendl;
if (from == elector->get_my_rank()) {
lderr(cct) << "I got a propose from my own rank, hopefully this is startup weirdness,dropping" << dendl;
return;
}
switch (strategy) {
case CLASSIC:
propose_classic_handler(from, mepoch);
break;
case DISALLOW:
propose_disallow_handler(from, mepoch);
break;
case CONNECTIVITY:
propose_connectivity_handler(from, mepoch, ct);
break;
default:
ceph_assert(0 == "how did election strategy become an invalid value?");
}
}
void ElectionLogic::propose_disallow_handler(int from, epoch_t mepoch)
{
if (propose_classic_prefix(from, mepoch)) {
return;
}
const set<int>& disallowed_leaders = elector->get_disallowed_leaders();
int my_rank = elector->get_my_rank();
bool me_disallowed = disallowed_leaders.count(my_rank);
bool from_disallowed = disallowed_leaders.count(from);
bool my_win = !me_disallowed && // we are allowed to lead
(my_rank < from || from_disallowed); // we are a better choice than them
bool their_win = !from_disallowed && // they are allowed to lead
(my_rank > from || me_disallowed) && // they are a better choice than us
(leader_acked < 0 || leader_acked >= from); // they are a better choice than our previously-acked choice
if (my_win) {
// i would win over them.
if (leader_acked >= 0) { // we already acked someone
ceph_assert(leader_acked < from || from_disallowed); // and they still win, of course
ldout(cct, 5) << "no, we already acked " << leader_acked << dendl;
} else {
// wait, i should win!
if (!electing_me) {
elector->trigger_new_election();
}
}
} else {
// they would win over me
if (their_win) {
defer(from);
} else {
// ignore them!
ldout(cct, 5) << "no, we already acked " << leader_acked << dendl;
}
}
}
void ElectionLogic::propose_classic_handler(int from, epoch_t mepoch)
{
if (propose_classic_prefix(from, mepoch)) {
return;
}
if (elector->get_my_rank() < from) {
// i would win over them.
if (leader_acked >= 0) { // we already acked someone
ceph_assert(leader_acked < from); // and they still win, of course
ldout(cct, 5) << "no, we already acked " << leader_acked << dendl;
} else {
// wait, i should win!
if (!electing_me) {
elector->trigger_new_election();
}
}
} else {
// they would win over me
if (leader_acked < 0 || // haven't acked anyone yet, or
leader_acked > from || // they would win over who you did ack, or
leader_acked == from) { // this is the guy we're already deferring to
defer(from);
} else {
// ignore them!
ldout(cct, 5) << "no, we already acked " << leader_acked << dendl;
}
}
}
double ElectionLogic::connectivity_election_score(int rank)
{
ldout(cct, 30) << __func__ << " of " << rank << dendl;
if (elector->get_disallowed_leaders().count(rank)) {
return -1;
}
double score;
int liveness;
if (stable_peer_tracker) {
ldout(cct, 30) << "stable_peer_tracker exists so using that ..." << dendl;
stable_peer_tracker->get_total_connection_score(rank, &score, &liveness);
} else {
ldout(cct, 30) << "stable_peer_tracker does not exists, using peer_tracker ..." << dendl;
peer_tracker->get_total_connection_score(rank, &score, &liveness);
}
return score;
}
void ElectionLogic::propose_connectivity_handler(int from, epoch_t mepoch,
const ConnectionTracker *ct)
{
ldout(cct, 10) << __func__ << " from " << from << " mepoch: "
<< mepoch << " epoch: " << epoch << dendl;
ldout(cct, 30) << "last_election_winner: " << last_election_winner << dendl;
if ((epoch % 2 == 0) &&
last_election_winner != elector->get_my_rank() &&
!elector->is_current_member(from)) {
// To prevent election flapping, peons ignore proposals from out-of-quorum
// peers unless their vote would materially change from the last election
ldout(cct, 30) << "Lets see if this out-of-quorum peer is worth it " << dendl;
int best_scorer = 0;
double best_score = 0;
double last_voted_for_score = 0;
ldout(cct, 30) << "elector->paxos_size(): " << elector->paxos_size() << dendl;
for (unsigned i = 0; i < elector->paxos_size(); ++i) {
double score = connectivity_election_score(i);
if (score > best_score) {
best_scorer = i;
best_score = score;
}
if (last_voted_for >= 0 && i == static_cast<unsigned>(last_voted_for)) {
last_voted_for_score = score;
}
}
ldout(cct, 30) << "best_scorer: " << best_scorer << " best_score: " << best_score
<< " last_voted_for: " << last_voted_for << " last_voted_for_score: "
<< last_voted_for_score << dendl;
if (best_scorer == last_voted_for ||
(best_score - last_voted_for_score < ignore_propose_margin)) {
// drop this message; it won't change our vote so we defer to leader
ldout(cct, 30) << "drop this message; it won't change our vote so we defer to leader " << dendl;
return;
}
}
if (mepoch > epoch) {
ldout(cct, 20) << "mepoch > epoch" << dendl;
connectivity_bump_epoch_in_election(mepoch);
} else if (mepoch < epoch) {
// got an "old" propose,
if (epoch % 2 == 0 && // in a non-election cycle
!elector->is_current_member(from)) { // from someone outside the quorum
// a mon just started up, call a new election so they can rejoin!
ldout(cct, 5) << " got propose from old epoch, "
<< from << " must have just started" << dendl;
ldout(cct, 10) << "triggering new election" << dendl;
// we may be active; make sure we reset things in the monitor appropriately.
elector->trigger_new_election();
} else {
ldout(cct, 5) << " ignoring old propose" << dendl;
}
return;
}
int my_rank = elector->get_my_rank();
double my_score = connectivity_election_score(my_rank);
double from_score = connectivity_election_score(from);
double leader_score = -1;
if (leader_acked >= 0) {
leader_score = connectivity_election_score(leader_acked);
}
ldout(cct, 20) << "propose from rank=" << from << ", tracker: "
<< (stable_peer_tracker ? *stable_peer_tracker : *peer_tracker) << dendl;
ldout(cct, 10) << "propose from rank=" << from << ",from_score=" << from_score
<< "; my score=" << my_score
<< "; currently acked " << leader_acked
<< ",leader_score=" << leader_score << dendl;
bool my_win = (my_score >= 0) && // My score is non-zero; I am allowed to lead
((my_rank < from && my_score >= from_score) || // We have same scores and I have lower rank, or
(my_score > from_score)); // my score is higher
bool their_win = (from_score >= 0) && // Their score is non-zero; they're allowed to lead, AND
((from < my_rank && from_score >= my_score) || // Either they have lower rank and same score, or
(from_score > my_score)) && // their score is higher, AND
((from <= leader_acked && from_score >= leader_score) || // same conditions compared to leader, or IS leader
(from_score > leader_score));
if (my_win) {
ldout(cct, 10) << " conditionally I win" << dendl;
// i would win over them.
if (leader_acked >= 0) { // we already acked someone
ceph_assert(leader_score >= from_score); // and they still win, of course
ldout(cct, 5) << "no, we already acked " << leader_acked << dendl;
} else {
// wait, i should win!
if (!electing_me) {
ldout(cct, 10) << " wait, i should win! triggering new election ..." << dendl;
elector->trigger_new_election();
}
}
} else {
ldout(cct, 10) << " conditionally they win" << dendl;
// they would win over me
if (their_win || from == leader_acked) {
if (leader_acked >= 0 && from != leader_acked) {
// we have to make sure our acked leader will ALSO defer to them, or else
// we can't, to maintain guarantees!
ldout(cct, 10) << " make sure acked leader defer to: " << from << dendl;
double leader_from_score;
int leader_from_liveness;
leader_peer_tracker->
get_total_connection_score(from, &leader_from_score,
&leader_from_liveness);
double leader_leader_score;
int leader_leader_liveness;
leader_peer_tracker->
get_total_connection_score(leader_acked, &leader_leader_score,
&leader_leader_liveness);
if ((from < leader_acked && leader_from_score >= leader_leader_score) ||
(leader_from_score > leader_leader_score)) {
ldout(cct, 10) << "defering to " << from << dendl;
defer(from);
leader_peer_tracker.reset(new ConnectionTracker(*ct));
} else { // we can't defer to them *this* round even though they should win...
double cur_leader_score, cur_from_score;
int cur_leader_live, cur_from_live;
peer_tracker->get_total_connection_score(leader_acked, &cur_leader_score, &cur_leader_live);
peer_tracker->get_total_connection_score(from, &cur_from_score, &cur_from_live);
if ((from < leader_acked && cur_from_score >= cur_leader_score) ||
(cur_from_score > cur_leader_score)) {
ldout(cct, 5) << "Bumping epoch and starting new election; acked "
<< leader_acked << " should defer to " << from
<< " but there is score disagreement!" << dendl;
bump_epoch(epoch+1);
start();
} else {
ldout(cct, 5) << "no, we already acked " << leader_acked
<< " and it won't defer to " << from
<< " despite better round scores" << dendl;
}
}
} else {
ldout(cct, 10) << "defering to " << from << dendl;
defer(from);
leader_peer_tracker.reset(new ConnectionTracker(*ct));
}
} else {
// ignore them!
ldout(cct, 5) << "no, we already acked " << leader_acked << " with score >=" << from_score << dendl;
}
}
}
void ElectionLogic::receive_ack(int from, epoch_t from_epoch)
{
ceph_assert(from_epoch % 2 == 1); // sender in an election epoch
if (from_epoch > epoch) {
ldout(cct, 5) << "woah, that's a newer epoch, i must have rebooted. bumping and re-starting!" << dendl;
bump_epoch(from_epoch);
start();
return;
}
// is that _everyone_?
if (electing_me) {
acked_me.insert(from);
if (acked_me.size() == elector->paxos_size()) {
// if yes, shortcut to election finish
declare_victory();
}
} else {
// ignore, i'm deferring already.
ceph_assert(leader_acked >= 0);
}
}
bool ElectionLogic::victory_makes_sense(int from)
{
bool makes_sense = false;
switch (strategy) {
case CLASSIC:
makes_sense = (from < elector->get_my_rank());
break;
case DISALLOW:
makes_sense = (from < elector->get_my_rank()) ||
elector->get_disallowed_leaders().count(elector->get_my_rank());
break;
case CONNECTIVITY:
double my_score, leader_score;
my_score = connectivity_election_score(elector->get_my_rank());
leader_score = connectivity_election_score(from);
ldout(cct, 5) << "victory from " << from << " makes sense? lscore:"
<< leader_score
<< "; my score:" << my_score << dendl;
makes_sense = (leader_score >= my_score);
break;
default:
ceph_assert(0 == "how did you get a nonsense election strategy assigned?");
}
return makes_sense;
}
bool ElectionLogic::receive_victory_claim(int from, epoch_t from_epoch)
{
bool election_okay = victory_makes_sense(from);
last_election_winner = from;
last_voted_for = leader_acked;
clear_live_election_state();
if (!election_okay) {
ceph_assert(strategy == CONNECTIVITY);
ldout(cct, 1) << "I should have been elected over this leader; bumping and restarting!" << dendl;
bump_epoch(from_epoch);
start();
return false;
}
// i should have seen this election if i'm getting the victory.
if (from_epoch != epoch + 1) {
ldout(cct, 5) << "woah, that's a funny epoch, i must have rebooted. bumping and re-starting!" << dendl;
bump_epoch(from_epoch);
start();
return false;
}
bump_epoch(from_epoch);
// they win
return true;
}
|