pacemaker  2.0.2-debe490
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
group.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2018 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This source code is licensed under the GNU Lesser General Public License
5  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
6  */
7 
8 #include <crm_internal.h>
9 
10 #include <crm/pengine/rules.h>
11 #include <crm/pengine/status.h>
12 #include <crm/pengine/internal.h>
13 #include <unpack.h>
14 #include <crm/msg_xml.h>
15 
16 #define VARIANT_GROUP 1
17 #include "./variant.h"
18 
19 gboolean
21 {
22  xmlNode *xml_obj = rsc->xml;
23  xmlNode *xml_native_rsc = NULL;
24  group_variant_data_t *group_data = NULL;
25  const char *group_ordered = g_hash_table_lookup(rsc->meta, XML_RSC_ATTR_ORDERED);
26  const char *group_colocated = g_hash_table_lookup(rsc->meta, "collocated");
27  const char *clone_id = NULL;
28 
29  pe_rsc_trace(rsc, "Processing resource %s...", rsc->id);
30 
31  group_data = calloc(1, sizeof(group_variant_data_t));
32  group_data->num_children = 0;
33  group_data->first_child = NULL;
34  group_data->last_child = NULL;
35  rsc->variant_opaque = group_data;
36 
37  group_data->ordered = TRUE;
38  group_data->colocated = TRUE;
39 
40  if (group_ordered != NULL) {
41  crm_str_to_boolean(group_ordered, &(group_data->ordered));
42  }
43  if (group_colocated != NULL) {
44  crm_str_to_boolean(group_colocated, &(group_data->colocated));
45  }
46 
48 
49  for (xml_native_rsc = __xml_first_child(xml_obj); xml_native_rsc != NULL;
50  xml_native_rsc = __xml_next_element(xml_native_rsc)) {
51  if (crm_str_eq((const char *)xml_native_rsc->name, XML_CIB_TAG_RESOURCE, TRUE)) {
52  resource_t *new_rsc = NULL;
53 
54  crm_xml_add(xml_native_rsc, XML_RSC_ATTR_INCARNATION, clone_id);
55  if (common_unpack(xml_native_rsc, &new_rsc, rsc, data_set) == FALSE) {
56  pe_err("Failed unpacking resource %s", crm_element_value(xml_obj, XML_ATTR_ID));
57  if (new_rsc != NULL && new_rsc->fns != NULL) {
58  new_rsc->fns->free(new_rsc);
59  }
60  }
61 
62  group_data->num_children++;
63  rsc->children = g_list_append(rsc->children, new_rsc);
64 
65  if (group_data->first_child == NULL) {
66  group_data->first_child = new_rsc;
67  }
68  group_data->last_child = new_rsc;
69  print_resource(LOG_TRACE, "Added ", new_rsc, FALSE);
70  }
71  }
72 
73  if (group_data->num_children == 0) {
74 #if 0
75  /* Bug #1287 */
76  crm_config_err("Group %s did not have any children", rsc->id);
77  return FALSE;
78 #else
79  crm_config_warn("Group %s did not have any children", rsc->id);
80  return TRUE;
81 #endif
82  }
83 
84  pe_rsc_trace(rsc, "Added %d children to resource %s...", group_data->num_children, rsc->id);
85 
86  return TRUE;
87 }
88 
89 gboolean
90 group_active(resource_t * rsc, gboolean all)
91 {
92  gboolean c_all = TRUE;
93  gboolean c_any = FALSE;
94  GListPtr gIter = rsc->children;
95 
96  for (; gIter != NULL; gIter = gIter->next) {
97  resource_t *child_rsc = (resource_t *) gIter->data;
98 
99  if (child_rsc->fns->active(child_rsc, all)) {
100  c_any = TRUE;
101  } else {
102  c_all = FALSE;
103  }
104  }
105 
106  if (c_any == FALSE) {
107  return FALSE;
108  } else if (all && c_all == FALSE) {
109  return FALSE;
110  }
111  return TRUE;
112 }
113 
114 static void
115 group_print_xml(resource_t * rsc, const char *pre_text, long options, void *print_data)
116 {
117  GListPtr gIter = rsc->children;
118  char *child_text = crm_concat(pre_text, " ", ' ');
119 
120  status_print("%s<group id=\"%s\" ", pre_text, rsc->id);
121  status_print("number_resources=\"%d\" ", g_list_length(rsc->children));
122  status_print(">\n");
123 
124  for (; gIter != NULL; gIter = gIter->next) {
125  resource_t *child_rsc = (resource_t *) gIter->data;
126 
127  child_rsc->fns->print(child_rsc, child_text, options, print_data);
128  }
129 
130  status_print("%s</group>\n", pre_text);
131  free(child_text);
132 }
133 
134 void
135 group_print(resource_t * rsc, const char *pre_text, long options, void *print_data)
136 {
137  char *child_text = NULL;
138  GListPtr gIter = rsc->children;
139 
140  if (pre_text == NULL) {
141  pre_text = " ";
142  }
143 
144  if (options & pe_print_xml) {
145  group_print_xml(rsc, pre_text, options, print_data);
146  return;
147  }
148 
149  child_text = crm_concat(pre_text, " ", ' ');
150 
151  status_print("%sResource Group: %s", pre_text ? pre_text : "", rsc->id);
152 
153  if (options & pe_print_html) {
154  status_print("\n<ul>\n");
155 
156  } else if ((options & pe_print_log) == 0) {
157  status_print("\n");
158  }
159 
160  if (options & pe_print_brief) {
161  print_rscs_brief(rsc->children, child_text, options, print_data, TRUE);
162 
163  } else {
164  for (; gIter != NULL; gIter = gIter->next) {
165  resource_t *child_rsc = (resource_t *) gIter->data;
166 
167  if (options & pe_print_html) {
168  status_print("<li>\n");
169  }
170  child_rsc->fns->print(child_rsc, child_text, options, print_data);
171  if (options & pe_print_html) {
172  status_print("</li>\n");
173  }
174  }
175  }
176 
177  if (options & pe_print_html) {
178  status_print("</ul>\n");
179  }
180  free(child_text);
181 }
182 
183 void
185 {
186  CRM_CHECK(rsc != NULL, return);
187 
188  pe_rsc_trace(rsc, "Freeing %s", rsc->id);
189 
190  for (GListPtr gIter = rsc->children; gIter != NULL; gIter = gIter->next) {
191  resource_t *child_rsc = (resource_t *) gIter->data;
192 
193  CRM_ASSERT(child_rsc);
194  pe_rsc_trace(child_rsc, "Freeing child %s", child_rsc->id);
195  child_rsc->fns->free(child_rsc);
196  }
197 
198  pe_rsc_trace(rsc, "Freeing child list");
199  g_list_free(rsc->children);
200 
201  common_free(rsc);
202 }
203 
204 enum rsc_role_e
205 group_resource_state(const resource_t * rsc, gboolean current)
206 {
207  enum rsc_role_e group_role = RSC_ROLE_UNKNOWN;
208  GListPtr gIter = rsc->children;
209 
210  for (; gIter != NULL; gIter = gIter->next) {
211  resource_t *child_rsc = (resource_t *) gIter->data;
212  enum rsc_role_e role = child_rsc->fns->state(child_rsc, current);
213 
214  if (role > group_role) {
215  group_role = role;
216  }
217  }
218 
219  pe_rsc_trace(rsc, "%s role: %s", rsc->id, role2text(group_role));
220  return group_role;
221 }
#define LOG_TRACE
Definition: logging.h:26
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:156
void group_free(resource_t *rsc)
Definition: group.c:184
enum rsc_role_e(* state)(const pe_resource_t *, gboolean)
Definition: pe_types.h:51
#define crm_config_err(fmt...)
Definition: crm_internal.h:179
xmlNode * xml
Definition: pe_types.h:286
#define XML_RSC_ATTR_INCARNATION
Definition: msg_xml.h:186
GHashTable * meta
Definition: pe_types.h:336
gboolean common_unpack(xmlNode *xml_obj, resource_t **rsc, resource_t *parent, pe_working_set_t *data_set)
Definition: complex.c:360
void common_free(resource_t *rsc)
Definition: complex.c:760
resource_object_functions_t * fns
Definition: pe_types.h:295
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:275
#define status_print(fmt, args...)
Definition: unpack.h:67
void print_rscs_brief(GListPtr rsc_list, const char *pre_text, long options, void *print_data, gboolean print_all)
Definition: native.c:927
enum rsc_role_e group_resource_state(const resource_t *rsc, gboolean current)
Definition: group.c:205
const char * role2text(enum rsc_role_e role)
Definition: common.c:329
#define XML_ATTR_ID
Definition: msg_xml.h:96
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:423
#define XML_CIB_TAG_RESOURCE
Definition: msg_xml.h:174
#define XML_RSC_ATTR_ORDERED
Definition: msg_xml.h:184
void group_print(resource_t *rsc, const char *pre_text, long options, void *print_data)
Definition: group.c:135
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
Definition: strings.c:220
void(* print)(pe_resource_t *, const char *, long, void *)
Definition: pe_types.h:49
#define crm_config_warn(fmt...)
Definition: crm_internal.h:180
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:183
Cluster status and scheduling.
GListPtr children
Definition: pe_types.h:340
void * variant_opaque
Definition: pe_types.h:294
#define CRM_ASSERT(expr)
Definition: results.h:42
gboolean group_active(resource_t *rsc, gboolean all)
Definition: group.c:90
void(* free)(pe_resource_t *)
Definition: pe_types.h:53
rsc_role_e
Definition: common.h:86
#define pe_rsc_trace(rsc, fmt, args...)
Definition: internal.h:18
gboolean(* active)(pe_resource_t *, gboolean)
Definition: pe_types.h:50
#define pe_err(fmt...)
Definition: internal.h:20
void print_resource(int log_level, const char *pre_text, resource_t *rsc, gboolean details)
Definition: utils.c:1297
gboolean group_unpack(resource_t *rsc, pe_working_set_t *data_set)
Definition: group.c:20
GList * GListPtr
Definition: crm.h:192
char * id
Definition: pe_types.h:284