From 020569504620d4bb29283cfa1c20dcbdab526438 Mon Sep 17 00:00:00 2001 From: Greg Farnum Date: Thu, 6 Jun 2019 10:34:19 -0700 Subject: [PATCH] elector: hoist init into ElectionLogic Signed-off-by: Greg Farnum --- src/mon/Elector.cc | 27 +++++++++++++++------------ src/mon/Elector.h | 5 +++-- src/mon/Monitor.cc | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/mon/Elector.cc b/src/mon/Elector.cc index 85ec8c619eb..4997d2fedee 100644 --- a/src/mon/Elector.cc +++ b/src/mon/Elector.cc @@ -38,6 +38,11 @@ void ElectionLogic::persist_epoch(epoch_t e) elector->mon->store->apply_transaction(t); } +epoch_t ElectionLogic::read_persisted_epoch() +{ + return elector->mon->store->get(Monitor::MONITOR_NAME, "election_epoch"); +} + void ElectionLogic::validate_store() { auto t(std::make_shared()); @@ -67,21 +72,19 @@ int ElectionLogic::elector_my_rank() } -void Elector::init() +void ElectionLogic::init() { - logic.epoch = mon->store->get(Monitor::MONITOR_NAME, "election_epoch"); - if (!logic.epoch) { + epoch = read_persisted_epoch(); + if (!epoch) { dout(1) << "init, first boot, initializing epoch at 1 " << dendl; - logic.epoch = 1; - } else if (logic.epoch % 2) { - dout(1) << "init, last seen epoch " << logic.epoch + epoch = 1; + } else if (epoch % 2) { + dout(1) << "init, last seen epoch " << epoch << ", mid-election, bumping" << dendl; - ++logic.epoch; - auto t(std::make_shared()); - t->put(Monitor::MONITOR_NAME, "election_epoch", logic.epoch); - mon->store->apply_transaction(t); + ++epoch; + persist_epoch(epoch); } else { - dout(1) << "init, last seen epoch " << logic.epoch << dendl; + dout(1) << "init, last seen epoch " << epoch << dendl; } } @@ -119,7 +122,7 @@ void Elector::start() logic.acked_me.clear(); peer_info.clear(); - init(); + logic.init(); // start by trying to elect me if (logic.epoch % 2 == 0) { diff --git a/src/mon/Elector.h b/src/mon/Elector.h index 538ba8f6427..9820576a4f1 100644 --- a/src/mon/Elector.h +++ b/src/mon/Elector.h @@ -37,7 +37,7 @@ public: ElectionLogic(Elector *e) : elector(e), epoch(0), electing_me(false), leader_acked(-1) {} - + void init(); void bump_epoch(epoch_t e); void defer(int who); void handle_propose_logic(epoch_t mepoch, int from); @@ -45,6 +45,7 @@ public: private: // call-outs void persist_epoch(epoch_t e); + epoch_t read_persisted_epoch(); void validate_store(); bool elector_is_current_member(int rank); void elector_trigger_new_election(); @@ -373,7 +374,7 @@ private: * * @post @p epoch is set to 1 or higher. */ - void init(); + // void init(); /** * Inform this class it is supposed to shutdown. * diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index acc4c34f061..6ee95fee85c 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -2095,7 +2095,7 @@ void Monitor::win_standalone_election() // bump election epoch, in case the previous epoch included other // monitors; we need to be able to make the distinction. - elector.init(); + elector.logic.init(); elector.advance_epoch(); rank = monmap->get_rank(name); -- 2.39.5