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