IPALLOCATION : If the node is held pinned down in "init" state
[sahlberg/ctdb.git] / server / ctdb_takeover.c
index 13e0039285f5afa42856b1abf8c9f1d331f2162f..1d6fe6e4b8ba13c4a71b447501d227674ebb4e19 100644 (file)
@@ -18,7 +18,7 @@
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 #include "includes.h"
-#include "lib/events/events.h"
+#include "lib/tevent/tevent.h"
 #include "lib/tdb/include/tdb.h"
 #include "lib/util/dlinklist.h"
 #include "system/network.h"
@@ -331,6 +331,7 @@ static void ctdb_do_takeip_callback(struct ctdb_context *ctdb, int status,
        struct ctdb_do_takeip_state *state =
                talloc_get_type(private_data, struct ctdb_do_takeip_state);
        int32_t ret;
+       TDB_DATA data;
 
        if (status != 0) {
                if (status == -ETIME) {
@@ -351,6 +352,13 @@ static void ctdb_do_takeip_callback(struct ctdb_context *ctdb, int status,
                return;
        }
 
+       data.dptr  = (uint8_t *)ctdb_addr_to_str(&state->vnn->public_address);
+       data.dsize = strlen((char *)data.dptr) + 1;
+       DEBUG(DEBUG_INFO,(__location__ " sending TAKE_IP for '%s'\n", data.dptr));
+
+       ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_TAKE_IP, data);
+
+
        /* the control succeeded */
        ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
        talloc_free(state);
@@ -482,15 +490,6 @@ static int32_t ctdb_do_updateip(struct ctdb_context *ctdb,
                return -1;
        }
 
-       if (vnn->iface == old) {
-               DEBUG(DEBUG_ERR,("update of IP %s/%u trying to "
-                                "assin a same interface '%s'\n",
-                                ctdb_addr_to_str(&vnn->public_address),
-                                vnn->public_netmask_bits,
-                                old->name));
-               return -1;
-       }
-
        state = talloc(vnn, struct ctdb_do_updateip_state);
        CTDB_NO_MEMORY(ctdb, state);
 
@@ -588,16 +587,19 @@ int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb,
                return -1;
        }
 
+       if (vnn->iface == NULL && vnn->pnn == -1 && have_ip && best_iface != NULL) {
+               DEBUG(DEBUG_ERR,("Taking over newly created ip\n"));
+               have_ip = false;
+       }
+
        if (vnn->iface == NULL && have_ip) {
                DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
-                                 "but we have no interface assigned, has someone manually configured it?"
-                                 "banning ourself\n",
+                                 "but we have no interface assigned, has someone manually configured it? Ignore for now.\n",
                                 ctdb_addr_to_str(&vnn->public_address)));
-               ctdb_ban_self(ctdb);
                return -1;
        }
 
-       if (vnn->pnn != ctdb->pnn && have_ip) {
+       if (vnn->pnn != ctdb->pnn && have_ip && vnn->pnn != -1) {
                DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
                                  "and we have it on iface[%s], but it was assigned to node %d"
                                  "and we are node %d, banning ourself\n",
@@ -607,6 +609,15 @@ int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb,
                return -1;
        }
 
+       if (vnn->pnn == -1 && have_ip) {
+               vnn->pnn = ctdb->pnn;
+               DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
+                                 "and we already have it on iface[%s], update local daemon\n",
+                                ctdb_addr_to_str(&vnn->public_address),
+                                 ctdb_vnn_iface_string(vnn)));
+               return 0;
+       }
+
        if (vnn->iface) {
                if (vnn->iface->link_up) {
                        /* only move when the rebalance gains something */
@@ -781,12 +792,10 @@ int32_t ctdb_control_release_ip(struct ctdb_context *ctdb,
        }
 
        if (vnn->iface == NULL) {
-               DEBUG(DEBUG_CRIT,(__location__ " release_ip of IP %s is known to the kernel, "
-                                 "but we have no interface assigned, has someone manually configured it?"
-                                 "banning ourself\n",
+               DEBUG(DEBUG_ERR,(__location__ " release_ip of IP %s is known to the kernel, "
+                                "but we have no interface assigned, has someone manually configured it? Ignore for now.\n",
                                 ctdb_addr_to_str(&vnn->public_address)));
-               ctdb_ban_self(ctdb);
-               return -1;
+               return 0;
        }
 
        DEBUG(DEBUG_NOTICE,("Release of IP %s/%u on interface %s  node:%d\n",
@@ -882,6 +891,10 @@ static int ctdb_add_public_address(struct ctdb_context *ctdb,
        vnn->public_address      = *addr;
        vnn->public_netmask_bits = mask;
        vnn->pnn                 = -1;
+       if (ctdb_sys_have_ip(addr)) {
+               DEBUG(DEBUG_ERR,("We are already hosting public address '%s'. setting PNN to ourself:%d\n", ctdb_addr_to_str(addr), ctdb->pnn));
+               vnn->pnn = ctdb->pnn;
+       }
 
        for (i=0; vnn->ifaces[i]; i++) {
                ret = ctdb_add_local_iface(ctdb, vnn->ifaces[i]);
@@ -892,6 +905,9 @@ static int ctdb_add_public_address(struct ctdb_context *ctdb,
                        talloc_free(vnn);
                        return -1;
                }
+               if (i == 0) {
+                       vnn->iface = ctdb_find_iface(ctdb, vnn->ifaces[i]);
+               }
        }
 
        DLIST_ADD(ctdb->vnn, vnn);
@@ -980,6 +996,7 @@ int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
                              const char *ip)
 {
        struct ctdb_vnn *svnn;
+       struct ctdb_iface *cur = NULL;
        bool ok;
        int ret;
 
@@ -1008,6 +1025,14 @@ int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
                return -1;
        }
 
+       /* assume the single public ip interface is initially "good" */
+       cur = ctdb_find_iface(ctdb, iface);
+       if (cur == NULL) {
+               DEBUG(DEBUG_CRIT,("Can not find public interface %s used by --single-public-ip", iface));
+               return -1;
+       }
+       cur->link_up = true;
+
        ret = ctdb_vnn_assign_iface(ctdb, svnn);
        if (ret != 0) {
                talloc_free(svnn);
@@ -1146,6 +1171,16 @@ static uint32_t *ip_key(ctdb_sock_addr *ip)
 
 static void *add_ip_callback(void *parm, void *data)
 {
+       struct ctdb_public_ip_list *this_ip = parm; 
+       struct ctdb_public_ip_list *prev_ip = data; 
+
+       if (prev_ip == NULL) {
+               return parm;
+       }
+       if (this_ip->pnn == -1) {
+               this_ip->pnn = prev_ip->pnn;
+       }
+
        return parm;
 }
 
@@ -1213,7 +1248,7 @@ int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap)
        int i, num_healthy, retries;
        struct ctdb_public_ip ip;
        struct ctdb_public_ipv4 ipv4;
-       uint32_t mask;
+       uint32_t mask, *nodes;
        struct ctdb_public_ip_list *all_ips, *tmp_ip;
        int maxnode, maxnum=0, minnode, minnum=0, num;
        TDB_DATA data;
@@ -1222,6 +1257,13 @@ int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap)
        struct ctdb_client_control_state *state;
        TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
 
+       /*
+        * ip failover is completely disabled, just send out the 
+        * ipreallocated event.
+        */
+       if (ctdb->tunable.disable_ip_failover != 0) {
+               goto ipreallocated;
+       }
 
        ZERO_STRUCT(ip);
 
@@ -1524,6 +1566,20 @@ finished:
                return -1;
        }
 
+ipreallocated:
+       /* tell all nodes to update natwg */
+       /* send the flags update natgw on all connected nodes */
+       data.dptr  = discard_const("ipreallocated");
+       data.dsize = strlen((char *)data.dptr) + 1; 
+       nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
+       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RUN_EVENTSCRIPTS,
+                                     nodes, 0, TAKEOVER_TIMEOUT(),
+                                     false, data,
+                                     NULL, NULL,
+                                     NULL) != 0) {
+               DEBUG(DEBUG_ERR, (__location__ " ctdb_control to updatenatgw failed\n"));
+       }
+
        talloc_free(tmp_ctx);
        return 0;
 }
@@ -1557,7 +1613,7 @@ int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
        struct ctdb_control_tcp_addr new_addr;
        struct ctdb_control_tcp_addr *tcp_sock = NULL;
        struct ctdb_tcp_list *tcp;
-       struct ctdb_control_tcp_vnn t;
+       struct ctdb_tcp_connection t;
        int ret;
        TDB_DATA data;
        struct ctdb_client_ip *ip;
@@ -1637,8 +1693,8 @@ int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
 
        DLIST_ADD(client->tcp_list, tcp);
 
-       t.src  = tcp_sock->src;
-       t.dest = tcp_sock->dest;
+       t.src_addr = tcp_sock->src;
+       t.dst_addr = tcp_sock->dest;
 
        data.dptr = (uint8_t *)&t;
        data.dsize = sizeof(t);
@@ -1694,22 +1750,24 @@ static struct ctdb_tcp_connection *ctdb_tcp_find(struct ctdb_tcp_array *array,
        return NULL;
 }
 
+
+
 /*
   called by a daemon to inform us of a TCP connection that one of its
   clients managing that should tickled with an ACK when IP takeover is
   done
  */
-int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata)
+int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata, bool tcp_update_needed)
 {
-       struct ctdb_control_tcp_vnn *p = (struct ctdb_control_tcp_vnn *)indata.dptr;
+       struct ctdb_tcp_connection *p = (struct ctdb_tcp_connection *)indata.dptr;
        struct ctdb_tcp_array *tcparray;
        struct ctdb_tcp_connection tcp;
        struct ctdb_vnn *vnn;
 
-       vnn = find_public_ip_vnn(ctdb, &p->dest);
+       vnn = find_public_ip_vnn(ctdb, &p->dst_addr);
        if (vnn == NULL) {
                DEBUG(DEBUG_INFO,(__location__ " got TCP_ADD control for an address which is not a public address '%s'\n",
-                       ctdb_addr_to_str(&p->dest)));
+                       ctdb_addr_to_str(&p->dst_addr)));
 
                return -1;
        }
@@ -1729,16 +1787,20 @@ int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata)
                tcparray->connections = talloc_size(tcparray, sizeof(struct ctdb_tcp_connection));
                CTDB_NO_MEMORY(ctdb, tcparray->connections);
 
-               tcparray->connections[tcparray->num].src_addr = p->src;
-               tcparray->connections[tcparray->num].dst_addr = p->dest;
+               tcparray->connections[tcparray->num].src_addr = p->src_addr;
+               tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
                tcparray->num++;
+
+               if (tcp_update_needed) {
+                       vnn->tcp_update_needed = true;
+               }
                return 0;
        }
 
 
        /* Do we already have this tickle ?*/
-       tcp.src_addr = p->src;
-       tcp.dst_addr = p->dest;
+       tcp.src_addr = p->src_addr;
+       tcp.dst_addr = p->dst_addr;
        if (ctdb_tcp_find(vnn->tcp_array, &tcp) != NULL) {
                DEBUG(DEBUG_DEBUG,("Already had tickle info for %s:%u for vnn:%u\n",
                        ctdb_addr_to_str(&tcp.dst_addr),
@@ -1754,8 +1816,8 @@ int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata)
        CTDB_NO_MEMORY(ctdb, tcparray->connections);
 
        vnn->tcp_array = tcparray;
-       tcparray->connections[tcparray->num].src_addr = p->src;
-       tcparray->connections[tcparray->num].dst_addr = p->dest;
+       tcparray->connections[tcparray->num].src_addr = p->src_addr;
+       tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
        tcparray->num++;
                                
        DEBUG(DEBUG_INFO,("Added tickle info for %s:%u from vnn %u\n",
@@ -1763,6 +1825,10 @@ int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata)
                ntohs(tcp.dst_addr.ip.sin_port),
                vnn->pnn));
 
+       if (tcp_update_needed) {
+               vnn->tcp_update_needed = true;
+       }
+
        return 0;
 }
 
@@ -1830,6 +1896,20 @@ static void ctdb_remove_tcp_connection(struct ctdb_context *ctdb, struct ctdb_tc
 }
 
 
+/*
+  called by a daemon to inform us of a TCP connection that one of its
+  clients used are no longer needed in the tickle database
+ */
+int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata)
+{
+       struct ctdb_tcp_connection *conn = (struct ctdb_tcp_connection *)indata.dptr;
+
+       ctdb_remove_tcp_connection(ctdb, conn);
+
+       return 0;
+}
+
+
 /*
   called when a daemon restarts - send all tickes for all public addresses
   we are serving immediately to the new node.
@@ -2254,7 +2334,8 @@ static void tickle_connection_traverse(void *param, void *data)
 
        /* have tried too many times, just give up */
        if (con->count >= 5) {
-               talloc_free(con);
+               /* can't delete in traverse: reparent to delete_cons */
+               talloc_steal(param, con);
                return;
        }
 
@@ -2274,11 +2355,13 @@ static void ctdb_tickle_sentenced_connections(struct event_context *ev, struct t
                                              struct timeval t, void *private_data)
 {
        struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
-
+       void *delete_cons = talloc_new(NULL);
 
        /* loop over all connections sending tickle ACKs */
-       trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, NULL);
+       trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, delete_cons);
 
+       /* now we've finished traverse, it's safe to do deletion. */
+       talloc_free(delete_cons);
 
        /* If there are no more connections to kill we can remove the
           entire killtcp structure
@@ -2300,7 +2383,9 @@ static void ctdb_tickle_sentenced_connections(struct event_context *ev, struct t
  */
 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
 {
-       killtcp->vnn->killtcp = NULL;
+       if (killtcp->vnn) {
+               killtcp->vnn->killtcp = NULL;
+       }
        return 0;
 }
 
@@ -2401,8 +2486,9 @@ static int ctdb_killtcp_add_connection(struct ctdb_context *ctdb,
 
        if (killtcp->fde == NULL) {
                killtcp->fde = event_add_fd(ctdb->ev, killtcp, killtcp->capture_fd, 
-                                           EVENT_FD_READ | EVENT_FD_AUTOCLOSE, 
+                                           EVENT_FD_READ,
                                            capture_tcp_handler, killtcp);
+               tevent_fd_set_auto_close(killtcp->fde);
 
                /* We also need to set up some events to tickle all these connections
                   until they are all reset
@@ -2849,3 +2935,24 @@ int verify_remote_ip_allocation(struct ctdb_context *ctdb, struct ctdb_all_publi
 
        return 0;
 }
+
+int update_ip_assignment_tree(struct ctdb_context *ctdb, struct ctdb_public_ip *ip)
+{
+       struct ctdb_public_ip_list *tmp_ip; 
+
+       if (ctdb->ip_tree == NULL) {
+               DEBUG(DEBUG_ERR,("No ctdb->ip_tree yet. Failed to update ip assignment\n"));
+               return -1;
+       }
+
+       tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ip->addr));
+       if (tmp_ip == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " Could not find record for address %s, update ip\n", ctdb_addr_to_str(&ip->addr)));
+               return -1;
+       }
+
+       DEBUG(DEBUG_NOTICE,("Updated ip assignment tree for ip : %s from node %u to node %u\n", ctdb_addr_to_str(&ip->addr), tmp_ip->pnn, ip->pnn));
+       tmp_ip->pnn = ip->pnn;
+
+       return 0;
+}