]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
elector: hoist bump_epoch into ElectionLogic
authorGreg Farnum <gfarnum@redhat.com>
Thu, 6 Jun 2019 16:10:39 +0000 (09:10 -0700)
committerGreg Farnum <gfarnum@redhat.com>
Mon, 19 Aug 2019 20:04:59 +0000 (13:04 -0700)
Mangle some pointers and the dout prefix to make things compile
during this transition.

Signed-off-by: Greg Farnum <gfarnum@redhat.com>
src/mon/Elector.cc
src/mon/Elector.h

index fdaaf0dd0f5c22fde8c5cddf2f69d853c1c69e18..fd24ef5b7e576a4fedddb8ab16ef6c068659ef36 100644 (file)
 
 #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<MonitorDBStore::Transaction>());
   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<MonitorDBStore::Transaction>());
   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<MonitorDBStore::Transaction>());
-  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<MonitorDBStore::Transaction>());
@@ -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<int>::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,
index 58e75f36aca45e95a0470f6ff666f9e7d6a5201b..f6d49b5669fab66512acea9ad1b502b3e05efcb9 100644 (file)
@@ -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);
   }
 
   /**