From 5deee5bfaa3989fb2a7b0a1d6bbc2fd94f54a87f Mon Sep 17 00:00:00 2001
From: "Gao,Yan" <ygao@suse.com>
Date: Mon, 1 Oct 2018 17:23:14 +0200
Subject: [PATCH 2/2] Refactor: fenced: Handle fencing requested with nodeid by
 utilizing the membership cache of known nodes

This partially reverts c52267dfb.
---
 fencing/commands.c |  9 +++------
 fencing/history.c  |  8 +++-----
 fencing/internal.h |  4 ----
 fencing/main.c     | 18 +++++-------------
 fencing/remote.c   | 30 +++---------------------------
 5 files changed, 14 insertions(+), 55 deletions(-)

Index: pacemaker-1.1.16+20170320.77ea74d/fencing/commands.c
===================================================================
--- pacemaker-1.1.16+20170320.77ea74d.orig/fencing/commands.c
+++ pacemaker-1.1.16+20170320.77ea74d/fencing/commands.c
@@ -2289,22 +2289,19 @@ stonith_fence(xmlNode * msg)
 
     } else {
         const char *host = crm_element_value(dev, F_STONITH_TARGET);
-        char *nodename = NULL;
 
         if (cmd->options & st_opt_cs_nodeid) {
             int nodeid = crm_atoi(host, NULL);
+            crm_node_t *node = crm_find_known_peer_full(nodeid, NULL, CRM_GET_PEER_ANY);
 
-            nodename = stonith_get_peer_name(nodeid);
-            if (nodename) {
-                host = nodename;
+            if (node) {
+                host = node->uname;
             }
         }
 
         /* If we get to here, then self-fencing is implicitly allowed */
         get_capable_devices(host, cmd->action, cmd->default_timeout,
                             TRUE, cmd, stonith_fence_get_devices_cb);
-
-        free(nodename);
     }
 
     return -EINPROGRESS;
Index: pacemaker-1.1.16+20170320.77ea74d/fencing/remote.c
===================================================================
--- pacemaker-1.1.16+20170320.77ea74d.orig/fencing/remote.c
+++ pacemaker-1.1.16+20170320.77ea74d/fencing/remote.c
@@ -920,30 +920,6 @@ stonith_manual_ack(xmlNode * msg, remote
     return -EINPROGRESS;
 }
 
-char *
-stonith_get_peer_name(unsigned int nodeid)
-{
-    crm_node_t *node = crm_find_peer(nodeid, NULL);
-    char *nodename = NULL;
-
-    if (node && node->uname) {
-        return strdup(node->uname);
-
-    } else if ((nodename = get_node_name(nodeid))) {
-        return nodename;
-
-    } else {
-        const char *last_known_name = g_hash_table_lookup(known_peer_names, GUINT_TO_POINTER(nodeid));
-
-        if (last_known_name) {
-            crm_debug("Use the last known name %s for nodeid %u", last_known_name, nodeid);
-            return strdup(last_known_name);
-        }
-    }
-
-    return NULL;
-}
-
 /*!
  * \internal
  * \brief Create a new remote stonith operation
@@ -1026,14 +1002,14 @@ create_remote_stonith_op(const char *cli
 
     if (op->call_options & st_opt_cs_nodeid) {
         int nodeid = crm_atoi(op->target, NULL);
-        char *nodename = stonith_get_peer_name(nodeid);
+        crm_node_t *node = crm_find_known_peer_full(nodeid, NULL, CRM_GET_PEER_ANY);
 
         /* Ensure the conversion only happens once */
         op->call_options &= ~st_opt_cs_nodeid;
 
-        if (nodename) {
+        if (node && node->uname) {
             free(op->target);
-            op->target = nodename;
+            op->target = strdup(node->uname);
 
         } else {
             crm_warn("Could not expand nodeid '%s' into a host name", op->target);
@@ -2034,7 +2010,6 @@ stonith_fence_history(xmlNode * msg, xml
     int rc = 0;
     const char *target = NULL;
     xmlNode *dev = get_xpath_object("//@" F_STONITH_TARGET, msg, LOG_TRACE);
-    char *nodename = NULL;
 
     if (dev) {
         int options = 0;
@@ -2043,10 +2018,10 @@ stonith_fence_history(xmlNode * msg, xml
         crm_element_value_int(msg, F_STONITH_CALLOPTS, &options);
         if (target && (options & st_opt_cs_nodeid)) {
             int nodeid = crm_atoi(target, NULL);
+            crm_node_t *node = crm_find_known_peer_full(nodeid, NULL, CRM_GET_PEER_ANY);
 
-            nodename = stonith_get_peer_name(nodeid);
-            if (nodename) {
-                target = nodename;
+            if (node) {
+                target = node->uname;
             }
         }
     }
@@ -2079,7 +2054,6 @@ stonith_fence_history(xmlNode * msg, xml
         }
     }
 
-    free(nodename);
     return rc;
 }
 
Index: pacemaker-1.1.16+20170320.77ea74d/fencing/internal.h
===================================================================
--- pacemaker-1.1.16+20170320.77ea74d.orig/fencing/internal.h
+++ pacemaker-1.1.16+20170320.77ea74d/fencing/internal.h
@@ -235,12 +235,8 @@ schedule_internal_command(const char *or
                           void (*done_cb) (GPid pid, int rc, const char *output,
                                            gpointer user_data));
 
-char *stonith_get_peer_name(unsigned int nodeid);
-
 extern char *stonith_our_uname;
 extern gboolean stand_alone;
 extern GHashTable *device_list;
 extern GHashTable *topology;
 extern long stonith_watchdog_timeout_ms;
-
-extern GHashTable *known_peer_names;
Index: pacemaker-1.1.16+20170320.77ea74d/fencing/main.c
===================================================================
--- pacemaker-1.1.16+20170320.77ea74d.orig/fencing/main.c
+++ pacemaker-1.1.16+20170320.77ea74d/fencing/main.c
@@ -62,8 +62,6 @@ gboolean stonith_shutdown_flag = FALSE;
 qb_ipcs_service_t *ipcs = NULL;
 xmlNode *local_cib = NULL;
 
-GHashTable *known_peer_names = NULL;
-
 static cib_t *cib_api = NULL;
 static void *cib_library = NULL;
 
@@ -1082,6 +1080,8 @@ update_cib_cache_cb(const char *event, x
         stonith_enabled_saved = FALSE; /* Trigger a full refresh below */
     }
 
+    crm_peer_caches_refresh(local_cib);
+
     stonith_enabled_xml = get_xpath_object("//nvpair[@name='stonith-enabled']", local_cib, LOG_TRACE);
     if (stonith_enabled_xml) {
         stonith_enabled_s = crm_element_value(stonith_enabled_xml, XML_NVPAIR_ATTR_VALUE);
@@ -1133,6 +1133,8 @@ init_cib_cache_cb(xmlNode * msg, int cal
     have_cib_devices = TRUE;
     local_cib = copy_xml(output);
 
+    crm_peer_caches_refresh(local_cib);
+
     fencing_topology_init();
     cib_devices_update();
 }
@@ -1177,9 +1179,6 @@ stonith_cleanup(void)
         qb_ipcs_destroy(ipcs);
     }
 
-    g_hash_table_destroy(known_peer_names);
-    known_peer_names = NULL;
-
     crm_peer_destroy();
     crm_client_cleanup();
     free(stonith_our_uname);
@@ -1259,17 +1258,11 @@ static void
 st_peer_update_callback(enum crm_status_type type, crm_node_t * node, const void *data)
 {
     if ((type != crm_status_processes) && !is_set(node->flags, crm_remote_node)) {
-        xmlNode *query = NULL;
-
-        if (node->id && node->uname) {
-            g_hash_table_insert(known_peer_names, GUINT_TO_POINTER(node->id), strdup(node->uname));
-        }
-
         /*
          * This is a hack until we can send to a nodeid and/or we fix node name lookups
          * These messages are ignored in stonith_peer_callback()
          */
-        query = create_xml_node(NULL, "stonith_command");
+        xmlNode *query = create_xml_node(NULL, "stonith_command");
 
         crm_xml_add(query, F_XML_TAGNAME, "stonith_command");
         crm_xml_add(query, F_TYPE, T_STONITH_NG);
@@ -1460,7 +1453,6 @@ main(int argc, char **argv)
     mainloop_add_signal(SIGTERM, stonith_shutdown);
 
     crm_peer_init();
-    known_peer_names = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free);
 
     if (stand_alone == FALSE) {
 #if SUPPORT_HEARTBEAT
