root/daemons/controld/controld_join_client.c

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

DEFINITIONS

This source file includes following definitions.
  1. update_dc_expected
  2. do_cl_join_query
  3. do_cl_join_announce
  4. do_cl_join_offer_respond
  5. join_query_callback
  6. set_join_state
  7. update_conn_host_cache
  8. do_cl_join_finalize_respond

   1 /*
   2  * Copyright 2004-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 
  12 #include <crm/crm.h>
  13 #include <crm/cib.h>
  14 #include <crm/msg_xml.h>
  15 #include <crm/common/xml.h>
  16 
  17 #include <pacemaker-controld.h>
  18 
  19 void join_query_callback(xmlNode * msg, int call_id, int rc, xmlNode * output, void *user_data);
  20 
  21 extern ha_msg_input_t *copy_ha_msg_input(ha_msg_input_t * orig);
  22 
  23 /*!
  24  * \internal
  25  * \brief Remember if DC is shutting down as we join
  26  *
  27  * If we're joining while the current DC is shutting down, update its expected
  28  * state, so we don't fence it if we become the new DC. (We weren't a peer
  29  * when it broadcast its shutdown request.)
  30  *
  31  * \param[in] msg  A join message from the DC
  32  */
  33 static void
  34 update_dc_expected(const xmlNode *msg)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36     if ((controld_globals.dc_name != NULL)
  37         && pcmk__xe_attr_is_true(msg, F_CRM_DC_LEAVING)) {
  38         crm_node_t *dc_node = crm_get_peer(0, controld_globals.dc_name);
  39 
  40         pcmk__update_peer_expected(__func__, dc_node, CRMD_JOINSTATE_DOWN);
  41     }
  42 }
  43 
  44 /*      A_CL_JOIN_QUERY         */
  45 /* is there a DC out there? */
  46 void
  47 do_cl_join_query(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
  48                  enum crmd_fsa_cause cause,
  49                  enum crmd_fsa_state cur_state,
  50                  enum crmd_fsa_input current_input, fsa_data_t * msg_data)
  51 {
  52     xmlNode *req = create_request(CRM_OP_JOIN_ANNOUNCE, NULL, NULL,
  53                                   CRM_SYSTEM_DC, CRM_SYSTEM_CRMD, NULL);
  54 
  55     sleep(1);                   // Give the cluster layer time to propagate to the DC
  56     update_dc(NULL);            /* Unset any existing value so that the result is not discarded */
  57     crm_debug("Querying for a DC");
  58     send_cluster_message(NULL, crm_msg_crmd, req, FALSE);
  59     free_xml(req);
  60 }
  61 
  62 /*       A_CL_JOIN_ANNOUNCE     */
  63 
  64 /* this is kind of a workaround for the fact that we may not be around or
  65  * are otherwise unable to reply when the DC sends out A_DC_JOIN_OFFER_ALL
  66  */
  67 void
  68 do_cl_join_announce(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
  69                     enum crmd_fsa_cause cause,
  70                     enum crmd_fsa_state cur_state,
  71                     enum crmd_fsa_input current_input, fsa_data_t * msg_data)
  72 {
  73     /* don't announce if we're in one of these states */
  74     if (cur_state != S_PENDING) {
  75         crm_warn("Not announcing cluster join because in state %s",
  76                  fsa_state2string(cur_state));
  77         return;
  78     }
  79 
  80     if (!pcmk_is_set(controld_globals.fsa_input_register, R_STARTING)) {
  81         /* send as a broadcast */
  82         xmlNode *req = create_request(CRM_OP_JOIN_ANNOUNCE, NULL, NULL,
  83                                       CRM_SYSTEM_DC, CRM_SYSTEM_CRMD, NULL);
  84 
  85         crm_debug("Announcing availability");
  86         update_dc(NULL);
  87         send_cluster_message(NULL, crm_msg_crmd, req, FALSE);
  88         free_xml(req);
  89 
  90     } else {
  91         /* Delay announce until we have finished local startup */
  92         crm_warn("Delaying announce of cluster join until local startup is complete");
  93         return;
  94     }
  95 }
  96 
  97 static int query_call_id = 0;
  98 
  99 /*       A_CL_JOIN_REQUEST      */
 100 /* aka. accept the welcome offer */
 101 void
 102 do_cl_join_offer_respond(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
 103                          enum crmd_fsa_cause cause,
 104                          enum crmd_fsa_state cur_state,
 105                          enum crmd_fsa_input current_input, fsa_data_t * msg_data)
 106 {
 107     cib_t *cib_conn = controld_globals.cib_conn;
 108 
 109     ha_msg_input_t *input = fsa_typed_data(fsa_dt_ha_msg);
 110     const char *welcome_from;
 111     const char *join_id;
 112 
 113     CRM_CHECK(input != NULL, return);
 114 
 115     welcome_from = crm_element_value(input->msg, F_CRM_HOST_FROM);
 116     join_id = crm_element_value(input->msg, F_CRM_JOIN_ID);
 117     crm_trace("Accepting cluster join offer from node %s "CRM_XS" join-%s",
 118               welcome_from, crm_element_value(input->msg, F_CRM_JOIN_ID));
 119 
 120     /* we only ever want the last one */
 121     if (query_call_id > 0) {
 122         crm_trace("Cancelling previous join query: %d", query_call_id);
 123         remove_cib_op_callback(query_call_id, FALSE);
 124         query_call_id = 0;
 125     }
 126 
 127     if (update_dc(input->msg) == FALSE) {
 128         crm_warn("Discarding cluster join offer from node %s (expected %s)",
 129                  welcome_from, controld_globals.dc_name);
 130         return;
 131     }
 132 
 133     update_dc_expected(input->msg);
 134 
 135     query_call_id = cib_conn->cmds->query(cib_conn, NULL, NULL,
 136                                           cib_scope_local|cib_no_children);
 137     fsa_register_cib_callback(query_call_id, strdup(join_id),
 138                               join_query_callback);
 139     crm_trace("Registered join query callback: %d", query_call_id);
 140 
 141     controld_set_fsa_action_flags(A_DC_TIMER_STOP);
 142     controld_trigger_fsa();
 143 }
 144 
 145 void
 146 join_query_callback(xmlNode * msg, int call_id, int rc, xmlNode * output, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148     char *join_id = user_data;
 149     xmlNode *generation = create_xml_node(NULL, XML_CIB_TAG_GENERATION_TUPPLE);
 150 
 151     CRM_LOG_ASSERT(join_id != NULL);
 152 
 153     if (query_call_id != call_id) {
 154         crm_trace("Query %d superseded", call_id);
 155         goto done;
 156     }
 157 
 158     query_call_id = 0;
 159     if(rc != pcmk_ok || output == NULL) {
 160         crm_err("Could not retrieve version details for join-%s: %s (%d)",
 161                 join_id, pcmk_strerror(rc), rc);
 162         register_fsa_error_adv(C_FSA_INTERNAL, I_ERROR, NULL, NULL, __func__);
 163 
 164     } else if (controld_globals.dc_name == NULL) {
 165         crm_debug("Membership is in flux, not continuing join-%s", join_id);
 166 
 167     } else {
 168         xmlNode *reply = NULL;
 169 
 170         crm_debug("Respond to join offer join-%s from %s",
 171                   join_id, controld_globals.dc_name);
 172         copy_in_properties(generation, output);
 173 
 174         reply = create_request(CRM_OP_JOIN_REQUEST, generation,
 175                                controld_globals.dc_name, CRM_SYSTEM_DC,
 176                                CRM_SYSTEM_CRMD, NULL);
 177 
 178         crm_xml_add(reply, F_CRM_JOIN_ID, join_id);
 179         crm_xml_add(reply, XML_ATTR_CRM_VERSION, CRM_FEATURE_SET);
 180         send_cluster_message(crm_get_peer(0, controld_globals.dc_name),
 181                              crm_msg_crmd, reply, TRUE);
 182         free_xml(reply);
 183     }
 184 
 185   done:
 186     free_xml(generation);
 187 }
 188 
 189 void
 190 set_join_state(const char *start_state, const char *node_name, const char *node_uuid,
     /* [previous][next][first][last][top][bottom][index][help] */
 191                bool remote)
 192 {
 193     if (pcmk__str_eq(start_state, "standby", pcmk__str_casei)) {
 194         crm_notice("Forcing node %s to join in %s state per configured "
 195                    "environment", node_name, start_state);
 196         cib__update_node_attr(controld_globals.logger_out,
 197                               controld_globals.cib_conn, cib_sync_call,
 198                               XML_CIB_TAG_NODES, node_uuid,
 199                               NULL, NULL, NULL, "standby", "on", NULL,
 200                               remote ? "remote" : NULL);
 201 
 202     } else if (pcmk__str_eq(start_state, "online", pcmk__str_casei)) {
 203         crm_notice("Forcing node %s to join in %s state per configured "
 204                    "environment", node_name, start_state);
 205         cib__update_node_attr(controld_globals.logger_out,
 206                               controld_globals.cib_conn, cib_sync_call,
 207                               XML_CIB_TAG_NODES, node_uuid,
 208                               NULL, NULL, NULL, "standby", "off", NULL,
 209                               remote ? "remote" : NULL);
 210 
 211     } else if (pcmk__str_eq(start_state, "default", pcmk__str_casei)) {
 212         crm_debug("Not forcing a starting state on node %s", node_name);
 213 
 214     } else {
 215         crm_warn("Unrecognized start state '%s', using 'default' (%s)",
 216                  start_state, node_name);
 217     }
 218 }
 219 
 220 static int
 221 update_conn_host_cache(xmlNode *node, void *userdata)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223     const char *remote = crm_element_value(node, XML_ATTR_ID);
 224     const char *conn_host = crm_element_value(node, PCMK__XA_CONN_HOST);
 225     const char *state = crm_element_value(node, XML_CIB_TAG_STATE);
 226 
 227     crm_node_t *remote_peer = crm_remote_peer_get(remote);
 228 
 229     if (remote_peer == NULL) {
 230         return pcmk_rc_ok;
 231     }
 232 
 233     if (conn_host != NULL) {
 234         pcmk__str_update(&remote_peer->conn_host, conn_host);
 235     }
 236 
 237     if (state != NULL) {
 238         pcmk__update_peer_state(__func__, remote_peer, state, 0);
 239     }
 240 
 241     return pcmk_rc_ok;
 242 }
 243 
 244 /*      A_CL_JOIN_RESULT        */
 245 /* aka. this is notification that we have (or have not) been accepted */
 246 void
 247 do_cl_join_finalize_respond(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
 248                             enum crmd_fsa_cause cause,
 249                             enum crmd_fsa_state cur_state,
 250                             enum crmd_fsa_input current_input, fsa_data_t * msg_data)
 251 {
 252     xmlNode *tmp1 = NULL;
 253     gboolean was_nack = TRUE;
 254     static gboolean first_join = TRUE;
 255     ha_msg_input_t *input = fsa_typed_data(fsa_dt_ha_msg);
 256     const char *start_state = pcmk__env_option(PCMK__ENV_NODE_START_STATE);
 257 
 258     int join_id = -1;
 259     const char *op = crm_element_value(input->msg, F_CRM_TASK);
 260     const char *welcome_from = crm_element_value(input->msg, F_CRM_HOST_FROM);
 261 
 262     if (!pcmk__str_eq(op, CRM_OP_JOIN_ACKNAK, pcmk__str_casei)) {
 263         crm_trace("Ignoring op=%s message", op);
 264         return;
 265     }
 266 
 267     /* calculate if it was an ack or a nack */
 268     if (pcmk__xe_attr_is_true(input->msg, CRM_OP_JOIN_ACKNAK)) {
 269         was_nack = FALSE;
 270     }
 271 
 272     crm_element_value_int(input->msg, F_CRM_JOIN_ID, &join_id);
 273 
 274     if (was_nack) {
 275         crm_err("Shutting down because cluster join with leader %s failed "
 276                 CRM_XS" join-%d NACK'd", welcome_from, join_id);
 277         register_fsa_error(C_FSA_INTERNAL, I_ERROR, NULL);
 278         controld_set_fsa_input_flags(R_STAYDOWN);
 279         return;
 280     }
 281 
 282     if (!AM_I_DC
 283         && pcmk__str_eq(welcome_from, controld_globals.our_nodename,
 284                         pcmk__str_casei)) {
 285         crm_warn("Discarding our own welcome - we're no longer the DC");
 286         return;
 287     }
 288 
 289     if (update_dc(input->msg) == FALSE) {
 290         crm_warn("Discarding %s from node %s (expected from %s)",
 291                  op, welcome_from, controld_globals.dc_name);
 292         return;
 293     }
 294 
 295     update_dc_expected(input->msg);
 296 
 297     /* record the node's feature set as a transient attribute */
 298     update_attrd(controld_globals.our_nodename, CRM_ATTR_FEATURE_SET,
 299                  CRM_FEATURE_SET, NULL, FALSE);
 300 
 301     /* send our status section to the DC */
 302     tmp1 = controld_query_executor_state();
 303     if (tmp1 != NULL) {
 304         xmlNode *remotes = NULL;
 305         xmlNode *reply = create_request(CRM_OP_JOIN_CONFIRM, tmp1,
 306                                         controld_globals.dc_name, CRM_SYSTEM_DC,
 307                                         CRM_SYSTEM_CRMD, NULL);
 308 
 309         crm_xml_add_int(reply, F_CRM_JOIN_ID, join_id);
 310 
 311         crm_debug("Confirming join-%d: sending local operation history to %s",
 312                   join_id, controld_globals.dc_name);
 313 
 314         /*
 315          * If this is the node's first join since the controller started on it,
 316          * set its initial state (standby or member) according to the user's
 317          * preference.
 318          *
 319          * We do not clear the LRM history here. Even if the DC failed to do it
 320          * when we last left, removing them here creates a race condition if the
 321          * controller is being recovered. Instead of a list of active resources
 322          * from the executor, we may end up with a blank status section. If we
 323          * are _NOT_ lucky, we will probe for the "wrong" instance of anonymous
 324          * clones and end up with multiple active instances on the machine.
 325          */
 326         if (first_join
 327             && !pcmk_is_set(controld_globals.fsa_input_register, R_SHUTDOWN)) {
 328 
 329             first_join = FALSE;
 330             if (start_state) {
 331                 set_join_state(start_state, controld_globals.our_nodename,
 332                                controld_globals.our_uuid, false);
 333             }
 334         }
 335 
 336         send_cluster_message(crm_get_peer(0, controld_globals.dc_name),
 337                              crm_msg_crmd, reply, TRUE);
 338         free_xml(reply);
 339 
 340         if (AM_I_DC == FALSE) {
 341             register_fsa_input_adv(cause, I_NOT_DC, NULL, A_NOTHING, TRUE,
 342                                    __func__);
 343         }
 344 
 345         free_xml(tmp1);
 346 
 347         /* Update the remote node cache with information about which node
 348          * is hosting the connection.
 349          */
 350         remotes = pcmk__xe_match(input->msg, XML_CIB_TAG_NODES, NULL, NULL);
 351         if (remotes != NULL) {
 352             pcmk__xe_foreach_child(remotes, XML_CIB_TAG_NODE, update_conn_host_cache, NULL);
 353         }
 354 
 355     } else {
 356         crm_err("Could not confirm join-%d with %s: Local operation history "
 357                 "failed", join_id, controld_globals.dc_name);
 358         register_fsa_error(C_FSA_INTERNAL, I_FAIL, NULL);
 359     }
 360 }

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