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