pacemaker  1.1.18-7fdfbbe
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 (C) 2004 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <crm_internal.h>
20 
21 #include <crm/pengine/rules.h>
22 #include <crm/pengine/status.h>
23 #include <crm/pengine/internal.h>
24 #include <unpack.h>
25 #include <crm/msg_xml.h>
26 
27 #define VARIANT_GROUP 1
28 #include "./variant.h"
29 
30 gboolean
32 {
33  xmlNode *xml_obj = rsc->xml;
34  xmlNode *xml_native_rsc = NULL;
35  group_variant_data_t *group_data = NULL;
36  const char *group_ordered = g_hash_table_lookup(rsc->meta, XML_RSC_ATTR_ORDERED);
37  const char *group_colocated = g_hash_table_lookup(rsc->meta, "collocated");
38  const char *clone_id = NULL;
39 
40  pe_rsc_trace(rsc, "Processing resource %s...", rsc->id);
41 
42  group_data = calloc(1, sizeof(group_variant_data_t));
43  group_data->num_children = 0;
44  group_data->first_child = NULL;
45  group_data->last_child = NULL;
46  rsc->variant_opaque = group_data;
47 
48  group_data->ordered = TRUE;
49  group_data->colocated = TRUE;
50 
51  if (group_ordered != NULL) {
52  crm_str_to_boolean(group_ordered, &(group_data->ordered));
53  }
54  if (group_colocated != NULL) {
55  crm_str_to_boolean(group_colocated, &(group_data->colocated));
56  }
57 
59 
60  for (xml_native_rsc = __xml_first_child(xml_obj); xml_native_rsc != NULL;
61  xml_native_rsc = __xml_next_element(xml_native_rsc)) {
62  if (crm_str_eq((const char *)xml_native_rsc->name, XML_CIB_TAG_RESOURCE, TRUE)) {
63  resource_t *new_rsc = NULL;
64 
65  crm_xml_add(xml_native_rsc, XML_RSC_ATTR_INCARNATION, clone_id);
66  if (common_unpack(xml_native_rsc, &new_rsc, rsc, data_set) == FALSE) {
67  pe_err("Failed unpacking resource %s", crm_element_value(xml_obj, XML_ATTR_ID));
68  if (new_rsc != NULL && new_rsc->fns != NULL) {
69  new_rsc->fns->free(new_rsc);
70  }
71  }
72 
73  group_data->num_children++;
74  rsc->children = g_list_append(rsc->children, new_rsc);
75 
76  if (group_data->first_child == NULL) {
77  group_data->first_child = new_rsc;
78  }
79  group_data->last_child = new_rsc;
80  print_resource(LOG_DEBUG_3, "Added ", new_rsc, FALSE);
81  }
82  }
83 
84  if (group_data->num_children == 0) {
85 #if 0
86  /* Bug #1287 */
87  crm_config_err("Group %s did not have any children", rsc->id);
88  return FALSE;
89 #else
90  crm_config_warn("Group %s did not have any children", rsc->id);
91  return TRUE;
92 #endif
93  }
94 
95  pe_rsc_trace(rsc, "Added %d children to resource %s...", group_data->num_children, rsc->id);
96 
97  return TRUE;
98 }
99 
100 gboolean
101 group_active(resource_t * rsc, gboolean all)
102 {
103  gboolean c_all = TRUE;
104  gboolean c_any = FALSE;
105  GListPtr gIter = rsc->children;
106 
107  for (; gIter != NULL; gIter = gIter->next) {
108  resource_t *child_rsc = (resource_t *) gIter->data;
109 
110  if (child_rsc->fns->active(child_rsc, all)) {
111  c_any = TRUE;
112  } else {
113  c_all = FALSE;
114  }
115  }
116 
117  if (c_any == FALSE) {
118  return FALSE;
119  } else if (all && c_all == FALSE) {
120  return FALSE;
121  }
122  return TRUE;
123 }
124 
125 static void
126 group_print_xml(resource_t * rsc, const char *pre_text, long options, void *print_data)
127 {
128  GListPtr gIter = rsc->children;
129  char *child_text = crm_concat(pre_text, " ", ' ');
130 
131  status_print("%s<group id=\"%s\" ", pre_text, rsc->id);
132  status_print("number_resources=\"%d\" ", g_list_length(rsc->children));
133  status_print(">\n");
134 
135  for (; gIter != NULL; gIter = gIter->next) {
136  resource_t *child_rsc = (resource_t *) gIter->data;
137 
138  child_rsc->fns->print(child_rsc, child_text, options, print_data);
139  }
140 
141  status_print("%s</group>\n", pre_text);
142  free(child_text);
143 }
144 
145 void
146 group_print(resource_t * rsc, const char *pre_text, long options, void *print_data)
147 {
148  char *child_text = NULL;
149  GListPtr gIter = rsc->children;
150 
151  if (pre_text == NULL) {
152  pre_text = " ";
153  }
154 
155  if (options & pe_print_xml) {
156  group_print_xml(rsc, pre_text, options, print_data);
157  return;
158  }
159 
160  child_text = crm_concat(pre_text, " ", ' ');
161 
162  status_print("%sResource Group: %s", pre_text ? pre_text : "", rsc->id);
163 
164  if (options & pe_print_html) {
165  status_print("\n<ul>\n");
166 
167  } else if ((options & pe_print_log) == 0) {
168  status_print("\n");
169  }
170 
171  if (options & pe_print_brief) {
172  print_rscs_brief(rsc->children, child_text, options, print_data, TRUE);
173 
174  } else {
175  for (; gIter != NULL; gIter = gIter->next) {
176  resource_t *child_rsc = (resource_t *) gIter->data;
177 
178  if (options & pe_print_html) {
179  status_print("<li>\n");
180  }
181  child_rsc->fns->print(child_rsc, child_text, options, print_data);
182  if (options & pe_print_html) {
183  status_print("</li>\n");
184  }
185  }
186  }
187 
188  if (options & pe_print_html) {
189  status_print("</ul>\n");
190  }
191  free(child_text);
192 }
193 
194 void
196 {
197  GListPtr gIter = rsc->children;
198 
199  CRM_CHECK(rsc != NULL, return);
200 
201  pe_rsc_trace(rsc, "Freeing %s", rsc->id);
202 
203  for (; gIter != NULL; gIter = gIter->next) {
204  resource_t *child_rsc = (resource_t *) gIter->data;
205 
206  CRM_ASSERT(child_rsc);
207  pe_rsc_trace(child_rsc, "Freeing child %s", child_rsc->id);
208  child_rsc->fns->free(child_rsc);
209  }
210 
211  pe_rsc_trace(rsc, "Freeing child list");
212  g_list_free(rsc->children);
213 
214  common_free(rsc);
215 }
216 
217 enum rsc_role_e
218 group_resource_state(const resource_t * rsc, gboolean current)
219 {
220  enum rsc_role_e group_role = RSC_ROLE_UNKNOWN;
221  GListPtr gIter = rsc->children;
222 
223  for (; gIter != NULL; gIter = gIter->next) {
224  resource_t *child_rsc = (resource_t *) gIter->data;
225  enum rsc_role_e role = child_rsc->fns->state(child_rsc, current);
226 
227  if (role > group_role) {
228  group_role = role;
229  }
230  }
231 
232  pe_rsc_trace(rsc, "%s role: %s", rsc->id, role2text(group_role));
233  return group_role;
234 }
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:164
xmlNode * xml
Definition: status.h:259
void group_free(resource_t *rsc)
Definition: group.c:195
#define crm_config_err(fmt...)
Definition: crm_internal.h:256
#define XML_RSC_ATTR_INCARNATION
Definition: msg_xml.h:211
gboolean common_unpack(xmlNode *xml_obj, resource_t **rsc, resource_t *parent, pe_working_set_t *data_set)
Definition: complex.c:465
void(* free)(resource_t *)
Definition: complex.h:51
void common_free(resource_t *rsc)
Definition: complex.c:911
#define status_print(fmt, args...)
Definition: unpack.h:79
void print_rscs_brief(GListPtr rsc_list, const char *pre_text, long options, void *print_data, gboolean print_all)
Definition: native.c:883
GListPtr children
Definition: status.h:301
enum rsc_role_e group_resource_state(const resource_t *rsc, gboolean current)
Definition: group.c:218
char * id
Definition: status.h:257
const char * role2text(enum rsc_role_e role)
Definition: common.c:346
gboolean(* active)(resource_t *, gboolean)
Definition: complex.h:48
#define XML_ATTR_ID
Definition: msg_xml.h:102
#define XML_CIB_TAG_RESOURCE
Definition: msg_xml.h:195
resource_object_functions_t * fns
Definition: status.h:266
void * variant_opaque
Definition: status.h:264
#define XML_RSC_ATTR_ORDERED
Definition: msg_xml.h:209
const char * crm_element_value(xmlNode *data, const char *name)
Definition: xml.c:5165
void group_print(resource_t *rsc, const char *pre_text, long options, void *print_data)
Definition: group.c:146
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
Definition: strings.c:213
#define crm_config_warn(fmt...)
Definition: crm_internal.h:257
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Definition: xml.c:2490
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:176
GHashTable * meta
Definition: status.h:297
void(* print)(resource_t *, const char *, long, void *)
Definition: complex.h:47
enum rsc_role_e(* state)(const resource_t *, gboolean)
Definition: complex.h:49
gboolean group_active(resource_t *rsc, gboolean all)
Definition: group.c:101
#define CRM_ASSERT(expr)
Definition: error.h:35
rsc_role_e
Definition: common.h:81
#define pe_rsc_trace(rsc, fmt, args...)
Definition: internal.h:26
char * crm_concat(const char *prefix, const char *suffix, char join)
Definition: strings.c:32
#define pe_err(fmt...)
Definition: internal.h:28
void print_resource(int log_level, const char *pre_text, resource_t *rsc, gboolean details)
Definition: utils.c:1284
#define LOG_DEBUG_3
Definition: logging.h:32
gboolean group_unpack(resource_t *rsc, pe_working_set_t *data_set)
Definition: group.c:31
GList * GListPtr
Definition: crm.h:218