Coverage Report

Created: 2025-11-26 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pacemaker/include/crm/common/resources_internal.h
Line
Count
Source
1
/*
2
 * Copyright 2024-2025 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
#ifndef PCMK__CRM_COMMON_RESOURCES_INTERNAL__H
11
#define PCMK__CRM_COMMON_RESOURCES_INTERNAL__H
12
13
#include <inttypes.h>                   // UINT64_C
14
#include <stdbool.h>                    // bool
15
#include <stdint.h>                     // uint32_t
16
#include <glib.h>                       // gboolean, gpointer, guint, etc.
17
#include <libxml/tree.h>                // xmlNode
18
19
#include <crm/common/resources.h>       // pcmk_resource_t
20
#include <crm/common/roles.h>           // enum rsc_role_e
21
#include <crm/common/scheduler_types.h> // pcmk_node_t, etc.
22
23
#ifdef __cplusplus
24
extern "C" {
25
#endif
26
27
/*!
28
 * \internal
29
 * \brief Set resource flags
30
 *
31
 * \param[in,out] resource      Resource to set flags for
32
 * \param[in]     flags_to_set  Group of enum pcmk_rsc_flags to set
33
 */
34
#define pcmk__set_rsc_flags(resource, flags_to_set) do {                    \
35
        (resource)->flags = pcmk__set_flags_as(__func__, __LINE__,          \
36
            LOG_TRACE, "Resource", (resource)->id, (resource)->flags,       \
37
            (flags_to_set), #flags_to_set);                                 \
38
    } while (0)
39
40
/*!
41
 * \internal
42
 * \brief Clear resource flags
43
 *
44
 * \param[in,out] resource        Resource to clear flags for
45
 * \param[in]     flags_to_clear  Group of enum pcmk_rsc_flags to clear
46
 */
47
#define pcmk__clear_rsc_flags(resource, flags_to_clear) do {                \
48
        (resource)->flags = pcmk__clear_flags_as(__func__, __LINE__,        \
49
            LOG_TRACE, "Resource", (resource)->id, (resource)->flags,       \
50
            (flags_to_clear), #flags_to_clear);                             \
51
    } while (0)
52
53
//! Resource variants supported by Pacemaker
54
enum pcmk__rsc_variant {
55
    // Order matters: some code compares greater or lesser than
56
    pcmk__rsc_variant_unknown    = -1,  //!< Unknown resource variant
57
    pcmk__rsc_variant_primitive  = 0,   //!< Primitive resource
58
    pcmk__rsc_variant_group      = 1,   //!< Group resource
59
    pcmk__rsc_variant_clone      = 2,   //!< Clone resource
60
    pcmk__rsc_variant_bundle     = 3,   //!< Bundle resource
61
};
62
63
//! How to recover a resource that is incorrectly active on multiple nodes
64
enum pcmk__multiply_active {
65
    pcmk__multiply_active_restart,      //!< Stop on all, start on desired
66
    pcmk__multiply_active_stop,         //!< Stop on all and leave stopped
67
    pcmk__multiply_active_block,        //!< Do nothing to resource
68
    pcmk__multiply_active_unexpected,   //!< Stop unexpected instances
69
};
70
71
//! Resource scheduling flags
72
enum pcmk__rsc_flags {
73
    // No resource flags set (compare with equality rather than bit set)
74
    pcmk__no_rsc_flags               = UINT64_C(0),
75
76
    // Whether resource has been removed from the configuration
77
    pcmk__rsc_removed                = (UINT64_C(1) << 0),
78
79
    /* NOTE: sbd (at least as of 1.5.2) uses pe_rsc_managed which equates to
80
     * this value, so the value should not be changed
81
     */
82
    // Whether resource is managed
83
    pcmk__rsc_managed                = (UINT64_C(1) << 1),
84
85
    // Whether resource is blocked from further action
86
    pcmk__rsc_blocked                = (UINT64_C(1) << 2),
87
88
    // Whether resource has been removed but was launched
89
    pcmk__rsc_removed_launched       = (UINT64_C(1) << 3),
90
91
    // Whether resource has clone notifications enabled
92
    pcmk__rsc_notify                 = (UINT64_C(1) << 4),
93
94
    // Whether resource is not an anonymous clone instance
95
    pcmk__rsc_unique                 = (UINT64_C(1) << 5),
96
97
    // Whether resource's class is "stonith"
98
    pcmk__rsc_fence_device           = (UINT64_C(1) << 6),
99
100
    // Whether resource can be promoted and demoted
101
    pcmk__rsc_promotable             = (UINT64_C(1) << 7),
102
103
    // Whether resource has not yet been assigned to a node
104
    pcmk__rsc_unassigned             = (UINT64_C(1) << 8),
105
106
    // Whether resource is in the process of being assigned to a node
107
    pcmk__rsc_assigning              = (UINT64_C(1) << 9),
108
109
    // Whether resource is in the process of modifying allowed node scores
110
    pcmk__rsc_updating_nodes         = (UINT64_C(1) << 10),
111
112
    // Whether resource is in the process of scheduling actions to restart
113
    pcmk__rsc_restarting             = (UINT64_C(1) << 11),
114
115
    // Whether resource must be stopped (instead of demoted) if it is failed
116
    pcmk__rsc_stop_if_failed         = (UINT64_C(1) << 12),
117
118
    // Whether a reload action has been scheduled for resource
119
    pcmk__rsc_reload                 = (UINT64_C(1) << 13),
120
121
    // Whether resource is a remote connection allowed to run on a remote node
122
    pcmk__rsc_remote_nesting_allowed = (UINT64_C(1) << 14),
123
124
    // Whether resource has \c PCMK_META_CRITICAL meta-attribute enabled
125
    pcmk__rsc_critical               = (UINT64_C(1) << 15),
126
127
    // Whether resource is considered failed
128
    pcmk__rsc_failed                 = (UINT64_C(1) << 16),
129
130
    // Flag for non-scheduler code to use to detect recursion loops
131
    pcmk__rsc_detect_loop            = (UINT64_C(1) << 17),
132
133
    // Whether resource is a Pacemaker Remote connection
134
    pcmk__rsc_is_remote_connection   = (UINT64_C(1) << 18),
135
136
    // Whether resource has pending start action in history
137
    pcmk__rsc_start_pending          = (UINT64_C(1) << 19),
138
139
    // Whether resource is probed only on nodes marked exclusive
140
    pcmk__rsc_exclusive_probes       = (UINT64_C(1) << 20),
141
142
    /*
143
     * Whether resource is multiply active with recovery set to
144
     * \c PCMK_VALUE_STOP_UNEXPECTED
145
     */
146
    pcmk__rsc_stop_unexpected        = (UINT64_C(1) << 22),
147
148
    // Whether resource is allowed to live-migrate
149
    pcmk__rsc_migratable             = (UINT64_C(1) << 23),
150
151
    // Whether resource has an ignorable failure
152
    pcmk__rsc_ignore_failure         = (UINT64_C(1) << 24),
153
154
    // Whether resource is an implicit container resource for a bundle replica
155
    pcmk__rsc_replica_container      = (UINT64_C(1) << 25),
156
157
    // Whether resource, its node, or entire cluster is in maintenance mode
158
    pcmk__rsc_maintenance            = (UINT64_C(1) << 26),
159
160
    // Whether resource can be started or promoted only on quorate nodes
161
    pcmk__rsc_needs_quorum           = (UINT64_C(1) << 28),
162
163
    // Whether resource requires fencing before recovery if on unclean node
164
    pcmk__rsc_needs_fencing          = (UINT64_C(1) << 29),
165
166
    // Whether resource can be started or promoted only on unfenced nodes
167
    pcmk__rsc_needs_unfencing        = (UINT64_C(1) << 30),
168
};
169
170
/*!
171
 * \internal
172
 * \brief Where to look for a resource
173
 */
174
enum pcmk__rsc_node {
175
    //! Nowhere
176
    pcmk__rsc_node_none     = 0,
177
178
    //! Where resource is assigned
179
    pcmk__rsc_node_assigned = (UINT32_C(1) << 0),
180
181
    //! Where resource is running
182
    pcmk__rsc_node_current  = (UINT32_C(1) << 1),
183
184
    //! Where resource is pending
185
    pcmk__rsc_node_pending  = (UINT32_C(1) << 2),
186
};
187
188
//! Resource assignment methods (implementation defined by libpacemaker)
189
typedef struct pcmk__assignment_methods pcmk__assignment_methods_t;
190
191
//! Resource object methods
192
typedef struct {
193
    /*!
194
     * \internal
195
     * \brief Parse variant-specific resource XML from CIB into struct members
196
     *
197
     * \param[in,out] rsc  Partially unpacked resource
198
     *
199
     * \return \c true if resource was unpacked successfully, otherwise \c false
200
     */
201
    bool (*unpack)(pcmk_resource_t *rsc);
202
203
    /*!
204
     * \internal
205
     * \brief Search for a resource ID in a resource and its children
206
     *
207
     * \param[in] rsc      Search this resource and its children
208
     * \param[in] id       Search for this resource ID
209
     * \param[in] on_node  If not NULL, limit search to resources on this node
210
     * \param[in] flags    Group of enum pe_find flags
211
     *
212
     * \return Resource that matches search criteria if any, otherwise NULL
213
     */
214
    pcmk_resource_t *(*find_rsc)(pcmk_resource_t *rsc, const char *search,
215
                                 const pcmk_node_t *node, uint32_t flags);
216
217
    /*!
218
     * \internal
219
     * \brief Check whether a resource is active
220
     *
221
     * \param[in] rsc  Resource to check
222
     * \param[in] all  If \p rsc is collective, all instances must be active
223
     *
224
     * \return TRUE if \p rsc is active, otherwise FALSE
225
     */
226
    bool (*active)(const pcmk_resource_t *rsc, bool all);
227
228
    /*!
229
     * \internal
230
     * \brief Get resource's current or assigned role
231
     *
232
     * \param[in] rsc      Resource to check
233
     * \param[in] current  If \c true, check current role; otherwise, check
234
     *                     assigned role
235
     *
236
     * \return Current or assigned role of \p rsc
237
     */
238
    enum rsc_role_e (*state)(const pcmk_resource_t *rsc, bool current);
239
240
    /*!
241
     * \internal
242
     * \brief List nodes where a resource (or any of its children) is
243
     *
244
     * \param[in]  rsc      Resource to check
245
     * \param[out] list     List to add result to
246
     * \param[in]  target   Which resource conditions to target (group of
247
     *                      enum pcmk__rsc_node flags)
248
     *
249
     * \return If list contains only one node, that node, otherwise NULL
250
     */
251
    pcmk_node_t *(*location)(const pcmk_resource_t *rsc, GList **list,
252
                             uint32_t target);
253
254
    /*!
255
     * \internal
256
     * \brief Free all memory used by a resource
257
     *
258
     * \param[in,out] rsc  Resource to free
259
     */
260
    void (*free)(pcmk_resource_t *rsc);
261
262
    /*!
263
     * \internal
264
     * \brief Increment cluster's instance counts for a resource
265
     *
266
     * Given a resource, increment its cluster's ninstances, disabled_resources,
267
     * and blocked_resources counts for the resource and its descendants.
268
     *
269
     * \param[in,out] rsc  Resource to count
270
     */
271
    void (*count)(pcmk_resource_t *rsc);
272
273
    /*!
274
     * \internal
275
     * \brief Check whether a given resource is in a list of resources
276
     *
277
     * \param[in] rsc           Resource ID to check for
278
     * \param[in] only_rsc      List of resource IDs to check
279
     * \param[in] check_parent  If \c true, check top ancestor as well
280
     *
281
     * \return \c true if \p rsc, its top parent if requested, or \c "*" is in
282
     *         \p only_rsc, or \c false otherwise
283
     */
284
    bool (*is_filtered)(const pcmk_resource_t *rsc, const GList *only_rsc,
285
                        bool check_parent);
286
287
    /*!
288
     * \internal
289
     * \brief Find a node (and optionally count all) where resource is active
290
     *
291
     * \param[in]  rsc          Resource to check
292
     * \param[out] count_all    If not NULL, set this to count of active nodes
293
     * \param[out] count_clean  If not NULL, set this to count of clean nodes
294
     *
295
     * \return A node where the resource is active, preferring the source node
296
     *         if the resource is involved in a partial migration, or a clean,
297
     *         online node if the resource's \c PCMK_META_REQUIRES is
298
     *         \c PCMK_VALUE_QUORUM or \c PCMK_VALUE_NOTHING, otherwise \c NULL.
299
     */
300
    pcmk_node_t *(*active_node)(const pcmk_resource_t *rsc,
301
                                unsigned int *count_all,
302
                                unsigned int *count_clean);
303
304
    /*!
305
     * \internal
306
     * \brief Get maximum resource instances per node
307
     *
308
     * \param[in] rsc  Resource to check
309
     *
310
     * \return Maximum number of \p rsc instances that can be active on one node
311
     */
312
    unsigned int (*max_per_node)(const pcmk_resource_t *rsc);
313
} pcmk__rsc_methods_t;
314
315
// Implementation of pcmk__resource_private_t
316
struct pcmk__resource_private {
317
    enum pcmk__rsc_variant variant; // Resource variant
318
    void *variant_opaque;           // Variant-specific data
319
    char *history_id;               // Resource instance ID in history
320
    GHashTable *meta;               // Resource meta-attributes
321
    GHashTable *utilization;        // Resource utilization attributes
322
    int priority;                   // Priority relative other resources
323
    int promotion_priority;         // Promotion priority on assigned node
324
    enum rsc_role_e orig_role;      // Resource's role at start of transition
325
    enum rsc_role_e next_role;      // Resource's role at end of transition
326
    int stickiness;                 // Extra preference for current node
327
    guint failure_expiration_ms;    // Failures expire after this much time
328
    int ban_after_failures;         // Ban from node after this many failures
329
    guint remote_reconnect_ms;      // Retry interval for remote connections
330
    char *pending_action;           // Pending action in history, if any
331
    const pcmk_node_t *pending_node;// Node on which pending_action is happening
332
    time_t lock_time;               // When shutdown lock started
333
    const pcmk_node_t *lock_node;   // Node that resource is shutdown-locked to
334
    GList *actions;                 // Actions scheduled for resource
335
    GList *children;                // Resource's child resources, if any
336
    pcmk_resource_t *parent;        // Resource's parent resource, if any
337
    pcmk_scheduler_t *scheduler;    // Scheduler data containing resource
338
339
    // Resource configuration (possibly expanded from template)
340
    xmlNode *xml;
341
342
    // Original resource configuration, if using template
343
    xmlNode *orig_xml;
344
345
    // Configuration of resource operations (possibly expanded from template)
346
    xmlNode *ops_xml;
347
348
    /*
349
     * Resource parameters may have node-attribute-based rules, which means the
350
     * values can vary by node. This table has node names as keys and parameter
351
     * name/value tables as values. Use pe_rsc_params() to get the table for a
352
     * given node rather than use this directly.
353
     */
354
    GHashTable *parameter_cache;
355
356
    /* A "launcher" is defined in one of these ways:
357
     *
358
     * - A Pacemaker Remote connection for a guest node or bundle node has its
359
     *   launcher set to the resource that starts the guest or the bundle
360
     *   replica's container.
361
     *
362
     * - If the user configures the PCMK__META_CONTAINER meta-attribute for this
363
     *   resource, the launcher is set to that.
364
     *
365
     *   If the launcher is a Pacemaker Remote connection resource, this
366
     *   resource may run only on the node created by that connection.
367
     *
368
     *   Otherwise, this resource will be colocated with and ordered after the
369
     *   launcher, and failures of this resource will cause the launcher to be
370
     *   recovered instead of this one. This is appropriate for monitoring-only
371
     *   resources that represent a service launched by the other resource.
372
     */
373
    pcmk_resource_t *launcher;
374
375
    // Resources launched by this one, if any (pcmk_resource_t *)
376
    GList *launched;
377
378
    // What to do if the resource is incorrectly active on multiple nodes
379
    enum pcmk__multiply_active multiply_active_policy;
380
381
    /* The assigned node (if not NULL) is the one where the resource *should*
382
     * be active by the end of the current scheduler transition. Only primitive
383
     * resources have an assigned node. This is a node copy (created by
384
     * pe__copy_node()) and so must be freed using pcmk__free_node_copy().
385
     *
386
     * @TODO This should probably be part of the primitive variant data.
387
     */
388
    pcmk_node_t *assigned_node;
389
390
    /* The active nodes are ones where the resource is (or might be, if
391
     * insufficient information is available to be sure) already active at the
392
     * start of the current scheduler transition.
393
     *
394
     * For primitive resources, there should be at most one, but could be more
395
     * if it is (incorrectly) multiply active. For collective resources, this
396
     * combines active nodes of all descendants.
397
     */
398
    GList *active_nodes;
399
400
    /* The next two tables store node copies (created by pe__copy_node()), which
401
     * share some members with the original node objects and must be freed with
402
     * pcmk__free_node_copy().
403
     */
404
405
    // Nodes where resource has been probed (key is node ID, not name)
406
    GHashTable *probed_nodes;
407
408
    // Nodes where resource is allowed to run (key is node ID, not name)
409
    GHashTable *allowed_nodes;
410
411
    // The source node, if migrate_to completed but migrate_from has not
412
    pcmk_node_t *partial_migration_source;
413
414
    // The destination node, if migrate_to completed but migrate_from has not
415
    pcmk_node_t *partial_migration_target;
416
417
    // Source nodes where stop is needed after migrate_from and migrate_to
418
    GList *dangling_migration_sources;
419
420
    /* Pay special attention to whether you want to use with_this_colocations
421
     * and this_with_colocations directly, which include only colocations
422
     * explicitly involving this resource, or call libpacemaker's
423
     * pcmk__with_this_colocations() and pcmk__this_with_colocations()
424
     * functions, which may return relevant colocations involving the resource's
425
     * ancestors as well.
426
     */
427
428
    // Colocations of other resources with this one
429
    GList *with_this_colocations;
430
431
    // Colocations of this resource with others
432
    GList *this_with_colocations;
433
434
    GList *location_constraints;        // Location constraints for resource
435
    GList *ticket_constraints;          // Ticket constraints for resource
436
437
    const pcmk__rsc_methods_t *fns;         // Resource object methods
438
    const pcmk__assignment_methods_t *cmds; // Resource assignment methods
439
};
440
441
void pcmk__free_resource(gpointer user_data);
442
const char *pcmk__multiply_active_text(const pcmk_resource_t *rsc);
443
444
/*!
445
 * \internal
446
 * \brief Get node where resource is currently active (if any)
447
 *
448
 * \param[in] rsc  Resource to check
449
 *
450
 * \return Node that \p rsc is active on, if any, otherwise NULL
451
 */
452
static inline pcmk_node_t *
453
pcmk__current_node(const pcmk_resource_t *rsc)
454
0
{
455
0
    if (rsc == NULL) {
456
0
        return NULL;
457
0
    }
458
0
    return rsc->priv->fns->active_node(rsc, NULL, NULL);
459
0
}
Unexecuted instantiation: results.c:pcmk__current_node
Unexecuted instantiation: scores.c:pcmk__current_node
Unexecuted instantiation: strings.c:pcmk__current_node
Unexecuted instantiation: utils.c:pcmk__current_node
Unexecuted instantiation: iso8601.c:pcmk__current_node
Unexecuted instantiation: logging.c:pcmk__current_node
Unexecuted instantiation: mainloop.c:pcmk__current_node
Unexecuted instantiation: options.c:pcmk__current_node
Unexecuted instantiation: output.c:pcmk__current_node
Unexecuted instantiation: output_log.c:pcmk__current_node
Unexecuted instantiation: output_text.c:pcmk__current_node
Unexecuted instantiation: output_xml.c:pcmk__current_node
Unexecuted instantiation: patchset_display.c:pcmk__current_node
Unexecuted instantiation: schemas.c:pcmk__current_node
Unexecuted instantiation: xml.c:pcmk__current_node
Unexecuted instantiation: xml_attr.c:pcmk__current_node
Unexecuted instantiation: xml_comment.c:pcmk__current_node
Unexecuted instantiation: xml_display.c:pcmk__current_node
Unexecuted instantiation: xml_element.c:pcmk__current_node
Unexecuted instantiation: xml_idref.c:pcmk__current_node
Unexecuted instantiation: xml_io.c:pcmk__current_node
Unexecuted instantiation: xpath.c:pcmk__current_node
Unexecuted instantiation: acl.c:pcmk__current_node
Unexecuted instantiation: actions.c:pcmk__current_node
Unexecuted instantiation: agents.c:pcmk__current_node
Unexecuted instantiation: cmdline.c:pcmk__current_node
Unexecuted instantiation: digest.c:pcmk__current_node
Unexecuted instantiation: health.c:pcmk__current_node
Unexecuted instantiation: io.c:pcmk__current_node
Unexecuted instantiation: ipc_client.c:pcmk__current_node
Unexecuted instantiation: ipc_common.c:pcmk__current_node
Unexecuted instantiation: ipc_controld.c:pcmk__current_node
Unexecuted instantiation: ipc_pacemakerd.c:pcmk__current_node
Unexecuted instantiation: ipc_schedulerd.c:pcmk__current_node
Unexecuted instantiation: ipc_server.c:pcmk__current_node
Unexecuted instantiation: messages.c:pcmk__current_node
Unexecuted instantiation: nodes.c:pcmk__current_node
Unexecuted instantiation: nvpair.c:pcmk__current_node
Unexecuted instantiation: options_display.c:pcmk__current_node
Unexecuted instantiation: patchset.c:pcmk__current_node
Unexecuted instantiation: procfs.c:pcmk__current_node
Unexecuted instantiation: rules.c:pcmk__current_node
Unexecuted instantiation: servers.c:pcmk__current_node
Unexecuted instantiation: cib.c:pcmk__current_node
Unexecuted instantiation: ipc_attrd.c:pcmk__current_node
Unexecuted instantiation: pid.c:pcmk__current_node
Unexecuted instantiation: attrs.c:pcmk__current_node
Unexecuted instantiation: strings_fuzzer.c:pcmk__current_node
Unexecuted instantiation: cib_file_fuzzer.c:pcmk__current_node
Unexecuted instantiation: cib_client.c:pcmk__current_node
Unexecuted instantiation: cib_file.c:pcmk__current_node
Unexecuted instantiation: cib_native.c:pcmk__current_node
Unexecuted instantiation: cib_ops.c:pcmk__current_node
Unexecuted instantiation: cib_remote.c:pcmk__current_node
Unexecuted instantiation: cib_utils.c:pcmk__current_node
Unexecuted instantiation: remote.c:pcmk__current_node
Unexecuted instantiation: tls.c:pcmk__current_node
Unexecuted instantiation: watchdog.c:pcmk__current_node
460
461
#ifdef __cplusplus
462
}
463
#endif
464
465
#endif // PCMK__CRM_COMMON_RESOURCES_INTERNAL__H