From 1123695ad22197bbadb1f3f2ca507716ff169b09 Mon Sep 17 00:00:00 2001
From: "Gao,Yan" <ygao@suse.com>
Date: Sat, 27 May 2023 08:40:54 +0200
Subject: [PATCH 4/6] Refactor: libcrmcluster: ability to search for a node
 cache entry by uuid instead of id

... for future use
---
 daemons/attrd/attrd_commands.c       |  2 +-
 daemons/attrd/attrd_commands.c            |  3 ++-
 daemons/based/based_messages.c       |  2 +-
 daemons/controld/controld_corosync.c |  3 ++-
 daemons/controld/controld_messages.c |  4 ++--
 include/crm/cluster/internal.h       |  3 ++-
 lib/cluster/cluster.c                |  2 +-
 lib/cluster/cpg.c                    |  2 +-
 lib/cluster/membership.c             | 21 +++++++++++++++++----
 9 files changed, 29 insertions(+), 13 deletions(-)

Index: pacemaker-2.0.1+20190417.13d370ca9/daemons/attrd/attrd_commands.c
===================================================================
--- pacemaker-2.0.1+20190417.13d370ca9.orig/daemons/attrd/attrd_commands.c
+++ pacemaker-2.0.1+20190417.13d370ca9/daemons/attrd/attrd_commands.c
@@ -193,7 +193,7 @@ attrd_client_peer_remove(const char *cli
 
         crm_element_value_int(xml, F_ATTRD_HOST_ID, &nodeid);
         if (nodeid > 0) {
-            crm_node_t *node = crm_find_peer(nodeid, NULL);
+            crm_node_t *node = crm_find_peer(nodeid, NULL, NULL);
             char *host_alloc = NULL;
 
             if (node && node->uname) {
@@ -695,7 +695,7 @@ attrd_lookup_or_create_value(GHashTable
         /* If we previously assumed this node was an unseen cluster node,
          * remove its entry from the cluster peer cache.
          */
-        crm_node_t *dup = crm_find_peer(0, host);
+        crm_node_t *dup = crm_find_peer(0, host, NULL);
 
         if (dup && (dup->uuid == NULL)) {
             reap_crm_member(0, host);
Index: pacemaker-2.0.1+20190417.13d370ca9/daemons/based/based_messages.c
===================================================================
--- pacemaker-2.0.1+20190417.13d370ca9.orig/daemons/based/based_messages.c
+++ pacemaker-2.0.1+20190417.13d370ca9/daemons/based/based_messages.c
@@ -265,7 +265,7 @@ cib_process_upgrade_server(const char *o
 
         if (rc != pcmk_ok) {
             // Notify originating peer so it can notify its local clients
-            crm_node_t *origin = crm_find_peer(0, host);
+            crm_node_t *origin = crm_find_peer(0, host, NULL);
 
             crm_info("Rejecting upgrade request from %s: %s "
                      CRM_XS " rc=%d peer=%s", host, pcmk_strerror(rc), rc,
Index: pacemaker-2.0.1+20190417.13d370ca9/daemons/controld/controld_messages.c
===================================================================
--- pacemaker-2.0.1+20190417.13d370ca9.orig/daemons/controld/controld_messages.c
+++ pacemaker-2.0.1+20190417.13d370ca9/daemons/controld/controld_messages.c
@@ -469,7 +469,7 @@ relay_message(xmlNode * msg, gboolean or
 #endif
 
         if (host_to) {
-            node_to = crm_find_peer(0, host_to);
+            node_to = crm_find_peer(0, host_to, NULL);
             if (node_to == NULL) {
                crm_err("Cannot route message to unknown node %s", host_to);
                return TRUE;
@@ -822,7 +822,7 @@ handle_request(xmlNode * stored_msg, enu
 
     if (strcmp(op, CRM_OP_SHUTDOWN_REQ) == 0) {
         const char *from = crm_element_value(stored_msg, F_CRM_HOST_FROM);
-        crm_node_t *node = crm_find_peer(0, from);
+        crm_node_t *node = crm_find_peer(0, from, NULL);
 
         crm_update_peer_expected(__FUNCTION__, node, CRMD_JOINSTATE_DOWN);
         if(AM_I_DC == FALSE) {
Index: pacemaker-2.0.1+20190417.13d370ca9/include/crm/cluster/internal.h
===================================================================
--- pacemaker-2.0.1+20190417.13d370ca9.orig/include/crm/cluster/internal.h
+++ pacemaker-2.0.1+20190417.13d370ca9/include/crm/cluster/internal.h
@@ -329,7 +329,8 @@ gboolean cluster_connect_quorum(gboolean
 gboolean node_name_is_valid(const char *key, const char *name);
 
 crm_node_t * crm_find_peer_full(unsigned int id, const char *uname, int flags);
-crm_node_t * crm_find_peer(unsigned int id, const char *uname);
+crm_node_t * crm_find_peer(unsigned int id, const char *uname,
+                           const char *uuid);
 
 void crm_peer_caches_refresh(xmlNode *cib);
 crm_node_t *crm_find_known_peer_full(unsigned int id, const char *uname, int flags);
Index: pacemaker-2.0.1+20190417.13d370ca9/lib/cluster/cluster.c
===================================================================
--- pacemaker-2.0.1+20190417.13d370ca9.orig/lib/cluster/cluster.c
+++ pacemaker-2.0.1+20190417.13d370ca9/lib/cluster/cluster.c
@@ -202,7 +202,7 @@ crm_peer_uname(const char *uuid)
         uint32_t id = crm_int_helper(uuid, NULL);
 
         if (id != 0) {
-            node = crm_find_peer(id, NULL);
+            node = crm_find_peer(id, NULL, NULL);
         } else {
             crm_err("Invalid node id: %s", uuid);
         }
Index: pacemaker-2.0.1+20190417.13d370ca9/lib/cluster/cpg.c
===================================================================
--- pacemaker-2.0.1+20190417.13d370ca9.orig/lib/cluster/cpg.c
+++ pacemaker-2.0.1+20190417.13d370ca9/lib/cluster/cpg.c
@@ -400,7 +400,7 @@ pcmk_cpg_membership(cpg_handle_t handle,
           cmp_member_list_nodeid);
 
     for (i = 0; i < left_list_entries; i++) {
-        crm_node_t *peer = crm_find_peer(left_list[i].nodeid, NULL);
+        crm_node_t *peer = crm_find_peer(left_list[i].nodeid, NULL, NULL);
 
         crm_info("Group event %s.%d: node %u (%s) left: %llu",
                  groupName->value, counter, left_list[i].nodeid,
Index: pacemaker-2.0.1+20190417.13d370ca9/lib/cluster/membership.c
===================================================================
--- pacemaker-2.0.1+20190417.13d370ca9.orig/lib/cluster/membership.c
+++ pacemaker-2.0.1+20190417.13d370ca9/lib/cluster/membership.c
@@ -494,7 +494,7 @@ crm_find_peer_full(unsigned int id, cons
     }
 
     if (node == NULL && (flags & CRM_GET_PEER_CLUSTER)) {
-        node = crm_find_peer(id, uname);
+        node = crm_find_peer(id, uname, NULL);
     }
     return node;
 }
@@ -519,7 +519,7 @@ crm_get_peer_full(unsigned int id, const
 }
 
 crm_node_t *
-crm_find_peer(unsigned int id, const char *uname)
+crm_find_peer(unsigned int id, const char *uname, const char *uuid)
 {
     GHashTableIter iter;
     crm_node_t *node = NULL;
@@ -550,6 +550,16 @@ crm_find_peer(unsigned int id, const cha
                 break;
             }
         }
+
+    } else if (uuid != NULL) {
+        g_hash_table_iter_init(&iter, crm_peer_cache);
+        while (g_hash_table_iter_next(&iter, NULL, (gpointer *) &node)) {
+            if (safe_str_eq(node->uuid, uuid)) {
+                crm_trace("UUID match: %s = %p", node->uuid, node);
+                by_id = node;
+                break;
+            }
+        }
     }
 
     node = by_id; /* Good default */
@@ -659,7 +669,7 @@ crm_get_peer(unsigned int id, const char
 
     crm_peer_init();
 
-    node = crm_find_peer(id, uname);
+    node = crm_find_peer(id, uname, NULL);
 
     /* if uname wasn't provided, and find_peer did not turn up a uname based on id.
      * we need to do a lookup of the node name using the id in the cluster membership. */
@@ -673,7 +683,7 @@ crm_get_peer(unsigned int id, const char
 
         /* try to turn up the node one more time now that we know the uname. */
         if (node == NULL) {
-            node = crm_find_peer(id, uname);
+            node = crm_find_peer(id, uname, NULL);
         }
     }
 
