From 2069016a1572daf5217f10819aa1fea66a4b7747 Mon Sep 17 00:00:00 2001 From: Greg Farnum Date: Thu, 6 Jun 2019 09:10:39 -0700 Subject: [PATCH] elector: hoist bump_epoch into ElectionLogic Mangle some pointers and the dout prefix to make things compile during this transition. Signed-off-by: Greg Farnum --- src/mon/Elector.cc | 51 +++++++++++++++++++++++----------------------- src/mon/Elector.h | 16 +++++++++++---- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/mon/Elector.cc b/src/mon/Elector.cc index fdaaf0dd0f5c2..fd24ef5b7e576 100644 --- a/src/mon/Elector.cc +++ b/src/mon/Elector.cc @@ -24,25 +24,25 @@ #define dout_subsys ceph_subsys_mon #undef dout_prefix -#define dout_prefix _prefix(_dout, mon, logic.epoch) -static ostream& _prefix(std::ostream *_dout, Monitor *mon, epoch_t epoch) { - return *_dout << "mon." << mon->name << "@" << mon->rank - << "(" << mon->get_state_name() - << ").elector(" << epoch << ") "; +#define dout_prefix _prefix(_dout, elector) +static ostream& _prefix(std::ostream *_dout, Elector* elector) { + return *_dout << "mon." << elector->mon->name << "@" << elector->mon->rank + << "(" << elector->mon->get_state_name() + << ").elector(" << elector->logic.epoch << ") "; } void ElectionLogic::persist_epoch(epoch_t e) { auto t(std::make_shared()); t->put(Monitor::MONITOR_NAME, "election_epoch", e); - mon->store->apply_transaction(t); + elector->mon->store->apply_transaction(t); } void ElectionLogic::validate_store() { auto t(std::make_shared()); t->put(Monitor::MONITOR_NAME, "election_writeable_test", rand()); - int r = mon->store->apply_transaction(t); + int r = elector->mon->store->apply_transaction(t); ceph_assert(r >= 0); } @@ -69,21 +69,22 @@ void Elector::shutdown() cancel_timer(); } -void Elector::bump_epoch(epoch_t e) +void ElectionLogic::bump_epoch(epoch_t e) { - dout(10) << "bump_epoch " << logic.epoch << " to " << e << dendl; - ceph_assert(logic.epoch <= e); - logic.epoch = e; - auto t(std::make_shared()); - t->put(Monitor::MONITOR_NAME, "election_epoch", logic.epoch); - mon->store->apply_transaction(t); - - mon->join_election(); - + dout(10) << __func__ << epoch << " to " << e << dendl; + ceph_assert(epoch <= e); + epoch = e; + validate_store(); // clear up some state - logic.electing_me = false; - logic.acked_me.clear(); + electing_me = false; + acked_me.clear(); + elector->_bump_epoch(); +} + +void Elector::_bump_epoch() +{ peer_info.clear(); + mon->join_election(); } @@ -101,7 +102,7 @@ void Elector::start() // start by trying to elect me if (logic.epoch % 2 == 0) { - bump_epoch(logic.epoch+1); // odd == election cycle + logic.bump_epoch(logic.epoch+1); // odd == election cycle } else { // do a trivial db write just to ensure it is writeable. auto t(std::make_shared()); @@ -234,7 +235,7 @@ void Elector::victory() cancel_timer(); ceph_assert(logic.epoch % 2 == 1); // election - bump_epoch(logic.epoch+1); // is over! + logic.bump_epoch(logic.epoch+1); // is over! // tell everyone! for (set::iterator p = quorum.begin(); @@ -296,7 +297,7 @@ void Elector::handle_propose(MonOpRequestRef op) << dendl; nak_old_peer(op); } else if (m->epoch > logic.epoch) { - bump_epoch(m->epoch); + logic.bump_epoch(m->epoch); } else if (m->epoch < logic.epoch) { // got an "old" propose, if (logic.epoch % 2 == 0 && // in a non-election cycle @@ -346,7 +347,7 @@ void Elector::handle_ack(MonOpRequestRef op) ceph_assert(m->epoch % 2 == 1); // election if (m->epoch > logic.epoch) { dout(5) << "woah, that's a newer epoch, i must have rebooted. bumping and re-starting!" << dendl; - bump_epoch(m->epoch); + logic.bump_epoch(m->epoch); start(); return; } @@ -418,12 +419,12 @@ void Elector::handle_victory(MonOpRequestRef op) // i should have seen this election if i'm getting the victory. if (m->epoch != logic.epoch + 1) { dout(5) << "woah, that's a funny epoch, i must have rebooted. bumping and re-starting!" << dendl; - bump_epoch(m->epoch); + logic.bump_epoch(m->epoch); start(); return; } - bump_epoch(m->epoch); + logic.bump_epoch(m->epoch); // they win mon->lose_election(logic.epoch, m->quorum, from, diff --git a/src/mon/Elector.h b/src/mon/Elector.h index 58e75f36aca45..f6d49b5669fab 100644 --- a/src/mon/Elector.h +++ b/src/mon/Elector.h @@ -37,6 +37,9 @@ public: ElectionLogic(Elector *e) : elector(e), epoch(0), electing_me(false), leader_acked(-1) {} + + void bump_epoch(epoch_t e); + private: void persist_epoch(epoch_t e); void validate_store(); @@ -53,8 +56,11 @@ class Elector { * @defgroup Elector_h_class Elector * @{ */ - private: + friend class ElectionLogic; + // FIXME! + public: ElectionLogic logic; + Elector *elector; /** * @defgroup Elector_h_internal_types Internal Types * @{ @@ -81,6 +87,7 @@ class Elector { */ Monitor *mon; +private: /** * Event callback responsible for dealing with an expired election once a * timer runs out and fires up. @@ -174,7 +181,7 @@ class Elector { * * @param e Epoch to which we will update our epoch */ - void bump_epoch(epoch_t e); + void _bump_epoch(); /** * Start new elections by proposing ourselves as the new Leader. @@ -347,7 +354,8 @@ class Elector { * @param m A Monitor instance */ explicit Elector(Monitor *m) : logic(this), - mon(m), + elector(this), + mon(m), participating(true) {} /** @@ -381,7 +389,7 @@ class Elector { * increase election epoch by 1 */ void advance_epoch() { - bump_epoch(logic.epoch + 1); + logic.bump_epoch(logic.epoch + 1); } /** -- 2.39.5