Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Consistent Hash implementation |
| 3 | * Please consult this very well detailed article for more information : |
| 4 | * http://www.spiteful.com/2008/03/17/programmers-toolbox-part-3-consistent-hashing/ |
| 5 | * |
| 6 | * Our implementation has to support both weighted hashing and weighted round |
| 7 | * robin because we'll use it to replace the previous map-based implementation |
| 8 | * which offered both algorithms. |
| 9 | * |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 10 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | */ |
| 18 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 19 | #include <haproxy/api.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 20 | #include <haproxy/tools.h> |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 21 | #include <import/eb32tree.h> |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 22 | |
| 23 | #include <types/global.h> |
| 24 | #include <types/server.h> |
| 25 | |
| 26 | #include <proto/backend.h> |
| 27 | #include <proto/queue.h> |
| 28 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 29 | /* Return next tree node after <node> which must still be in the tree, or be |
| 30 | * NULL. Lookup wraps around the end to the beginning. If the next node is the |
| 31 | * same node, return NULL. This is designed to find a valid next node before |
| 32 | * deleting one from the tree. |
| 33 | */ |
| 34 | static inline struct eb32_node *chash_skip_node(struct eb_root *root, struct eb32_node *node) |
| 35 | { |
| 36 | struct eb32_node *stop = node; |
| 37 | |
| 38 | if (!node) |
| 39 | return NULL; |
| 40 | node = eb32_next(node); |
| 41 | if (!node) |
| 42 | node = eb32_first(root); |
| 43 | if (node == stop) |
| 44 | return NULL; |
| 45 | return node; |
| 46 | } |
| 47 | |
| 48 | /* Remove all of a server's entries from its tree. This may be used when |
| 49 | * setting a server down. |
| 50 | */ |
| 51 | static inline void chash_dequeue_srv(struct server *s) |
| 52 | { |
| 53 | while (s->lb_nodes_now > 0) { |
| 54 | if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway |
| 55 | s->lb_nodes_now = s->lb_nodes_tot; |
| 56 | s->lb_nodes_now--; |
| 57 | if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node) |
| 58 | s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last); |
| 59 | eb32_delete(&s->lb_nodes[s->lb_nodes_now].node); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* Adjust the number of entries of a server in its tree. The server must appear |
| 64 | * as many times as its weight indicates it. If it's there too often, we remove |
| 65 | * the last occurrences. If it's not there enough, we add more occurrences. To |
| 66 | * remove a server from the tree, normally call this with eweight=0. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 67 | * |
| 68 | * The server's lock and the lbprm's lock must be held. |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 69 | */ |
| 70 | static inline void chash_queue_dequeue_srv(struct server *s) |
| 71 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 72 | while (s->lb_nodes_now > s->next_eweight) { |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 73 | if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway |
| 74 | s->lb_nodes_now = s->lb_nodes_tot; |
| 75 | s->lb_nodes_now--; |
| 76 | if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node) |
| 77 | s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last); |
| 78 | eb32_delete(&s->lb_nodes[s->lb_nodes_now].node); |
| 79 | } |
| 80 | |
Olivier Houchard | f8eb8d5 | 2017-10-17 15:52:59 +0200 | [diff] [blame] | 81 | /* Attempt to increase the total number of nodes, if the user |
| 82 | * increased the weight beyond the original weight |
| 83 | */ |
| 84 | if (s->lb_nodes_tot < s->next_eweight) { |
Christopher Faulet | 0a52c17 | 2019-08-01 10:09:29 +0200 | [diff] [blame] | 85 | struct tree_occ *new_nodes; |
Olivier Houchard | f8eb8d5 | 2017-10-17 15:52:59 +0200 | [diff] [blame] | 86 | |
Christopher Faulet | 0a52c17 | 2019-08-01 10:09:29 +0200 | [diff] [blame] | 87 | /* First we need to remove all server's entries from its tree |
| 88 | * because the realloc will change all nodes pointers */ |
| 89 | chash_dequeue_srv(s); |
| 90 | |
| 91 | new_nodes = realloc(s->lb_nodes, s->next_eweight * sizeof(*new_nodes)); |
Olivier Houchard | f8eb8d5 | 2017-10-17 15:52:59 +0200 | [diff] [blame] | 92 | if (new_nodes) { |
| 93 | unsigned int j; |
| 94 | |
| 95 | s->lb_nodes = new_nodes; |
| 96 | memset(&s->lb_nodes[s->lb_nodes_tot], 0, |
| 97 | (s->next_eweight - s->lb_nodes_tot) * sizeof(*s->lb_nodes)); |
| 98 | for (j = s->lb_nodes_tot; j < s->next_eweight; j++) { |
| 99 | s->lb_nodes[j].server = s; |
| 100 | s->lb_nodes[j].node.key = full_hash(s->puid * SRV_EWGHT_RANGE + j); |
| 101 | } |
| 102 | s->lb_nodes_tot = s->next_eweight; |
| 103 | } |
| 104 | } |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 105 | while (s->lb_nodes_now < s->next_eweight) { |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 106 | if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway |
| 107 | break; |
| 108 | if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node) |
| 109 | s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last); |
| 110 | eb32_insert(s->lb_tree, &s->lb_nodes[s->lb_nodes_now].node); |
| 111 | s->lb_nodes_now++; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* This function updates the server trees according to server <srv>'s new |
| 116 | * state. It should be called when server <srv>'s status changes to down. |
| 117 | * It is not important whether the server was already down or not. It is not |
| 118 | * important either that the new state is completely down (the caller may not |
| 119 | * know all the variables of a server's state). |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 120 | * |
| 121 | * The server's lock must be held. The lbprm lock will be used. |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 122 | */ |
| 123 | static void chash_set_server_status_down(struct server *srv) |
| 124 | { |
| 125 | struct proxy *p = srv->proxy; |
| 126 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 127 | if (!srv_lb_status_changed(srv)) |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 128 | return; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 130 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 131 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 132 | if (srv_willbe_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 133 | goto out_update_state; |
| 134 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 135 | if (!srv_currently_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 136 | /* server was already down */ |
| 137 | goto out_update_backend; |
| 138 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 139 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 140 | p->lbprm.tot_wbck -= srv->cur_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 141 | p->srv_bck--; |
| 142 | |
| 143 | if (srv == p->lbprm.fbck) { |
| 144 | /* we lost the first backup server in a single-backup |
| 145 | * configuration, we must search another one. |
| 146 | */ |
| 147 | struct server *srv2 = p->lbprm.fbck; |
| 148 | do { |
| 149 | srv2 = srv2->next; |
| 150 | } while (srv2 && |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 151 | !((srv2->flags & SRV_F_BACKUP) && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 152 | srv_willbe_usable(srv2))); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 153 | p->lbprm.fbck = srv2; |
| 154 | } |
| 155 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 156 | p->lbprm.tot_wact -= srv->cur_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 157 | p->srv_act--; |
| 158 | } |
| 159 | |
| 160 | chash_dequeue_srv(srv); |
| 161 | |
| 162 | out_update_backend: |
| 163 | /* check/update tot_used, tot_weight */ |
| 164 | update_backend_weight(p); |
| 165 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 166 | srv_lb_commit_status(srv); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 167 | |
| 168 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* This function updates the server trees according to server <srv>'s new |
| 172 | * state. It should be called when server <srv>'s status changes to up. |
| 173 | * It is not important whether the server was already down or not. It is not |
| 174 | * important either that the new state is completely UP (the caller may not |
| 175 | * know all the variables of a server's state). This function will not change |
| 176 | * the weight of a server which was already up. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 177 | * |
| 178 | * The server's lock must be held. The lbprm lock will be used. |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 179 | */ |
| 180 | static void chash_set_server_status_up(struct server *srv) |
| 181 | { |
| 182 | struct proxy *p = srv->proxy; |
| 183 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 184 | if (!srv_lb_status_changed(srv)) |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 185 | return; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 186 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 187 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 188 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 189 | if (!srv_willbe_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 190 | goto out_update_state; |
| 191 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 192 | if (srv_currently_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 193 | /* server was already up */ |
| 194 | goto out_update_backend; |
| 195 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 196 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 197 | p->lbprm.tot_wbck += srv->next_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 198 | p->srv_bck++; |
| 199 | |
| 200 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 201 | if (!p->lbprm.fbck) { |
| 202 | /* there was no backup server anymore */ |
| 203 | p->lbprm.fbck = srv; |
| 204 | } else { |
| 205 | /* we may have restored a backup server prior to fbck, |
| 206 | * in which case it should replace it. |
| 207 | */ |
| 208 | struct server *srv2 = srv; |
| 209 | do { |
| 210 | srv2 = srv2->next; |
| 211 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 212 | if (srv2) |
| 213 | p->lbprm.fbck = srv; |
| 214 | } |
| 215 | } |
| 216 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 217 | p->lbprm.tot_wact += srv->next_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 218 | p->srv_act++; |
| 219 | } |
| 220 | |
| 221 | /* note that eweight cannot be 0 here */ |
| 222 | chash_queue_dequeue_srv(srv); |
| 223 | |
| 224 | out_update_backend: |
| 225 | /* check/update tot_used, tot_weight */ |
| 226 | update_backend_weight(p); |
| 227 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 228 | srv_lb_commit_status(srv); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 229 | |
| 230 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* This function must be called after an update to server <srv>'s effective |
| 234 | * weight. It may be called after a state change too. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 235 | * |
| 236 | * The server's lock must be held. The lbprm lock may be used. |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 237 | */ |
| 238 | static void chash_update_server_weight(struct server *srv) |
| 239 | { |
| 240 | int old_state, new_state; |
| 241 | struct proxy *p = srv->proxy; |
| 242 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 243 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 244 | return; |
| 245 | |
| 246 | /* If changing the server's weight changes its state, we simply apply |
| 247 | * the procedures we already have for status change. If the state |
| 248 | * remains down, the server is not in any tree, so it's as easy as |
| 249 | * updating its values. If the state remains up with different weights, |
| 250 | * there are some computations to perform to find a new place and |
| 251 | * possibly a new tree for this server. |
| 252 | */ |
| 253 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 254 | old_state = srv_currently_usable(srv); |
| 255 | new_state = srv_willbe_usable(srv); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 256 | |
| 257 | if (!old_state && !new_state) { |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 258 | srv_lb_commit_status(srv); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | else if (!old_state && new_state) { |
| 262 | chash_set_server_status_up(srv); |
| 263 | return; |
| 264 | } |
| 265 | else if (old_state && !new_state) { |
| 266 | chash_set_server_status_down(srv); |
| 267 | return; |
| 268 | } |
| 269 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 270 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 271 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 272 | /* only adjust the server's presence in the tree */ |
| 273 | chash_queue_dequeue_srv(srv); |
| 274 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 275 | if (srv->flags & SRV_F_BACKUP) |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 276 | p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 277 | else |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 278 | p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 279 | |
| 280 | update_backend_weight(p); |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 281 | srv_lb_commit_status(srv); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 282 | |
| 283 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 287 | * This function implements the "Consistent Hashing with Bounded Loads" algorithm |
| 288 | * of Mirrokni, Thorup, and Zadimoghaddam (arxiv:1608.01350), adapted for use with |
| 289 | * unequal server weights. |
| 290 | */ |
| 291 | int chash_server_is_eligible(struct server *s) |
| 292 | { |
| 293 | /* The total number of slots to allocate is the total number of outstanding requests |
| 294 | * (including the one we're about to make) times the load-balance-factor, rounded up. |
| 295 | */ |
Willy Tarreau | 76e84f5 | 2019-01-14 16:50:58 +0100 | [diff] [blame] | 296 | unsigned tot_slots = ((s->proxy->served + 1) * s->proxy->lbprm.hash_balance_factor + 99) / 100; |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 297 | unsigned slots_per_weight = tot_slots / s->proxy->lbprm.tot_weight; |
| 298 | unsigned remainder = tot_slots % s->proxy->lbprm.tot_weight; |
| 299 | |
| 300 | /* Allocate a whole number of slots per weight unit... */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 301 | unsigned slots = s->cur_eweight * slots_per_weight; |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 302 | |
| 303 | /* And then distribute the rest among servers proportionally to their weight. */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 304 | slots += ((s->cumulative_weight + s->cur_eweight) * remainder) / s->proxy->lbprm.tot_weight |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 305 | - (s->cumulative_weight * remainder) / s->proxy->lbprm.tot_weight; |
| 306 | |
| 307 | /* But never leave a server with 0. */ |
| 308 | if (slots == 0) |
| 309 | slots = 1; |
| 310 | |
| 311 | return s->served < slots; |
| 312 | } |
| 313 | |
| 314 | /* |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 315 | * This function returns the running server from the CHASH tree, which is at |
| 316 | * the closest distance from the value of <hash>. Doing so ensures that even |
| 317 | * with a well imbalanced hash, if some servers are close to each other, they |
| 318 | * will still both receive traffic. If any server is found, it will be returned. |
Willy Tarreau | 59884a6 | 2019-01-02 14:48:31 +0100 | [diff] [blame] | 319 | * It will also skip server <avoid> if the hash result ends on this one. |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 320 | * If no valid server is found, NULL is returned. |
| 321 | */ |
Willy Tarreau | 59884a6 | 2019-01-02 14:48:31 +0100 | [diff] [blame] | 322 | struct server *chash_get_server_hash(struct proxy *p, unsigned int hash, const struct server *avoid) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 323 | { |
| 324 | struct eb32_node *next, *prev; |
| 325 | struct server *nsrv, *psrv; |
| 326 | struct eb_root *root; |
| 327 | unsigned int dn, dp; |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 328 | int loop; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 329 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 330 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
| 331 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 332 | if (p->srv_act) |
| 333 | root = &p->lbprm.chash.act; |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 334 | else if (p->lbprm.fbck) { |
| 335 | nsrv = p->lbprm.fbck; |
| 336 | goto out; |
| 337 | } |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 338 | else if (p->srv_bck) |
| 339 | root = &p->lbprm.chash.bck; |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 340 | else { |
| 341 | nsrv = NULL; |
| 342 | goto out; |
| 343 | } |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 344 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 345 | /* find the node after and the node before */ |
| 346 | next = eb32_lookup_ge(root, hash); |
| 347 | if (!next) |
| 348 | next = eb32_first(root); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 349 | if (!next) { |
| 350 | nsrv = NULL; /* tree is empty */ |
| 351 | goto out; |
| 352 | } |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 353 | |
| 354 | prev = eb32_prev(next); |
| 355 | if (!prev) |
| 356 | prev = eb32_last(root); |
| 357 | |
| 358 | nsrv = eb32_entry(next, struct tree_occ, node)->server; |
| 359 | psrv = eb32_entry(prev, struct tree_occ, node)->server; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 360 | |
Andrew Rodland | 18330ab | 2017-04-26 02:57:03 -0400 | [diff] [blame] | 361 | /* OK we're located between two servers, let's |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 362 | * compare distances between hash and the two servers |
| 363 | * and select the closest server. |
| 364 | */ |
| 365 | dp = hash - prev->key; |
| 366 | dn = next->key - hash; |
| 367 | |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 368 | if (dp <= dn) { |
| 369 | next = prev; |
| 370 | nsrv = psrv; |
| 371 | } |
| 372 | |
| 373 | loop = 0; |
Willy Tarreau | 76e84f5 | 2019-01-14 16:50:58 +0100 | [diff] [blame] | 374 | while (nsrv == avoid || (p->lbprm.hash_balance_factor && !chash_server_is_eligible(nsrv))) { |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 375 | next = eb32_next(next); |
| 376 | if (!next) { |
| 377 | next = eb32_first(root); |
| 378 | if (++loop > 1) // protection against accidental loop |
| 379 | break; |
| 380 | } |
| 381 | nsrv = eb32_entry(next, struct tree_occ, node)->server; |
| 382 | } |
| 383 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 384 | out: |
| 385 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 386 | return nsrv; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /* Return next server from the CHASH tree in backend <p>. If the tree is empty, |
| 390 | * return NULL. Saturated servers are skipped. |
| 391 | */ |
| 392 | struct server *chash_get_next_server(struct proxy *p, struct server *srvtoavoid) |
| 393 | { |
| 394 | struct server *srv, *avoided; |
| 395 | struct eb32_node *node, *stop, *avoided_node; |
| 396 | struct eb_root *root; |
| 397 | |
| 398 | srv = avoided = NULL; |
| 399 | avoided_node = NULL; |
| 400 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 401 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 402 | if (p->srv_act) |
| 403 | root = &p->lbprm.chash.act; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 404 | else if (p->lbprm.fbck) { |
| 405 | srv = p->lbprm.fbck; |
| 406 | goto out; |
| 407 | } |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 408 | else if (p->srv_bck) |
| 409 | root = &p->lbprm.chash.bck; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 410 | else { |
| 411 | srv = NULL; |
| 412 | goto out; |
| 413 | } |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 414 | |
| 415 | stop = node = p->lbprm.chash.last; |
| 416 | do { |
| 417 | struct server *s; |
| 418 | |
| 419 | if (node) |
| 420 | node = eb32_next(node); |
| 421 | if (!node) |
| 422 | node = eb32_first(root); |
| 423 | |
| 424 | p->lbprm.chash.last = node; |
Willy Tarreau | 1ed90ac | 2017-11-05 10:54:50 +0100 | [diff] [blame] | 425 | if (!node) { |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 426 | /* no node is available */ |
Willy Tarreau | 1ed90ac | 2017-11-05 10:54:50 +0100 | [diff] [blame] | 427 | srv = NULL; |
| 428 | goto out; |
| 429 | } |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 430 | |
Willy Tarreau | d16a1b2 | 2013-04-12 14:46:51 +0200 | [diff] [blame] | 431 | /* Note: if we came here after a down/up cycle with no last |
| 432 | * pointer, and after a redispatch (srvtoavoid is set), we |
| 433 | * must set stop to non-null otherwise we can loop forever. |
| 434 | */ |
| 435 | if (!stop) |
| 436 | stop = node; |
| 437 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 438 | /* OK, we have a server. However, it may be saturated, in which |
| 439 | * case we don't want to reconsider it for now, so we'll simply |
| 440 | * skip it. Same if it's the server we try to avoid, in which |
| 441 | * case we simply remember it for later use if needed. |
| 442 | */ |
| 443 | s = eb32_entry(node, struct tree_occ, node)->server; |
| 444 | if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) { |
| 445 | if (s != srvtoavoid) { |
| 446 | srv = s; |
| 447 | break; |
| 448 | } |
| 449 | avoided = s; |
| 450 | avoided_node = node; |
| 451 | } |
| 452 | } while (node != stop); |
| 453 | |
| 454 | if (!srv) { |
| 455 | srv = avoided; |
| 456 | p->lbprm.chash.last = avoided_node; |
| 457 | } |
| 458 | |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 459 | out: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 460 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 461 | return srv; |
| 462 | } |
| 463 | |
| 464 | /* This function is responsible for building the active and backup trees for |
| 465 | * constistent hashing. The servers receive an array of initialized nodes |
| 466 | * with their assigned keys. It also sets p->lbprm.wdiv to the eweight to |
| 467 | * uweight ratio. |
| 468 | */ |
| 469 | void chash_init_server_tree(struct proxy *p) |
| 470 | { |
| 471 | struct server *srv; |
| 472 | struct eb_root init_head = EB_ROOT; |
| 473 | int node; |
| 474 | |
| 475 | p->lbprm.set_server_status_up = chash_set_server_status_up; |
| 476 | p->lbprm.set_server_status_down = chash_set_server_status_down; |
| 477 | p->lbprm.update_server_eweight = chash_update_server_weight; |
| 478 | p->lbprm.server_take_conn = NULL; |
| 479 | p->lbprm.server_drop_conn = NULL; |
| 480 | |
| 481 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 482 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 483 | srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult; |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 484 | srv_lb_commit_status(srv); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | recount_servers(p); |
| 488 | update_backend_weight(p); |
| 489 | |
| 490 | p->lbprm.chash.act = init_head; |
| 491 | p->lbprm.chash.bck = init_head; |
| 492 | p->lbprm.chash.last = NULL; |
| 493 | |
| 494 | /* queue active and backup servers in two distinct groups */ |
| 495 | for (srv = p->srv; srv; srv = srv->next) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 496 | srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.chash.bck : &p->lbprm.chash.act; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 497 | srv->lb_nodes_tot = srv->uweight * BE_WEIGHT_SCALE; |
| 498 | srv->lb_nodes_now = 0; |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 499 | srv->lb_nodes = calloc(srv->lb_nodes_tot, sizeof(struct tree_occ)); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 500 | for (node = 0; node < srv->lb_nodes_tot; node++) { |
| 501 | srv->lb_nodes[node].server = srv; |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 502 | srv->lb_nodes[node].node.key = full_hash(srv->puid * SRV_EWGHT_RANGE + node); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 505 | if (srv_currently_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 506 | chash_queue_dequeue_srv(srv); |
| 507 | } |
| 508 | } |