ctdbd_conn: Use sys_poll_intr
[Samba.git] / ctdb / server / ipalloc_nondeterministic.c
blob300d8f1982fc87cfcc1ff9390237b6e74d247411
1 /*
2 ctdb ip takeover code
4 Copyright (C) Ronnie Sahlberg 2007
5 Copyright (C) Andrew Tridgell 2007
6 Copyright (C) Martin Schwenke 2011
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.
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.
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/>.
22 #include "replace.h"
23 #include "system/network.h"
25 #include "ctdb_private.h"
27 #include "lib/util/debug.h"
28 #include "common/logging.h"
29 #include "common/common.h"
31 #include "server/ipalloc_private.h"
33 /* Basic non-deterministic rebalancing algorithm.
35 static void basic_failback(struct ipalloc_state *ipalloc_state,
36 int num_ips)
38 int i, numnodes;
39 int maxnode, maxnum, minnode, minnum, num, retries;
40 struct public_ip_list *t;
42 numnodes = ipalloc_state->num;
43 retries = 0;
45 try_again:
46 maxnum=0;
47 minnum=0;
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.
54 for (t = ipalloc_state->all_ips; t != NULL; t = t->next) {
55 if (t->pnn == -1) {
56 continue;
59 /* Get the highest and lowest number of ips's served by any
60 valid node which can serve this ip.
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;
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;
82 if (minnode == -1) {
83 minnode = i;
84 minnum = num;
85 } else {
86 if (num < minnum) {
87 minnode = i;
88 minnum = num;
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)));
97 continue;
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.
106 if ((maxnum > minnum+1) &&
107 (retries < (num_ips + 5))){
108 struct public_ip_list *tt;
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;;
123 bool ipalloc_nondeterministic(struct ipalloc_state *ipalloc_state)
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++;
132 unassign_unsuitable_ips(ipalloc_state);
134 basic_allocate_unassigned(ipalloc_state);
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;
141 /* Now, try to make sure the ip adresses are evenly distributed
142 across the nodes.
144 basic_failback(ipalloc_state, num_ips);
146 return true;