root/daemons/controld/controld_schedulerd.c

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

DEFINITIONS

This source file includes following definitions.
  1. controld_shutdown_schedulerd_ipc
  2. save_cib_contents
  3. handle_disconnect
  4. handle_reply
  5. scheduler_event_callback
  6. new_schedulerd_ipc_connection
  7. do_pe_control
  8. controld_sched_timeout
  9. controld_stop_sched_timer
  10. controld_expect_sched_reply
  11. controld_free_sched_timer
  12. do_pe_invoke
  13. force_local_option
  14. do_pe_invoke_callback

   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 <unistd.h>  /* pid_t, sleep, ssize_t */
  13 
  14 #include <crm/cib.h>
  15 #include <crm/cluster.h>
  16 #include <crm/common/xml.h>
  17 #include <crm/crm.h>
  18 #include <crm/msg_xml.h>
  19 #include <crm/common/xml_internal.h>
  20 #include <crm/common/ipc.h>
  21 #include <crm/common/ipc_schedulerd.h>
  22 
  23 #include <pacemaker-controld.h>
  24 
  25 static void handle_disconnect(void);
  26 
  27 static pcmk_ipc_api_t *schedulerd_api = NULL;
  28 
  29 /*!
  30  * \internal
  31  * \brief Close any scheduler connection and free associated memory
  32  */
  33 void
  34 controld_shutdown_schedulerd_ipc(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36     controld_clear_fsa_input_flags(R_PE_REQUIRED);
  37     pcmk_disconnect_ipc(schedulerd_api);
  38     handle_disconnect();
  39 
  40     pcmk_free_ipc_api(schedulerd_api);
  41     schedulerd_api = NULL;
  42 }
  43 
  44 /*!
  45  * \internal
  46  * \brief Save CIB query result to file, raising FSA error
  47  *
  48  * \param[in] msg        Ignored
  49  * \param[in] call_id    Call ID of CIB query
  50  * \param[in] rc         Return code of CIB query
  51  * \param[in] output     Result of CIB query
  52  * \param[in] user_data  Unique identifier for filename
  53  *
  54  * \note This is intended to be called after a scheduler connection fails.
  55  */
  56 static void
  57 save_cib_contents(xmlNode *msg, int call_id, int rc, xmlNode *output,
     /* [previous][next][first][last][top][bottom][index][help] */
  58                   void *user_data)
  59 {
  60     const char *id = user_data;
  61 
  62     register_fsa_error_adv(C_FSA_INTERNAL, I_ERROR, NULL, NULL, __func__);
  63     CRM_CHECK(id != NULL, return);
  64 
  65     if (rc == pcmk_ok) {
  66         char *filename = crm_strdup_printf(PE_STATE_DIR "/pe-core-%s.bz2", id);
  67 
  68         if (write_xml_file(output, filename, TRUE) < 0) {
  69             crm_err("Could not save Cluster Information Base to %s after scheduler crash",
  70                     filename);
  71         } else {
  72             crm_notice("Saved Cluster Information Base to %s after scheduler crash",
  73                        filename);
  74         }
  75         free(filename);
  76     }
  77 }
  78 
  79 /*!
  80  * \internal
  81  * \brief Respond to scheduler connection failure
  82  */
  83 static void
  84 handle_disconnect(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86     // If we aren't connected to the scheduler, we can't expect a reply
  87     controld_expect_sched_reply(NULL);
  88 
  89     if (pcmk_is_set(controld_globals.fsa_input_register, R_PE_REQUIRED)) {
  90         int rc = pcmk_ok;
  91         char *uuid_str = crm_generate_uuid();
  92 
  93         crm_crit("Lost connection to the scheduler "
  94                  CRM_XS " CIB will be saved to " PE_STATE_DIR "/pe-core-%s.bz2",
  95                  uuid_str);
  96 
  97         /*
  98          * The scheduler died...
  99          *
 100          * Save the current CIB so that we have a chance of
 101          * figuring out what killed it.
 102          *
 103          * Delay raising the I_ERROR until the query below completes or
 104          * 5s is up, whichever comes first.
 105          *
 106          */
 107         rc = controld_globals.cib_conn->cmds->query(controld_globals.cib_conn,
 108                                                     NULL, NULL,
 109                                                     cib_scope_local);
 110         fsa_register_cib_callback(rc, uuid_str, save_cib_contents);
 111     }
 112 
 113     controld_clear_fsa_input_flags(R_PE_CONNECTED);
 114     controld_trigger_fsa();
 115     return;
 116 }
 117 
 118 static void
 119 handle_reply(pcmk_schedulerd_api_reply_t *reply)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121     const char *msg_ref = NULL;
 122 
 123     if (!AM_I_DC) {
 124         return;
 125     }
 126 
 127     msg_ref = reply->data.graph.reference;
 128 
 129     if (msg_ref == NULL) {
 130         crm_err("%s - Ignoring calculation with no reference", CRM_OP_PECALC);
 131 
 132     } else if (pcmk__str_eq(msg_ref, controld_globals.fsa_pe_ref,
 133                             pcmk__str_none)) {
 134         ha_msg_input_t fsa_input;
 135         xmlNode *crm_data_node;
 136 
 137         controld_stop_sched_timer();
 138 
 139         /* do_te_invoke (which will eventually process the fsa_input we are constructing
 140          * here) requires that fsa_input.xml be non-NULL.  That will only happen if
 141          * copy_ha_msg_input (which is called by register_fsa_input_adv) sees the
 142          * fsa_input.msg that it is expecting. The scheduler's IPC dispatch function
 143          * gave us the values we need, we just need to put them into XML.
 144          *
 145          * The name of the top level element here is irrelevant.  Nothing checks it.
 146          */
 147         fsa_input.msg = create_xml_node(NULL, "dummy-reply");
 148         crm_xml_add(fsa_input.msg, XML_ATTR_REFERENCE, msg_ref);
 149         crm_xml_add(fsa_input.msg, F_CRM_TGRAPH_INPUT, reply->data.graph.input);
 150 
 151         crm_data_node = create_xml_node(fsa_input.msg, F_CRM_DATA);
 152         add_node_copy(crm_data_node, reply->data.graph.tgraph);
 153         register_fsa_input_later(C_IPC_MESSAGE, I_PE_SUCCESS, &fsa_input);
 154 
 155         free_xml(fsa_input.msg);
 156 
 157     } else {
 158         crm_info("%s calculation %s is obsolete", CRM_OP_PECALC, msg_ref);
 159     }
 160 }
 161 
 162 static void
 163 scheduler_event_callback(pcmk_ipc_api_t *api, enum pcmk_ipc_event event_type,
     /* [previous][next][first][last][top][bottom][index][help] */
 164                          crm_exit_t status, void *event_data, void *user_data)
 165 {
 166     pcmk_schedulerd_api_reply_t *reply = event_data;
 167 
 168     switch (event_type) {
 169         case pcmk_ipc_event_disconnect:
 170             handle_disconnect();
 171             break;
 172 
 173         case pcmk_ipc_event_reply:
 174             handle_reply(reply);
 175             break;
 176 
 177         default:
 178             break;
 179     }
 180 }
 181 
 182 static bool
 183 new_schedulerd_ipc_connection(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 184 {
 185     int rc;
 186 
 187     controld_set_fsa_input_flags(R_PE_REQUIRED);
 188 
 189     if (schedulerd_api == NULL) {
 190         rc = pcmk_new_ipc_api(&schedulerd_api, pcmk_ipc_schedulerd);
 191 
 192         if (rc != pcmk_rc_ok) {
 193             crm_err("Error connecting to the scheduler: %s", pcmk_rc_str(rc));
 194             return false;
 195         }
 196     }
 197 
 198     pcmk_register_ipc_callback(schedulerd_api, scheduler_event_callback, NULL);
 199 
 200     rc = pcmk__connect_ipc(schedulerd_api, pcmk_ipc_dispatch_main, 3);
 201     if (rc != pcmk_rc_ok) {
 202         crm_err("Error connecting to %s: %s",
 203                 pcmk_ipc_name(schedulerd_api, true), pcmk_rc_str(rc));
 204         return false;
 205     }
 206 
 207     controld_set_fsa_input_flags(R_PE_CONNECTED);
 208     return true;
 209 }
 210 
 211 static void do_pe_invoke_callback(xmlNode *msg, int call_id, int rc,
 212                                   xmlNode *output, void *user_data);
 213 
 214 /*       A_PE_START, A_PE_STOP, O_PE_RESTART    */
 215 void
 216 do_pe_control(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
 217               enum crmd_fsa_cause cause,
 218               enum crmd_fsa_state cur_state,
 219               enum crmd_fsa_input current_input, fsa_data_t * msg_data)
 220 {
 221     if (pcmk_is_set(action, A_PE_STOP)) {
 222         controld_clear_fsa_input_flags(R_PE_REQUIRED);
 223         pcmk_disconnect_ipc(schedulerd_api);
 224         handle_disconnect();
 225     }
 226     if (pcmk_is_set(action, A_PE_START)
 227         && !pcmk_is_set(controld_globals.fsa_input_register, R_PE_CONNECTED)) {
 228 
 229         if (cur_state == S_STOPPING) {
 230             crm_info("Ignoring request to connect to scheduler while shutting down");
 231 
 232         } else if (!new_schedulerd_ipc_connection()) {
 233             crm_warn("Could not connect to scheduler");
 234             register_fsa_error(C_FSA_INTERNAL, I_FAIL, NULL);
 235         }
 236     }
 237 }
 238 
 239 static int fsa_pe_query = 0;
 240 static mainloop_timer_t *controld_sched_timer = NULL;
 241 
 242 // @TODO Make this a configurable cluster option if there's demand for it
 243 #define SCHED_TIMEOUT_MS (120000)
 244 
 245 /*!
 246  * \internal
 247  * \brief Handle a timeout waiting for scheduler reply
 248  *
 249  * \param[in] user_data  Ignored
 250  *
 251  * \return FALSE (indicating that timer should not be restarted)
 252  */
 253 static gboolean
 254 controld_sched_timeout(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 255 {
 256     if (AM_I_DC) {
 257         /* If this node is the DC but can't communicate with the scheduler, just
 258          * exit (and likely get fenced) so this node doesn't interfere with any
 259          * further DC elections.
 260          *
 261          * @TODO We could try something less drastic first, like disconnecting
 262          * and reconnecting to the scheduler, but something is likely going
 263          * seriously wrong, so perhaps it's better to just fail as quickly as
 264          * possible.
 265          */
 266         crmd_exit(CRM_EX_FATAL);
 267     }
 268     return FALSE;
 269 }
 270 
 271 void
 272 controld_stop_sched_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274     if ((controld_sched_timer != NULL)
 275         && (controld_globals.fsa_pe_ref != NULL)) {
 276         crm_trace("Stopping timer for scheduler reply %s",
 277                   controld_globals.fsa_pe_ref);
 278     }
 279     mainloop_timer_stop(controld_sched_timer);
 280 }
 281 
 282 /*!
 283  * \internal
 284  * \brief Set the scheduler request currently being waited on
 285  *
 286  * \param[in] ref  Request to expect reply to (or NULL for none)
 287  *
 288  * \note This function takes ownership of \p ref.
 289  */
 290 void
 291 controld_expect_sched_reply(char *ref)
     /* [previous][next][first][last][top][bottom][index][help] */
 292 {
 293     if (ref) {
 294         if (controld_sched_timer == NULL) {
 295             controld_sched_timer = mainloop_timer_add("scheduler_reply_timer",
 296                                                       SCHED_TIMEOUT_MS, FALSE,
 297                                                       controld_sched_timeout,
 298                                                       NULL);
 299         }
 300         mainloop_timer_start(controld_sched_timer);
 301     } else {
 302         controld_stop_sched_timer();
 303     }
 304     free(controld_globals.fsa_pe_ref);
 305     controld_globals.fsa_pe_ref = ref;
 306 }
 307 
 308 /*!
 309  * \internal
 310  * \brief Free the scheduler reply timer
 311  */
 312 void
 313 controld_free_sched_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 314 {
 315     if (controld_sched_timer != NULL) {
 316         mainloop_timer_del(controld_sched_timer);
 317         controld_sched_timer = NULL;
 318     }
 319 }
 320 
 321 /*       A_PE_INVOKE    */
 322 void
 323 do_pe_invoke(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
 324              enum crmd_fsa_cause cause,
 325              enum crmd_fsa_state cur_state,
 326              enum crmd_fsa_input current_input, fsa_data_t * msg_data)
 327 {
 328     cib_t *cib_conn = controld_globals.cib_conn;
 329 
 330     if (AM_I_DC == FALSE) {
 331         crm_err("Not invoking scheduler because not DC: %s",
 332                 fsa_action2string(action));
 333         return;
 334     }
 335 
 336     if (!pcmk_is_set(controld_globals.fsa_input_register, R_PE_CONNECTED)) {
 337         if (pcmk_is_set(controld_globals.fsa_input_register, R_SHUTDOWN)) {
 338             crm_err("Cannot shut down gracefully without the scheduler");
 339             register_fsa_input_before(C_FSA_INTERNAL, I_TERMINATE, NULL);
 340 
 341         } else {
 342             crm_info("Waiting for the scheduler to connect");
 343             crmd_fsa_stall(FALSE);
 344             controld_set_fsa_action_flags(A_PE_START);
 345             controld_trigger_fsa();
 346         }
 347         return;
 348     }
 349 
 350     if (cur_state != S_POLICY_ENGINE) {
 351         crm_notice("Not invoking scheduler because in state %s",
 352                    fsa_state2string(cur_state));
 353         return;
 354     }
 355     if (!pcmk_is_set(controld_globals.fsa_input_register, R_HAVE_CIB)) {
 356         crm_err("Attempted to invoke scheduler without consistent Cluster Information Base!");
 357 
 358         /* start the join from scratch */
 359         register_fsa_input_before(C_FSA_INTERNAL, I_ELECTION, NULL);
 360         return;
 361     }
 362 
 363     fsa_pe_query = cib_conn->cmds->query(cib_conn, NULL, NULL, cib_scope_local);
 364 
 365     crm_debug("Query %d: Requesting the current CIB: %s", fsa_pe_query,
 366               fsa_state2string(controld_globals.fsa_state));
 367 
 368     controld_expect_sched_reply(NULL);
 369     fsa_register_cib_callback(fsa_pe_query, NULL, do_pe_invoke_callback);
 370 }
 371 
 372 static void
 373 force_local_option(xmlNode *xml, const char *attr_name, const char *attr_value)
     /* [previous][next][first][last][top][bottom][index][help] */
 374 {
 375     int max = 0;
 376     int lpc = 0;
 377     const char *xpath_base = NULL;
 378     char *xpath_string = NULL;
 379     xmlXPathObjectPtr xpathObj = NULL;
 380 
 381     xpath_base = pcmk_cib_xpath_for(XML_CIB_TAG_CRMCONFIG);
 382     if (xpath_base == NULL) {
 383         crm_err(XML_CIB_TAG_CRMCONFIG " CIB element not known (bug?)");
 384         return;
 385     }
 386 
 387     xpath_string = crm_strdup_printf("%s//%s//nvpair[@name='%s']",
 388                                      xpath_base, XML_CIB_TAG_PROPSET,
 389                                      attr_name);
 390     xpathObj = xpath_search(xml, xpath_string);
 391     max = numXpathResults(xpathObj);
 392     free(xpath_string);
 393 
 394     for (lpc = 0; lpc < max; lpc++) {
 395         xmlNode *match = getXpathResult(xpathObj, lpc);
 396         crm_trace("Forcing %s/%s = %s", ID(match), attr_name, attr_value);
 397         crm_xml_add(match, XML_NVPAIR_ATTR_VALUE, attr_value);
 398     }
 399 
 400     if(max == 0) {
 401         xmlNode *configuration = NULL;
 402         xmlNode *crm_config = NULL;
 403         xmlNode *cluster_property_set = NULL;
 404 
 405         crm_trace("Creating %s-%s for %s=%s",
 406                   CIB_OPTIONS_FIRST, attr_name, attr_name, attr_value);
 407 
 408         configuration = pcmk__xe_match(xml, XML_CIB_TAG_CONFIGURATION, NULL,
 409                                        NULL);
 410         if (configuration == NULL) {
 411             configuration = create_xml_node(xml, XML_CIB_TAG_CONFIGURATION);
 412         }
 413 
 414         crm_config = pcmk__xe_match(configuration, XML_CIB_TAG_CRMCONFIG, NULL,
 415                                     NULL);
 416         if (crm_config == NULL) {
 417             crm_config = create_xml_node(configuration, XML_CIB_TAG_CRMCONFIG);
 418         }
 419 
 420         cluster_property_set = pcmk__xe_match(crm_config, XML_CIB_TAG_PROPSET,
 421                                               NULL, NULL);
 422         if (cluster_property_set == NULL) {
 423             cluster_property_set = create_xml_node(crm_config, XML_CIB_TAG_PROPSET);
 424             crm_xml_add(cluster_property_set, XML_ATTR_ID, CIB_OPTIONS_FIRST);
 425         }
 426 
 427         xml = create_xml_node(cluster_property_set, XML_CIB_TAG_NVPAIR);
 428 
 429         crm_xml_set_id(xml, "%s-%s", CIB_OPTIONS_FIRST, attr_name);
 430         crm_xml_add(xml, XML_NVPAIR_ATTR_NAME, attr_name);
 431         crm_xml_add(xml, XML_NVPAIR_ATTR_VALUE, attr_value);
 432     }
 433     freeXpathObject(xpathObj);
 434 }
 435 
 436 static void
 437 do_pe_invoke_callback(xmlNode * msg, int call_id, int rc, xmlNode * output, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 438 {
 439     char *ref = NULL;
 440     pid_t watchdog = pcmk__locate_sbd();
 441 
 442     if (rc != pcmk_ok) {
 443         crm_err("Could not retrieve the Cluster Information Base: %s "
 444                 CRM_XS " rc=%d call=%d", pcmk_strerror(rc), rc, call_id);
 445         register_fsa_error_adv(C_FSA_INTERNAL, I_ERROR, NULL, NULL, __func__);
 446         return;
 447 
 448     } else if (call_id != fsa_pe_query) {
 449         crm_trace("Skipping superseded CIB query: %d (current=%d)", call_id, fsa_pe_query);
 450         return;
 451 
 452     } else if (!AM_I_DC
 453                || !pcmk_is_set(controld_globals.fsa_input_register,
 454                                R_PE_CONNECTED)) {
 455         crm_debug("No need to invoke the scheduler anymore");
 456         return;
 457 
 458     } else if (controld_globals.fsa_state != S_POLICY_ENGINE) {
 459         crm_debug("Discarding scheduler request in state: %s",
 460                   fsa_state2string(controld_globals.fsa_state));
 461         return;
 462 
 463     /* this callback counts as 1 */
 464     } else if (num_cib_op_callbacks() > 1) {
 465         crm_debug("Re-asking for the CIB: %d other peer updates still pending",
 466                   (num_cib_op_callbacks() - 1));
 467         sleep(1);
 468         controld_set_fsa_action_flags(A_PE_INVOKE);
 469         controld_trigger_fsa();
 470         return;
 471     }
 472 
 473     CRM_LOG_ASSERT(output != NULL);
 474 
 475     /* Refresh the remote node cache and the known node cache when the
 476      * scheduler is invoked */
 477     pcmk__refresh_node_caches_from_cib(output);
 478 
 479     crm_xml_add(output, XML_ATTR_DC_UUID, controld_globals.our_uuid);
 480     pcmk__xe_set_bool_attr(output, XML_ATTR_HAVE_QUORUM,
 481                            pcmk_is_set(controld_globals.flags,
 482                                        controld_has_quorum));
 483 
 484     force_local_option(output, XML_ATTR_HAVE_WATCHDOG, pcmk__btoa(watchdog));
 485 
 486     if (pcmk_is_set(controld_globals.flags, controld_ever_had_quorum)
 487         && !crm_have_quorum) {
 488         crm_xml_add_int(output, XML_ATTR_QUORUM_PANIC, 1);
 489     }
 490 
 491     rc = pcmk_rc2legacy(pcmk_schedulerd_api_graph(schedulerd_api, output, &ref));
 492 
 493     if (rc < 0) {
 494         crm_err("Could not contact the scheduler: %s " CRM_XS " rc=%d",
 495                 pcmk_strerror(rc), rc);
 496         register_fsa_error_adv(C_FSA_INTERNAL, I_ERROR, NULL, NULL, __func__);
 497     } else {
 498         CRM_ASSERT(ref != NULL);
 499         controld_expect_sched_reply(ref);
 500         crm_debug("Invoking the scheduler: query=%d, ref=%s, seq=%llu, "
 501                   "quorate=%s", fsa_pe_query, controld_globals.fsa_pe_ref,
 502                   crm_peer_seq, pcmk__btoa(pcmk_is_set(controld_globals.flags,
 503                                                        controld_has_quorum)));
 504     }
 505 }

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