root/daemons/attrd/attrd_elections.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. attrd_election_cb
  2. attrd_election_init
  3. attrd_election_fini
  4. attrd_start_election_if_needed
  5. attrd_election_won
  6. attrd_handle_election_op
  7. attrd_check_for_new_writer
  8. attrd_declare_winner
  9. attrd_remove_voter
  10. attrd_xml_add_writer

   1 /*
   2  * Copyright 2013-2023 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 #include <crm/msg_xml.h>
  12 #include <crm/cluster.h>
  13 #include <crm/cluster/election_internal.h>
  14 
  15 #include "pacemaker-attrd.h"
  16 
  17 static char *peer_writer = NULL;
  18 static election_t *writer = NULL;
  19 
  20 static gboolean
  21 attrd_election_cb(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
  22 {
  23     attrd_declare_winner();
  24 
  25     /* Update the peers after an election */
  26     attrd_peer_sync(NULL, NULL);
  27 
  28     /* After winning an election, update the CIB with the values of all
  29      * attributes as the winner knows them.
  30      */
  31     attrd_write_attributes(attrd_write_all);
  32     return G_SOURCE_REMOVE;
  33 }
  34 
  35 void
  36 attrd_election_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38     writer = election_init(T_ATTRD, attrd_cluster->uname, 120000,
  39                            attrd_election_cb);
  40 }
  41 
  42 void
  43 attrd_election_fini(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45     election_fini(writer);
  46 }
  47 
  48 void
  49 attrd_start_election_if_needed(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51     if ((peer_writer == NULL)
  52         && (election_state(writer) != election_in_progress)
  53         && !attrd_shutting_down(false)) {
  54 
  55         crm_info("Starting an election to determine the writer");
  56         election_vote(writer);
  57     }
  58 }
  59 
  60 bool
  61 attrd_election_won(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63     return (election_state(writer) == election_won);
  64 }
  65 
  66 void
  67 attrd_handle_election_op(const crm_node_t *peer, xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69     enum election_result rc = 0;
  70     enum election_result previous = election_state(writer);
  71 
  72     crm_xml_add(xml, F_CRM_HOST_FROM, peer->uname);
  73 
  74     // Don't become writer if we're shutting down
  75     rc = election_count_vote(writer, xml, !attrd_shutting_down(false));
  76 
  77     switch(rc) {
  78         case election_start:
  79             crm_debug("Unsetting writer (was %s) and starting new election",
  80                       peer_writer? peer_writer : "unset");
  81             free(peer_writer);
  82             peer_writer = NULL;
  83             election_vote(writer);
  84             break;
  85 
  86         case election_lost:
  87             /* The election API should really distinguish between "we just lost
  88              * to this peer" and "we already lost previously, and we are
  89              * discarding this vote for some reason", but it doesn't.
  90              *
  91              * In the first case, we want to tentatively set the peer writer to
  92              * this peer, even though another peer may eventually win (which we
  93              * will learn via attrd_check_for_new_writer()), so
  94              * attrd_start_election_if_needed() doesn't start a new election.
  95              *
  96              * Approximate a test for that case as best as possible.
  97              */
  98             if ((peer_writer == NULL) || (previous != election_lost)) {
  99                 pcmk__str_update(&peer_writer, peer->uname);
 100                 crm_debug("Election lost, presuming %s is writer for now",
 101                           peer_writer);
 102             }
 103             break;
 104 
 105         case election_in_progress:
 106             election_check(writer);
 107             break;
 108 
 109         default:
 110             crm_info("Ignoring election op from %s due to error", peer->uname);
 111             break;
 112     }
 113 }
 114 
 115 bool
 116 attrd_check_for_new_writer(const crm_node_t *peer, const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118     int peer_state = 0;
 119 
 120     crm_element_value_int(xml, PCMK__XA_ATTR_WRITER, &peer_state);
 121     if (peer_state == election_won) {
 122         if ((election_state(writer) == election_won)
 123            && !pcmk__str_eq(peer->uname, attrd_cluster->uname, pcmk__str_casei)) {
 124             crm_notice("Detected another attribute writer (%s), starting new election",
 125                        peer->uname);
 126             election_vote(writer);
 127 
 128         } else if (!pcmk__str_eq(peer->uname, peer_writer, pcmk__str_casei)) {
 129             crm_notice("Recorded new attribute writer: %s (was %s)",
 130                        peer->uname, (peer_writer? peer_writer : "unset"));
 131             pcmk__str_update(&peer_writer, peer->uname);
 132         }
 133     }
 134     return (peer_state == election_won);
 135 }
 136 
 137 void
 138 attrd_declare_winner(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 139 {
 140     crm_notice("Recorded local node as attribute writer (was %s)",
 141                (peer_writer? peer_writer : "unset"));
 142     pcmk__str_update(&peer_writer, attrd_cluster->uname);
 143 }
 144 
 145 void
 146 attrd_remove_voter(const crm_node_t *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148     election_remove(writer, peer->uname);
 149     if (peer_writer && pcmk__str_eq(peer->uname, peer_writer, pcmk__str_casei)) {
 150         free(peer_writer);
 151         peer_writer = NULL;
 152         crm_notice("Lost attribute writer %s", peer->uname);
 153 
 154         /* Clear any election dampening in effect. Otherwise, if the lost writer
 155          * had just won, the election could fizzle out with no new writer.
 156          */
 157         election_clear_dampening(writer);
 158 
 159         /* If the writer received attribute updates during its shutdown, it will
 160          * not have written them to the CIB. Ensure we get a new writer so they
 161          * are written out. This means that every node that sees the writer
 162          * leave will start a new election, but that's better than losing
 163          * attributes.
 164          */
 165         attrd_start_election_if_needed();
 166 
 167     /* If an election is in progress, we need to call election_check(), in case
 168      * this lost peer is the only one that hasn't voted, otherwise the election
 169      * would be pending until it's timed out.
 170      */
 171     } else if (election_state(writer) == election_in_progress) {
 172        crm_debug("Checking election status upon loss of voter %s", peer->uname);
 173        election_check(writer);
 174     }
 175 }
 176 
 177 void
 178 attrd_xml_add_writer(xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 179 {
 180     crm_xml_add_int(xml, PCMK__XA_ATTR_WRITER, election_state(writer));
 181 }

/* [previous][next][first][last][top][bottom][index][help] */