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