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");
39 dout(1) << "init, first boot, initializing epoch at 1 " << dendl
;
41 } else if (epoch
% 2) {
42 dout(1) << "init, last seen epoch " << epoch
43 << ", mid-election, bumping" << dendl
;
45 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
46 t
->put(Monitor::MONITOR_NAME
, "election_epoch", epoch
);
47 mon
->store
->apply_transaction(t
);
49 dout(1) << "init, last seen epoch " << epoch
<< dendl
;
53 void Elector::shutdown()
58 void Elector::bump_epoch(epoch_t e
)
60 dout(10) << "bump_epoch " << epoch
<< " to " << e
<< dendl
;
63 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
64 t
->put(Monitor::MONITOR_NAME
, "election_epoch", epoch
);
65 mon
->store
->apply_transaction(t
);
69 // clear up some state
78 dout(0) << "not starting new election -- not participating" << dendl
;
81 dout(5) << "start -- can i be leader?" << dendl
;
86 // start by trying to elect me
88 bump_epoch(epoch
+1); // odd == election cycle
90 // do a trivial db write just to ensure it is writeable.
91 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
92 t
->put(Monitor::MONITOR_NAME
, "election_writeable_test", rand());
93 int r
= mon
->store
->apply_transaction(t
);
96 start_stamp
= ceph_clock_now();
98 acked_me
[mon
->rank
].cluster_features
= CEPH_FEATURES_ALL
;
99 acked_me
[mon
->rank
].mon_features
= ceph::features::mon::get_supported();
100 mon
->collect_metadata(&acked_me
[mon
->rank
].metadata
);
103 // bcast to everyone else
104 for (unsigned i
=0; i
<mon
->monmap
->size(); ++i
) {
105 if ((int)i
== mon
->rank
) continue;
107 new MMonElection(MMonElection::OP_PROPOSE
, epoch
, mon
->monmap
);
108 m
->mon_features
= ceph::features::mon::get_supported();
109 mon
->messenger
->send_message(m
, mon
->monmap
->get_inst(i
));
115 void Elector::defer(int who
)
117 dout(5) << "defer to " << who
<< dendl
;
127 ack_stamp
= ceph_clock_now();
128 MMonElection
*m
= new MMonElection(MMonElection::OP_ACK
, epoch
, mon
->monmap
);
129 m
->mon_features
= ceph::features::mon::get_supported();
130 mon
->collect_metadata(&m
->metadata
);
132 // This field is unused completely in luminous, but jewel uses it to
133 // determine whether we are a dumpling mon due to some crufty old
134 // code. It only needs to see this buffer non-empty, so put
135 // something useless there.
136 m
->sharing_bl
= mon
->get_local_commands_bl(mon
->get_required_mon_features());
138 mon
->messenger
->send_message(m
, mon
->monmap
->get_inst(who
));
141 reset_timer(1.0); // give the leader some extra time to declare victory
145 void Elector::reset_timer(double plus
)
150 * This class is used as the callback when the expire_event timer fires up.
152 * If the expire_event is fired, then it means that we had an election going,
153 * either started by us or by some other participant, but it took too long,
156 * When the election expires, we will check if we were the ones who won, and
157 * if so we will declare victory. If that is not the case, then we assume
158 * that the one we defered to didn't declare victory quickly enough (in fact,
159 * as far as we know, we may even be dead); so, just propose ourselves as the
162 expire_event
= new C_MonContext(mon
, [this](int) {
165 mon
->timer
.add_event_after(g_conf
->mon_election_timeout
+ plus
,
170 void Elector::cancel_timer()
173 mon
->timer
.cancel_event(expire_event
);
178 void Elector::expire()
180 dout(5) << "election timer expired" << dendl
;
184 acked_me
.size() > (unsigned)(mon
->monmap
->size() / 2)) {
188 // whoever i deferred to didn't declare victory quickly enough.
189 if (mon
->has_ever_joined
)
197 void Elector::victory()
202 uint64_t cluster_features
= CEPH_FEATURES_ALL
;
203 mon_feature_t mon_features
= ceph::features::mon::get_supported();
205 map
<int,Metadata
> metadata
;
206 for (map
<int, elector_info_t
>::iterator p
= acked_me
.begin();
209 quorum
.insert(p
->first
);
210 cluster_features
&= p
->second
.cluster_features
;
211 mon_features
&= p
->second
.mon_features
;
212 metadata
[p
->first
] = p
->second
.metadata
;
217 assert(epoch
% 2 == 1); // election
218 bump_epoch(epoch
+1); // is over!
221 for (set
<int>::iterator p
= quorum
.begin();
224 if (*p
== mon
->rank
) continue;
225 MMonElection
*m
= new MMonElection(MMonElection::OP_VICTORY
, epoch
,
228 m
->quorum_features
= cluster_features
;
229 m
->mon_features
= mon_features
;
230 m
->sharing_bl
= mon
->get_local_commands_bl(mon_features
);
231 mon
->messenger
->send_message(m
, mon
->monmap
->get_inst(*p
));
235 mon
->win_election(epoch
, quorum
,
236 cluster_features
, mon_features
, metadata
);
240 void Elector::handle_propose(MonOpRequestRef op
)
242 op
->mark_event("elector:handle_propose");
243 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
244 dout(5) << "handle_propose from " << m
->get_source() << dendl
;
245 int from
= m
->get_source().num();
247 assert(m
->epoch
% 2 == 1); // election
248 uint64_t required_features
= mon
->get_required_features();
249 mon_feature_t required_mon_features
= mon
->get_required_mon_features();
251 dout(10) << __func__
<< " required features " << required_features
252 << " " << required_mon_features
253 << ", peer features " << m
->get_connection()->get_features()
254 << " " << m
->mon_features
257 if ((required_features
^ m
->get_connection()->get_features()) &
259 dout(5) << " ignoring propose from mon" << from
260 << " without required features" << dendl
;
263 } else if (!m
->mon_features
.contains_all(required_mon_features
)) {
264 // all the features in 'required_mon_features' not in 'm->mon_features'
265 mon_feature_t missing
= required_mon_features
.diff(m
->mon_features
);
266 dout(5) << " ignoring propose from mon." << from
267 << " without required mon_features " << missing
270 } else if (m
->epoch
> epoch
) {
271 bump_epoch(m
->epoch
);
272 } else if (m
->epoch
< epoch
) {
273 // got an "old" propose,
274 if (epoch
% 2 == 0 && // in a non-election cycle
275 mon
->quorum
.count(from
) == 0) { // from someone outside the quorum
276 // a mon just started up, call a new election so they can rejoin!
277 dout(5) << " got propose from old epoch, quorum is " << mon
->quorum
278 << ", " << m
->get_source() << " must have just started" << dendl
;
279 // we may be active; make sure we reset things in the monitor appropriately.
280 mon
->start_election();
282 dout(5) << " ignoring old propose" << dendl
;
287 if (mon
->rank
< from
) {
288 // i would win over them.
289 if (leader_acked
>= 0) { // we already acked someone
290 assert(leader_acked
< from
); // and they still win, of course
291 dout(5) << "no, we already acked " << leader_acked
<< dendl
;
293 // wait, i should win!
295 mon
->start_election();
299 // they would win over me
300 if (leader_acked
< 0 || // haven't acked anyone yet, or
301 leader_acked
> from
|| // they would win over who you did ack, or
302 leader_acked
== from
) { // this is the guy we're already deferring to
306 dout(5) << "no, we already acked " << leader_acked
<< dendl
;
311 void Elector::handle_ack(MonOpRequestRef op
)
313 op
->mark_event("elector:handle_ack");
314 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
315 dout(5) << "handle_ack from " << m
->get_source() << dendl
;
316 int from
= m
->get_source().num();
318 assert(m
->epoch
% 2 == 1); // election
319 if (m
->epoch
> epoch
) {
320 dout(5) << "woah, that's a newer epoch, i must have rebooted. bumping and re-starting!" << dendl
;
321 bump_epoch(m
->epoch
);
325 assert(m
->epoch
== epoch
);
326 uint64_t required_features
= mon
->get_required_features();
327 if ((required_features
^ m
->get_connection()->get_features()) &
329 dout(5) << " ignoring ack from mon" << from
330 << " without required features" << dendl
;
334 mon_feature_t required_mon_features
= mon
->get_required_mon_features();
335 if (!m
->mon_features
.contains_all(required_mon_features
)) {
336 mon_feature_t missing
= required_mon_features
.diff(m
->mon_features
);
337 dout(5) << " ignoring ack from mon." << from
338 << " without required mon_features " << missing
345 acked_me
[from
].cluster_features
= m
->get_connection()->get_features();
346 acked_me
[from
].mon_features
= m
->mon_features
;
347 acked_me
[from
].metadata
= m
->metadata
;
348 dout(5) << " so far i have {";
349 for (map
<int, elector_info_t
>::const_iterator p
= acked_me
.begin();
352 if (p
!= acked_me
.begin())
354 *_dout
<< " mon." << p
->first
<< ":"
355 << " features " << p
->second
.cluster_features
356 << " " << p
->second
.mon_features
;
358 *_dout
<< " }" << dendl
;
360 // is that _everyone_?
361 if (acked_me
.size() == mon
->monmap
->size()) {
362 // if yes, shortcut to election finish
366 // ignore, i'm deferring already.
367 assert(leader_acked
>= 0);
372 void Elector::handle_victory(MonOpRequestRef op
)
374 op
->mark_event("elector:handle_victory");
375 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
376 dout(5) << "handle_victory from " << m
->get_source()
377 << " quorum_features " << m
->quorum_features
378 << " " << m
->mon_features
380 int from
= m
->get_source().num();
382 assert(from
< mon
->rank
);
383 assert(m
->epoch
% 2 == 0);
387 // i should have seen this election if i'm getting the victory.
388 if (m
->epoch
!= epoch
+ 1) {
389 dout(5) << "woah, that's a funny epoch, i must have rebooted. bumping and re-starting!" << dendl
;
390 bump_epoch(m
->epoch
);
395 bump_epoch(m
->epoch
);
398 mon
->lose_election(epoch
, m
->quorum
, from
,
399 m
->quorum_features
, m
->mon_features
);
404 // stash leader's commands
405 assert(m
->sharing_bl
.length());
406 vector
<MonCommand
> new_cmds
;
407 bufferlist::iterator bi
= m
->sharing_bl
.begin();
408 MonCommand::decode_vector(new_cmds
, bi
);
409 mon
->set_leader_commands(new_cmds
);
412 void Elector::nak_old_peer(MonOpRequestRef op
)
414 op
->mark_event("elector:nak_old_peer");
415 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
416 uint64_t supported_features
= m
->get_connection()->get_features();
417 uint64_t required_features
= mon
->get_required_features();
418 mon_feature_t required_mon_features
= mon
->get_required_mon_features();
419 dout(10) << "sending nak to peer " << m
->get_source()
420 << " that only supports " << supported_features
421 << " " << m
->mon_features
422 << " of the required " << required_features
423 << " " << required_mon_features
426 MMonElection
*reply
= new MMonElection(MMonElection::OP_NAK
, m
->epoch
,
428 reply
->quorum_features
= required_features
;
429 reply
->mon_features
= required_mon_features
;
430 mon
->features
.encode(reply
->sharing_bl
);
431 m
->get_connection()->send_message(reply
);
434 void Elector::handle_nak(MonOpRequestRef op
)
436 op
->mark_event("elector:handle_nak");
437 MMonElection
*m
= static_cast<MMonElection
*>(op
->get_req());
438 dout(1) << "handle_nak from " << m
->get_source()
439 << " quorum_features " << m
->quorum_features
440 << " " << m
->mon_features
444 bufferlist::iterator bi
= m
->sharing_bl
.begin();
446 CompatSet diff
= Monitor::get_supported_features().unsupported(other
);
448 mon_feature_t mon_supported
= ceph::features::mon::get_supported();
449 // all features in 'm->mon_features' not in 'mon_supported'
450 mon_feature_t mon_diff
= m
->mon_features
.diff(mon_supported
);
452 derr
<< "Shutting down because I do not support required monitor features: { "
453 << diff
<< " } " << mon_diff
<< dendl
;
459 void Elector::dispatch(MonOpRequestRef op
)
461 op
->mark_event("elector:dispatch");
462 assert(op
->is_type_election());
464 switch (op
->get_req()->get_type()) {
466 case MSG_MON_ELECTION
:
468 if (!participating
) {
471 if (op
->get_req()->get_source().num() >= mon
->monmap
->size()) {
472 dout(5) << " ignoring bogus election message with bad mon rank "
473 << op
->get_req()->get_source() << dendl
;
477 MMonElection
*em
= static_cast<MMonElection
*>(op
->get_req());
479 // assume an old message encoding would have matched
480 if (em
->fsid
!= mon
->monmap
->fsid
) {
481 dout(0) << " ignoring election msg fsid "
482 << em
->fsid
<< " != " << mon
->monmap
->fsid
<< dendl
;
486 if (!mon
->monmap
->contains(em
->get_source_addr())) {
487 dout(1) << "discarding election message: " << em
->get_source_addr()
488 << " not in my monmap " << *mon
->monmap
<< dendl
;
493 peermap
.decode(em
->monmap_bl
);
494 if (peermap
.epoch
> mon
->monmap
->epoch
) {
495 dout(0) << em
->get_source_inst() << " has newer monmap epoch " << peermap
.epoch
496 << " > my epoch " << mon
->monmap
->epoch
499 mon
->monmap
->decode(em
->monmap_bl
);
500 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
501 t
->put("monmap", mon
->monmap
->epoch
, em
->monmap_bl
);
502 t
->put("monmap", "last_committed", mon
->monmap
->epoch
);
503 mon
->store
->apply_transaction(t
);
504 //mon->monmon()->paxos->stash_latest(mon->monmap->epoch, em->monmap_bl);
509 if (peermap
.epoch
< mon
->monmap
->epoch
) {
510 dout(0) << em
->get_source_inst() << " has older monmap epoch " << peermap
.epoch
511 << " < my epoch " << mon
->monmap
->epoch
516 case MMonElection::OP_PROPOSE
:
521 if (em
->epoch
< epoch
) {
522 dout(5) << "old epoch, dropping" << dendl
;
527 case MMonElection::OP_ACK
:
530 case MMonElection::OP_VICTORY
:
533 case MMonElection::OP_NAK
:
547 void Elector::start_participating()
549 if (!participating
) {
550 participating
= true;