root/lib/pacemaker/pcmk_resource.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. best_op
  2. pcmk__resource_digests
  3. pcmk_resource_digests

   1 /*
   2  * Copyright 2021-2022 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 General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <errno.h>
  13 #include <glib.h>
  14 #include <libxml/tree.h>
  15 
  16 #include <crm/common/mainloop.h>
  17 #include <crm/common/results.h>
  18 #include <crm/common/output_internal.h>
  19 #include <crm/pengine/internal.h>
  20 
  21 #include <pacemaker.h>
  22 #include <pacemaker-internal.h>
  23 
  24 // Search path for resource operation history (takes node name and resource ID)
  25 #define XPATH_OP_HISTORY "//" XML_CIB_TAG_STATUS                            \
  26                          "/" XML_CIB_TAG_STATE "[@" XML_ATTR_UNAME "='%s']" \
  27                          "/" XML_CIB_TAG_LRM "/" XML_LRM_TAG_RESOURCES      \
  28                          "/" XML_LRM_TAG_RESOURCE "[@" XML_ATTR_ID "='%s']"
  29 
  30 static xmlNode *
  31 best_op(pe_resource_t *rsc, pe_node_t *node, pe_working_set_t *data_set)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33     char *xpath = NULL;
  34     xmlNode *history = NULL;
  35     xmlNode *best = NULL;
  36     bool best_effective_op = false;
  37     guint best_interval = 0;
  38     bool best_failure = false;
  39     const char *best_digest = NULL;
  40 
  41     // Find node's resource history
  42     xpath = crm_strdup_printf(XPATH_OP_HISTORY, node->details->uname, rsc->id);
  43     history = get_xpath_object(xpath, data_set->input, LOG_NEVER);
  44     free(xpath);
  45 
  46     // Examine each history entry
  47     for (xmlNode *lrm_rsc_op = first_named_child(history, XML_LRM_TAG_RSC_OP);
  48          lrm_rsc_op != NULL; lrm_rsc_op = crm_next_same_xml(lrm_rsc_op)) {
  49 
  50         const char *digest = crm_element_value(lrm_rsc_op,
  51                                                XML_LRM_ATTR_RESTART_DIGEST);
  52         guint interval_ms = 0;
  53         const char *task = crm_element_value(lrm_rsc_op, XML_LRM_ATTR_TASK);
  54         bool effective_op = false;
  55         bool failure = pcmk__ends_with(ID(lrm_rsc_op), "_last_failure_0");
  56 
  57 
  58         crm_element_value_ms(lrm_rsc_op, XML_LRM_ATTR_INTERVAL, &interval_ms);
  59         effective_op = interval_ms == 0
  60                        && pcmk__strcase_any_of(task, RSC_STATUS,
  61                                                RSC_START, RSC_PROMOTE,
  62                                                RSC_MIGRATED, NULL);
  63 
  64         if (best == NULL) {
  65             goto is_best;
  66         }
  67 
  68         if (best_effective_op) {
  69             // Do not use an ineffective op if there's an effective one.
  70             if (!effective_op) {
  71                 continue;
  72             }
  73         // Do not use an ineffective non-recurring op if there's a recurring one.
  74         } else if (best_interval != 0
  75                    && !effective_op
  76                    && interval_ms == 0) {
  77             continue;
  78         }
  79 
  80         // Do not use last failure if there's a successful one.
  81         if (!best_failure && failure) {
  82             continue;
  83         }
  84 
  85         // Do not use an op without a restart digest if there's one with.
  86         if (best_digest != NULL && digest == NULL) {
  87             continue;
  88         }
  89 
  90         // Do not use an older op if there's a newer one.
  91         if (pe__is_newer_op(best, lrm_rsc_op, true) > 0) {
  92             continue;
  93         }
  94 
  95 is_best:
  96          best = lrm_rsc_op;
  97          best_effective_op = effective_op;
  98          best_interval = interval_ms;
  99          best_failure = failure;
 100          best_digest = digest;
 101     }
 102     return best;
 103 }
 104 
 105 /*!
 106  * \internal
 107  * \brief Calculate and output resource operation digests
 108  *
 109  * \param[in]  out        Output object
 110  * \param[in]  rsc        Resource to calculate digests for
 111  * \param[in]  node       Node whose operation history should be used
 112  * \param[in]  overrides  Hash table of configuration parameters to override
 113  *
 114  * \return Standard Pacemaker return code
 115  */
 116 int
 117 pcmk__resource_digests(pcmk__output_t *out, pe_resource_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 118                        pe_node_t *node, GHashTable *overrides)
 119 {
 120     const char *task = NULL;
 121     xmlNode *xml_op = NULL;
 122     op_digest_cache_t *digests = NULL;
 123     guint interval_ms = 0;
 124     int rc = pcmk_rc_ok;
 125 
 126     if ((out == NULL) || (rsc == NULL) || (node == NULL)) {
 127         return EINVAL;
 128     }
 129     if (rsc->variant != pe_native) {
 130         // Only primitives get operation digests
 131         return EOPNOTSUPP;
 132     }
 133 
 134     // Find XML of operation history to use
 135     xml_op = best_op(rsc, node, rsc->cluster);
 136 
 137     // Generate an operation key
 138     if (xml_op != NULL) {
 139         task = crm_element_value(xml_op, XML_LRM_ATTR_TASK);
 140         crm_element_value_ms(xml_op, XML_LRM_ATTR_INTERVAL_MS, &interval_ms);
 141     }
 142     if (task == NULL) { // Assume start if no history is available
 143         task = RSC_START;
 144         interval_ms = 0;
 145     }
 146 
 147     // Calculate and show digests
 148     digests = pe__calculate_digests(rsc, task, &interval_ms, node, xml_op,
 149                                     overrides, true, rsc->cluster);
 150     rc = out->message(out, "digests", rsc, node, task, interval_ms, digests);
 151 
 152     pe__free_digests(digests);
 153     return rc;
 154 }
 155 
 156 int
 157 pcmk_resource_digests(xmlNodePtr *xml, pe_resource_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 158                       pe_node_t *node, GHashTable *overrides,
 159                       pe_working_set_t *data_set)
 160 {
 161     pcmk__output_t *out = NULL;
 162     int rc = pcmk_rc_ok;
 163 
 164     rc = pcmk__xml_output_new(&out, xml);
 165     if (rc != pcmk_rc_ok) {
 166         return rc;
 167     }
 168     pcmk__register_lib_messages(out);
 169     rc = pcmk__resource_digests(out, rsc, node, overrides);
 170     pcmk__xml_output_finish(out, xml);
 171     return rc;
 172 }

/* [previous][next][first][last][top][bottom][index][help] */