minor PgxcNodeListAndCount() refactoring to fix compiler warnings
authorTomas Vondra <[email protected]>
Tue, 23 Aug 2016 16:36:13 +0000 (18:36 +0200)
committerPavan Deolasee <[email protected]>
Fri, 26 Aug 2016 11:44:53 +0000 (17:14 +0530)
While tweaking the code, I've noticed this is the only place using
NodeHealthStatus, apparently attempting to save a few bytes. That
seems rather pointless given how little memory this will need and
how short-lived the memory is (and also how AllocSet doubles the
allocation sizes anyway), so I got rid of this. This makes copying
the old state easier (a simple memcpy instead of for loops).

Note: The PgxcNodeListAndCount() seems a bit unfortunate, as the
name suggests to list and count nodes, but in practice it refreshes
the status of nodes from catalog, while keeping the health status.

src/backend/pgxc/nodemgr/nodemgr.c
src/include/pgxc/nodemgr.h

index 683cd095d54994908378ae09e65ccb45680f9065..5e3290d419fa54736638f4e5038a9a80ef7a89eb 100644 (file)
@@ -326,37 +326,32 @@ PgxcNodeListAndCount(void)
        Relation rel;
        HeapScanDesc scan;
        HeapTuple   tuple;
-       NodeHealthStatus *nodehealth;
-       int     numNodes = 0;
+       NodeDefinition *nodes = NULL;
+       int     numNodes;
 
        LWLockAcquire(NodeTableLock, LW_EXCLUSIVE);
 
+       numNodes = *shmemNumCoords + *shmemNumDataNodes;
+
+       Assert((*shmemNumCoords >= 0) && (*shmemNumDataNodes >= 0));
+
        /*
         * Save the existing health status values because nodes
         * might get added or deleted here. We will save
         * nodeoid, status. No need to differentiate between
         * coords and datanodes since oids will be unique anyways
         */
-       if (*shmemNumDataNodes != 0 || *shmemNumCoords != 0)
+       if (numNodes > 0)
        {
-               int i, j;
+               nodes = (NodeDefinition*)palloc(numNodes * sizeof(NodeDefinition));
 
-               numNodes = *shmemNumCoords + *shmemNumDataNodes;
-               nodehealth = palloc0(
-                                       numNodes * sizeof(NodeHealthStatus));
+               /* XXX It's possible to call memcpy with */
+               if (*shmemNumCoords > 0)
+                       memcpy(nodes, coDefs, *shmemNumCoords * sizeof(NodeDefinition));
 
-               for (i = 0; i < *shmemNumCoords; i++)
-               {
-                       nodehealth[i].nodeoid = coDefs[i].nodeoid;
-                       nodehealth[i].nodeishealthy = coDefs[i].nodeishealthy;
-               }
-
-               j = i;
-               for (i = 0; i < *shmemNumDataNodes; i++)
-               {
-                       nodehealth[j].nodeoid = dnDefs[i].nodeoid;
-                       nodehealth[j++].nodeishealthy = dnDefs[i].nodeishealthy;
-               }
+               if (*shmemNumDataNodes > 0)
+                       memcpy(nodes + *shmemNumCoords, dnDefs,
+                                  *shmemNumDataNodes * sizeof(NodeDefinition));
        }
 
        *shmemNumCoords = 0;
@@ -404,9 +399,9 @@ PgxcNodeListAndCount(void)
                node->nodeishealthy = true;
                for (i = 0; i < numNodes; i++)
                {
-                       if (nodehealth[i].nodeoid == node->nodeoid)
+                       if (nodes[i].nodeoid == node->nodeoid)
                        {
-                               node->nodeishealthy = nodehealth[i].nodeishealthy;
+                               node->nodeishealthy = nodes[i].nodeishealthy;
                                break;
                        }
                }
@@ -418,7 +413,7 @@ PgxcNodeListAndCount(void)
                        *shmemNumCoords, *shmemNumDataNodes);
 
        if (numNodes)
-               pfree(nodehealth);
+               pfree(nodes);
 
        /* Finally sort the lists */
        if (*shmemNumCoords > 1)
index 976384a4e867b5a18244658df3349cf56c0cca59..0c1dca7abec451fe8324271e3ae8c7f01c22ebea 100644 (file)
@@ -41,12 +41,6 @@ typedef struct
        bool            nodeishealthy;
 } NodeDefinition;
 
-typedef struct
-{
-       Oid                     nodeoid;
-       bool            nodeishealthy;
-} NodeHealthStatus;
-
 extern void NodeTablesShmemInit(void);
 extern Size NodeTablesShmemSize(void);