ctdb-takeover: Short circuit if fetching IPs from no nodes
[sfrench/samba-autobuild/.git] / ctdb / server / ctdb_takeover_helper.c
index b2062f095ff2805f8c159c95f845337bacf08081..053970510854804e5d4de4a631f1fbabf9b22c83 100644 (file)
@@ -88,8 +88,6 @@ determine_algorithm(const struct ctdb_tunable_list *tunables)
 /**********************************************************************/
 
 struct get_public_ips_state {
-       struct tevent_context *ev;
-       struct ctdb_client_context *client;
        uint32_t *pnns;
        int count;
        struct ctdb_public_ip_list *ips;
@@ -102,7 +100,7 @@ static struct tevent_req *get_public_ips_send(
                                struct tevent_context *ev,
                                struct ctdb_client_context *client,
                                uint32_t *pnns,
-                               int count,
+                               int count, int num_nodes,
                                bool available_only)
 {
        struct tevent_req *req, *subreq;
@@ -116,7 +114,19 @@ static struct tevent_req *get_public_ips_send(
 
        state->pnns = pnns;
        state->count = count;
-       state->ips = NULL;
+
+       state->ips  = talloc_zero_array(state,
+                                       struct ctdb_public_ip_list,
+                                       num_nodes);
+       if (tevent_req_nomem(state->ips, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       /* Short circuit if no nodes being asked for IPs */
+       if (state->count == 0) {
+               tevent_req_done(req);
+               return tevent_req_post(req, ev);
+       }
 
        ctdb_req_control_get_public_ips(&request, available_only);
        subreq = ctdb_client_control_multi_send(mem_ctx, ev, client,
@@ -163,12 +173,6 @@ static void get_public_ips_done(struct tevent_req *subreq)
                return;
        }
 
-       state->ips = talloc_zero_array(state, struct ctdb_public_ip_list,
-                                      state->count);
-       if (tevent_req_nomem(state->ips, req)) {
-               return;
-       }
-
        for (i = 0; i < state->count; i++) {
                uint32_t pnn;
                struct ctdb_public_ip_list *ips;
@@ -294,6 +298,12 @@ static struct tevent_req *release_ip_send(TALLOC_CTX *mem_ctx,
                        }
                }
 
+               if (substate->count == 0) {
+                       /* No releases to send for this address... */
+                       TALLOC_FREE(substate);
+                       continue;
+               }
+
                ip.pnn = tmp_ip->pnn;
                ip.addr = tmp_ip->addr;
                ctdb_req_control_release_ip(&request, &ip);
@@ -310,6 +320,12 @@ static struct tevent_req *release_ip_send(TALLOC_CTX *mem_ctx,
                state->num_sent++;
        }
 
+       /* None sent, finished... */
+       if (state->num_sent == 0) {
+               tevent_req_done(req);
+               return tevent_req_post(req, ev);
+       }
+
        return req;
 }
 
@@ -832,7 +848,7 @@ static void takeover_nodemap_done(struct tevent_req *subreq)
 
        subreq = get_public_ips_send(state, state->ev, state->client,
                                     state->pnns_active, state->num_active,
-                                    false);
+                                    state->num_nodes, false);
        if (tevent_req_nomem(subreq, req)) {
                return;
        }
@@ -860,7 +876,7 @@ static void takeover_known_ips_done(struct tevent_req *subreq)
 
        subreq = get_public_ips_send(state, state->ev, state->client,
                                     state->pnns_active, state->num_active,
-                                    true);
+                                    state->num_nodes, true);
        if (tevent_req_nomem(subreq, req)) {
                return;
        }
@@ -1160,7 +1176,8 @@ int main(int argc, const char *argv[])
                force_rebalance_nodes = parse_node_list(mem_ctx, argv[3]);
                if (force_rebalance_nodes == NULL) {
                        usage(argv[0]);
-                       exit(1);
+                       ret = EINVAL;
+                       goto done;
                }
        }