pacemaker  2.1.6-802a72226b
Scalable High-Availability cluster resource manager
group.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2023 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/msg_xml.h>
18 #include <crm/common/output.h>
21 #include <pe_status_private.h>
22 
23 typedef struct group_variant_data_s {
24  pe_resource_t *last_child; // Last group member
25  uint32_t flags; // Group of enum pe__group_flags
27 
38 {
39  if (group != NULL) {
40  CRM_CHECK((group->variant == pe_group)
41  && (group->variant_opaque != NULL), return NULL);
42  return ((group_variant_data_t *) group->variant_opaque)->last_child;
43  }
44  return NULL;
45 }
46 
56 bool
57 pe__group_flag_is_set(const pe_resource_t *group, uint32_t flags)
58 {
59  group_variant_data_t *group_data = NULL;
60 
61  CRM_CHECK((group != NULL) && (group->variant == pe_group)
62  && (group->variant_opaque != NULL), 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(pe_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  pe_warn_once(wo_bit,
93  "Support for the '%s' group meta-attribute is deprecated "
94  "and will be removed in a future release "
95  "(use a resource set instead)", option);
96  }
97 }
98 
99 static int
100 inactive_resources(pe_resource_t *rsc)
101 {
102  int retval = 0;
103 
104  for (GList *gIter = rsc->children; gIter != NULL; gIter = gIter->next) {
105  pe_resource_t *child_rsc = (pe_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 pe_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 
131  if (pcmk_is_set(rsc->flags, pe_rsc_maintenance)) {
132  pcmk__add_separated_word(&attrs, 64, "maintenance", ", ");
133 
134  } else if (!pcmk_is_set(rsc->flags, pe_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(pe_resource_t *rsc, pe_resource_t *child, gboolean parent_passes,
154  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  pe_rsc_trace(rsc, "Processing resource %s...", rsc->id);
188 
189  group_data = calloc(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, XML_RSC_ATTR_ORDERED, pe__group_ordered,
196  set_group_flag(rsc, "collocated", pe__group_colocated, pe_wo_group_coloc);
197 
199 
200  for (xml_native_rsc = pcmk__xe_first_child(xml_obj); xml_native_rsc != NULL;
201  xml_native_rsc = pcmk__xe_next(xml_native_rsc)) {
202 
203  if (pcmk__str_eq((const char *)xml_native_rsc->name,
205  pe_resource_t *new_rsc = NULL;
206 
207  crm_xml_add(xml_native_rsc, XML_RSC_ATTR_INCARNATION, clone_id);
208  if (pe__unpack_resource(xml_native_rsc, &new_rsc, rsc,
209  data_set) != pcmk_rc_ok) {
210  continue;
211  }
212 
213  rsc->children = g_list_append(rsc->children, new_rsc);
214  group_data->last_child = new_rsc;
215  pe_rsc_trace(rsc, "Added %s member %s", rsc->id, new_rsc->id);
216  }
217  }
218 
219  if (rsc->children == NULL) {
220  /* The schema does not allow empty groups, but if validation is
221  * disabled, we allow them (members can be added later).
222  *
223  * @COMPAT At a major release bump, we should consider this a failure so
224  * that group methods can assume children is not NULL, and there
225  * are no strange effects from phantom groups due to their
226  * presence or meta-attributes.
227  */
228  pcmk__config_warn("Group %s will be ignored because it does not have "
229  "any members", rsc->id);
230  }
231  return TRUE;
232 }
233 
234 gboolean
235 group_active(pe_resource_t * rsc, gboolean all)
236 {
237  gboolean c_all = TRUE;
238  gboolean c_any = FALSE;
239  GList *gIter = rsc->children;
240 
241  for (; gIter != NULL; gIter = gIter->next) {
242  pe_resource_t *child_rsc = (pe_resource_t *) gIter->data;
243 
244  if (child_rsc->fns->active(child_rsc, all)) {
245  c_any = TRUE;
246  } else {
247  c_all = FALSE;
248  }
249  }
250 
251  if (c_any == FALSE) {
252  return FALSE;
253  } else if (all && c_all == FALSE) {
254  return FALSE;
255  }
256  return TRUE;
257 }
258 
263 static void
264 group_print_xml(pe_resource_t *rsc, const char *pre_text, long options,
265  void *print_data)
266 {
267  GList *gIter = rsc->children;
268  char *child_text = crm_strdup_printf("%s ", pre_text);
269 
270  status_print("%s<group " XML_ATTR_ID "=\"%s\" ", pre_text, rsc->id);
271  status_print("number_resources=\"%d\" ", g_list_length(rsc->children));
272  status_print(">\n");
273 
274  for (; gIter != NULL; gIter = gIter->next) {
275  pe_resource_t *child_rsc = (pe_resource_t *) gIter->data;
276 
277  child_rsc->fns->print(child_rsc, child_text, options, print_data);
278  }
279 
280  status_print("%s</group>\n", pre_text);
281  free(child_text);
282 }
283 
288 void
289 group_print(pe_resource_t *rsc, const char *pre_text, long options,
290  void *print_data)
291 {
292  char *child_text = NULL;
293  GList *gIter = rsc->children;
294 
295  if (pre_text == NULL) {
296  pre_text = " ";
297  }
298 
299  if (options & pe_print_xml) {
300  group_print_xml(rsc, pre_text, options, print_data);
301  return;
302  }
303 
304  child_text = crm_strdup_printf("%s ", pre_text);
305 
306  status_print("%sResource Group: %s", pre_text ? pre_text : "", rsc->id);
307 
308  if (options & pe_print_html) {
309  status_print("\n<ul>\n");
310 
311  } else if ((options & pe_print_log) == 0) {
312  status_print("\n");
313  }
314 
315  if (options & pe_print_brief) {
316  print_rscs_brief(rsc->children, child_text, options, print_data, TRUE);
317 
318  } else {
319  for (; gIter != NULL; gIter = gIter->next) {
320  pe_resource_t *child_rsc = (pe_resource_t *) gIter->data;
321 
322  if (options & pe_print_html) {
323  status_print("<li>\n");
324  }
325  child_rsc->fns->print(child_rsc, child_text, options, print_data);
326  if (options & pe_print_html) {
327  status_print("</li>\n");
328  }
329  }
330  }
331 
332  if (options & pe_print_html) {
333  status_print("</ul>\n");
334  }
335  free(child_text);
336 }
337 
338 PCMK__OUTPUT_ARGS("group", "uint32_t", "pe_resource_t *", "GList *", "GList *")
339 int
340 pe__group_xml(pcmk__output_t *out, va_list args)
341 {
342  uint32_t show_opts = va_arg(args, uint32_t);
343  pe_resource_t *rsc = va_arg(args, pe_resource_t *);
344  GList *only_node = va_arg(args, GList *);
345  GList *only_rsc = va_arg(args, GList *);
346 
347  const char *desc = NULL;
348  GList *gIter = rsc->children;
349 
350  int rc = pcmk_rc_no_output;
351 
352  gboolean parent_passes = pcmk__str_in_list(rsc_printable_id(rsc), only_rsc, pcmk__str_star_matches) ||
353  (strstr(rsc->id, ":") != NULL && pcmk__str_in_list(rsc->id, only_rsc, pcmk__str_star_matches));
354 
355  desc = pe__resource_description(rsc, show_opts);
356 
357  if (rsc->fns->is_filtered(rsc, only_rsc, TRUE)) {
358  return rc;
359  }
360 
361  for (; gIter != NULL; gIter = gIter->next) {
362  pe_resource_t *child_rsc = (pe_resource_t *) gIter->data;
363 
364  if (skip_child_rsc(rsc, child_rsc, parent_passes, only_rsc, show_opts)) {
365  continue;
366  }
367 
368  if (rc == pcmk_rc_no_output) {
369  char *count = pcmk__itoa(g_list_length(gIter));
370  const char *maint_s = pe__rsc_bool_str(rsc, pe_rsc_maintenance);
371  const char *managed_s = pe__rsc_bool_str(rsc, pe_rsc_managed);
372  const char *disabled_s = pcmk__btoa(pe__resource_is_disabled(rsc));
373 
374  rc = pe__name_and_nvpairs_xml(out, true, "group", 5,
375  XML_ATTR_ID, rsc->id,
376  "number_resources", count,
377  "maintenance", maint_s,
378  "managed", managed_s,
379  "disabled", disabled_s,
380  "description", desc);
381  free(count);
382  CRM_ASSERT(rc == pcmk_rc_ok);
383  }
384 
385  out->message(out, crm_map_element_name(child_rsc->xml), show_opts, child_rsc,
386  only_node, only_rsc);
387  }
388 
389  if (rc == pcmk_rc_ok) {
391  }
392 
393  return rc;
394 }
395 
396 PCMK__OUTPUT_ARGS("group", "uint32_t", "pe_resource_t *", "GList *", "GList *")
397 int
399 {
400  uint32_t show_opts = va_arg(args, uint32_t);
401  pe_resource_t *rsc = va_arg(args, pe_resource_t *);
402  GList *only_node = va_arg(args, GList *);
403  GList *only_rsc = va_arg(args, GList *);
404 
405  const char *desc = NULL;
406  int rc = pcmk_rc_no_output;
407 
408  gboolean parent_passes = pcmk__str_in_list(rsc_printable_id(rsc), only_rsc, pcmk__str_star_matches) ||
409  (strstr(rsc->id, ":") != NULL && pcmk__str_in_list(rsc->id, only_rsc, pcmk__str_star_matches));
410 
411  gboolean active = rsc->fns->active(rsc, TRUE);
412  gboolean partially_active = rsc->fns->active(rsc, FALSE);
413 
414  desc = pe__resource_description(rsc, show_opts);
415 
416  if (rsc->fns->is_filtered(rsc, only_rsc, TRUE)) {
417  return rc;
418  }
419 
420  if (pcmk_is_set(show_opts, pcmk_show_brief)) {
421  GList *rscs = pe__filter_rsc_list(rsc->children, only_rsc);
422 
423  if (rscs != NULL) {
424  group_header(out, &rc, rsc, !active && partially_active ? inactive_resources(rsc) : 0,
425  pcmk_is_set(show_opts, pcmk_show_inactive_rscs), desc);
426  pe__rscs_brief_output(out, rscs, show_opts | pcmk_show_inactive_rscs);
427 
428  rc = pcmk_rc_ok;
429  g_list_free(rscs);
430  }
431 
432  } else {
433  for (GList *gIter = rsc->children; gIter; gIter = gIter->next) {
434  pe_resource_t *child_rsc = (pe_resource_t *) gIter->data;
435 
436  if (skip_child_rsc(rsc, child_rsc, parent_passes, only_rsc, show_opts)) {
437  continue;
438  }
439 
440  group_header(out, &rc, rsc, !active && partially_active ? inactive_resources(rsc) : 0,
441  pcmk_is_set(show_opts, pcmk_show_inactive_rscs), desc);
442  out->message(out, crm_map_element_name(child_rsc->xml), show_opts,
443  child_rsc, only_node, only_rsc);
444  }
445  }
446 
447  PCMK__OUTPUT_LIST_FOOTER(out, rc);
448 
449  return rc;
450 }
451 
452 void
454 {
455  CRM_CHECK(rsc != NULL, return);
456 
457  pe_rsc_trace(rsc, "Freeing %s", rsc->id);
458 
459  for (GList *gIter = rsc->children; gIter != NULL; gIter = gIter->next) {
460  pe_resource_t *child_rsc = (pe_resource_t *) gIter->data;
461 
462  CRM_ASSERT(child_rsc);
463  pe_rsc_trace(child_rsc, "Freeing child %s", child_rsc->id);
464  child_rsc->fns->free(child_rsc);
465  }
466 
467  pe_rsc_trace(rsc, "Freeing child list");
468  g_list_free(rsc->children);
469 
470  common_free(rsc);
471 }
472 
473 enum rsc_role_e
474 group_resource_state(const pe_resource_t * rsc, gboolean current)
475 {
476  enum rsc_role_e group_role = RSC_ROLE_UNKNOWN;
477  GList *gIter = rsc->children;
478 
479  for (; gIter != NULL; gIter = gIter->next) {
480  pe_resource_t *child_rsc = (pe_resource_t *) gIter->data;
481  enum rsc_role_e role = child_rsc->fns->state(child_rsc, current);
482 
483  if (role > group_role) {
484  group_role = role;
485  }
486  }
487 
488  pe_rsc_trace(rsc, "%s role: %s", rsc->id, role2text(group_role));
489  return group_role;
490 }
491 
492 gboolean
493 pe__group_is_filtered(const pe_resource_t *rsc, GList *only_rsc,
494  gboolean check_parent)
495 {
496  gboolean passes = FALSE;
497 
498  if (check_parent
500  false)),
501  only_rsc, pcmk__str_star_matches)) {
502  passes = TRUE;
503  } else if (pcmk__str_in_list(rsc_printable_id(rsc), only_rsc, pcmk__str_star_matches)) {
504  passes = TRUE;
505  } else if (strstr(rsc->id, ":") != NULL && pcmk__str_in_list(rsc->id, only_rsc, pcmk__str_star_matches)) {
506  passes = TRUE;
507  } else {
508  for (const GList *iter = rsc->children;
509  iter != NULL; iter = iter->next) {
510 
511  const pe_resource_t *child_rsc = (const pe_resource_t *) iter->data;
512 
513  if (!child_rsc->fns->is_filtered(child_rsc, only_rsc, FALSE)) {
514  passes = TRUE;
515  break;
516  }
517  }
518  }
519 
520  return !passes;
521 }
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:235
int pe__group_xml(pcmk__output_t *out, va_list args)
Definition: group.c:340
char data[0]
Definition: cpg.c:55
Control output from tools.
#define pcmk__config_warn(fmt...)
GList * children
Definition: pe_types.h:409
xmlNode * xml
Definition: pe_types.h:349
#define XML_RSC_ATTR_INCARNATION
Definition: msg_xml.h:241
GHashTable * meta
Definition: pe_types.h:405
resource_object_functions_t * fns
Definition: pe_types.h:358
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:302
void print_rscs_brief(GList *rsc_list, const char *pre_text, long options, void *print_data, gboolean print_all)
Definition: native.c:1229
bool pe__resource_is_disabled(const pe_resource_t *rsc)
Definition: utils.c:755
const pe_resource_t * pe__const_top_resource(const pe_resource_t *rsc, bool include_bundle)
Definition: complex.c:947
void pcmk__add_separated_word(GString **list, size_t init_size, const char *word, const char *separator)
Definition: strings.c:703
const char * pe__resource_description(const pe_resource_t *rsc, uint32_t show_opts)
Definition: pe_output.c:19
GList * pe__filter_rsc_list(GList *rscs, GList *filter)
Definition: utils.c:811
const char * role2text(enum rsc_role_e role)
Definition: common.c:450
bool pe__group_flag_is_set(const pe_resource_t *group, uint32_t flags)
Definition: group.c:57
int pe__rscs_brief_output(pcmk__output_t *out, GList *rsc_list, unsigned int options)
gboolean group_unpack(pe_resource_t *rsc, pe_working_set_t *data_set)
Definition: group.c:180
#define XML_ATTR_ID
Definition: msg_xml.h:147
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:496
#define XML_CIB_TAG_RESOURCE
Definition: msg_xml.h:230
#define PCMK__OUTPUT_ARGS(ARGS...)
int pe__name_and_nvpairs_xml(pcmk__output_t *out, bool is_list, const char *tag_name, size_t pairs_count,...)
Definition: pe_output.c:568
#define pe_warn_once(pe_wo_bit, fmt...)
Definition: internal.h:177
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
void common_free(pe_resource_t *rsc)
Definition: complex.c:964
int pe__group_default(pcmk__output_t *out, va_list args)
Definition: group.c:398
unsigned long long flags
Definition: pe_types.h:373
#define XML_RSC_ATTR_ORDERED
Definition: msg_xml.h:239
pe_working_set_t * data_set
void(* print)(pe_resource_t *, const char *, long, void *)
Definition: pe_types.h:52
void(* free)(pe_resource_t *)
Definition: pe_types.h:56
enum pe_obj_types variant
Definition: pe_types.h:356
gboolean(* is_filtered)(const pe_resource_t *, GList *, gboolean)
Definition: pe_types.h:58
void pcmk__output_xml_pop_parent(pcmk__output_t *out)
Definition: output_xml.c:518
pe_resource_t * pe__last_group_member(const pe_resource_t *group)
Definition: group.c:37
#define PCMK__OUTPUT_LIST_FOOTER(out_obj, retcode)
enum rsc_role_e(* state)(const pe_resource_t *, gboolean)
Definition: pe_types.h:54
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:427
void group_print(pe_resource_t *rsc, const char *pre_text, long options, void *print_data)
Definition: group.c:289
const char * rsc_printable_id(const pe_resource_t *rsc)
Definition: utils.c:583
#define PCMK__OUTPUT_LIST_HEADER(out_obj, cond, retcode, title...)
Cluster status and scheduling.
void * variant_opaque
Definition: pe_types.h:357
#define CRM_ASSERT(expr)
Definition: results.h:42
#define status_print(fmt, args...)
enum rsc_role_e group_resource_state(const pe_resource_t *rsc, gboolean current)
Definition: group.c:474
This structure contains everything that makes up a single output formatter.
#define pcmk__plural_s(i)
rsc_role_e
Possible roles that a resource can be in.
Definition: common.h:92
struct group_variant_data_s group_variant_data_t
#define pe_rsc_maintenance
Definition: pe_types.h:308
#define pe_rsc_trace(rsc, fmt, args...)
Definition: internal.h:50
void group_free(pe_resource_t *rsc)
Definition: group.c:453
gboolean pe__group_is_filtered(const pe_resource_t *rsc, GList *only_rsc, gboolean check_parent)
Definition: group.c:493
#define pe_rsc_managed
Definition: pe_types.h:273
gboolean group_active(pe_resource_t *rsc, gboolean all)
Definition: group.c:235
uint64_t flags
Definition: remote.c:215
int pe__unpack_resource(xmlNode *xml_obj, pe_resource_t **rsc, pe_resource_t *parent, pe_working_set_t *data_set)
Definition: complex.c:590
gboolean(* active)(pe_resource_t *, gboolean)
Definition: pe_types.h:53
char * id
Definition: pe_types.h:347
gboolean pcmk__str_in_list(const gchar *s, const GList *lst, uint32_t flags)
Definition: strings.c:888