From 215ca8f16f8679012ba891999c780e3ec0eada4e Mon Sep 17 00:00:00 2001 From: Ken Gaillot Date: Jan 22 2021 12:12:40 +0000 Subject: Refactor: scheduler: functionize checking whether colocation applies This allows us to have more consistent trace messages, and will reduce code duplication when more checks are added later. patch_name: 019-rhbz1371576.patch present_in_specfile: true location_in_specfile: 19 squash_commits: true --- diff --git a/include/pcmki/pcmki_sched_utils.h b/include/pcmki/pcmki_sched_utils.h index b8d7750..c7ae1b8 100644 --- a/include/pcmki/pcmki_sched_utils.h +++ b/include/pcmki/pcmki_sched_utils.h @@ -72,6 +72,9 @@ enum filter_colocation_res { extern enum filter_colocation_res filter_colocation_constraint(pe_resource_t * rsc_lh, pe_resource_t * rsc_rh, pcmk__colocation_t *constraint, gboolean preview); +bool pcmk__colocation_applies(pe_resource_t *rsc, + pcmk__colocation_t *colocation, + bool promoted_only); extern int compare_capacity(const pe_node_t * node1, const pe_node_t * node2); extern void calculate_utilization(GHashTable * current_utilization, diff --git a/lib/pacemaker/pcmk_sched_clone.c b/lib/pacemaker/pcmk_sched_clone.c index 9485a98..5a06151 100644 --- a/lib/pacemaker/pcmk_sched_clone.c +++ b/lib/pacemaker/pcmk_sched_clone.c @@ -658,14 +658,12 @@ pcmk__clone_allocate(pe_resource_t *rsc, pe_node_t *prefer, for (GListPtr gIter = rsc->rsc_cons_lhs; gIter != NULL; gIter = gIter->next) { pcmk__colocation_t *constraint = (pcmk__colocation_t *) gIter->data; - if (constraint->score == 0) { - continue; + if (pcmk__colocation_applies(rsc, constraint, false)) { + rsc->allowed_nodes = constraint->rsc_lh->cmds->merge_weights(constraint->rsc_lh, + rsc->id, rsc->allowed_nodes, constraint->node_attribute, + constraint->score / (float) INFINITY, + pe_weights_rollback|pe_weights_positive); } - rsc->allowed_nodes = - constraint->rsc_lh->cmds->merge_weights(constraint->rsc_lh, rsc->id, rsc->allowed_nodes, - constraint->node_attribute, - (float)constraint->score / INFINITY, - (pe_weights_rollback | pe_weights_positive)); } pe__show_node_weights(!show_scores, rsc, __func__, rsc->allowed_nodes); diff --git a/lib/pacemaker/pcmk_sched_group.c b/lib/pacemaker/pcmk_sched_group.c index f1ce0be..5334f23 100644 --- a/lib/pacemaker/pcmk_sched_group.c +++ b/lib/pacemaker/pcmk_sched_group.c @@ -516,13 +516,13 @@ pcmk__group_merge_weights(pe_resource_t *rsc, const char *rhs, for (; gIter != NULL; gIter = gIter->next) { pcmk__colocation_t *constraint = (pcmk__colocation_t *) gIter->data; - if (constraint->score == 0) { - continue; + if (pcmk__colocation_applies(rsc, constraint, false)) { + nodes = pcmk__native_merge_weights(constraint->rsc_lh, rsc->id, + nodes, + constraint->node_attribute, + constraint->score / (float) INFINITY, + flags); } - nodes = pcmk__native_merge_weights(constraint->rsc_lh, rsc->id, nodes, - constraint->node_attribute, - constraint->score / (float) INFINITY, - flags); } pe__clear_resource_flags(rsc, pe_rsc_merging); diff --git a/lib/pacemaker/pcmk_sched_native.c b/lib/pacemaker/pcmk_sched_native.c index 2132c6b..010c0fc 100644 --- a/lib/pacemaker/pcmk_sched_native.c +++ b/lib/pacemaker/pcmk_sched_native.c @@ -562,17 +562,11 @@ pcmk__native_allocate(pe_resource_t *rsc, pe_node_t *prefer, for (gIter = rsc->rsc_cons_lhs; gIter != NULL; gIter = gIter->next) { pcmk__colocation_t *constraint = (pcmk__colocation_t *) gIter->data; - if (constraint->score == 0) { - continue; + if (pcmk__colocation_applies(rsc, constraint, false)) { + rsc->allowed_nodes = constraint->rsc_lh->cmds->merge_weights(constraint->rsc_lh, + rsc->id, rsc->allowed_nodes, constraint->node_attribute, + constraint->score / (float) INFINITY, pe_weights_rollback); } - pe_rsc_trace(rsc, "Merging score of '%s' constraint (%s with %s)", - constraint->id, constraint->rsc_lh->id, - constraint->rsc_rh->id); - rsc->allowed_nodes = - constraint->rsc_lh->cmds->merge_weights(constraint->rsc_lh, rsc->id, rsc->allowed_nodes, - constraint->node_attribute, - (float)constraint->score / INFINITY, - pe_weights_rollback); } if (rsc->next_role == RSC_ROLE_STOPPED) { diff --git a/lib/pacemaker/pcmk_sched_promotable.c b/lib/pacemaker/pcmk_sched_promotable.c index 9a5474a..a0eeaad 100644 --- a/lib/pacemaker/pcmk_sched_promotable.c +++ b/lib/pacemaker/pcmk_sched_promotable.c @@ -345,23 +345,14 @@ promotion_order(pe_resource_t *rsc, pe_working_set_t *data_set) for (; gIter != NULL; gIter = gIter->next) { pcmk__colocation_t *constraint = (pcmk__colocation_t *) gIter->data; - if (constraint->score == 0) { - continue; - } - - /* (re-)adds location preferences of resource that wish to be - * colocated with the master instance - */ - if (constraint->role_rh == RSC_ROLE_MASTER) { - pe_rsc_trace(rsc, "LHS: %s with %s: %d", constraint->rsc_lh->id, constraint->rsc_rh->id, - constraint->score); - rsc->allowed_nodes = - constraint->rsc_lh->cmds->merge_weights(constraint->rsc_lh, rsc->id, - rsc->allowed_nodes, - constraint->node_attribute, - (float)constraint->score / INFINITY, - (pe_weights_rollback | - pe_weights_positive)); + if (pcmk__colocation_applies(rsc, constraint, true)) { + /* (Re-)add location preferences of resource that wishes to be + * colocated with the promoted instance. + */ + rsc->allowed_nodes = constraint->rsc_lh->cmds->merge_weights(constraint->rsc_lh, + rsc->id, rsc->allowed_nodes, constraint->node_attribute, + constraint->score / (float) INFINITY, + pe_weights_rollback|pe_weights_positive); } } diff --git a/lib/pacemaker/pcmk_sched_utils.c b/lib/pacemaker/pcmk_sched_utils.c index eaaf526..aba417a 100644 --- a/lib/pacemaker/pcmk_sched_utils.c +++ b/lib/pacemaker/pcmk_sched_utils.c @@ -765,3 +765,34 @@ pcmk__create_history_xml(xmlNode *parent, lrmd_event_data_t *op, free(key); return xml_op; } + +/*! + * \internal + * \brief Check whether a colocation constraint should apply + * + * \param[in] rsc Resource of interest (for logging) + * \param[in] colocation Colocation constraint to check + * \param[in] promoted_only If true, constraint applies if right-hand is promoted + */ +bool +pcmk__colocation_applies(pe_resource_t *rsc, pcmk__colocation_t *colocation, + bool promoted_only) +{ + CRM_CHECK((rsc != NULL) && (colocation != NULL), return false); + + if (colocation->score == 0) { + pe_rsc_trace(rsc, "Ignoring colocation constraint %s: 0 score", + colocation->id); + return false; + } + if (promoted_only && (colocation->role_rh != RSC_ROLE_MASTER)) { + pe_rsc_trace(rsc, "Ignoring colocation constraint %s: role", + colocation->id); + return false; + } + pe_rsc_trace(rsc, "Applying colocation constraint %s: %s with %s%s (%d)", + colocation->id, colocation->rsc_lh->id, + (promoted_only? "promoted " : ""), + colocation->rsc_rh->id, colocation->score); + return true; +}