ctdb-tools-ctdb: Add and use function filter_nodemap_by_natgw_nodes()
authorMartin Schwenke <martin@meltin.net>
Mon, 3 Mar 2014 00:41:32 +0000 (11:41 +1100)
committerAmitay Isaacs <amitay@samba.org>
Sun, 23 Mar 2014 03:20:14 +0000 (04:20 +0100)
Add another filter function, like the ones for capabilities and flags
to, for filtering by NAT gateway nodes.  This makes the main
natgw_list function more readable.

Note that this drops the early filtering of disconnected nodes, so
they will now be listed in a NAT gateway group.  This makes sense.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/tools/ctdb.c

index 5040012b4fbb8713975006a5018bf2e9e9292821..4869850f5332bf6a16f7e6e3dabcdf83f2d3ae9b 100644 (file)
@@ -1131,6 +1131,38 @@ static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
                                nodemap->num * sizeof(struct ctdb_node_and_flags));
 }
 
+static struct ctdb_node_map *
+filter_nodemap_by_natgw_nodes(struct ctdb_context *ctdb,
+                             struct ctdb_node_map *nodemap,
+                             struct natgw_node *natgw_nodes)
+{
+       int i;
+       struct natgw_node *n;
+       struct ctdb_node_map *ret;
+
+       ret = talloc_nodemap(nodemap);
+       CTDB_NO_MEMORY_NULL(ctdb, ret);
+
+       ret->num = 0;
+
+       for (i = 0; i < nodemap->num; i++) {
+               for(n = natgw_nodes; n != NULL ; n = n->next) {
+                       if (!strcmp(n->addr,
+                                   ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
+                               break;
+                       }
+               }
+               if (n == NULL) {
+                       continue;
+               }
+
+               ret->nodes[ret->num] = nodemap->nodes[i];
+               ret->num++;
+       }
+
+       return ret;
+}
+
 static struct ctdb_node_map *
 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
                               struct ctdb_node_map *nodemap,
@@ -1203,8 +1235,8 @@ static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **a
        char **lines;
        struct natgw_node *natgw_nodes = NULL;
        struct natgw_node *natgw_node;
-       struct ctdb_node_map *nodemap=NULL;
-       struct ctdb_node_map *cnodemap;
+       struct ctdb_node_map *orig_nodemap=NULL;
+       struct ctdb_node_map *cnodemap, *nodemap;
        uint32_t mypnn, pnn;
        const char *ip;
 
@@ -1259,39 +1291,21 @@ static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **a
                natgw_nodes = natgw_node;
        }
 
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
+       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
+                                  tmp_ctx, &orig_nodemap);
        if (ret != 0) {
                DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
                talloc_free(tmp_ctx);
                return -1;
        }
 
-       /* Trim the nodemap so it only includes connected nodes in the
-        * current natgw group.
-        */
-       i=0;
-       while(i<nodemap->num) {
-               for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
-                       if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
-                               break;
-                       }
-               }
-
-               /* this node was not in the natgw so we just remove it from
-                * the list
-                */
-               if ((natgw_node == NULL) 
-               ||  (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
-                       int j;
-
-                       for (j=i+1; j<nodemap->num; j++) {
-                               nodemap->nodes[j-1] = nodemap->nodes[j];
-                       }
-                       nodemap->num--;
-                       continue;
-               }
-
-               i++;
+       /* Get a nodemap that includes only the nodes in the NATGW
+        * group */
+       nodemap = filter_nodemap_by_natgw_nodes(ctdb, orig_nodemap,
+                                               natgw_nodes);
+       if (nodemap == NULL) {
+               ret = -1;
+               goto done;
        }
 
        /* Get a nodemap that includes only the nodes with the NATGW