1 /*
2 * Copyright 2004-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__H
11 #define PCMK__CRM_COMMON_RESOURCES__H
12
13 #include <stdbool.h> // bool
14 #include <sys/types.h> // time_t
15 #include <libxml/tree.h> // xmlNode
16 #include <glib.h> // gboolean, guint, GList, GHashTable
17
18 #include <crm/common/roles.h> // enum rsc_role_e
19 #include <crm/common/scheduler_types.h> // pcmk_resource_t, etc.
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 /*!
26 * \file
27 * \brief Scheduler API for resources
28 * \ingroup core
29 */
30
31 // Resource variants supported by Pacemaker
32 //!@{
33 //! \deprecated Do not use
34 enum pe_obj_types {
35 // Order matters: some code compares greater or lesser than
36 pcmk_rsc_variant_unknown = -1, // Unknown resource variant
37 pcmk_rsc_variant_primitive = 0, // Primitive resource
38 pcmk_rsc_variant_group = 1, // Group resource
39 pcmk_rsc_variant_clone = 2, // Clone resource
40 pcmk_rsc_variant_bundle = 3, // Bundle resource
41
42 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
43 pe_unknown = pcmk_rsc_variant_unknown,
44 pe_native = pcmk_rsc_variant_primitive,
45 pe_group = pcmk_rsc_variant_group,
46 pe_clone = pcmk_rsc_variant_clone,
47 pe_container = pcmk_rsc_variant_bundle,
48 #endif
49 };
50
51 // What resource needs before it can be recovered from a failed node
52 enum rsc_start_requirement {
53 pcmk_requires_nothing = 0, // Resource can be recovered immediately
54 pcmk_requires_quorum = 1, // Resource can be recovered if quorate
55 pcmk_requires_fencing = 2, // Resource can be recovered after fencing
56
57 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
58 rsc_req_nothing = pcmk_requires_nothing,
59 rsc_req_quorum = pcmk_requires_quorum,
60 rsc_req_stonith = pcmk_requires_fencing,
61 #endif
62 };
63
64 // How to recover a resource that is incorrectly active on multiple nodes
65 enum rsc_recovery_type {
66 pcmk_multiply_active_restart = 0, // Stop on all, start on desired
67 pcmk_multiply_active_stop = 1, // Stop on all and leave stopped
68 pcmk_multiply_active_block = 2, // Do nothing to resource
69 pcmk_multiply_active_unexpected = 3, // Stop unexpected instances
70
71 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
72 recovery_stop_start = pcmk_multiply_active_restart,
73 recovery_stop_only = pcmk_multiply_active_stop,
74 recovery_block = pcmk_multiply_active_block,
75 recovery_stop_unexpected = pcmk_multiply_active_unexpected,
76 #endif
77 };
78
79 // Resource scheduling flags
80 enum pcmk_rsc_flags {
81 // No resource flags set (compare with equality rather than bit set)
82 pcmk_no_rsc_flags = 0ULL,
83
84 // Whether resource has been removed from the configuration
85 pcmk_rsc_removed = (1ULL << 0),
86
87 // Whether resource is managed
88 pcmk_rsc_managed = (1ULL << 1),
89
90 // Whether resource is blocked from further action
91 pcmk_rsc_blocked = (1ULL << 2),
92
93 // Whether resource has been removed but has a container
94 pcmk_rsc_removed_filler = (1ULL << 3),
95
96 // Whether resource has clone notifications enabled
97 pcmk_rsc_notify = (1ULL << 4),
98
99 // Whether resource is not an anonymous clone instance
100 pcmk_rsc_unique = (1ULL << 5),
101
102 // Whether resource's class is "stonith"
103 pcmk_rsc_fence_device = (1ULL << 6),
104
105 // Whether resource can be promoted and demoted
106 pcmk_rsc_promotable = (1ULL << 7),
107
108 // Whether resource has not yet been assigned to a node
109 pcmk_rsc_unassigned = (1ULL << 8),
110
111 // Whether resource is in the process of being assigned to a node
112 pcmk_rsc_assigning = (1ULL << 9),
113
114 // Whether resource is in the process of modifying allowed node scores
115 pcmk_rsc_updating_nodes = (1ULL << 10),
116
117 // Whether resource is in the process of scheduling actions to restart
118 pcmk_rsc_restarting = (1ULL << 11),
119
120 // Whether resource must be stopped (instead of demoted) if it is failed
121 pcmk_rsc_stop_if_failed = (1ULL << 12),
122
123 // Whether a reload action has been scheduled for resource
124 pcmk_rsc_reload = (1ULL << 13),
125
126 // Whether resource is a remote connection allowed to run on a remote node
127 pcmk_rsc_remote_nesting_allowed = (1ULL << 14),
128
129 // Whether resource has \c PCMK_META_CRITICAL meta-attribute enabled
130 pcmk_rsc_critical = (1ULL << 15),
131
132 // Whether resource is considered failed
133 pcmk_rsc_failed = (1ULL << 16),
134
135 // Flag for non-scheduler code to use to detect recursion loops
136 pcmk_rsc_detect_loop = (1ULL << 17),
137
138 // \deprecated Do not use
139 pcmk_rsc_runnable = (1ULL << 18),
140
141 // Whether resource has pending start action in history
142 pcmk_rsc_start_pending = (1ULL << 19),
143
144 // \deprecated Do not use
145 pcmk_rsc_starting = (1ULL << 20),
146
147 // \deprecated Do not use
148 pcmk_rsc_stopping = (1ULL << 21),
149
150 /*
151 * Whether resource is multiply active with recovery set to
152 * \c PCMK_VALUE_STOP_UNEXPECTED
153 */
154 pcmk_rsc_stop_unexpected = (1ULL << 22),
155
156 // Whether resource is allowed to live-migrate
157 pcmk_rsc_migratable = (1ULL << 23),
158
159 // Whether resource has an ignorable failure
160 pcmk_rsc_ignore_failure = (1ULL << 24),
161
162 // Whether resource is an implicit container resource for a bundle replica
163 pcmk_rsc_replica_container = (1ULL << 25),
164
165 // Whether resource, its node, or entire cluster is in maintenance mode
166 pcmk_rsc_maintenance = (1ULL << 26),
167
168 // \deprecated Do not use
169 pcmk_rsc_has_filler = (1ULL << 27),
170
171 // Whether resource can be started or promoted only on quorate nodes
172 pcmk_rsc_needs_quorum = (1ULL << 28),
173
174 // Whether resource requires fencing before recovery if on unclean node
175 pcmk_rsc_needs_fencing = (1ULL << 29),
176
177 // Whether resource can be started or promoted only on unfenced nodes
178 pcmk_rsc_needs_unfencing = (1ULL << 30),
179 };
180 //!@}
181
182 //! Search options for resources (exact resource ID always matches)
183 enum pe_find {
184 //! Also match clone instance ID from resource history
185 pcmk_rsc_match_history = (1 << 0),
186
187 //! Also match anonymous clone instances by base name
188 pcmk_rsc_match_anon_basename = (1 << 1),
189
190 //! Match only clones and their instances, by either clone or instance ID
191 pcmk_rsc_match_clone_only = (1 << 2),
192
193 //! If matching by node, compare current node instead of assigned node
194 pcmk_rsc_match_current_node = (1 << 3),
195
196 //! \deprecated Do not use
197 pe_find_inactive = (1 << 4),
198
199 //! Match clone instances (even unique) by base name as well as exact ID
200 pcmk_rsc_match_basename = (1 << 5),
201
202 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
203 //! \deprecated Use pcmk_rsc_match_history instead
204 pe_find_renamed = pcmk_rsc_match_history,
205
206 //! \deprecated Use pcmk_rsc_match_anon_basename instead
207 pe_find_anon = pcmk_rsc_match_anon_basename,
208
209 //! \deprecated Use pcmk_rsc_match_clone_only instead
210 pe_find_clone = pcmk_rsc_match_clone_only,
211
212 //! \deprecated Use pcmk_rsc_match_current_node instead
213 pe_find_current = pcmk_rsc_match_current_node,
214
215 //! \deprecated Use pcmk_rsc_match_basename instead
216 pe_find_any = pcmk_rsc_match_basename,
217 #endif
218 };
219
220 //!@{
221 //! \deprecated Do not use
222 enum pe_restart {
223 pe_restart_restart,
224 pe_restart_ignore,
225 };
226
227 enum pe_print_options {
228 pe_print_log = (1 << 0),
229 pe_print_html = (1 << 1),
230 pe_print_ncurses = (1 << 2),
231 pe_print_printf = (1 << 3),
232 pe_print_dev = (1 << 4), // Ignored
233 pe_print_details = (1 << 5), // Ignored
234 pe_print_max_details = (1 << 6), // Ignored
235 pe_print_rsconly = (1 << 7),
236 pe_print_ops = (1 << 8),
237 pe_print_suppres_nl = (1 << 9),
238 pe_print_xml = (1 << 10),
239 pe_print_brief = (1 << 11),
240 pe_print_pending = (1 << 12),
241 pe_print_clone_details = (1 << 13),
242 pe_print_clone_active = (1 << 14), // Print clone instances only if active
243 pe_print_implicit = (1 << 15) // Print implicitly created resources
244 };
245 //!@}
246
247 // Resource assignment methods (implementation defined by libpacemaker)
248 //! \deprecated Do not use (public access will be removed in a future release)
249 typedef struct resource_alloc_functions_s pcmk_assignment_methods_t;
250
251 // Resource object methods
252 // @COMPAT Make this internal when we can break API backward compatibility
253 //!@{
254 //! \deprecated Do not use (public access will be removed in a future release)
255 typedef struct resource_object_functions_s {
256 /*
257 * \brief Parse variant-specific resource XML from CIB into struct members
258 *
259 * \param[in,out] rsc Partially unpacked resource
260 * \param[in,out] scheduler Scheduler data
261 *
262 * \return TRUE if resource was unpacked successfully, otherwise FALSE
263 */
264 gboolean (*unpack)(pcmk_resource_t *rsc, pcmk_scheduler_t *scheduler);
265
266 /*
267 * \brief Search for a resource ID in a resource and its children
268 *
269 * \param[in] rsc Search this resource and its children
270 * \param[in] id Search for this resource ID
271 * \param[in] on_node If not NULL, limit search to resources on this node
272 * \param[in] flags Group of enum pe_find flags
273 *
274 * \return Resource that matches search criteria if any, otherwise NULL
275 */
276 pcmk_resource_t *(*find_rsc)(pcmk_resource_t *rsc, const char *search,
277 const pcmk_node_t *node, int flags);
278
279 /*
280 * \brief Get value of a resource instance attribute
281 *
282 * \param[in,out] rsc Resource to check
283 * \param[in] node Node to use to evaluate rules
284 * \param[in] create Ignored
285 * \param[in] name Name of instance attribute to check
286 * \param[in,out] scheduler Scheduler data
287 *
288 * \return Value of requested attribute if available, otherwise NULL
289 * \note The caller is responsible for freeing the result using free().
290 */
291 char *(*parameter)(pcmk_resource_t *rsc, pcmk_node_t *node, gboolean create,
292 const char *name, pcmk_scheduler_t *scheduler);
293
294 // \deprecated Do not use
295 void (*print)(pcmk_resource_t *rsc, const char *pre_text, long options,
296 void *print_data);
297
298 /*
299 * \brief Check whether a resource is active
300 *
301 * \param[in] rsc Resource to check
302 * \param[in] all If \p rsc is collective, all instances must be active
303 *
304 * \return TRUE if \p rsc is active, otherwise FALSE
305 */
306 gboolean (*active)(pcmk_resource_t *rsc, gboolean all);
307
308 /*
309 * \brief Get resource's current or assigned role
310 *
311 * \param[in] rsc Resource to check
312 * \param[in] current If TRUE, check current role, otherwise assigned role
313 *
314 * \return Current or assigned role of \p rsc
315 */
316 enum rsc_role_e (*state)(const pcmk_resource_t *rsc, gboolean current);
317
318 /*
319 * \brief List nodes where a resource (or any of its children) is
320 *
321 * \param[in] rsc Resource to check
322 * \param[out] list List to add result to
323 * \param[in] current If 0, list nodes where \p rsc is assigned;
324 * if 1, where active; if 2, where active or pending
325 *
326 * \return If list contains only one node, that node, otherwise NULL
327 */
328 pcmk_node_t *(*location)(const pcmk_resource_t *rsc, GList **list,
329 int current);
330
331 /*
332 * \brief Free all memory used by a resource
333 *
334 * \param[in,out] rsc Resource to free
335 */
336 void (*free)(pcmk_resource_t *rsc);
337
338 /*
339 * \brief Increment cluster's instance counts for a resource
340 *
341 * Given a resource, increment its cluster's ninstances, disabled_resources,
342 * and blocked_resources counts for the resource and its descendants.
343 *
344 * \param[in,out] rsc Resource to count
345 */
346 void (*count)(pcmk_resource_t *rsc);
347
348 /*
349 * \brief Check whether a given resource is in a list of resources
350 *
351 * \param[in] rsc Resource ID to check for
352 * \param[in] only_rsc List of resource IDs to check
353 * \param[in] check_parent If TRUE, check top ancestor as well
354 *
355 * \return TRUE if \p rsc, its top parent if requested, or '*' is in
356 * \p only_rsc, otherwise FALSE
357 */
358 gboolean (*is_filtered)(const pcmk_resource_t *rsc, GList *only_rsc,
359 gboolean check_parent);
360
361 /*
362 * \brief Find a node (and optionally count all) where resource is active
363 *
364 * \param[in] rsc Resource to check
365 * \param[out] count_all If not NULL, set this to count of active nodes
366 * \param[out] count_clean If not NULL, set this to count of clean nodes
367 *
368 * \return A node where the resource is active, preferring the source node
369 * if the resource is involved in a partial migration, or a clean,
370 * online node if the resource's \c PCMK_META_REQUIRES is
371 * \c PCMK_VALUE_QUORUM or \c PCMK_VALUE_NOTHING, otherwise \c NULL.
372 */
373 pcmk_node_t *(*active_node)(const pcmk_resource_t *rsc,
374 unsigned int *count_all,
375 unsigned int *count_clean);
376
377 /*
378 * \brief Get maximum resource instances per node
379 *
380 * \param[in] rsc Resource to check
381 *
382 * \return Maximum number of \p rsc instances that can be active on one node
383 */
384 unsigned int (*max_per_node)(const pcmk_resource_t *rsc);
385 } pcmk_rsc_methods_t;
386 //!@}
387
388 // Implementation of pcmk_resource_t
389 // @COMPAT Make this internal when we can break API backward compatibility
390 //!@{
391 //! \deprecated Do not use (public access will be removed in a future release)
392 struct pe_resource_s {
393 // NOTE: sbd (as of at least 1.5.2) uses this
394 //! \deprecated Call pcmk_resource_id() instead
395 char *id; // Resource ID in configuration
396
397 char *clone_name; // Resource instance ID in history
398
399 // Resource configuration (possibly expanded from template)
400 xmlNode *xml;
401
402 // Original resource configuration, if using template
403 xmlNode *orig_xml;
404
405 // Configuration of resource operations (possibly expanded from template)
406 xmlNode *ops_xml;
407
408 pcmk_scheduler_t *cluster; // Cluster that resource is part of
409 pcmk_resource_t *parent; // Resource's parent resource, if any
410 enum pe_obj_types variant; // Resource variant
411 void *variant_opaque; // Variant-specific (and private) data
412 pcmk_rsc_methods_t *fns; // Resource object methods
413 pcmk_assignment_methods_t *cmds; // Resource assignment methods
414
415 enum rsc_recovery_type recovery_type; // How to recover if failed
416
417 enum pe_restart restart_type; // \deprecated Do not use
418 int priority; // Configured priority
419 int stickiness; // Extra preference for current node
420 int sort_index; // Promotion score on assigned node
421 int failure_timeout; // Failure timeout
422 int migration_threshold; // Migration threshold
423 guint remote_reconnect_ms; // Retry interval for remote connections
424 char *pending_task; // Pending action in history, if any
425
426 // NOTE: sbd (as of at least 1.5.2) uses this
427 //! \deprecated Call pcmk_resource_is_managed() instead
428 unsigned long long flags; // Group of enum pcmk_rsc_flags
429
430 // @TODO Merge these into flags
431 gboolean is_remote_node; // Whether this is a remote connection
432 gboolean exclusive_discover; // Whether exclusive probing is enabled
433
434 /* Pay special attention to whether you want to use rsc_cons_lhs and
435 * rsc_cons directly, which include only colocations explicitly involving
436 * this resource, or call libpacemaker's pcmk__with_this_colocations() and
437 * pcmk__this_with_colocations() functions, which may return relevant
438 * colocations involving the resource's ancestors as well.
439 */
440
441 GList *rsc_cons_lhs; // Colocations of other resources with this one
442 GList *rsc_cons; // Colocations of this resource with others
443 GList *rsc_location; // Location constraints for resource
444 GList *actions; // Actions scheduled for resource
445 GList *rsc_tickets; // Ticket constraints for resource
446
447 pcmk_node_t *allocated_to; // Node resource is assigned to
448
449 // The destination node, if migrate_to completed but migrate_from has not
450 pcmk_node_t *partial_migration_target;
451
452 // The source node, if migrate_to completed but migrate_from has not
453 pcmk_node_t *partial_migration_source;
454
455 // Nodes where resource may be active
456 GList *running_on;
457
458 // Nodes where resource has been probed (key is node ID, not name)
459 GHashTable *known_on;
460
461 // Nodes where resource may run (key is node ID, not name)
462 GHashTable *allowed_nodes;
463
464 enum rsc_role_e role; // Resource's current role
465 enum rsc_role_e next_role; // Resource's scheduled next role
466
467 GHashTable *meta; // Resource's meta-attributes
468 GHashTable *parameters; // \deprecated Use pe_rsc_params() instead
469 GHashTable *utilization; // Resource's utilization attributes
470
471 GList *children; // Resource's child resources, if any
472
473 // Source nodes where stop is needed after migrate_from and migrate_to
474 GList *dangling_migrations;
475
476 pcmk_resource_t *container; // Resource containing this one, if any
477 GList *fillers; // Resources contained by this one, if any
478
479 // @COMPAT These should be made const at next API compatibility break
480 pcmk_node_t *pending_node; // Node on which pending_task is happening
481 pcmk_node_t *lock_node; // Resource shutdown-locked to this node
482
483 time_t lock_time; // When shutdown lock started
484
485 /*
486 * Resource parameters may have node-attribute-based rules, which means the
487 * values can vary by node. This table has node names as keys and parameter
488 * name/value tables as values. Use pe_rsc_params() to get the table for a
489 * given node rather than use this directly.
490 */
491 GHashTable *parameter_cache;
492 };
493 //!@}
494
495 const char *pcmk_resource_id(const pcmk_resource_t *rsc);
496 bool pcmk_resource_is_managed(const pcmk_resource_t *rsc);
497
498 #ifdef __cplusplus
499 }
500 #endif
501
502 #endif // PCMK__CRM_COMMON_RESOURCES__H