ctdb-recoverd: Add and use function this_node_can_be_leader()
authorMartin Schwenke <martin@meltin.net>
Mon, 13 Dec 2021 23:57:03 +0000 (10:57 +1100)
committerMartin Schwenke <martins@samba.org>
Mon, 17 Jan 2022 10:21:32 +0000 (10:21 +0000)
This makes the code self-documenting.

In ctdb_election_data() there is a slight behaviour change.  An
inactive node will now try to lose an election.  This case should not happen
because:

* An inactive node can't win an election round and then send a reply.

* Any inactive node should never start an election.  There are
  currently places where this happens and they will be fixed later.

There is an instance where this could be used in
validate_recovery_master() but this involves a more serious logic
change.  Overhaul this function later.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/server/ctdb_recoverd.c

index d749cf06191ada0ca980a5b9a6e2ad818d109c4d..affa3b2bbd1786a70e4e211c0cadb989b366598a 100644 (file)
@@ -279,6 +279,12 @@ static bool this_node_is_leader(struct ctdb_recoverd *rec)
        return rec->leader == rec->pnn;
 }
 
+static bool this_node_can_be_leader(struct ctdb_recoverd *rec)
+{
+       return (rec->node_flags & NODE_FLAGS_INACTIVE) == 0 &&
+               (rec->ctdb->capabilities & CTDB_CAP_RECMASTER) != 0;
+}
+
 /*
   ban a node for a period of time
  */
@@ -1308,8 +1314,8 @@ static void ctdb_election_data(struct ctdb_recoverd *rec, struct election_messag
                }
        }
 
-       /* we shouldnt try to win this election if we cant be a recmaster */
-       if ((ctdb->capabilities & CTDB_CAP_RECMASTER) == 0) {
+       if (!this_node_can_be_leader(rec)) {
+               /* Try to lose... */
                em->num_connected = 0;
                em->priority_time = timeval_current();
        }
@@ -1327,18 +1333,7 @@ static bool ctdb_election_win(struct ctdb_recoverd *rec, struct election_message
 
        ctdb_election_data(rec, &myem);
 
-       /* we cant win if we don't have the recmaster capability */
-       if ((rec->ctdb->capabilities & CTDB_CAP_RECMASTER) == 0) {
-               return false;
-       }
-
-       /* we cant win if we are banned */
-       if (rec->node_flags & NODE_FLAGS_BANNED) {
-               return false;
-       }
-
-       /* we cant win if we are stopped */
-       if (rec->node_flags & NODE_FLAGS_STOPPED) {
+       if (!this_node_can_be_leader(rec)) {
                return false;
        }