pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
group.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2024 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 <stdint.h>
13 
14 #include <crm/pengine/rules.h>
15 #include <crm/pengine/status.h>
16 #include <crm/pengine/internal.h>
17 #include <crm/common/xml.h>
18 #include <crm/common/output.h>
21 #include <pe_status_private.h>
22 
23 typedef struct group_variant_data_s {
24  pcmk_resource_t *last_child; // Last group member
25  uint32_t flags; // Group of enum pcmk__group_flags
27 
38 {
39  if (group != NULL) {
40  CRM_CHECK(pcmk__is_group(group) && (group->variant_opaque != NULL),
41  return NULL);
42  return ((group_variant_data_t *) group->variant_opaque)->last_child;
43  }
44  return NULL;
45 }
46 
56 bool
58 {
59  group_variant_data_t *group_data = NULL;
60 
61  CRM_CHECK(pcmk__is_group(group) && (group->variant_opaque != NULL),
62  return false);
63  group_data = (group_variant_data_t *) group->variant_opaque;
64  return pcmk_all_flags_set(group_data->flags, flags);
65 }
66 
76 static void
77 set_group_flag(pcmk_resource_t *group, const char *option, uint32_t flag,
78  uint32_t wo_bit)
79 {
80  const char *value_s = NULL;
81  int value = 0;
82 
83  value_s = g_hash_table_lookup(group->meta, option);
84 
85  // We don't actually need the null check but it speeds up the common case
86  if ((value_s == NULL) || (crm_str_to_boolean(value_s, &value) < 0)
87  || (value != 0)) {
88 
89  ((group_variant_data_t *) group->variant_opaque)->flags |= flag;
90 
91  } else {
92  pcmk__warn_once(wo_bit,
93  "Support for the '%s' group meta-attribute is "
94  "deprecated and will be removed in a future release "
95  "(use a resource set instead)", option);
96  }
97 }
98 
99 static int
100 inactive_resources(pcmk_resource_t *rsc)
101 {
102  int retval = 0;
103 
104  for (GList *gIter = rsc->children; gIter != NULL; gIter = gIter->next) {
105  pcmk_resource_t *child_rsc = (pcmk_resource_t *) gIter->data;
106 
107  if (!child_rsc->fns->active(child_rsc, TRUE)) {
108  retval++;
109  }
110  }
111 
112  return retval;
113 }
114 
115 static void
116 group_header(pcmk__output_t *out, int *rc, const pcmk_resource_t *rsc,
117  int n_inactive, bool show_inactive, const char *desc)
118 {
119  GString *attrs = NULL;
120 
121  if (n_inactive > 0 && !show_inactive) {
122  attrs = g_string_sized_new(64);
123  g_string_append_printf(attrs, "%d member%s inactive", n_inactive,
124  pcmk__plural_s(n_inactive));
125  }
126 
127  if (pe__resource_is_disabled(rsc)) {
128  pcmk__add_separated_word(&attrs, 64, "disabled", ", ");
129  }
130 
132  pcmk__add_separated_word(&attrs, 64, "maintenance", ", ");
133 
134  } else if (!pcmk_is_set(rsc->flags, pcmk_rsc_managed)) {
135  pcmk__add_separated_word(&attrs, 64, "unmanaged", ", ");
136  }
137 
138  if (attrs != NULL) {
139  PCMK__OUTPUT_LIST_HEADER(out, FALSE, *rc, "Resource Group: %s (%s)%s%s%s",
140  rsc->id,
141  (const char *) attrs->str, desc ? " (" : "",
142  desc ? desc : "", desc ? ")" : "");
143  g_string_free(attrs, TRUE);
144  } else {
145  PCMK__OUTPUT_LIST_HEADER(out, FALSE, *rc, "Resource Group: %s%s%s%s",
146  rsc->id,
147  desc ? " (" : "", desc ? desc : "",
148  desc ? ")" : "");
149  }
150 }
151 
152 static bool
153 skip_child_rsc(pcmk_resource_t *rsc, pcmk_resource_t *child,
154  gboolean parent_passes, GList *only_rsc, uint32_t show_opts)
155 {
156  bool star_list = pcmk__list_of_1(only_rsc) &&
157  pcmk__str_eq("*", g_list_first(only_rsc)->data, pcmk__str_none);
158  bool child_filtered = child->fns->is_filtered(child, only_rsc, FALSE);
159  bool child_active = child->fns->active(child, FALSE);
160  bool show_inactive = pcmk_is_set(show_opts, pcmk_show_inactive_rscs);
161 
162  /* If the resource is in only_rsc by name (so, ignoring "*") then allow
163  * it regardless of if it's active or not.
164  */
165  if (!star_list && !child_filtered) {
166  return false;
167 
168  } else if (!child_filtered && (child_active || show_inactive)) {
169  return false;
170 
171  } else if (parent_passes && (child_active || show_inactive)) {
172  return false;
173 
174  }
175 
176  return true;
177 }
178 
179 gboolean
181 {
182  xmlNode *xml_obj = rsc->xml;
183  xmlNode *xml_native_rsc = NULL;
184  group_variant_data_t *group_data = NULL;
185  const char *clone_id = NULL;
186 
187  pcmk__rsc_trace(rsc, "Processing resource %s...", rsc->id);
188 
189  group_data = pcmk__assert_alloc(1, sizeof(group_variant_data_t));
190  group_data->last_child = NULL;
191  rsc->variant_opaque = group_data;
192 
193  // @COMPAT These are deprecated since 2.1.5
194  set_group_flag(rsc, PCMK_META_ORDERED, pcmk__group_ordered,
196  set_group_flag(rsc, "collocated", pcmk__group_colocated,
198 
199  clone_id = crm_element_value(rsc->xml, PCMK__META_CLONE);
200 
201  for (xml_native_rsc = pcmk__xe_first_child(xml_obj, NULL, NULL, NULL);
202  xml_native_rsc != NULL;
203  xml_native_rsc = pcmk__xe_next(xml_native_rsc)) {
204 
205  if (pcmk__xe_is(xml_native_rsc, PCMK_XE_PRIMITIVE)) {
206  pcmk_resource_t *new_rsc = NULL;
207 
208  crm_xml_add(xml_native_rsc, PCMK__META_CLONE, clone_id);
209  if (pe__unpack_resource(xml_native_rsc, &new_rsc, rsc,
210  scheduler) != pcmk_rc_ok) {
211  continue;
212  }
213 
214  rsc->children = g_list_append(rsc->children, new_rsc);
215  group_data->last_child = new_rsc;
216  pcmk__rsc_trace(rsc, "Added %s member %s", rsc->id, new_rsc->id);
217  }
218  }
219 
220  if (rsc->children == NULL) {
221  /* The schema does not allow empty groups, but if validation is
222  * disabled, we allow them (members can be added later).
223  *
224  * @COMPAT At a major release bump, we should consider this a failure so
225  * that group methods can assume children is not NULL, and there
226  * are no strange effects from phantom groups due to their
227  * presence or meta-attributes.
228  */
229  pcmk__config_warn("Group %s will be ignored because it does not have "
230  "any members", rsc->id);
231  }
232  return TRUE;
233 }
234 
235 gboolean
236 group_active(pcmk_resource_t *rsc, gboolean all)
237 {
238  gboolean c_all = TRUE;
239  gboolean c_any = FALSE;
240  GList *gIter = rsc->children;
241 
242  for (; gIter != NULL; gIter = gIter->next) {
243  pcmk_resource_t *child_rsc = (pcmk_resource_t *) gIter->data;
244 
245  if (child_rsc->fns->active(child_rsc, all)) {
246  c_any = TRUE;
247  } else {
248  c_all = FALSE;
249  }
250  }
251 
252  if (c_any == FALSE) {
253  return FALSE;
254  } else if (all && c_all == FALSE) {
255  return FALSE;
256  }
257  return TRUE;
258 }
259 
264 static void
265 group_print_xml(pcmk_resource_t *rsc, const char *pre_text, long options,
266  void *print_data)
267 {
268  GList *gIter = rsc->children;
269  char *child_text = crm_strdup_printf("%s ", pre_text);
270 
271  status_print("%s<group " PCMK_XA_ID "=\"%s\" ", pre_text, rsc->id);
272  status_print("number_resources=\"%d\" ", g_list_length(rsc->children));
273  status_print(">\n");
274 
275  for (; gIter != NULL; gIter = gIter->next) {
276  pcmk_resource_t *child_rsc = (pcmk_resource_t *) gIter->data;
277 
278  child_rsc->fns->print(child_rsc, child_text, options, print_data);
279  }
280 
281  status_print("%s</group>\n", pre_text);
282  free(child_text);
283 }
284 
289 void
290 group_print(pcmk_resource_t *rsc, const char *pre_text, long options,
291  void *print_data)
292 {
293  char *child_text = NULL;
294  GList *gIter = rsc->children;
295 
296  if (pre_text == NULL) {
297  pre_text = " ";
298  }
299 
300  if (options & pe_print_xml) {
301  group_print_xml(rsc, pre_text, options, print_data);
302  return;
303  }
304 
305  child_text = crm_strdup_printf("%s ", pre_text);
306 
307  status_print("%sResource Group: %s", pre_text ? pre_text : "", rsc->id);
308 
309  if (options & pe_print_html) {
310  status_print("\n<ul>\n");
311 
312  } else if ((options & pe_print_log) == 0) {
313  status_print("\n");
314  }
315 
316  if (options & pe_print_brief) {
317  print_rscs_brief(rsc->children, child_text, options, print_data, TRUE);
318 
319  } else {
320  for (; gIter != NULL; gIter = gIter->next) {
321  pcmk_resource_t *child_rsc = (pcmk_resource_t *) gIter->data;
322 
323  if (options & pe_print_html) {
324  status_print("<li>\n");
325  }
326  child_rsc->fns->print(child_rsc, child_text, options, print_data);
327  if (options & pe_print_html) {
328  status_print("</li>\n");
329  }
330  }
331  }
332 
333  if (options & pe_print_html) {
334  status_print("</ul>\n");
335  }
336  free(child_text);
337 }
338 
339 PCMK__OUTPUT_ARGS("group", "uint32_t", "pcmk_resource_t *", "GList *",
340  "GList *")
341 int
342 pe__group_xml(pcmk__output_t *out, va_list args)
343 {
344  uint32_t show_opts = va_arg(args, uint32_t);
345  pcmk_resource_t *rsc = va_arg(args, pcmk_resource_t *);
346  GList *only_node = va_arg(args, GList *);
347  GList *only_rsc = va_arg(args, GList *);
348 
349  const char *desc = NULL;
350  GList *gIter = rsc->children;
351 
352  int rc = pcmk_rc_no_output;
353 
354  gboolean parent_passes = pcmk__str_in_list(rsc_printable_id(rsc), only_rsc, pcmk__str_star_matches) ||
355  (strstr(rsc->id, ":") != NULL && pcmk__str_in_list(rsc->id, only_rsc, pcmk__str_star_matches));
356 
357  desc = pe__resource_description(rsc, show_opts);
358 
359  if (rsc->fns->is_filtered(rsc, only_rsc, TRUE)) {
360  return rc;
361  }
362 
363  for (; gIter != NULL; gIter = gIter->next) {
364  pcmk_resource_t *child_rsc = (pcmk_resource_t *) gIter->data;
365 
366  if (skip_child_rsc(rsc, child_rsc, parent_passes, only_rsc, show_opts)) {
367  continue;
368  }
369 
370  if (rc == pcmk_rc_no_output) {
371  char *count = pcmk__itoa(g_list_length(gIter));
372  const char *maintenance = pcmk__flag_text(rsc->flags,
374  const char *managed = pcmk__flag_text(rsc->flags, pcmk_rsc_managed);
375  const char *disabled = pcmk__btoa(pe__resource_is_disabled(rsc));
376 
377  rc = pe__name_and_nvpairs_xml(out, true, PCMK_XE_GROUP,
378  PCMK_XA_ID, rsc->id,
380  PCMK_XA_MAINTENANCE, maintenance,
381  PCMK_XA_MANAGED, managed,
382  PCMK_XA_DISABLED, disabled,
383  PCMK_XA_DESCRIPTION, desc,
384  NULL);
385  free(count);
386  pcmk__assert(rc == pcmk_rc_ok);
387  }
388 
389  out->message(out, (const char *) child_rsc->xml->name, show_opts,
390  child_rsc, only_node, only_rsc);
391  }
392 
393  if (rc == pcmk_rc_ok) {
395  }
396 
397  return rc;
398 }
399 
400 PCMK__OUTPUT_ARGS("group", "uint32_t", "pcmk_resource_t *", "GList *",
401  "GList *")
402 int
403 pe__group_default(pcmk__output_t *out, va_list args)
404 {
405  uint32_t show_opts = va_arg(args, uint32_t);
406  pcmk_resource_t *rsc = va_arg(args, pcmk_resource_t *);
407  GList *only_node = va_arg(args, GList *);
408  GList *only_rsc = va_arg(args, GList *);
409 
410  const char *desc = NULL;
411  int rc = pcmk_rc_no_output;
412 
413  gboolean parent_passes = pcmk__str_in_list(rsc_printable_id(rsc), only_rsc, pcmk__str_star_matches) ||
414  (strstr(rsc->id, ":") != NULL && pcmk__str_in_list(rsc->id, only_rsc, pcmk__str_star_matches));
415 
416  gboolean active = rsc->fns->active(rsc, TRUE);
417  gboolean partially_active = rsc->fns->active(rsc, FALSE);
418 
419  desc = pe__resource_description(rsc, show_opts);
420 
421  if (rsc->fns->is_filtered(rsc, only_rsc, TRUE)) {
422  return rc;
423  }
424 
425  if (pcmk_is_set(show_opts, pcmk_show_brief)) {
426  GList *rscs = pe__filter_rsc_list(rsc->children, only_rsc);
427 
428  if (rscs != NULL) {
429  group_header(out, &rc, rsc, !active && partially_active ? inactive_resources(rsc) : 0,
430  pcmk_is_set(show_opts, pcmk_show_inactive_rscs), desc);
431  pe__rscs_brief_output(out, rscs, show_opts | pcmk_show_inactive_rscs);
432 
433  rc = pcmk_rc_ok;
434  g_list_free(rscs);
435  }
436 
437  } else {
438  for (GList *gIter = rsc->children; gIter; gIter = gIter->next) {
439  pcmk_resource_t *child_rsc = (pcmk_resource_t *) gIter->data;
440 
441  if (skip_child_rsc(rsc, child_rsc, parent_passes, only_rsc, show_opts)) {
442  continue;
443  }
444 
445  group_header(out, &rc, rsc, !active && partially_active ? inactive_resources(rsc) : 0,
446  pcmk_is_set(show_opts, pcmk_show_inactive_rscs), desc);
447  out->message(out, (const char *) child_rsc->xml->name, show_opts,
448  child_rsc, only_node, only_rsc);
449  }
450  }
451 
452  PCMK__OUTPUT_LIST_FOOTER(out, rc);
453 
454  return rc;
455 }
456 
457 void
459 {
460  CRM_CHECK(rsc != NULL, return);
461 
462  pcmk__rsc_trace(rsc, "Freeing %s", rsc->id);
463 
464  for (GList *gIter = rsc->children; gIter != NULL; gIter = gIter->next) {
465  pcmk_resource_t *child_rsc = (pcmk_resource_t *) gIter->data;
466 
467  pcmk__assert(child_rsc);
468  pcmk__rsc_trace(child_rsc, "Freeing child %s", child_rsc->id);
469  child_rsc->fns->free(child_rsc);
470  }
471 
472  pcmk__rsc_trace(rsc, "Freeing child list");
473  g_list_free(rsc->children);
474 
475  common_free(rsc);
476 }
477 
478 enum rsc_role_e
479 group_resource_state(const pcmk_resource_t * rsc, gboolean current)
480 {
481  enum rsc_role_e group_role = pcmk_role_unknown;
482  GList *gIter = rsc->children;
483 
484  for (; gIter != NULL; gIter = gIter->next) {
485  pcmk_resource_t *child_rsc = (pcmk_resource_t *) gIter->data;
486  enum rsc_role_e role = child_rsc->fns->state(child_rsc, current);
487 
488  if (role > group_role) {
489  group_role = role;
490  }
491  }
492 
493  pcmk__rsc_trace(rsc, "%s role: %s", rsc->id, pcmk_role_text(group_role));
494  return group_role;
495 }
496 
497 gboolean
498 pe__group_is_filtered(const pcmk_resource_t *rsc, GList *only_rsc,
499  gboolean check_parent)
500 {
501  gboolean passes = FALSE;
502 
503  if (check_parent
505  false)),
506  only_rsc, pcmk__str_star_matches)) {
507  passes = TRUE;
508  } else if (pcmk__str_in_list(rsc_printable_id(rsc), only_rsc, pcmk__str_star_matches)) {
509  passes = TRUE;
510  } else if (strstr(rsc->id, ":") != NULL && pcmk__str_in_list(rsc->id, only_rsc, pcmk__str_star_matches)) {
511  passes = TRUE;
512  } else {
513  for (const GList *iter = rsc->children;
514  iter != NULL; iter = iter->next) {
515 
516  const pcmk_resource_t *child_rsc = iter->data;
517 
518  if (!child_rsc->fns->is_filtered(child_rsc, only_rsc, FALSE)) {
519  passes = TRUE;
520  break;
521  }
522  }
523  }
524 
525  return !passes;
526 }
527 
536 unsigned int
538 {
539  pcmk__assert(pcmk__is_group(rsc));
540  return 1U;
541 }
const pcmk_resource_t * pe__const_top_resource(const pcmk_resource_t *rsc, bool include_bundle)
Definition: complex.c:1071
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:245
const char * pcmk_role_text(enum rsc_role_e role)
Get readable description of a resource role.
Definition: roles.c:23
char data[0]
Definition: cpg.c:58
#define PCMK_XA_MANAGED
Definition: xml_names.h:323
Control output from tools.
#define PCMK_XE_PRIMITIVE
Definition: xml_names.h:164
bool pe__group_flag_is_set(const pcmk_resource_t *group, uint32_t flags)
Definition: group.c:57
#define pcmk__config_warn(fmt...)
GList * children
Definition: resources.h:471
#define pcmk__rsc_trace(rsc, fmt, args...)
gboolean group_unpack(pcmk_resource_t *rsc, pcmk_scheduler_t *scheduler)
Definition: group.c:180
xmlNode * xml
Definition: resources.h:400
enum rsc_role_e(* state)(const pcmk_resource_t *rsc, gboolean current)
Definition: resources.h:316
int pe__group_default(pcmk__output_t *out, va_list args)
void group_free(pcmk_resource_t *rsc)
Definition: group.c:458
const char * pe__resource_description(const pcmk_resource_t *rsc, uint32_t show_opts)
Definition: pe_output.c:22
GHashTable * meta
Definition: resources.h:467
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
Definition: nvpair.c:313
void print_rscs_brief(GList *rsc_list, const char *pre_text, long options, void *print_data, gboolean print_all)
Definition: native.c:1271
void pcmk__add_separated_word(GString **list, size_t init_size, const char *word, const char *separator)
Definition: strings.c:806
#define PCMK__META_CLONE
const char * rsc_printable_id(const pcmk_resource_t *rsc)
Definition: utils.c:553
void(* print)(pcmk_resource_t *rsc, const char *pre_text, long options, void *print_data)
Definition: resources.h:295
int pe__unpack_resource(xmlNode *xml_obj, pcmk_resource_t **rsc, pcmk_resource_t *parent, pcmk_scheduler_t *scheduler)
Definition: complex.c:691
unsigned int pe__group_max_per_node(const pcmk_resource_t *rsc)
Definition: group.c:537
#define PCMK_XA_DISABLED
Definition: xml_names.h:265
gboolean group_active(pcmk_resource_t *rsc, gboolean all)
Definition: group.c:236
GList * pe__filter_rsc_list(GList *rscs, GList *filter)
Definition: utils.c:795
void common_free(pcmk_resource_t *rsc)
Definition: complex.c:1088
void(* free)(pcmk_resource_t *rsc)
Definition: resources.h:336
int pe__rscs_brief_output(pcmk__output_t *out, GList *rsc_list, unsigned int options)
PCMK__OUTPUT_ARGS("group", "uint32_t", "pcmk_resource_t *", "GList *", "GList *")
Definition: group.c:339
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:458
int pe__name_and_nvpairs_xml(pcmk__output_t *out, bool is_list, const char *tag_name,...) G_GNUC_NULL_TERMINATED
Definition: pe_output.c:611
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
Definition: xml.c:481
void group_print(pcmk_resource_t *rsc, const char *pre_text, long options, void *print_data)
Definition: group.c:290
#define PCMK_XA_DESCRIPTION
Definition: xml_names.h:261
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:94
#define PCMK_XA_NUMBER_RESOURCES
Definition: xml_names.h:343
unsigned long long flags
Definition: resources.h:428
Wrappers for and extensions to libxml2.
rsc_role_e
Definition: roles.h:34
#define PCMK_XA_ID
Definition: xml_names.h:301
#define pcmk__warn_once(wo_flag, fmt...)
void pcmk__output_xml_pop_parent(pcmk__output_t *out)
Definition: output_xml.c:562
#define PCMK_XA_MAINTENANCE
Definition: xml_names.h:321
#define pcmk__assert(expr)
#define PCMK__OUTPUT_LIST_FOOTER(out_obj, retcode)
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:508
gboolean(* active)(pcmk_resource_t *rsc, gboolean all)
Definition: resources.h:306
#define PCMK__OUTPUT_LIST_HEADER(out_obj, cond, retcode, title...)
pcmk_resource_t * pe__last_group_member(const pcmk_resource_t *group)
Definition: group.c:37
Cluster status and scheduling.
pcmk_rsc_methods_t * fns
Definition: resources.h:412
void * variant_opaque
Definition: resources.h:411
pcmk_scheduler_t * scheduler
#define status_print(fmt, args...)
int pe__group_xml(pcmk__output_t *out, va_list args)
This structure contains everything that makes up a single output formatter.
bool pe__resource_is_disabled(const pcmk_resource_t *rsc)
Definition: utils.c:737
#define PCMK_XE_GROUP
Definition: xml_names.h:119
#define pcmk__plural_s(i)
gboolean(* is_filtered)(const pcmk_resource_t *rsc, GList *only_rsc, gboolean check_parent)
Definition: resources.h:358
struct group_variant_data_s group_variant_data_t
Resource role is unknown.
Definition: roles.h:35
#define PCMK_META_ORDERED
Definition: options.h:99
#define pcmk__assert_alloc(nmemb, size)
Definition: internal.h:297
enum rsc_role_e group_resource_state(const pcmk_resource_t *rsc, gboolean current)
Definition: group.c:479
gboolean pe__group_is_filtered(const pcmk_resource_t *rsc, GList *only_rsc, gboolean check_parent)
Definition: group.c:498
uint64_t flags
Definition: remote.c:215
gboolean pcmk__str_in_list(const gchar *s, const GList *lst, uint32_t flags)
Definition: strings.c:993