pacemaker 2.1.5-a3f44794f94
Scalable High-Availability cluster resource manager
pcmk_sched_utilization.c
Go to the documentation of this file.
1/*
2 * Copyright 2014-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 General Public License version 2
7 * or later (GPLv2+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11#include <crm/msg_xml.h>
12#include <pacemaker-internal.h>
13
15
16// Name for a pseudo-op to use in ordering constraints for utilization
17#define LOAD_STOPPED "load_stopped"
18
31static int
32utilization_value(const char *s)
33{
34 int value = 0;
35
36 if ((s != NULL) && (pcmk__scan_min_int(s, &value, INT_MIN) == EINVAL)) {
37 pe_warn("Using 0 for utilization instead of invalid value '%s'", value);
38 value = 0;
39 }
40 return value;
41}
42
43
44/*
45 * Functions for comparing node capacities
46 */
47
48struct compare_data {
49 const pe_node_t *node1;
50 const pe_node_t *node2;
51 bool node2_only;
52 int result;
53};
54
67static void
68compare_utilization_value(gpointer key, gpointer value, gpointer user_data)
69{
70 int node1_capacity = 0;
71 int node2_capacity = 0;
72 struct compare_data *data = user_data;
73 const char *node2_value = NULL;
74
75 if (data->node2_only) {
76 if (g_hash_table_lookup(data->node1->details->utilization, key)) {
77 return; // We've already compared this attribute
78 }
79 } else {
80 node1_capacity = utilization_value((const char *) value);
81 }
82
83 node2_value = g_hash_table_lookup(data->node2->details->utilization, key);
84 node2_capacity = utilization_value(node2_value);
85
86 if (node1_capacity > node2_capacity) {
87 data->result--;
88 } else if (node1_capacity < node2_capacity) {
89 data->result++;
90 }
91}
92
104int
106{
107 struct compare_data data = {
108 .node1 = node1,
109 .node2 = node2,
110 .node2_only = false,
111 .result = 0,
112 };
113
114 // Compare utilization values that node1 and maybe node2 have
115 g_hash_table_foreach(node1->details->utilization, compare_utilization_value,
116 &data);
117
118 // Compare utilization values that only node2 has
119 data.node2_only = true;
120 g_hash_table_foreach(node2->details->utilization, compare_utilization_value,
121 &data);
122
123 return data.result;
124}
125
126
127/*
128 * Functions for updating node capacities
129 */
130
131struct calculate_data {
132 GHashTable *current_utilization;
133 bool plus;
134};
135
144static void
145update_utilization_value(gpointer key, gpointer value, gpointer user_data)
146{
147 int result = 0;
148 const char *current = NULL;
149 struct calculate_data *data = user_data;
150
151 current = g_hash_table_lookup(data->current_utilization, key);
152 if (data->plus) {
153 result = utilization_value(current) + utilization_value(value);
154 } else if (current) {
155 result = utilization_value(current) - utilization_value(value);
156 }
157 g_hash_table_replace(data->current_utilization,
158 strdup(key), pcmk__itoa(result));
159}
160
168void
169pcmk__consume_node_capacity(GHashTable *current_utilization, pe_resource_t *rsc)
170{
171 struct calculate_data data = {
172 .current_utilization = current_utilization,
173 .plus = false,
174 };
175
176 g_hash_table_foreach(rsc->utilization, update_utilization_value, &data);
177}
178
186void
187pcmk__release_node_capacity(GHashTable *current_utilization,
188 const pe_resource_t *rsc)
189{
190 struct calculate_data data = {
191 .current_utilization = current_utilization,
192 .plus = true,
193 };
194
195 g_hash_table_foreach(rsc->utilization, update_utilization_value, &data);
196}
197
198
199/*
200 * Functions for checking for sufficient node capacity
201 */
202
203struct capacity_data {
204 pe_node_t *node;
205 const char *rsc_id;
206 bool is_enough;
207};
208
217static void
218check_capacity(gpointer key, gpointer value, gpointer user_data)
219{
220 int required = 0;
221 int remaining = 0;
222 const char *node_value_s = NULL;
223 struct capacity_data *data = user_data;
224
225 node_value_s = g_hash_table_lookup(data->node->details->utilization, key);
226
227 required = utilization_value(value);
228 remaining = utilization_value(node_value_s);
229
230 if (required > remaining) {
231 crm_debug("Remaining capacity for %s on %s (%d) is insufficient "
232 "for resource %s usage (%d)",
233 (const char *) key, pe__node_name(data->node), remaining,
234 data->rsc_id, required);
235 data->is_enough = false;
236 }
237}
238
249static bool
250have_enough_capacity(pe_node_t *node, const char *rsc_id,
251 GHashTable *utilization)
252{
253 struct capacity_data data = {
254 .node = node,
255 .rsc_id = rsc_id,
256 .is_enough = true,
257 };
258
259 g_hash_table_foreach(utilization, check_capacity, &data);
260 return data.is_enough;
261}
262
274static GHashTable *
275sum_resource_utilization(pe_resource_t *orig_rsc, GList *rscs)
276{
277 GHashTable *utilization = pcmk__strkey_table(free, free);
278
279 for (GList *iter = rscs; iter != NULL; iter = iter->next) {
280 pe_resource_t *rsc = (pe_resource_t *) iter->data;
281
282 rsc->cmds->add_utilization(rsc, orig_rsc, rscs, utilization);
283 }
284 return utilization;
285}
286
296const pe_node_t *
298{
299 bool any_capable = false;
300 char *rscs_id = NULL;
301 pe_node_t *node = NULL;
302 const pe_node_t *most_capable_node = NULL;
303 GList *colocated_rscs = NULL;
304 GHashTable *unallocated_utilization = NULL;
305 GHashTableIter iter;
306
307 CRM_CHECK(rsc != NULL, return NULL);
308
309 // The default placement strategy ignores utilization
310 if (pcmk__str_eq(rsc->cluster->placement_strategy, "default",
312 return NULL;
313 }
314
315 // Check whether any resources are colocated with this one
316 colocated_rscs = rsc->cmds->colocated_resources(rsc, NULL, NULL);
317 if (colocated_rscs == NULL) {
318 return NULL;
319 }
320
321 rscs_id = crm_strdup_printf("%s and its colocated resources", rsc->id);
322
323 // If rsc isn't in the list, add it so we include its utilization
324 if (g_list_find(colocated_rscs, rsc) == NULL) {
325 colocated_rscs = g_list_append(colocated_rscs, rsc);
326 }
327
328 // Sum utilization of colocated resources that haven't been allocated yet
329 unallocated_utilization = sum_resource_utilization(rsc, colocated_rscs);
330
331 // Check whether any node has enough capacity for all the resources
332 g_hash_table_iter_init(&iter, rsc->allowed_nodes);
333 while (g_hash_table_iter_next(&iter, NULL, (void **) &node)) {
334 if (!pcmk__node_available(node, true, false)) {
335 continue;
336 }
337
338 if (have_enough_capacity(node, rscs_id, unallocated_utilization)) {
339 any_capable = true;
340 }
341
342 // Keep track of node with most free capacity
343 if ((most_capable_node == NULL)
344 || (pcmk__compare_node_capacities(node, most_capable_node) < 0)) {
345 most_capable_node = node;
346 }
347 }
348
349 if (any_capable) {
350 // If so, ban resource from any node with insufficient capacity
351 g_hash_table_iter_init(&iter, rsc->allowed_nodes);
352 while (g_hash_table_iter_next(&iter, NULL, (void **) &node)) {
353 if (pcmk__node_available(node, true, false)
354 && !have_enough_capacity(node, rscs_id,
355 unallocated_utilization)) {
356 pe_rsc_debug(rsc, "%s does not have enough capacity for %s",
357 pe__node_name(node), rscs_id);
358 resource_location(rsc, node, -INFINITY, "__limit_utilization__",
359 rsc->cluster);
360 }
361 }
362 most_capable_node = NULL;
363
364 } else {
365 // Otherwise, ban from nodes with insufficient capacity for rsc alone
366 g_hash_table_iter_init(&iter, rsc->allowed_nodes);
367 while (g_hash_table_iter_next(&iter, NULL, (void **) &node)) {
368 if (pcmk__node_available(node, true, false)
369 && !have_enough_capacity(node, rsc->id, rsc->utilization)) {
370 pe_rsc_debug(rsc, "%s does not have enough capacity for %s",
371 pe__node_name(node), rsc->id);
372 resource_location(rsc, node, -INFINITY, "__limit_utilization__",
373 rsc->cluster);
374 }
375 }
376 }
377
378 g_hash_table_destroy(unallocated_utilization);
379 g_list_free(colocated_rscs);
380 free(rscs_id);
381
382 pe__show_node_weights(true, rsc, "Post-utilization",
383 rsc->allowed_nodes, rsc->cluster);
384 return most_capable_node;
385}
386
396static pe_action_t *
397new_load_stopped_op(const pe_node_t *node, pe_working_set_t *data_set)
398{
399 char *load_stopped_task = crm_strdup_printf(LOAD_STOPPED "_%s",
400 node->details->uname);
401 pe_action_t *load_stopped = get_pseudo_op(load_stopped_task, data_set);
402
403 if (load_stopped->node == NULL) {
404 load_stopped->node = pe__copy_node(node);
406 }
407 free(load_stopped_task);
408 return load_stopped;
409}
410
418void
420{
421 GList *iter = NULL;
422 pe_node_t *node = NULL;
423 pe_action_t *load_stopped = NULL;
424
425 pe_rsc_trace(rsc, "Creating utilization constraints for %s - strategy: %s",
426 rsc->id, rsc->cluster->placement_strategy);
427
428 // "stop rsc then load_stopped" constraints for current nodes
429 for (iter = rsc->running_on; iter != NULL; iter = iter->next) {
430 node = (pe_node_t *) iter->data;
431 load_stopped = new_load_stopped_op(node, rsc->cluster);
432 pcmk__new_ordering(rsc, stop_key(rsc), NULL, NULL, NULL, load_stopped,
433 pe_order_load, rsc->cluster);
434 }
435
436 // "load_stopped then start/migrate_to rsc" constraints for allowed nodes
437 for (GList *iter = allowed_nodes; iter; iter = iter->next) {
438 node = (pe_node_t *) iter->data;
439 load_stopped = new_load_stopped_op(node, rsc->cluster);
440 pcmk__new_ordering(NULL, NULL, load_stopped, rsc, start_key(rsc), NULL,
441 pe_order_load, rsc->cluster);
442 pcmk__new_ordering(NULL, NULL, load_stopped,
443 rsc, pcmk__op_key(rsc->id, RSC_MIGRATE, 0), NULL,
444 pe_order_load, rsc->cluster);
445 }
446}
447
455void
457{
459 return;
460 }
461 for (GList *iter = data_set->nodes; iter != NULL; iter = iter->next) {
462 pe_node_t *node = (pe_node_t *) iter->data;
464
465 out->message(out, "node-capacity", node, desc);
466 }
467}
char * pcmk__op_key(const char *rsc_id, const char *op_type, guint interval_ms)
Generate an operation key (RESOURCE_ACTION_INTERVAL)
Definition: operations.c:45
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
Definition: util.h:121
char data[0]
Definition: cpg.c:10
#define INFINITY
Definition: crm.h:99
#define RSC_MIGRATE
Definition: crm.h:196
G_GNUC_INTERNAL bool pcmk__node_available(const pe_node_t *node, bool consider_score, bool consider_guest)
G_GNUC_INTERNAL void pcmk__new_ordering(pe_resource_t *first_rsc, char *first_task, pe_action_t *first_action, pe_resource_t *then_rsc, char *then_task, pe_action_t *then_action, uint32_t flags, pe_working_set_t *data_set)
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:227
#define crm_debug(fmt, args...)
Definition: logging.h:364
pe_working_set_t * data_set
pcmk__action_result_t result
Definition: pcmk_fence.c:35
void pcmk__show_node_capacities(const char *desc, pe_working_set_t *data_set)
void pcmk__release_node_capacity(GHashTable *current_utilization, const pe_resource_t *rsc)
void pcmk__create_utilization_constraints(pe_resource_t *rsc, GList *allowed_nodes)
#define LOAD_STOPPED
int pcmk__compare_node_capacities(const pe_node_t *node1, const pe_node_t *node2)
void pcmk__consume_node_capacity(GHashTable *current_utilization, pe_resource_t *rsc)
const pe_node_t * pcmk__ban_insufficient_capacity(pe_resource_t *rsc)
pe_node_t node1
pe_node_t node2
@ pe_order_load
Definition: pe_types.h:512
@ pe_action_optional
Definition: pe_types.h:301
#define pe_flag_show_utilization
Definition: pe_types.h:135
#define pe__show_node_weights(level, rsc, text, nodes, data_set)
Definition: internal.h:394
#define start_key(rsc)
Definition: internal.h:420
pe_action_t * get_pseudo_op(const char *name, pe_working_set_t *data_set)
Definition: pe_actions.c:977
#define stop_key(rsc)
Definition: internal.h:414
#define pe_rsc_debug(rsc, fmt, args...)
Definition: internal.h:46
#define pe_warn(fmt...)
Definition: internal.h:54
#define pe_rsc_trace(rsc, fmt, args...)
Definition: internal.h:47
void resource_location(pe_resource_t *rsc, pe_node_t *node, int score, const char *tag, pe_working_set_t *data_set)
Definition: utils.c:385
#define pe__clear_action_flags(action, flags_to_clear)
Definition: internal.h:95
pe_node_t * pe__copy_node(const pe_node_t *this_node)
Definition: utils.c:89
int pcmk__scan_min_int(const char *text, int *result, int minimum)
Definition: strings.c:127
GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
Definition: strings.c:611
@ pcmk__str_casei
This structure contains everything that makes up a single output formatter.
pe_node_t * node
Definition: pe_types.h:407
struct pe_node_shared_s * details
Definition: pe_types.h:252
const char * uname
Definition: pe_types.h:216
GHashTable * utilization
Definition: pe_types.h:242
GList * running_on
Definition: pe_types.h:373
pe_working_set_t * cluster
Definition: pe_types.h:335
char * id
Definition: pe_types.h:329
GHashTable * utilization
Definition: pe_types.h:382
GHashTable * allowed_nodes
Definition: pe_types.h:375
resource_alloc_functions_t * cmds
Definition: pe_types.h:341
const char * placement_strategy
Definition: pe_types.h:151
unsigned long long flags
Definition: pe_types.h:153
GList * nodes
Definition: pe_types.h:164
void(* add_utilization)(const pe_resource_t *rsc, const pe_resource_t *orig_rsc, GList *all_rscs, GHashTable *utilization)
GList *(* colocated_resources)(pe_resource_t *rsc, pe_resource_t *orig_rsc, GList *colocated_rscs)