root/lib/pengine/complex.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_resource_type
  2. dup_attr
  3. expand_parents_fixed_nvpairs
  4. get_meta_attributes
  5. get_rsc_attributes
  6. pe_get_versioned_attributes
  7. template_op_key
  8. unpack_template
  9. add_template_rsc
  10. detect_promotable
  11. free_params_table
  12. pe_rsc_params
  13. common_unpack
  14. common_update_score
  15. is_parent
  16. uber_parent
  17. common_free
  18. pe__find_active_on
  19. pe__find_active_requires
  20. pe__count_common
  21. pe__set_next_role

   1 /*
   2  * Copyright 2004-2022 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 Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <crm/pengine/rules.h>
  13 #include <crm/pengine/internal.h>
  14 #include <crm/msg_xml.h>
  15 #include <crm/common/xml_internal.h>
  16 
  17 void populate_hash(xmlNode * nvpair_list, GHashTable * hash, const char **attrs, int attrs_length);
  18 
  19 resource_object_functions_t resource_class_functions[] = {
  20     {
  21          native_unpack,
  22          native_find_rsc,
  23          native_parameter,
  24          native_print,
  25          native_active,
  26          native_resource_state,
  27          native_location,
  28          native_free,
  29          pe__count_common,
  30          pe__native_is_filtered,
  31     },
  32     {
  33          group_unpack,
  34          native_find_rsc,
  35          native_parameter,
  36          group_print,
  37          group_active,
  38          group_resource_state,
  39          native_location,
  40          group_free,
  41          pe__count_common,
  42          pe__group_is_filtered,
  43     },
  44     {
  45          clone_unpack,
  46          native_find_rsc,
  47          native_parameter,
  48          clone_print,
  49          clone_active,
  50          clone_resource_state,
  51          native_location,
  52          clone_free,
  53          pe__count_common,
  54          pe__clone_is_filtered,
  55     },
  56     {
  57          pe__unpack_bundle,
  58          native_find_rsc,
  59          native_parameter,
  60          pe__print_bundle,
  61          pe__bundle_active,
  62          pe__bundle_resource_state,
  63          native_location,
  64          pe__free_bundle,
  65          pe__count_bundle,
  66          pe__bundle_is_filtered,
  67     }
  68 };
  69 
  70 static enum pe_obj_types
  71 get_resource_type(const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73     if (pcmk__str_eq(name, XML_CIB_TAG_RESOURCE, pcmk__str_casei)) {
  74         return pe_native;
  75 
  76     } else if (pcmk__str_eq(name, XML_CIB_TAG_GROUP, pcmk__str_casei)) {
  77         return pe_group;
  78 
  79     } else if (pcmk__str_eq(name, XML_CIB_TAG_INCARNATION, pcmk__str_casei)) {
  80         return pe_clone;
  81 
  82     } else if (pcmk__str_eq(name, PCMK_XE_PROMOTABLE_LEGACY, pcmk__str_casei)) {
  83         // @COMPAT deprecated since 2.0.0
  84         return pe_clone;
  85 
  86     } else if (pcmk__str_eq(name, XML_CIB_TAG_CONTAINER, pcmk__str_casei)) {
  87         return pe_container;
  88     }
  89 
  90     return pe_unknown;
  91 }
  92 
  93 static void
  94 dup_attr(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96     add_hash_param(user_data, key, value);
  97 }
  98 
  99 static void
 100 expand_parents_fixed_nvpairs(pe_resource_t * rsc, pe_rule_eval_data_t * rule_data, GHashTable * meta_hash, pe_working_set_t * data_set)
     /* [previous][next][first][last][top][bottom][index][help] */
 101 {
 102     GHashTable *parent_orig_meta = pcmk__strkey_table(free, free);
 103     pe_resource_t *p = rsc->parent;
 104 
 105     if (p == NULL) {
 106         return ;
 107     }
 108 
 109     /* Search all parent resources, get the fixed value of "meta_attributes" set only in the original xml, and stack it in the hash table. */
 110     /* The fixed value of the lower parent resource takes precedence and is not overwritten. */
 111     while(p != NULL) {
 112         /* A hash table for comparison is generated, including the id-ref. */
 113         pe__unpack_dataset_nvpairs(p->xml, XML_TAG_META_SETS,
 114                                rule_data, parent_orig_meta, NULL, FALSE, data_set);
 115         p = p->parent; 
 116     }
 117 
 118     /* If there is a fixed value of "meta_attributes" of the parent resource, it will be processed. */
 119     if (parent_orig_meta != NULL) {
 120         GHashTableIter iter;
 121         char *key = NULL;
 122         char *value = NULL;
 123 
 124         g_hash_table_iter_init(&iter, parent_orig_meta);
 125         while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &value)) {
 126             /* Parameters set in the original xml of the parent resource will also try to overwrite the child resource. */
 127             /* Attributes that already exist in the child lease are not updated. */
 128             dup_attr(key, value, meta_hash);
 129         }
 130     }
 131 
 132     if (parent_orig_meta != NULL) {
 133         g_hash_table_destroy(parent_orig_meta);
 134     }
 135     
 136     return ;
 137 
 138 }
 139 void
 140 get_meta_attributes(GHashTable * meta_hash, pe_resource_t * rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 141                     pe_node_t * node, pe_working_set_t * data_set)
 142 {
 143     pe_rsc_eval_data_t rsc_rule_data = {
 144         .standard = crm_element_value(rsc->xml, XML_AGENT_ATTR_CLASS),
 145         .provider = crm_element_value(rsc->xml, XML_AGENT_ATTR_PROVIDER),
 146         .agent = crm_element_value(rsc->xml, XML_EXPR_ATTR_TYPE)
 147     };
 148 
 149     pe_rule_eval_data_t rule_data = {
 150         .node_hash = NULL,
 151         .role = RSC_ROLE_UNKNOWN,
 152         .now = data_set->now,
 153         .match_data = NULL,
 154         .rsc_data = &rsc_rule_data,
 155         .op_data = NULL
 156     };
 157 
 158     if (node) {
 159         rule_data.node_hash = node->details->attrs;
 160     }
 161 
 162     for (xmlAttrPtr a = pcmk__xe_first_attr(rsc->xml); a != NULL; a = a->next) {
 163         const char *prop_name = (const char *) a->name;
 164         const char *prop_value = crm_element_value(rsc->xml, prop_name);
 165 
 166         add_hash_param(meta_hash, prop_name, prop_value);
 167     }
 168 
 169     pe__unpack_dataset_nvpairs(rsc->xml, XML_TAG_META_SETS, &rule_data,
 170                                meta_hash, NULL, FALSE, data_set);
 171 
 172     /* Set the "meta_attributes" explicitly set in the parent resource to the hash table of the child resource. */
 173     /* If it is already explicitly set as a child, it will not be overwritten. */
 174     if (rsc->parent != NULL) {
 175         expand_parents_fixed_nvpairs(rsc, &rule_data, meta_hash, data_set);
 176     }
 177 
 178     /* check the defaults */
 179     pe__unpack_dataset_nvpairs(data_set->rsc_defaults, XML_TAG_META_SETS,
 180                                &rule_data, meta_hash, NULL, FALSE, data_set);
 181 
 182     /* If there is "meta_attributes" that the parent resource has not explicitly set, set a value that is not set from rsc_default either. */
 183     /* The values already set up to this point will not be overwritten. */
 184     if (rsc->parent) {
 185         g_hash_table_foreach(rsc->parent->meta, dup_attr, meta_hash);
 186     }
 187 }
 188 
 189 void
 190 get_rsc_attributes(GHashTable * meta_hash, pe_resource_t * rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 191                    pe_node_t * node, pe_working_set_t * data_set)
 192 {
 193     pe_rule_eval_data_t rule_data = {
 194         .node_hash = NULL,
 195         .role = RSC_ROLE_UNKNOWN,
 196         .now = data_set->now,
 197         .match_data = NULL,
 198         .rsc_data = NULL,
 199         .op_data = NULL
 200     };
 201 
 202     if (node) {
 203         rule_data.node_hash = node->details->attrs;
 204     }
 205 
 206     pe__unpack_dataset_nvpairs(rsc->xml, XML_TAG_ATTR_SETS, &rule_data,
 207                                meta_hash, NULL, FALSE, data_set);
 208 
 209     /* set anything else based on the parent */
 210     if (rsc->parent != NULL) {
 211         get_rsc_attributes(meta_hash, rsc->parent, node, data_set);
 212 
 213     } else {
 214         /* and finally check the defaults */
 215         pe__unpack_dataset_nvpairs(data_set->rsc_defaults, XML_TAG_ATTR_SETS,
 216                                    &rule_data, meta_hash, NULL, FALSE, data_set);
 217     }
 218 }
 219 
 220 #if ENABLE_VERSIONED_ATTRS
 221 void
 222 pe_get_versioned_attributes(xmlNode * meta_hash, pe_resource_t * rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 223                             pe_node_t * node, pe_working_set_t * data_set)
 224 {
 225     pe_rule_eval_data_t rule_data = {
 226         .node_hash = (node == NULL)? NULL : node->details->attrs,
 227         .role = RSC_ROLE_UNKNOWN,
 228         .now = data_set->now,
 229         .match_data = NULL,
 230         .rsc_data = NULL,
 231         .op_data = NULL
 232     };
 233 
 234     pe_eval_versioned_attributes(data_set->input, rsc->xml, XML_TAG_ATTR_SETS,
 235                                  &rule_data, meta_hash, NULL);
 236 
 237     /* set anything else based on the parent */
 238     if (rsc->parent != NULL) {
 239         pe_get_versioned_attributes(meta_hash, rsc->parent, node, data_set);
 240 
 241     } else {
 242         /* and finally check the defaults */
 243         pe_eval_versioned_attributes(data_set->input, data_set->rsc_defaults,
 244                                      XML_TAG_ATTR_SETS, &rule_data, meta_hash,
 245                                      NULL);
 246     }
 247 }
 248 #endif
 249 
 250 static char *
 251 template_op_key(xmlNode * op)
     /* [previous][next][first][last][top][bottom][index][help] */
 252 {
 253     const char *name = crm_element_value(op, "name");
 254     const char *role = crm_element_value(op, "role");
 255     char *key = NULL;
 256 
 257     if ((role == NULL)
 258         || pcmk__strcase_any_of(role, RSC_ROLE_STARTED_S, RSC_ROLE_UNPROMOTED_S,
 259                                 RSC_ROLE_UNPROMOTED_LEGACY_S, NULL)) {
 260         role = RSC_ROLE_UNKNOWN_S;
 261     }
 262 
 263     key = crm_strdup_printf("%s-%s", name, role);
 264     return key;
 265 }
 266 
 267 static gboolean
 268 unpack_template(xmlNode * xml_obj, xmlNode ** expanded_xml, pe_working_set_t * data_set)
     /* [previous][next][first][last][top][bottom][index][help] */
 269 {
 270     xmlNode *cib_resources = NULL;
 271     xmlNode *template = NULL;
 272     xmlNode *new_xml = NULL;
 273     xmlNode *child_xml = NULL;
 274     xmlNode *rsc_ops = NULL;
 275     xmlNode *template_ops = NULL;
 276     const char *template_ref = NULL;
 277     const char *clone = NULL;
 278     const char *id = NULL;
 279 
 280     if (xml_obj == NULL) {
 281         pe_err("No resource object for template unpacking");
 282         return FALSE;
 283     }
 284 
 285     template_ref = crm_element_value(xml_obj, XML_CIB_TAG_RSC_TEMPLATE);
 286     if (template_ref == NULL) {
 287         return TRUE;
 288     }
 289 
 290     id = ID(xml_obj);
 291     if (id == NULL) {
 292         pe_err("'%s' object must have a id", crm_element_name(xml_obj));
 293         return FALSE;
 294     }
 295 
 296     if (pcmk__str_eq(template_ref, id, pcmk__str_none)) {
 297         pe_err("The resource object '%s' should not reference itself", id);
 298         return FALSE;
 299     }
 300 
 301     cib_resources = get_xpath_object("//"XML_CIB_TAG_RESOURCES, data_set->input, LOG_TRACE);
 302     if (cib_resources == NULL) {
 303         pe_err("No resources configured");
 304         return FALSE;
 305     }
 306 
 307     template = pcmk__xe_match(cib_resources, XML_CIB_TAG_RSC_TEMPLATE,
 308                               XML_ATTR_ID, template_ref);
 309     if (template == NULL) {
 310         pe_err("No template named '%s'", template_ref);
 311         return FALSE;
 312     }
 313 
 314     new_xml = copy_xml(template);
 315     xmlNodeSetName(new_xml, xml_obj->name);
 316     crm_xml_replace(new_xml, XML_ATTR_ID, id);
 317 
 318     clone = crm_element_value(xml_obj, XML_RSC_ATTR_INCARNATION);
 319     if(clone) {
 320         crm_xml_add(new_xml, XML_RSC_ATTR_INCARNATION, clone);
 321     }
 322 
 323     template_ops = find_xml_node(new_xml, "operations", FALSE);
 324 
 325     for (child_xml = pcmk__xe_first_child(xml_obj); child_xml != NULL;
 326          child_xml = pcmk__xe_next(child_xml)) {
 327         xmlNode *new_child = NULL;
 328 
 329         new_child = add_node_copy(new_xml, child_xml);
 330 
 331         if (pcmk__str_eq((const char *)new_child->name, "operations", pcmk__str_none)) {
 332             rsc_ops = new_child;
 333         }
 334     }
 335 
 336     if (template_ops && rsc_ops) {
 337         xmlNode *op = NULL;
 338         GHashTable *rsc_ops_hash = pcmk__strkey_table(free, NULL);
 339 
 340         for (op = pcmk__xe_first_child(rsc_ops); op != NULL;
 341              op = pcmk__xe_next(op)) {
 342 
 343             char *key = template_op_key(op);
 344 
 345             g_hash_table_insert(rsc_ops_hash, key, op);
 346         }
 347 
 348         for (op = pcmk__xe_first_child(template_ops); op != NULL;
 349              op = pcmk__xe_next(op)) {
 350 
 351             char *key = template_op_key(op);
 352 
 353             if (g_hash_table_lookup(rsc_ops_hash, key) == NULL) {
 354                 add_node_copy(rsc_ops, op);
 355             }
 356 
 357             free(key);
 358         }
 359 
 360         if (rsc_ops_hash) {
 361             g_hash_table_destroy(rsc_ops_hash);
 362         }
 363 
 364         free_xml(template_ops);
 365     }
 366 
 367     /*free_xml(*expanded_xml); */
 368     *expanded_xml = new_xml;
 369 
 370     /* Disable multi-level templates for now */
 371     /*if(unpack_template(new_xml, expanded_xml, data_set) == FALSE) {
 372        free_xml(*expanded_xml);
 373        *expanded_xml = NULL;
 374 
 375        return FALSE;
 376        } */
 377 
 378     return TRUE;
 379 }
 380 
 381 static gboolean
 382 add_template_rsc(xmlNode * xml_obj, pe_working_set_t * data_set)
     /* [previous][next][first][last][top][bottom][index][help] */
 383 {
 384     const char *template_ref = NULL;
 385     const char *id = NULL;
 386 
 387     if (xml_obj == NULL) {
 388         pe_err("No resource object for processing resource list of template");
 389         return FALSE;
 390     }
 391 
 392     template_ref = crm_element_value(xml_obj, XML_CIB_TAG_RSC_TEMPLATE);
 393     if (template_ref == NULL) {
 394         return TRUE;
 395     }
 396 
 397     id = ID(xml_obj);
 398     if (id == NULL) {
 399         pe_err("'%s' object must have a id", crm_element_name(xml_obj));
 400         return FALSE;
 401     }
 402 
 403     if (pcmk__str_eq(template_ref, id, pcmk__str_none)) {
 404         pe_err("The resource object '%s' should not reference itself", id);
 405         return FALSE;
 406     }
 407 
 408     if (add_tag_ref(data_set->template_rsc_sets, template_ref, id) == FALSE) {
 409         return FALSE;
 410     }
 411 
 412     return TRUE;
 413 }
 414 
 415 static bool
 416 detect_promotable(pe_resource_t *rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
 417 {
 418     const char *promotable = g_hash_table_lookup(rsc->meta,
 419                                                  XML_RSC_ATTR_PROMOTABLE);
 420 
 421     if (crm_is_true(promotable)) {
 422         return TRUE;
 423     }
 424 
 425     // @COMPAT deprecated since 2.0.0
 426     if (pcmk__str_eq(crm_element_name(rsc->xml), PCMK_XE_PROMOTABLE_LEGACY,
 427                      pcmk__str_casei)) {
 428         /* @TODO in some future version, pe_warn_once() here,
 429          *       then drop support in even later version
 430          */
 431         g_hash_table_insert(rsc->meta, strdup(XML_RSC_ATTR_PROMOTABLE),
 432                             strdup(XML_BOOLEAN_TRUE));
 433         return TRUE;
 434     }
 435     return FALSE;
 436 }
 437 
 438 static void
 439 free_params_table(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 440 {
 441     g_hash_table_destroy((GHashTable *) data);
 442 }
 443 
 444 /*!
 445  * \brief Get a table of resource parameters
 446  *
 447  * \param[in] rsc       Resource to query
 448  * \param[in] node      Node for evaluating rules (NULL for defaults)
 449  * \param[in] data_set  Cluster working set
 450  *
 451  * \return Hash table containing resource parameter names and values
 452  *         (or NULL if \p rsc or \p data_set is NULL)
 453  * \note The returned table will be destroyed when the resource is freed, so
 454  *       callers should not destroy it.
 455  */
 456 GHashTable *
 457 pe_rsc_params(pe_resource_t *rsc, pe_node_t *node, pe_working_set_t *data_set)
     /* [previous][next][first][last][top][bottom][index][help] */
 458 {
 459     GHashTable *params_on_node = NULL;
 460 
 461     /* A NULL node is used to request the resource's default parameters
 462      * (not evaluated for node), but we always want something non-NULL
 463      * as a hash table key.
 464      */
 465     const char *node_name = "";
 466 
 467     // Sanity check
 468     if ((rsc == NULL) || (data_set == NULL)) {
 469         return NULL;
 470     }
 471     if ((node != NULL) && (node->details->uname != NULL)) {
 472         node_name = node->details->uname;
 473     }
 474 
 475     // Find the parameter table for given node
 476     if (rsc->parameter_cache == NULL) {
 477         rsc->parameter_cache = pcmk__strikey_table(free, free_params_table);
 478     } else {
 479         params_on_node = g_hash_table_lookup(rsc->parameter_cache, node_name);
 480     }
 481 
 482     // If none exists yet, create one with parameters evaluated for node
 483     if (params_on_node == NULL) {
 484         params_on_node = pcmk__strkey_table(free, free);
 485         get_rsc_attributes(params_on_node, rsc, node, data_set);
 486         g_hash_table_insert(rsc->parameter_cache, strdup(node_name),
 487                             params_on_node);
 488     }
 489     return params_on_node;
 490 }
 491 
 492 gboolean
 493 common_unpack(xmlNode * xml_obj, pe_resource_t ** rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 494               pe_resource_t * parent, pe_working_set_t * data_set)
 495 {
 496     bool isdefault = FALSE;
 497     xmlNode *expanded_xml = NULL;
 498     xmlNode *ops = NULL;
 499     const char *value = NULL;
 500     const char *rclass = NULL; /* Look for this after any templates have been expanded */
 501     const char *id = crm_element_value(xml_obj, XML_ATTR_ID);
 502     bool guest_node = FALSE;
 503     bool remote_node = FALSE;
 504     bool has_versioned_params = FALSE;
 505 
 506     pe_rule_eval_data_t rule_data = {
 507         .node_hash = NULL,
 508         .role = RSC_ROLE_UNKNOWN,
 509         .now = data_set->now,
 510         .match_data = NULL,
 511         .rsc_data = NULL,
 512         .op_data = NULL
 513     };
 514 
 515     crm_log_xml_trace(xml_obj, "Processing resource input...");
 516 
 517     if (id == NULL) {
 518         pe_err("Must specify id tag in <resource>");
 519         return FALSE;
 520 
 521     } else if (rsc == NULL) {
 522         pe_err("Nowhere to unpack resource into");
 523         return FALSE;
 524 
 525     }
 526 
 527     if (unpack_template(xml_obj, &expanded_xml, data_set) == FALSE) {
 528         return FALSE;
 529     }
 530 
 531     *rsc = calloc(1, sizeof(pe_resource_t));
 532     (*rsc)->cluster = data_set;
 533 
 534     if (expanded_xml) {
 535         crm_log_xml_trace(expanded_xml, "Expanded resource...");
 536         (*rsc)->xml = expanded_xml;
 537         (*rsc)->orig_xml = xml_obj;
 538 
 539     } else {
 540         (*rsc)->xml = xml_obj;
 541         (*rsc)->orig_xml = NULL;
 542     }
 543 
 544     /* Do not use xml_obj from here on, use (*rsc)->xml in case templates are involved */
 545     rclass = crm_element_value((*rsc)->xml, XML_AGENT_ATTR_CLASS);
 546     (*rsc)->parent = parent;
 547 
 548     ops = find_xml_node((*rsc)->xml, "operations", FALSE);
 549     (*rsc)->ops_xml = expand_idref(ops, data_set->input);
 550 
 551     (*rsc)->variant = get_resource_type(crm_element_name((*rsc)->xml));
 552     if ((*rsc)->variant == pe_unknown) {
 553         pe_err("Unknown resource type: %s", crm_element_name((*rsc)->xml));
 554         free(*rsc);
 555         return FALSE;
 556     }
 557 
 558 #if ENABLE_VERSIONED_ATTRS
 559     (*rsc)->versioned_parameters = create_xml_node(NULL, XML_TAG_RSC_VER_ATTRS);
 560 #endif
 561 
 562     (*rsc)->meta = pcmk__strkey_table(free, free);
 563     (*rsc)->allowed_nodes = pcmk__strkey_table(NULL, free);
 564     (*rsc)->known_on = pcmk__strkey_table(NULL, free);
 565 
 566     value = crm_element_value((*rsc)->xml, XML_RSC_ATTR_INCARNATION);
 567     if (value) {
 568         (*rsc)->id = crm_strdup_printf("%s:%s", id, value);
 569         add_hash_param((*rsc)->meta, XML_RSC_ATTR_INCARNATION, value);
 570 
 571     } else {
 572         (*rsc)->id = strdup(id);
 573     }
 574 
 575     (*rsc)->fns = &resource_class_functions[(*rsc)->variant];
 576     pe_rsc_trace((*rsc), "Unpacking resource...");
 577 
 578     get_meta_attributes((*rsc)->meta, *rsc, NULL, data_set);
 579     (*rsc)->parameters = pe_rsc_params(*rsc, NULL, data_set); // \deprecated
 580 #if ENABLE_VERSIONED_ATTRS
 581     pe_get_versioned_attributes((*rsc)->versioned_parameters, *rsc, NULL, data_set);
 582 #endif
 583 
 584     (*rsc)->flags = 0;
 585     pe__set_resource_flags(*rsc, pe_rsc_runnable|pe_rsc_provisional);
 586 
 587     if (!pcmk_is_set(data_set->flags, pe_flag_maintenance_mode)) {
 588         pe__set_resource_flags(*rsc, pe_rsc_managed);
 589     }
 590 
 591     (*rsc)->rsc_cons = NULL;
 592     (*rsc)->rsc_tickets = NULL;
 593     (*rsc)->actions = NULL;
 594     (*rsc)->role = RSC_ROLE_STOPPED;
 595     (*rsc)->next_role = RSC_ROLE_UNKNOWN;
 596 
 597     (*rsc)->recovery_type = recovery_stop_start;
 598     (*rsc)->stickiness = 0;
 599     (*rsc)->migration_threshold = INFINITY;
 600     (*rsc)->failure_timeout = 0;
 601 
 602     value = g_hash_table_lookup((*rsc)->meta, XML_CIB_ATTR_PRIORITY);
 603     (*rsc)->priority = char2score(value);
 604 
 605     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_CRITICAL);
 606     if ((value == NULL) || crm_is_true(value)) {
 607         pe__set_resource_flags(*rsc, pe_rsc_critical);
 608     }
 609 
 610     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_NOTIFY);
 611     if (crm_is_true(value)) {
 612         pe__set_resource_flags(*rsc, pe_rsc_notify);
 613     }
 614 
 615     if (xml_contains_remote_node((*rsc)->xml)) {
 616         (*rsc)->is_remote_node = TRUE;
 617         if (g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_CONTAINER)) {
 618             guest_node = TRUE;
 619         } else {
 620             remote_node = TRUE;
 621         }
 622     }
 623 
 624     value = g_hash_table_lookup((*rsc)->meta, XML_OP_ATTR_ALLOW_MIGRATE);
 625 #if ENABLE_VERSIONED_ATTRS
 626     has_versioned_params = xml_has_children((*rsc)->versioned_parameters);
 627 #endif
 628     if (crm_is_true(value) && has_versioned_params) {
 629         pe_rsc_trace((*rsc), "Migration is disabled for resources with versioned parameters");
 630     } else if (crm_is_true(value)) {
 631         pe__set_resource_flags(*rsc, pe_rsc_allow_migrate);
 632     } else if ((value == NULL) && remote_node && !has_versioned_params) {
 633         /* By default, we want remote nodes to be able
 634          * to float around the cluster without having to stop all the
 635          * resources within the remote-node before moving. Allowing
 636          * migration support enables this feature. If this ever causes
 637          * problems, migration support can be explicitly turned off with
 638          * allow-migrate=false.
 639          * We don't support migration for versioned resources, though. */
 640         pe__set_resource_flags(*rsc, pe_rsc_allow_migrate);
 641     }
 642 
 643     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_MANAGED);
 644     if (value != NULL && !pcmk__str_eq("default", value, pcmk__str_casei)) {
 645         if (crm_is_true(value)) {
 646             pe__set_resource_flags(*rsc, pe_rsc_managed);
 647         } else {
 648             pe__clear_resource_flags(*rsc, pe_rsc_managed);
 649         }
 650     }
 651 
 652     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_MAINTENANCE);
 653     if (crm_is_true(value)) {
 654         pe__clear_resource_flags(*rsc, pe_rsc_managed);
 655         pe__set_resource_flags(*rsc, pe_rsc_maintenance);
 656     }
 657     if (pcmk_is_set(data_set->flags, pe_flag_maintenance_mode)) {
 658         pe__clear_resource_flags(*rsc, pe_rsc_managed);
 659         pe__set_resource_flags(*rsc, pe_rsc_maintenance);
 660     }
 661 
 662     if (pe_rsc_is_clone(uber_parent(*rsc))) {
 663         value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_UNIQUE);
 664         if (crm_is_true(value)) {
 665             pe__set_resource_flags(*rsc, pe_rsc_unique);
 666         }
 667         if (detect_promotable(*rsc)) {
 668             pe__set_resource_flags(*rsc, pe_rsc_promotable);
 669         }
 670     } else {
 671         pe__set_resource_flags(*rsc, pe_rsc_unique);
 672     }
 673 
 674     pe_rsc_trace((*rsc), "Options for %s", (*rsc)->id);
 675 
 676     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_RESTART);
 677     if (pcmk__str_eq(value, "restart", pcmk__str_casei)) {
 678         (*rsc)->restart_type = pe_restart_restart;
 679         pe_rsc_trace((*rsc), "\tDependency restart handling: restart");
 680         pe_warn_once(pe_wo_restart_type,
 681                      "Support for restart-type is deprecated and will be removed in a future release");
 682 
 683     } else {
 684         (*rsc)->restart_type = pe_restart_ignore;
 685         pe_rsc_trace((*rsc), "\tDependency restart handling: ignore");
 686     }
 687 
 688     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_MULTIPLE);
 689     if (pcmk__str_eq(value, "stop_only", pcmk__str_casei)) {
 690         (*rsc)->recovery_type = recovery_stop_only;
 691         pe_rsc_trace((*rsc), "\tMultiple running resource recovery: stop only");
 692 
 693     } else if (pcmk__str_eq(value, "block", pcmk__str_casei)) {
 694         (*rsc)->recovery_type = recovery_block;
 695         pe_rsc_trace((*rsc), "\tMultiple running resource recovery: block");
 696 
 697     } else if (pcmk__str_eq(value, "stop_unexpected", pcmk__str_casei)) {
 698         (*rsc)->recovery_type = recovery_stop_unexpected;
 699         pe_rsc_trace((*rsc), "\tMultiple running resource recovery: "
 700                              "stop unexpected instances");
 701 
 702     } else { // "stop_start"
 703         if (!pcmk__str_eq(value, "stop_start",
 704                           pcmk__str_casei|pcmk__str_null_matches)) {
 705             pe_warn("%s is not a valid value for " XML_RSC_ATTR_MULTIPLE
 706                     ", using default of \"stop_start\"", value);
 707         }
 708         (*rsc)->recovery_type = recovery_stop_start;
 709         pe_rsc_trace((*rsc), "\tMultiple running resource recovery: stop/start");
 710     }
 711 
 712     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_STICKINESS);
 713     if (value != NULL && !pcmk__str_eq("default", value, pcmk__str_casei)) {
 714         (*rsc)->stickiness = char2score(value);
 715     }
 716 
 717     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_FAIL_STICKINESS);
 718     if (value != NULL && !pcmk__str_eq("default", value, pcmk__str_casei)) {
 719         (*rsc)->migration_threshold = char2score(value);
 720         if ((*rsc)->migration_threshold < 0) {
 721             /* @TODO We use 1 here to preserve previous behavior, but this
 722              * should probably use the default (INFINITY) or 0 (to disable)
 723              * instead.
 724              */
 725             pe_warn_once(pe_wo_neg_threshold,
 726                          XML_RSC_ATTR_FAIL_STICKINESS
 727                          " must be non-negative, using 1 instead");
 728             (*rsc)->migration_threshold = 1;
 729         }
 730     }
 731 
 732     if (pcmk__str_eq(rclass, PCMK_RESOURCE_CLASS_STONITH, pcmk__str_casei)) {
 733         pe__set_working_set_flags(data_set, pe_flag_have_stonith_resource);
 734         pe__set_resource_flags(*rsc, pe_rsc_fence_device);
 735     }
 736 
 737     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_REQUIRES);
 738 
 739   handle_requires_pref:
 740     if (pcmk__str_eq(value, "nothing", pcmk__str_casei)) {
 741 
 742     } else if (pcmk__str_eq(value, "quorum", pcmk__str_casei)) {
 743         pe__set_resource_flags(*rsc, pe_rsc_needs_quorum);
 744 
 745     } else if (pcmk__str_eq(value, "unfencing", pcmk__str_casei)) {
 746         if (pcmk_is_set((*rsc)->flags, pe_rsc_fence_device)) {
 747             pcmk__config_warn("Resetting '" XML_RSC_ATTR_REQUIRES "' for %s "
 748                               "to 'quorum' because fencing devices cannot "
 749                               "require unfencing", (*rsc)->id);
 750             value = "quorum";
 751             isdefault = TRUE;
 752             goto handle_requires_pref;
 753 
 754         } else if (!pcmk_is_set(data_set->flags, pe_flag_stonith_enabled)) {
 755             pcmk__config_warn("Resetting '" XML_RSC_ATTR_REQUIRES "' for %s "
 756                               "to 'quorum' because fencing is disabled",
 757                               (*rsc)->id);
 758             value = "quorum";
 759             isdefault = TRUE;
 760             goto handle_requires_pref;
 761 
 762         } else {
 763             pe__set_resource_flags(*rsc, pe_rsc_needs_fencing
 764                                            |pe_rsc_needs_unfencing);
 765         }
 766 
 767     } else if (pcmk__str_eq(value, "fencing", pcmk__str_casei)) {
 768         pe__set_resource_flags(*rsc, pe_rsc_needs_fencing);
 769         if (!pcmk_is_set(data_set->flags, pe_flag_stonith_enabled)) {
 770             pcmk__config_warn("%s requires fencing but fencing is disabled",
 771                               (*rsc)->id);
 772         }
 773 
 774     } else {
 775         const char *orig_value = value;
 776 
 777         isdefault = TRUE;
 778         if (pcmk_is_set((*rsc)->flags, pe_rsc_fence_device)) {
 779             value = "quorum";
 780 
 781         } else if (((*rsc)->variant == pe_native)
 782                    && pcmk__str_eq(crm_element_value((*rsc)->xml, XML_AGENT_ATTR_CLASS), PCMK_RESOURCE_CLASS_OCF, pcmk__str_casei)
 783                    && pcmk__str_eq(crm_element_value((*rsc)->xml, XML_AGENT_ATTR_PROVIDER), "pacemaker", pcmk__str_casei)
 784                    && pcmk__str_eq(crm_element_value((*rsc)->xml, XML_ATTR_TYPE), "remote", pcmk__str_casei)
 785             ) {
 786             value = "quorum";
 787 
 788         } else if (pcmk_is_set(data_set->flags, pe_flag_enable_unfencing)) {
 789             value = "unfencing";
 790 
 791         } else if (pcmk_is_set(data_set->flags, pe_flag_stonith_enabled)) {
 792             value = "fencing";
 793 
 794         } else if (data_set->no_quorum_policy == no_quorum_ignore) {
 795             value = "nothing";
 796 
 797         } else {
 798             value = "quorum";
 799         }
 800 
 801         if (orig_value != NULL) {
 802             pcmk__config_err("Resetting '" XML_RSC_ATTR_REQUIRES "' for %s "
 803                              "to '%s' because '%s' is not valid",
 804                               (*rsc)->id, value, orig_value);
 805         }
 806 
 807         goto handle_requires_pref;
 808     }
 809 
 810     pe_rsc_trace((*rsc), "\tRequired to start: %s%s", value, isdefault?" (default)":"");
 811     value = g_hash_table_lookup((*rsc)->meta, XML_RSC_ATTR_FAIL_TIMEOUT);
 812     if (value != NULL) {
 813         // Stored as seconds
 814         (*rsc)->failure_timeout = (int) (crm_parse_interval_spec(value) / 1000);
 815     }
 816 
 817     if (remote_node) {
 818         GHashTable *params = pe_rsc_params(*rsc, NULL, data_set);
 819 
 820         /* Grabbing the value now means that any rules based on node attributes
 821          * will evaluate to false, so such rules should not be used with
 822          * reconnect_interval.
 823          *
 824          * @TODO Evaluate per node before using
 825          */
 826         value = g_hash_table_lookup(params, XML_REMOTE_ATTR_RECONNECT_INTERVAL);
 827         if (value) {
 828             /* reconnect delay works by setting failure_timeout and preventing the
 829              * connection from starting until the failure is cleared. */
 830             (*rsc)->remote_reconnect_ms = crm_parse_interval_spec(value);
 831             /* we want to override any default failure_timeout in use when remote
 832              * reconnect_interval is in use. */ 
 833             (*rsc)->failure_timeout = (*rsc)->remote_reconnect_ms / 1000;
 834         }
 835     }
 836 
 837     get_target_role(*rsc, &((*rsc)->next_role));
 838     pe_rsc_trace((*rsc), "\tDesired next state: %s",
 839                  (*rsc)->next_role != RSC_ROLE_UNKNOWN ? role2text((*rsc)->next_role) : "default");
 840 
 841     if ((*rsc)->fns->unpack(*rsc, data_set) == FALSE) {
 842         return FALSE;
 843     }
 844 
 845     if (pcmk_is_set(data_set->flags, pe_flag_symmetric_cluster)) {
 846         // This tag must stay exactly the same because it is tested elsewhere
 847         resource_location(*rsc, NULL, 0, "symmetric_default", data_set);
 848     } else if (guest_node) {
 849         /* remote resources tied to a container resource must always be allowed
 850          * to opt-in to the cluster. Whether the connection resource is actually
 851          * allowed to be placed on a node is dependent on the container resource */
 852         resource_location(*rsc, NULL, 0, "remote_connection_default", data_set);
 853     }
 854 
 855     pe_rsc_trace((*rsc), "\tAction notification: %s",
 856                  pcmk_is_set((*rsc)->flags, pe_rsc_notify)? "required" : "not required");
 857 
 858     (*rsc)->utilization = pcmk__strkey_table(free, free);
 859 
 860     pe__unpack_dataset_nvpairs((*rsc)->xml, XML_TAG_UTILIZATION, &rule_data,
 861                                (*rsc)->utilization, NULL, FALSE, data_set);
 862 
 863 /*      data_set->resources = g_list_append(data_set->resources, (*rsc)); */
 864 
 865     if (expanded_xml) {
 866         if (add_template_rsc(xml_obj, data_set) == FALSE) {
 867             return FALSE;
 868         }
 869     }
 870     return TRUE;
 871 }
 872 
 873 void
 874 common_update_score(pe_resource_t * rsc, const char *id, int score)
     /* [previous][next][first][last][top][bottom][index][help] */
 875 {
 876     pe_node_t *node = NULL;
 877 
 878     node = pe_hash_table_lookup(rsc->allowed_nodes, id);
 879     if (node != NULL) {
 880         pe_rsc_trace(rsc, "Updating score for %s on %s: %d + %d", rsc->id, id, node->weight, score);
 881         node->weight = pcmk__add_scores(node->weight, score);
 882     }
 883 
 884     if (rsc->children) {
 885         GList *gIter = rsc->children;
 886 
 887         for (; gIter != NULL; gIter = gIter->next) {
 888             pe_resource_t *child_rsc = (pe_resource_t *) gIter->data;
 889 
 890             common_update_score(child_rsc, id, score);
 891         }
 892     }
 893 }
 894 
 895 gboolean
 896 is_parent(pe_resource_t *child, pe_resource_t *rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
 897 {
 898     pe_resource_t *parent = child;
 899 
 900     if (parent == NULL || rsc == NULL) {
 901         return FALSE;
 902     }
 903     while (parent->parent != NULL) {
 904         if (parent->parent == rsc) {
 905             return TRUE;
 906         }
 907         parent = parent->parent;
 908     }
 909     return FALSE;
 910 }
 911 
 912 pe_resource_t *
 913 uber_parent(pe_resource_t * rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
 914 {
 915     pe_resource_t *parent = rsc;
 916 
 917     if (parent == NULL) {
 918         return NULL;
 919     }
 920     while (parent->parent != NULL && parent->parent->variant != pe_container) {
 921         parent = parent->parent;
 922     }
 923     return parent;
 924 }
 925 
 926 void
 927 common_free(pe_resource_t * rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
 928 {
 929     if (rsc == NULL) {
 930         return;
 931     }
 932 
 933     pe_rsc_trace(rsc, "Freeing %s %d", rsc->id, rsc->variant);
 934 
 935     g_list_free(rsc->rsc_cons);
 936     g_list_free(rsc->rsc_cons_lhs);
 937     g_list_free(rsc->rsc_tickets);
 938     g_list_free(rsc->dangling_migrations);
 939 
 940     if (rsc->parameter_cache != NULL) {
 941         g_hash_table_destroy(rsc->parameter_cache);
 942     }
 943 #if ENABLE_VERSIONED_ATTRS
 944     if (rsc->versioned_parameters != NULL) {
 945         free_xml(rsc->versioned_parameters);
 946     }
 947 #endif
 948     if (rsc->meta != NULL) {
 949         g_hash_table_destroy(rsc->meta);
 950     }
 951     if (rsc->utilization != NULL) {
 952         g_hash_table_destroy(rsc->utilization);
 953     }
 954 
 955     if ((rsc->parent == NULL) && pcmk_is_set(rsc->flags, pe_rsc_orphan)) {
 956         free_xml(rsc->xml);
 957         rsc->xml = NULL;
 958         free_xml(rsc->orig_xml);
 959         rsc->orig_xml = NULL;
 960 
 961         /* if rsc->orig_xml, then rsc->xml is an expanded xml from a template */
 962     } else if (rsc->orig_xml) {
 963         free_xml(rsc->xml);
 964         rsc->xml = NULL;
 965     }
 966     if (rsc->running_on) {
 967         g_list_free(rsc->running_on);
 968         rsc->running_on = NULL;
 969     }
 970     if (rsc->known_on) {
 971         g_hash_table_destroy(rsc->known_on);
 972         rsc->known_on = NULL;
 973     }
 974     if (rsc->actions) {
 975         g_list_free(rsc->actions);
 976         rsc->actions = NULL;
 977     }
 978     if (rsc->allowed_nodes) {
 979         g_hash_table_destroy(rsc->allowed_nodes);
 980         rsc->allowed_nodes = NULL;
 981     }
 982     g_list_free(rsc->fillers);
 983     g_list_free(rsc->rsc_location);
 984     pe_rsc_trace(rsc, "Resource freed");
 985     free(rsc->id);
 986     free(rsc->clone_name);
 987     free(rsc->allocated_to);
 988     free(rsc->variant_opaque);
 989     free(rsc->pending_task);
 990     free(rsc);
 991 }
 992 
 993 /*!
 994  * \brief
 995  * \internal Find a node (and optionally count all) where resource is active
 996  *
 997  * \param[in]  rsc          Resource to check
 998  * \param[out] count_all    If not NULL, will be set to count of active nodes
 999  * \param[out] count_clean  If not NULL, will be set to count of clean nodes
1000  *
1001  * \return An active node (or NULL if resource is not active anywhere)
1002  *
1003  * \note The order of preference is: an active node that is the resource's
1004  *       partial migration source; if the resource's "requires" is "quorum" or
1005  *       "nothing", the first active node in the list that is clean and online;
1006  *       the first active node in the list.
1007  */
1008 pe_node_t *
1009 pe__find_active_on(const pe_resource_t *rsc, unsigned int *count_all,
     /* [previous][next][first][last][top][bottom][index][help] */
1010                    unsigned int *count_clean)
1011 {
1012     pe_node_t *active = NULL;
1013     pe_node_t *node = NULL;
1014     bool keep_looking = FALSE;
1015     bool is_happy = FALSE;
1016 
1017     if (count_all) {
1018         *count_all = 0;
1019     }
1020     if (count_clean) {
1021         *count_clean = 0;
1022     }
1023     if (rsc == NULL) {
1024         return NULL;
1025     }
1026 
1027     for (GList *node_iter = rsc->running_on; node_iter != NULL;
1028          node_iter = node_iter->next) {
1029 
1030         node = node_iter->data;
1031         keep_looking = FALSE;
1032 
1033         is_happy = node->details->online && !node->details->unclean;
1034 
1035         if (count_all) {
1036             ++*count_all;
1037         }
1038         if (count_clean && is_happy) {
1039             ++*count_clean;
1040         }
1041         if (count_all || count_clean) {
1042             // If we're counting, we need to go through entire list
1043             keep_looking = TRUE;
1044         }
1045 
1046         if (rsc->partial_migration_source != NULL) {
1047             if (node->details == rsc->partial_migration_source->details) {
1048                 // This is the migration source
1049                 active = node;
1050             } else {
1051                 keep_looking = TRUE;
1052             }
1053         } else if (!pcmk_is_set(rsc->flags, pe_rsc_needs_fencing)) {
1054             if (is_happy && (!active || !active->details->online
1055                              || active->details->unclean)) {
1056                 // This is the first clean node
1057                 active = node;
1058             } else {
1059                 keep_looking = TRUE;
1060             }
1061         }
1062         if (active == NULL) {
1063             // This is first node in list
1064             active = node;
1065         }
1066 
1067         if (keep_looking == FALSE) {
1068             // Don't waste time iterating if we don't have to
1069             break;
1070         }
1071     }
1072     return active;
1073 }
1074 
1075 /*!
1076  * \brief
1077  * \internal Find and count active nodes according to "requires"
1078  *
1079  * \param[in]  rsc    Resource to check
1080  * \param[out] count  If not NULL, will be set to count of active nodes
1081  *
1082  * \return An active node (or NULL if resource is not active anywhere)
1083  *
1084  * \note This is a convenience wrapper for pe__find_active_on() where the count
1085  *       of all active nodes or only clean active nodes is desired according to
1086  *       the "requires" meta-attribute.
1087  */
1088 pe_node_t *
1089 pe__find_active_requires(const pe_resource_t *rsc, unsigned int *count)
     /* [previous][next][first][last][top][bottom][index][help] */
1090 {
1091     if (rsc && !pcmk_is_set(rsc->flags, pe_rsc_needs_fencing)) {
1092         return pe__find_active_on(rsc, NULL, count);
1093     }
1094     return pe__find_active_on(rsc, count, NULL);
1095 }
1096 
1097 void
1098 pe__count_common(pe_resource_t *rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
1099 {
1100     if (rsc->children != NULL) {
1101         for (GList *item = rsc->children; item != NULL; item = item->next) {
1102             ((pe_resource_t *) item->data)->fns->count(item->data);
1103         }
1104 
1105     } else if (!pcmk_is_set(rsc->flags, pe_rsc_orphan)
1106                || (rsc->role > RSC_ROLE_STOPPED)) {
1107         rsc->cluster->ninstances++;
1108         if (pe__resource_is_disabled(rsc)) {
1109             rsc->cluster->disabled_resources++;
1110         }
1111         if (pcmk_is_set(rsc->flags, pe_rsc_block)) {
1112             rsc->cluster->blocked_resources++;
1113         }
1114     }
1115 }
1116 
1117 /*!
1118  * \internal
1119  * \brief Update a resource's next role
1120  *
1121  * \param[in,out] rsc   Resource to be updated
1122  * \param[in]     role  Resource's new next role
1123  * \param[in]     why   Human-friendly reason why role is changing (for logs)
1124  */
1125 void
1126 pe__set_next_role(pe_resource_t *rsc, enum rsc_role_e role, const char *why)
     /* [previous][next][first][last][top][bottom][index][help] */
1127 {
1128     CRM_ASSERT((rsc != NULL) && (why != NULL));
1129     if (rsc->next_role != role) {
1130         pe_rsc_trace(rsc, "Resetting next role for %s from %s to %s (%s)",
1131                      rsc->id, role2text(rsc->next_role), role2text(role), why);
1132         rsc->next_role = role;
1133     }
1134 }

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