-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathattrd_attributes.c
476 lines (416 loc) · 15.1 KB
/
attrd_attributes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* Copyright 2013-2025 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
* This source code is licensed under the GNU General Public License version 2
* or later (GPLv2+) WITHOUT ANY WARRANTY.
*/
#include <crm_internal.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <glib.h>
#include <crm/common/logging.h>
#include <crm/common/results.h>
#include <crm/common/strings_internal.h>
#include <crm/common/xml.h>
#include "pacemaker-attrd.h"
static attribute_t *
attrd_create_attribute(xmlNode *xml)
{
int is_private = 0;
long long dampen = 0;
const char *name = crm_element_value(xml, PCMK__XA_ATTR_NAME);
const char *set_type = crm_element_value(xml, PCMK__XA_ATTR_SET_TYPE);
const char *dampen_s = crm_element_value(xml, PCMK__XA_ATTR_DAMPENING);
attribute_t *a = NULL;
if (set_type == NULL) {
set_type = PCMK_XE_INSTANCE_ATTRIBUTES;
}
/* Set type is meaningful only when writing to the CIB. Private
* attributes are not written.
*/
crm_element_value_int(xml, PCMK__XA_ATTR_IS_PRIVATE, &is_private);
if (!is_private && !pcmk__str_any_of(set_type,
PCMK_XE_INSTANCE_ATTRIBUTES,
PCMK_XE_UTILIZATION, NULL)) {
crm_warn("Ignoring attribute %s with invalid set type %s",
pcmk__s(name, "(unidentified)"), set_type);
return NULL;
}
a = pcmk__assert_alloc(1, sizeof(attribute_t));
a->id = pcmk__str_copy(name);
a->set_type = pcmk__str_copy(set_type);
a->set_id = crm_element_value_copy(xml, PCMK__XA_ATTR_SET);
a->user = crm_element_value_copy(xml, PCMK__XA_ATTR_USER);
a->values = pcmk__strikey_table(NULL, attrd_free_attribute_value);
if (is_private) {
attrd_set_attr_flags(a, attrd_attr_is_private);
}
if (dampen_s != NULL) {
dampen = crm_get_msec(dampen_s);
}
if (dampen > 0) {
a->timeout_ms = (int) QB_MIN(dampen, INT_MAX);
a->timer = attrd_add_timer(a->id, a->timeout_ms, a);
} else if (dampen < 0) {
crm_warn("Ignoring invalid delay %s for attribute %s", dampen_s, a->id);
}
crm_trace("Created attribute %s with %s write delay and %s CIB user",
a->id,
((dampen > 0)? pcmk__readable_interval(a->timeout_ms) : "no"),
pcmk__s(a->user, "default"));
g_hash_table_replace(attributes, a->id, a);
return a;
}
static int
attrd_update_dampening(attribute_t *a, xmlNode *xml, const char *attr)
{
const char *dvalue = crm_element_value(xml, PCMK__XA_ATTR_DAMPENING);
long long dampen = 0;
if (dvalue == NULL) {
crm_warn("Could not update %s: peer did not specify value for delay",
attr);
return EINVAL;
}
dampen = crm_get_msec(dvalue);
if (dampen < 0) {
crm_warn("Could not update %s: invalid delay value %dms (%s)",
attr, dampen, dvalue);
return EINVAL;
}
if (a->timeout_ms != dampen) {
mainloop_timer_del(a->timer);
a->timeout_ms = (int) QB_MIN(dampen, INT_MAX);
if (dampen > 0) {
a->timer = attrd_add_timer(attr, a->timeout_ms, a);
crm_info("Update attribute %s delay to %dms (%s)",
attr, dampen, dvalue);
} else {
a->timer = NULL;
crm_info("Update attribute %s to remove delay", attr);
}
/* If dampening changed, do an immediate write-out,
* otherwise repeated dampening changes would prevent write-outs
*/
attrd_write_or_elect_attribute(a);
}
return pcmk_rc_ok;
}
GHashTable *attributes = NULL;
/*!
* \internal
* \brief Create an XML representation of an attribute for use in peer messages
*
* \param[in,out] parent Create attribute XML as child element of this
* \param[in] a Attribute to represent
* \param[in] v Attribute value to represent
* \param[in] force_write If true, value should be written even if unchanged
*
* \return XML representation of attribute
*/
xmlNode *
attrd_add_value_xml(xmlNode *parent, const attribute_t *a,
const attribute_value_t *v, bool force_write)
{
xmlNode *xml = pcmk__xe_create(parent, __func__);
crm_xml_add(xml, PCMK__XA_ATTR_NAME, a->id);
crm_xml_add(xml, PCMK__XA_ATTR_SET_TYPE, a->set_type);
crm_xml_add(xml, PCMK__XA_ATTR_SET, a->set_id);
crm_xml_add(xml, PCMK__XA_ATTR_USER, a->user);
crm_xml_add(xml, PCMK__XA_ATTR_HOST, v->nodename);
/* @COMPAT Prior to 2.1.10 and 3.0.1, the node's cluster ID was added
* instead of its XML ID. For Corosync and Pacemaker Remote nodes, those are
* the same, but if we ever support node XML IDs that differ from their
* cluster IDs, we will have to drop support for rolling upgrades from
* versions before those.
*/
crm_xml_add(xml, PCMK__XA_ATTR_HOST_ID, attrd_get_node_xml_id(v->nodename));
crm_xml_add(xml, PCMK__XA_ATTR_VALUE, v->current);
crm_xml_add_int(xml, PCMK__XA_ATTR_DAMPENING,
pcmk__timeout_ms2s(a->timeout_ms));
crm_xml_add_int(xml, PCMK__XA_ATTR_IS_PRIVATE,
pcmk_is_set(a->flags, attrd_attr_is_private));
crm_xml_add_int(xml, PCMK__XA_ATTR_IS_REMOTE,
pcmk_is_set(v->flags, attrd_value_remote));
crm_xml_add_int(xml, PCMK__XA_ATTRD_IS_FORCE_WRITE, force_write);
return xml;
}
void
attrd_clear_value_seen(void)
{
GHashTableIter aIter;
GHashTableIter vIter;
attribute_t *a;
attribute_value_t *v = NULL;
g_hash_table_iter_init(&aIter, attributes);
while (g_hash_table_iter_next(&aIter, NULL, (gpointer *) & a)) {
g_hash_table_iter_init(&vIter, a->values);
while (g_hash_table_iter_next(&vIter, NULL, (gpointer *) & v)) {
attrd_clear_value_flags(v, attrd_value_from_peer);
}
}
}
attribute_t *
attrd_populate_attribute(xmlNode *xml, const char *attr)
{
attribute_t *a = NULL;
bool update_both = false;
const char *op = crm_element_value(xml, PCMK_XA_TASK);
// NULL because PCMK__ATTRD_CMD_SYNC_RESPONSE has no PCMK_XA_TASK
update_both = pcmk__str_eq(op, PCMK__ATTRD_CMD_UPDATE_BOTH,
pcmk__str_null_matches);
// Look up or create attribute entry
a = g_hash_table_lookup(attributes, attr);
if (a == NULL) {
if (update_both || pcmk__str_eq(op, PCMK__ATTRD_CMD_UPDATE, pcmk__str_none)) {
a = attrd_create_attribute(xml);
if (a == NULL) {
return NULL;
}
} else {
crm_warn("Could not update %s: attribute not found", attr);
return NULL;
}
}
// Update attribute dampening
if (update_both || pcmk__str_eq(op, PCMK__ATTRD_CMD_UPDATE_DELAY, pcmk__str_none)) {
int rc = attrd_update_dampening(a, xml, attr);
if (rc != pcmk_rc_ok || !update_both) {
return NULL;
}
}
return a;
}
/*!
* \internal
* \brief Get the XML ID used to write out an attribute set
*
* \param[in] attr Attribute to get set ID for
* \param[in] node_state_id XML ID of node state that attribute value is for
*
* \return Newly allocated string with XML ID to use for \p attr set
*/
char *
attrd_set_id(const attribute_t *attr, const char *node_state_id)
{
char *set_id = NULL;
pcmk__assert((attr != NULL) && (node_state_id != NULL));
if (pcmk__str_empty(attr->set_id)) {
/* @COMPAT This should really take the set type into account. Currently
* we use the same XML ID for transient attributes and utilization
* attributes. It doesn't cause problems because the status section is
* not limited by the schema in any way, but it's still unfortunate.
* For backward compatibility reasons, we can't change this.
*/
set_id = crm_strdup_printf("%s-%s", PCMK_XE_STATUS, node_state_id);
} else {
/* @COMPAT When the user specifies a set ID for an attribute, it is the
* same for every node. That is less than ideal, but again, the schema
* doesn't enforce anything for the status section. We couldn't change
* it without allowing the set ID to vary per value rather than per
* attribute, which would break backward compatibility, pose design
* challenges, and potentially cause problems in rolling upgrades.
*/
set_id = pcmk__str_copy(attr->set_id);
}
pcmk__xml_sanitize_id(set_id);
return set_id;
}
/*!
* \internal
* \brief Get the XML ID used to write out an attribute value
*
* \param[in] attr Attribute to get value XML ID for
* \param[in] node_state_id UUID of node that attribute value is for
*
* \return Newly allocated string with XML ID of \p attr value
*/
char *
attrd_nvpair_id(const attribute_t *attr, const char *node_state_id)
{
char *nvpair_id = NULL;
if (attr->set_id != NULL) {
nvpair_id = crm_strdup_printf("%s-%s", attr->set_id, attr->id);
} else {
nvpair_id = crm_strdup_printf(PCMK_XE_STATUS "-%s-%s",
node_state_id, attr->id);
}
pcmk__xml_sanitize_id(nvpair_id);
return nvpair_id;
}
/*!
* \internal
* \brief Check whether an attribute is one that must be written to the CIB
*
* \param[in] a Attribute to check
*
* \return false if we are in standalone mode or \p a is private, otherwise true
*/
bool
attrd_for_cib(const attribute_t *a)
{
return !stand_alone && (a != NULL)
&& !pcmk_is_set(a->flags, attrd_attr_is_private);
}
/*!
* \internal
* \brief Drop NULL attribute values as indicated by given function
*
* Drop all NULL node attribute values that a given function indicates should
* be, based on the XML ID of an element that was removed from the CIB.
*
* \param[in] cib_id ID of XML element that was removed from CIB
* (a name/value pair, an attribute set, or a node state)
* \param[in] set_type If not NULL, drop only attributes with this set type
* \param[in] func Call this function for every attribute/value
* combination; keep the value if it returns 0, drop
* the value and keep checking the attribute's other
* values if it returns 1, or drop value and stop checking
* the attribute's further values if it returns 2
*/
static void
drop_removed_values(const char *cib_id, const char *set_type,
int (*func)(const attribute_t *, const attribute_value_t *,
const char *))
{
attribute_t *a = NULL;
GHashTableIter attr_iter;
const char *entry_type = pcmk__s(set_type, "status entry"); // for log
CRM_CHECK((cib_id != NULL) && (func != NULL), return);
// Check every attribute ...
g_hash_table_iter_init(&attr_iter, attributes);
while (g_hash_table_iter_next(&attr_iter, NULL, (gpointer *) &a)) {
attribute_value_t *v = NULL;
GHashTableIter value_iter;
if (!attrd_for_cib(a)
|| ((set_type != NULL)
&& !pcmk__str_eq(a->set_type, set_type, pcmk__str_none))) {
continue;
}
// Check every value of the attribute ...
g_hash_table_iter_init(&value_iter, a->values);
while (g_hash_table_iter_next(&value_iter, NULL, (gpointer *) &v)) {
int rc = 0;
if (v->current != NULL) {
continue;
}
if (attrd_get_node_xml_id(v->nodename) == NULL) {
/* This shouldn't be a significant issue, since we will know the
* XML ID if *any* attribute for the node has ever been written.
*/
crm_trace("Ignoring %s[%s] after CIB erasure of %s %s because "
"its node XML ID is unknown (possibly attribute was "
"never written to CIB)",
a->id, v->nodename, entry_type, cib_id);
continue;
}
rc = func(a, v, cib_id);
if (rc > 0) {
crm_debug("Dropping %s[%s] after CIB erasure of %s %s",
a->id, v->nodename, entry_type, cib_id);
g_hash_table_iter_remove(&value_iter);
if (rc > 1) {
return;
}
}
}
}
}
/*!
* \internal
* \brief Check whether an attribute value has a given XML ID
*
* \param[in] a Attribute being checked
* \param[in] v Attribute value being checked
* \param[in] cib_id ID of name/value pair element that was removed from CIB
*
* \return 2 if value matches XML ID, otherwise 0
*/
static int
nvpair_matches(const attribute_t *a, const attribute_value_t *v,
const char *cib_id)
{
char *id = attrd_nvpair_id(a, attrd_get_node_xml_id(v->nodename));
/* The attribute manager doesn't enforce uniqueness for value XML IDs
* (schema validation could be disabled), but in practice they should be,
* so we can stop looping if we find a match.
*/
int rc = pcmk__str_eq(id, cib_id, pcmk__str_none)? 2 : 0;
free(id);
return rc;
}
/*!
* \internal
* \brief Drop attribute value corresponding to given removed CIB entry
*
* \param[in] cib_id ID of name/value pair element that was removed from CIB
*/
void
attrd_drop_removed_value(const char *cib_id)
{
drop_removed_values(cib_id, NULL, nvpair_matches);
}
/*!
* \internal
* \brief Check whether an attribute value has a given attribute set ID
*
* \param[in] a Attribute being checked
* \param[in] v Attribute value being checked
* \param[in] cib_id ID of attribute set that was removed from CIB
*
* \return 1 if value matches XML ID, otherwise 0
*/
static int
set_id_matches(const attribute_t *a, const attribute_value_t *v,
const char *cib_id)
{
char *id = attrd_set_id(a, attrd_get_node_xml_id(v->nodename));
int rc = pcmk__str_eq(id, cib_id, pcmk__str_none)? 1: 0;
free(id);
return rc;
}
/*!
* \internal
* \brief Drop all removed attribute values for an attribute set
*
* \param[in] set_type XML element name of set that was removed
* \param[in] cib_id ID of attribute set that was removed from CIB
*/
void
attrd_drop_removed_set(const char *set_type, const char *cib_id)
{
drop_removed_values(cib_id, set_type, set_id_matches);
}
/*!
* \internal
* \brief Check whether an attribute value has a given node state XML ID
*
* \param[in] a Attribute being checked
* \param[in] v Attribute value being checked
* \param[in] cib_id ID of node state that was removed from CIB
*
* \return 1 if value matches removed ID, otherwise 0
*/
static int
node_matches(const attribute_t *a, const attribute_value_t *v,
const char *cib_id)
{
if (pcmk__str_eq(cib_id, attrd_get_node_xml_id(v->nodename),
pcmk__str_none)) {
return 1;
}
return 0;
}
/*!
* \internal
* \brief Drop all removed attribute values for a node
*
* \param[in] cib_id ID of node state that was removed from CIB
*/
void
attrd_drop_removed_values(const char *cib_id)
{
drop_removed_values(cib_id, NULL, node_matches);
}