1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
18 #include "common/Timer.h"
19 #include "MonitorDBStore.h"
20 #include "messages/MMonElection.h"
22 #include "common/config.h"
23 #include "include/assert.h"
25 #define dout_subsys ceph_subsys_mon
27 #define dout_prefix _prefix(_dout, mon, epoch)
28 static ostream
& _prefix(std::ostream
*_dout
, Monitor
*mon
, epoch_t epoch
) {
29 return *_dout
<< "mon." << mon
->name
<< "@" << mon
->rank
30 << "(" << mon
->get_state_name()
31 << ").elector(" << epoch
<< ") ";
37 epoch
= mon
->store
->get(Monitor::MONITOR_NAME
, "election_epoch");
40 dout(1) << "init, last seen epoch " << epoch
<< dendl
;
43 void Elector::shutdown()
48 void Elector::bump_epoch(epoch_t e
)
50 dout(10) << "bump_epoch " << epoch
<< " to " << e
<< dendl
;
53 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
54 t
->put(Monitor::MONITOR_NAME
, "election_epoch", epoch
);
55 mon
->store
->apply_transaction(t
);
59 // clear up some state
68 dout(0) << "not starting new election -- not participating" << dendl
;
71 dout(5) << "start -- can i be leader?" << dendl
;
76 // start by trying to elect me
78 bump_epoch(epoch
+1); // odd == election cycle
80 // do a trivial db write just to ensure it is writeable.
81 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
82 t
->put(Monitor::MONITOR_NAME
, "election_writeable_test", rand());
83 int r
= mon
->store
->apply_transaction(t
);
86 start_stamp
= ceph_clock_now();
88 acked_me
[mon
->rank
].cluster_features
= CEPH_FEATURES_ALL
;
89 acked_me
[mon
->rank
].mon_features
= ceph::features::mon::get_supported();
92 // bcast to everyone else
93 for (unsigned i
=0; i
<mon
->monmap
->size(); ++i
) {
94 if ((int)i
== mon
->rank
) continue;
96 new MMonElection(MMonElection::OP_PROPOSE
, epoch
, mon
->monmap
);
97 m
->mon_features
= ceph::features::mon::get_supported();
98 mon
->messenger
->send_message(m
, mon
->monmap
->get_inst(i
));
104 void Elector::defer(int who
)
106 dout(5) << "defer to " << who
<< dendl
;
116 ack_stamp
= ceph_clock_now();
117 MMonElection
*m
= new MMonElection(MMonElection::OP_ACK
, epoch
, mon
->monmap
);
118 m
->mon_features
= ceph::features::mon::get_supported();
119 m
->sharing_bl
= mon
->get_supported_commands_bl();
120 mon
->messenger
->send_message(m
, mon
->monmap
->get_inst(who
));
123 reset_timer(1.0); // give the leader some extra time to declare victory
127 void Elector::reset_timer(double plus
)
132 * This class is used as the callback when the expire_event timer fires up.
134 * If the expire_event is fired, then it means that we had an election going,
135 * either started by us or by some other participant, but it took too long,
138 * When the election expires, we will check if we were the ones who won, and
139 * if so we will declare victory. If that is not the case, then we assume
140 * that the one we defered to didn't declare victory quickly enough (in fact,
141 * as far as we know, we may even be dead); so, just propose ourselves as the
144 expire_event
= new C_MonContext(mon
, [this](int) {
147 mon
->timer
.add_event_after(g_conf
->mon_election_timeout
+ plus
,
152 void Elector::cancel_timer()
155 mon
->timer
.cancel_event(expire_event
);
160 void Elector::expire()
162 dout(5) << "election timer expired" << dendl
;
166 acked_me
.size() > (unsigned)(mon
->monmap
->size() / 2)) {
170 // whoever i deferred to didn't declare victory quickly enough.
171 if (mon
->has_ever_joined
)
179 void Elector::victory()
184 uint64_t cluster_features
= CEPH_FEATURES_ALL
;
185 mon_feature_t mon_features
= ceph::features::mon::get_supported();
187 for (map
<int, elector_features_t
>::iterator p
= acked_me
.begin();
190 quorum
.insert(p
->first
);
191 cluster_features
&= p
->second
.cluster_features
;
192 mon_features
&= p
->second
.mon_features
;
197 assert(epoch
% 2 == 1); // election
198 bump_epoch(epoch
+1); // is over!
200 // decide my supported commands for peons to advertise
201 const bufferlist
*cmds_bl
= NULL
;
202 const MonCommand
*cmds
;
204 mon
->get_locally_supported_monitor_commands(&cmds
, &cmdsize
);
205 cmds_bl
= &mon
->get_supported_commands_bl();
208 for (set
<int>::iterator p
= quorum
.begin();
211 if (*p
== mon
->rank
) continue;
212 MMonElection
*m
= new MMonElection(MMonElection::OP_VICTORY
, epoch
, mon
->monmap
);
214 m
->quorum_features
= cluster_features
;
215 m
->mon_features
= mon_features
;
216 m
->sharing_bl
= *cmds_bl
;
217 mon
->messenger
->send_message(m
, mon
->monmap
->get_inst(*p
));
221 mon
->win_election(epoch
, quorum
,
222 cluster_features
, mon_features
,
227 void Elector::handle_propose(MonOpRequestRef op
)
229 op
->mark_event("elector:handle_propose");
230 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
231 dout(5) << "handle_propose from " << m
->get_source() << dendl
;
232 int from
= m
->get_source().num();
234 assert(m
->epoch
% 2 == 1); // election
235 uint64_t required_features
= mon
->get_required_features();
236 mon_feature_t required_mon_features
= mon
->get_required_mon_features();
238 dout(10) << __func__
<< " required features " << required_features
239 << " " << required_mon_features
240 << ", peer features " << m
->get_connection()->get_features()
241 << " " << m
->mon_features
244 if ((required_features
^ m
->get_connection()->get_features()) &
246 dout(5) << " ignoring propose from mon" << from
247 << " without required features" << dendl
;
250 } else if (!m
->mon_features
.contains_all(required_mon_features
)) {
251 // all the features in 'required_mon_features' not in 'm->mon_features'
252 mon_feature_t missing
= required_mon_features
.diff(m
->mon_features
);
253 dout(5) << " ignoring propose from mon." << from
254 << " without required mon_features " << missing
257 } else if (m
->epoch
> epoch
) {
258 bump_epoch(m
->epoch
);
259 } else if (m
->epoch
< epoch
) {
260 // got an "old" propose,
261 if (epoch
% 2 == 0 && // in a non-election cycle
262 mon
->quorum
.count(from
) == 0) { // from someone outside the quorum
263 // a mon just started up, call a new election so they can rejoin!
264 dout(5) << " got propose from old epoch, quorum is " << mon
->quorum
265 << ", " << m
->get_source() << " must have just started" << dendl
;
266 // we may be active; make sure we reset things in the monitor appropriately.
267 mon
->start_election();
269 dout(5) << " ignoring old propose" << dendl
;
274 if (mon
->rank
< from
) {
275 // i would win over them.
276 if (leader_acked
>= 0) { // we already acked someone
277 assert(leader_acked
< from
); // and they still win, of course
278 dout(5) << "no, we already acked " << leader_acked
<< dendl
;
280 // wait, i should win!
282 mon
->start_election();
286 // they would win over me
287 if (leader_acked
< 0 || // haven't acked anyone yet, or
288 leader_acked
> from
|| // they would win over who you did ack, or
289 leader_acked
== from
) { // this is the guy we're already deferring to
293 dout(5) << "no, we already acked " << leader_acked
<< dendl
;
298 void Elector::handle_ack(MonOpRequestRef op
)
300 op
->mark_event("elector:handle_ack");
301 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
302 dout(5) << "handle_ack from " << m
->get_source() << dendl
;
303 int from
= m
->get_source().num();
305 assert(m
->epoch
% 2 == 1); // election
306 if (m
->epoch
> epoch
) {
307 dout(5) << "woah, that's a newer epoch, i must have rebooted. bumping and re-starting!" << dendl
;
308 bump_epoch(m
->epoch
);
312 assert(m
->epoch
== epoch
);
313 uint64_t required_features
= mon
->get_required_features();
314 if ((required_features
^ m
->get_connection()->get_features()) &
316 dout(5) << " ignoring ack from mon" << from
317 << " without required features" << dendl
;
321 mon_feature_t required_mon_features
= mon
->get_required_mon_features();
322 if (!m
->mon_features
.contains_all(required_mon_features
)) {
323 mon_feature_t missing
= required_mon_features
.diff(m
->mon_features
);
324 dout(5) << " ignoring ack from mon." << from
325 << " without required mon_features " << missing
332 acked_me
[from
].cluster_features
= m
->get_connection()->get_features();
333 acked_me
[from
].mon_features
= m
->mon_features
;
334 dout(5) << " so far i have {";
335 for (map
<int, elector_features_t
>::const_iterator p
= acked_me
.begin();
338 if (p
!= acked_me
.begin())
340 *_dout
<< " mon." << p
->first
<< ":"
341 << " features " << p
->second
.cluster_features
342 << " " << p
->second
.mon_features
;
344 *_dout
<< " }" << dendl
;
346 // is that _everyone_?
347 if (acked_me
.size() == mon
->monmap
->size()) {
348 // if yes, shortcut to election finish
352 // ignore, i'm deferring already.
353 assert(leader_acked
>= 0);
358 void Elector::handle_victory(MonOpRequestRef op
)
360 op
->mark_event("elector:handle_victory");
361 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
362 dout(5) << "handle_victory from " << m
->get_source()
363 << " quorum_features " << m
->quorum_features
364 << " " << m
->mon_features
366 int from
= m
->get_source().num();
368 assert(from
< mon
->rank
);
369 assert(m
->epoch
% 2 == 0);
373 // i should have seen this election if i'm getting the victory.
374 if (m
->epoch
!= epoch
+ 1) {
375 dout(5) << "woah, that's a funny epoch, i must have rebooted. bumping and re-starting!" << dendl
;
376 bump_epoch(m
->epoch
);
381 bump_epoch(m
->epoch
);
384 mon
->lose_election(epoch
, m
->quorum
, from
,
385 m
->quorum_features
, m
->mon_features
);
390 // stash leader's commands
391 assert(m
->sharing_bl
.length());
392 MonCommand
*new_cmds
;
394 bufferlist::iterator bi
= m
->sharing_bl
.begin();
395 MonCommand::decode_array(&new_cmds
, &cmdsize
, bi
);
396 mon
->set_leader_supported_commands(new_cmds
, cmdsize
);
399 void Elector::nak_old_peer(MonOpRequestRef op
)
401 op
->mark_event("elector:nak_old_peer");
402 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
403 uint64_t supported_features
= m
->get_connection()->get_features();
404 uint64_t required_features
= mon
->get_required_features();
405 mon_feature_t required_mon_features
= mon
->get_required_mon_features();
406 dout(10) << "sending nak to peer " << m
->get_source()
407 << " that only supports " << supported_features
408 << " " << m
->mon_features
409 << " of the required " << required_features
410 << " " << required_mon_features
413 MMonElection
*reply
= new MMonElection(MMonElection::OP_NAK
, m
->epoch
,
415 reply
->quorum_features
= required_features
;
416 reply
->mon_features
= required_mon_features
;
417 mon
->features
.encode(reply
->sharing_bl
);
418 m
->get_connection()->send_message(reply
);
421 void Elector::handle_nak(MonOpRequestRef op
)
423 op
->mark_event("elector:handle_nak");
424 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
425 dout(1) << "handle_nak from " << m
->get_source()
426 << " quorum_features " << m
->quorum_features
427 << " " << m
->mon_features
431 bufferlist::iterator bi
= m
->sharing_bl
.begin();
433 CompatSet diff
= Monitor::get_supported_features().unsupported(other
);
435 mon_feature_t mon_supported
= ceph::features::mon::get_supported();
436 // all features in 'm->mon_features' not in 'mon_supported'
437 mon_feature_t mon_diff
= m
->mon_features
.diff(mon_supported
);
439 derr
<< "Shutting down because I do not support required monitor features: { "
440 << diff
<< " } " << mon_diff
<< dendl
;
446 void Elector::dispatch(MonOpRequestRef op
)
448 op
->mark_event("elector:dispatch");
449 assert(op
->is_type_election());
451 switch (op
->get_req()->get_type()) {
453 case MSG_MON_ELECTION
:
455 if (!participating
) {
458 if (op
->get_req()->get_source().num() >= mon
->monmap
->size()) {
459 dout(5) << " ignoring bogus election message with bad mon rank "
460 << op
->get_req()->get_source() << dendl
;
464 MMonElection
*em
= static_cast<MMonElection
*>(op
->get_req());
466 // assume an old message encoding would have matched
467 if (em
->fsid
!= mon
->monmap
->fsid
) {
468 dout(0) << " ignoring election msg fsid "
469 << em
->fsid
<< " != " << mon
->monmap
->fsid
<< dendl
;
473 if (!mon
->monmap
->contains(em
->get_source_addr())) {
474 dout(1) << "discarding election message: " << em
->get_source_addr()
475 << " not in my monmap " << *mon
->monmap
<< dendl
;
480 peermap
.decode(em
->monmap_bl
);
481 if (peermap
.epoch
> mon
->monmap
->epoch
) {
482 dout(0) << em
->get_source_inst() << " has newer monmap epoch " << peermap
.epoch
483 << " > my epoch " << mon
->monmap
->epoch
486 mon
->monmap
->decode(em
->monmap_bl
);
487 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
488 t
->put("monmap", mon
->monmap
->epoch
, em
->monmap_bl
);
489 t
->put("monmap", "last_committed", mon
->monmap
->epoch
);
490 mon
->store
->apply_transaction(t
);
491 //mon->monmon()->paxos->stash_latest(mon->monmap->epoch, em->monmap_bl);
496 if (peermap
.epoch
< mon
->monmap
->epoch
) {
497 dout(0) << em
->get_source_inst() << " has older monmap epoch " << peermap
.epoch
498 << " < my epoch " << mon
->monmap
->epoch
503 case MMonElection::OP_PROPOSE
:
508 if (em
->epoch
< epoch
) {
509 dout(5) << "old epoch, dropping" << dendl
;
514 case MMonElection::OP_ACK
:
517 case MMonElection::OP_VICTORY
:
520 case MMonElection::OP_NAK
:
534 void Elector::start_participating()
536 if (!participating
) {
537 participating
= true;