ctdb-ipalloc: Split IP allocation into its own build subsystem
[obnox/samba/samba-obnox.git] / ctdb / server / ipalloc_nondeterministic.c
1 /*
2    ctdb ip takeover code
3
4    Copyright (C) Ronnie Sahlberg  2007
5    Copyright (C) Andrew Tridgell  2007
6    Copyright (C) Martin Schwenke  2011
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "replace.h"
23 #include "system/network.h"
24
25 #include "ctdb_private.h"
26
27 #include "lib/util/debug.h"
28 #include "common/logging.h"
29 #include "common/common.h"
30
31 #include "server/ipalloc_private.h"
32
33 /* Basic non-deterministic rebalancing algorithm.
34  */
35 static void basic_failback(struct ipalloc_state *ipalloc_state,
36                            int num_ips)
37 {
38         int i, numnodes;
39         int maxnode, maxnum, minnode, minnum, num, retries;
40         struct public_ip_list *t;
41
42         numnodes = ipalloc_state->num;
43         retries = 0;
44
45 try_again:
46         maxnum=0;
47         minnum=0;
48
49         /* for each ip address, loop over all nodes that can serve
50            this ip and make sure that the difference between the node
51            serving the most and the node serving the least ip's are
52            not greater than 1.
53         */
54         for (t = ipalloc_state->all_ips; t != NULL; t = t->next) {
55                 if (t->pnn == -1) {
56                         continue;
57                 }
58
59                 /* Get the highest and lowest number of ips's served by any 
60                    valid node which can serve this ip.
61                 */
62                 maxnode = -1;
63                 minnode = -1;
64                 for (i=0; i<numnodes; i++) {
65                         /* only check nodes that can actually serve this ip */
66                         if (!can_node_takeover_ip(ipalloc_state, i,
67                                                   t)) {
68                                 /* no it couldnt   so skip to the next node */
69                                 continue;
70                         }
71
72                         num = node_ip_coverage(i, ipalloc_state->all_ips);
73                         if (maxnode == -1) {
74                                 maxnode = i;
75                                 maxnum  = num;
76                         } else {
77                                 if (num > maxnum) {
78                                         maxnode = i;
79                                         maxnum  = num;
80                                 }
81                         }
82                         if (minnode == -1) {
83                                 minnode = i;
84                                 minnum  = num;
85                         } else {
86                                 if (num < minnum) {
87                                         minnode = i;
88                                         minnum  = num;
89                                 }
90                         }
91                 }
92                 if (maxnode == -1) {
93                         DEBUG(DEBUG_WARNING,
94                               (__location__ " Could not find maxnode. May not be able to serve ip '%s'\n",
95                                ctdb_addr_to_str(&t->addr)));
96
97                         continue;
98                 }
99
100                 /* if the spread between the smallest and largest coverage by
101                    a node is >=2 we steal one of the ips from the node with
102                    most coverage to even things out a bit.
103                    try to do this a limited number of times since we dont
104                    want to spend too much time balancing the ip coverage.
105                 */
106                 if ((maxnum > minnum+1) &&
107                     (retries < (num_ips + 5))){
108                         struct public_ip_list *tt;
109
110                         /* Reassign one of maxnode's VNNs */
111                         for (tt = ipalloc_state->all_ips; tt != NULL; tt = tt->next) {
112                                 if (tt->pnn == maxnode) {
113                                         (void)find_takeover_node(ipalloc_state,
114                                                                  tt);
115                                         retries++;
116                                         goto try_again;;
117                                 }
118                         }
119                 }
120         }
121 }
122
123 bool ipalloc_nondeterministic(struct ipalloc_state *ipalloc_state)
124 {
125         /* This should be pushed down into basic_failback. */
126         struct public_ip_list *t;
127         int num_ips = 0;
128         for (t = ipalloc_state->all_ips; t != NULL; t = t->next) {
129                 num_ips++;
130         }
131
132         unassign_unsuitable_ips(ipalloc_state);
133
134         basic_allocate_unassigned(ipalloc_state);
135
136         /* If we don't want IPs to fail back then don't rebalance IPs. */
137         if (1 == ipalloc_state->no_ip_failback) {
138                 return true;
139         }
140
141         /* Now, try to make sure the ip adresses are evenly distributed
142            across the nodes.
143         */
144         basic_failback(ipalloc_state, num_ips);
145
146         return true;
147 }