root/daemons/controld/controld_based.c

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

DEFINITIONS

This source file includes following definitions.
  1. do_cib_updated
  2. do_cib_replaced
  3. controld_disconnect_cib_manager
  4. do_cib_control
  5. crmd_cib_smart_opt
  6. controld_action_is_recordable
  7. cib_delete_callback
  8. controld_delete_node_state
  9. controld_delete_resource_history

   1 /*
   2  * Copyright 2004-2020 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>  /* sleep */
  13 
  14 #include <crm/common/alerts_internal.h>
  15 #include <crm/common/xml.h>
  16 #include <crm/crm.h>
  17 #include <crm/msg_xml.h>
  18 
  19 #include <pacemaker-controld.h>
  20 
  21 int cib_retries = 0;
  22 
  23 void
  24 do_cib_updated(const char *event, xmlNode * msg)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26     if (pcmk__alert_in_patchset(msg, TRUE)) {
  27         mainloop_set_trigger(config_read);
  28     }
  29 }
  30 
  31 void
  32 do_cib_replaced(const char *event, xmlNode * msg)
     /* [previous][next][first][last][top][bottom][index][help] */
  33 {
  34     crm_debug("Updating the CIB after a replace: DC=%s", pcmk__btoa(AM_I_DC));
  35     if (AM_I_DC == FALSE) {
  36         return;
  37 
  38     } else if ((fsa_state == S_FINALIZE_JOIN)
  39                && pcmk_is_set(fsa_input_register, R_CIB_ASKED)) {
  40         /* no need to restart the join - we asked for this replace op */
  41         return;
  42     }
  43 
  44     /* start the join process again so we get everyone's LRM status */
  45     populate_cib_nodes(node_update_quick|node_update_all, __func__);
  46     register_fsa_input(C_FSA_INTERNAL, I_ELECTION, NULL);
  47 }
  48 
  49 void
  50 controld_disconnect_cib_manager(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52     CRM_ASSERT(fsa_cib_conn != NULL);
  53 
  54     crm_info("Disconnecting from the CIB manager");
  55 
  56     controld_clear_fsa_input_flags(R_CIB_CONNECTED);
  57 
  58     fsa_cib_conn->cmds->del_notify_callback(fsa_cib_conn, T_CIB_REPLACE_NOTIFY, do_cib_replaced);
  59     fsa_cib_conn->cmds->del_notify_callback(fsa_cib_conn, T_CIB_DIFF_NOTIFY, do_cib_updated);
  60     cib_free_callbacks(fsa_cib_conn);
  61     if (fsa_cib_conn->state != cib_disconnected) {
  62         /* Does not require a set_slave() reply to sign out from based. */
  63         fsa_cib_conn->cmds->set_slave(fsa_cib_conn, cib_scope_local | cib_discard_reply);
  64         fsa_cib_conn->cmds->signoff(fsa_cib_conn);
  65     }
  66 
  67     crm_notice("Disconnected from the CIB manager");
  68 }
  69 
  70 /* A_CIB_STOP, A_CIB_START, O_CIB_RESTART */
  71 void
  72 do_cib_control(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
  73                enum crmd_fsa_cause cause,
  74                enum crmd_fsa_state cur_state,
  75                enum crmd_fsa_input current_input, fsa_data_t * msg_data)
  76 {
  77     CRM_ASSERT(fsa_cib_conn != NULL);
  78 
  79     if (action & A_CIB_STOP) {
  80 
  81         if (fsa_cib_conn->state != cib_disconnected && last_resource_update != 0) {
  82             crm_info("Waiting for resource update %d to complete", last_resource_update);
  83             crmd_fsa_stall(FALSE);
  84             return;
  85         }
  86 
  87         controld_disconnect_cib_manager();
  88 
  89     }
  90 
  91     if (action & A_CIB_START) {
  92         int rc = pcmk_ok;
  93 
  94         if (cur_state == S_STOPPING) {
  95             crm_err("Ignoring request to connect to the CIB manager after shutdown");
  96             return;
  97         }
  98 
  99         rc = fsa_cib_conn->cmds->signon(fsa_cib_conn, CRM_SYSTEM_CRMD, cib_command_nonblocking);
 100 
 101         if (rc != pcmk_ok) {
 102             /* a short wait that usually avoids stalling the FSA */
 103             sleep(1);
 104             rc = fsa_cib_conn->cmds->signon(fsa_cib_conn, CRM_SYSTEM_CRMD, cib_command_nonblocking);
 105         }
 106 
 107         if (rc != pcmk_ok) {
 108             crm_info("Could not connect to the CIB manager: %s", pcmk_strerror(rc));
 109 
 110         } else if (pcmk_ok !=
 111                    fsa_cib_conn->cmds->set_connection_dnotify(fsa_cib_conn,
 112                                                               crmd_cib_connection_destroy)) {
 113             crm_err("Could not set dnotify callback");
 114 
 115         } else if (pcmk_ok !=
 116                    fsa_cib_conn->cmds->add_notify_callback(fsa_cib_conn, T_CIB_REPLACE_NOTIFY,
 117                                                            do_cib_replaced)) {
 118             crm_err("Could not set CIB notification callback (replace)");
 119 
 120         } else if (pcmk_ok !=
 121                    fsa_cib_conn->cmds->add_notify_callback(fsa_cib_conn, T_CIB_DIFF_NOTIFY,
 122                                                            do_cib_updated)) {
 123             crm_err("Could not set CIB notification callback (update)");
 124 
 125         } else {
 126             controld_set_fsa_input_flags(R_CIB_CONNECTED);
 127             cib_retries = 0;
 128         }
 129 
 130         if (!pcmk_is_set(fsa_input_register, R_CIB_CONNECTED)) {
 131 
 132             cib_retries++;
 133             crm_warn("Couldn't complete CIB registration %d"
 134                      " times... pause and retry", cib_retries);
 135 
 136             if (cib_retries < 30) {
 137                 controld_start_timer(wait_timer);
 138                 crmd_fsa_stall(FALSE);
 139 
 140             } else {
 141                 crm_err("Could not complete CIB"
 142                         " registration  %d times..." " hard error", cib_retries);
 143                 register_fsa_error(C_FSA_INTERNAL, I_ERROR, NULL);
 144             }
 145         }
 146     }
 147 }
 148 
 149 /*!
 150  * \internal
 151  * \brief Get CIB call options to use local scope if master unavailable
 152  *
 153  * \return CIB call options
 154  */
 155 int crmd_cib_smart_opt()
     /* [previous][next][first][last][top][bottom][index][help] */
 156 {
 157     int call_opt = cib_quorum_override;
 158 
 159     if (fsa_state == S_ELECTION || fsa_state == S_PENDING) {
 160         crm_info("Sending update to local CIB in state: %s", fsa_state2string(fsa_state));
 161         cib__set_call_options(call_opt, "update", cib_scope_local);
 162     }
 163     return call_opt;
 164 }
 165 
 166 /*!
 167  * \internal
 168  * \brief Check whether an action type should be recorded in the CIB
 169  *
 170  * \param[in] action  Action type
 171  *
 172  * \return TRUE if action should be recorded, FALSE otherwise
 173  */
 174 bool
 175 controld_action_is_recordable(const char *action)
     /* [previous][next][first][last][top][bottom][index][help] */
 176 {
 177     return !pcmk__strcase_any_of(action, CRMD_ACTION_CANCEL, CRMD_ACTION_DELETE,
 178                             CRMD_ACTION_NOTIFY, CRMD_ACTION_METADATA, NULL);
 179 }
 180 
 181 static void
 182 cib_delete_callback(xmlNode *msg, int call_id, int rc, xmlNode *output,
     /* [previous][next][first][last][top][bottom][index][help] */
 183                     void *user_data)
 184 {
 185     char *desc = user_data;
 186 
 187     if (rc == 0) {
 188         crm_debug("Deletion of %s (via CIB call %d) succeeded", desc, call_id);
 189     } else {
 190         crm_warn("Deletion of %s (via CIB call %d) failed: %s " CRM_XS " rc=%d",
 191                  desc, call_id, pcmk_strerror(rc), rc);
 192     }
 193 }
 194 
 195 // Searches for various portions of node_state to delete
 196 
 197 // Match a particular node's node_state (takes node name 1x)
 198 #define XPATH_NODE_STATE        "//" XML_CIB_TAG_STATE "[@" XML_ATTR_UNAME "='%s']"
 199 
 200 // Node's lrm section (name 1x)
 201 #define XPATH_NODE_LRM          XPATH_NODE_STATE "/" XML_CIB_TAG_LRM
 202 
 203 // Node's lrm_rsc_op entries and lrm_resource entries without lock (name 2x)
 204 #define XPATH_NODE_LRM_UNLOCKED XPATH_NODE_STATE "//" XML_LRM_TAG_RSC_OP    \
 205                                 "|" XPATH_NODE_STATE                        \
 206                                 "//" XML_LRM_TAG_RESOURCE                   \
 207                                 "[not(@" XML_CONFIG_ATTR_SHUTDOWN_LOCK ")]"
 208 
 209 // Node's transient_attributes section (name 1x)
 210 #define XPATH_NODE_ATTRS        XPATH_NODE_STATE "/" XML_TAG_TRANSIENT_NODEATTRS
 211 
 212 // Everything under node_state (name 1x)
 213 #define XPATH_NODE_ALL          XPATH_NODE_STATE "/*"
 214 
 215 // Unlocked history + transient attributes (name 3x)
 216 #define XPATH_NODE_ALL_UNLOCKED XPATH_NODE_LRM_UNLOCKED "|" XPATH_NODE_ATTRS
 217 
 218 /*!
 219  * \internal
 220  * \brief Delete subsection of a node's CIB node_state
 221  *
 222  * \param[in] uname    Desired node
 223  * \param[in] section  Subsection of node_state to delete
 224  * \param[in] options  CIB call options to use
 225  */
 226 void
 227 controld_delete_node_state(const char *uname, enum controld_section_e section,
     /* [previous][next][first][last][top][bottom][index][help] */
 228                            int options)
 229 {
 230     char *xpath = NULL;
 231     char *desc = NULL;
 232 
 233     CRM_CHECK(uname != NULL, return);
 234     switch (section) {
 235         case controld_section_lrm:
 236             xpath = crm_strdup_printf(XPATH_NODE_LRM, uname);
 237             desc = crm_strdup_printf("resource history for node %s", uname);
 238             break;
 239         case controld_section_lrm_unlocked:
 240             xpath = crm_strdup_printf(XPATH_NODE_LRM_UNLOCKED, uname, uname);
 241             desc = crm_strdup_printf("resource history (other than shutdown "
 242                                      "locks) for node %s", uname);
 243             break;
 244         case controld_section_attrs:
 245             xpath = crm_strdup_printf(XPATH_NODE_ATTRS, uname);
 246             desc = crm_strdup_printf("transient attributes for node %s", uname);
 247             break;
 248         case controld_section_all:
 249             xpath = crm_strdup_printf(XPATH_NODE_ALL, uname);
 250             desc = crm_strdup_printf("all state for node %s", uname);
 251             break;
 252         case controld_section_all_unlocked:
 253             xpath = crm_strdup_printf(XPATH_NODE_ALL_UNLOCKED,
 254                                       uname, uname, uname);
 255             desc = crm_strdup_printf("all state (other than shutdown locks) "
 256                                      "for node %s", uname);
 257             break;
 258     }
 259 
 260     if (fsa_cib_conn == NULL) {
 261         crm_warn("Unable to delete %s: no CIB connection", desc);
 262         free(desc);
 263     } else {
 264         int call_id;
 265 
 266         cib__set_call_options(options, "node state deletion",
 267                               cib_quorum_override|cib_xpath|cib_multiple);
 268         call_id = fsa_cib_conn->cmds->remove(fsa_cib_conn, xpath, NULL, options);
 269         crm_info("Deleting %s (via CIB call %d) " CRM_XS " xpath=%s",
 270                  desc, call_id, xpath);
 271         fsa_register_cib_callback(call_id, FALSE, desc, cib_delete_callback);
 272         // CIB library handles freeing desc
 273     }
 274     free(xpath);
 275 }
 276 
 277 // Takes node name and resource ID
 278 #define XPATH_RESOURCE_HISTORY "//" XML_CIB_TAG_STATE                       \
 279                                "[@" XML_ATTR_UNAME "='%s']/"                \
 280                                XML_CIB_TAG_LRM "/" XML_LRM_TAG_RESOURCES    \
 281                                "/" XML_LRM_TAG_RESOURCE                     \
 282                                "[@" XML_ATTR_ID "='%s']"
 283 // @TODO could add "and @XML_CONFIG_ATTR_SHUTDOWN_LOCK" to limit to locks
 284 
 285 /*!
 286  * \internal
 287  * \brief Clear resource history from CIB for a given resource and node
 288  *
 289  * \param[in]  rsc_id        ID of resource to be cleared
 290  * \param[in]  node          Node whose resource history should be cleared
 291  * \param[in]  user_name     ACL user name to use
 292  * \param[in]  call_options  CIB call options
 293  *
 294  * \return Standard Pacemaker return code
 295  */
 296 int
 297 controld_delete_resource_history(const char *rsc_id, const char *node,
     /* [previous][next][first][last][top][bottom][index][help] */
 298                                  const char *user_name, int call_options)
 299 {
 300     char *desc = NULL;
 301     char *xpath = NULL;
 302     int rc = pcmk_rc_ok;
 303 
 304     CRM_CHECK((rsc_id != NULL) && (node != NULL), return EINVAL);
 305 
 306     desc = crm_strdup_printf("resource history for %s on %s", rsc_id, node);
 307     if (fsa_cib_conn == NULL) {
 308         crm_err("Unable to clear %s: no CIB connection", desc);
 309         free(desc);
 310         return ENOTCONN;
 311     }
 312 
 313     // Ask CIB to delete the entry
 314     xpath = crm_strdup_printf(XPATH_RESOURCE_HISTORY, node, rsc_id);
 315     rc = cib_internal_op(fsa_cib_conn, CIB_OP_DELETE, NULL, xpath, NULL,
 316                          NULL, call_options|cib_xpath, user_name);
 317 
 318     if (rc < 0) {
 319         rc = pcmk_legacy2rc(rc);
 320         crm_err("Could not delete resource status of %s on %s%s%s: %s "
 321                 CRM_XS " rc=%d", rsc_id, node,
 322                 (user_name? " for user " : ""), (user_name? user_name : ""),
 323                 pcmk_rc_str(rc), rc);
 324         free(desc);
 325         free(xpath);
 326         return rc;
 327     }
 328 
 329     if (pcmk_is_set(call_options, cib_sync_call)) {
 330         if (pcmk_is_set(call_options, cib_dryrun)) {
 331             crm_debug("Deletion of %s would succeed", desc);
 332         } else {
 333             crm_debug("Deletion of %s succeeded", desc);
 334         }
 335         free(desc);
 336 
 337     } else {
 338         crm_info("Clearing %s (via CIB call %d) " CRM_XS " xpath=%s",
 339                  desc, rc, xpath);
 340         fsa_register_cib_callback(rc, FALSE, desc, cib_delete_callback);
 341         // CIB library handles freeing desc
 342     }
 343 
 344     free(xpath);
 345     return pcmk_rc_ok;
 346 }

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