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 | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 19 | #include <import/eb32tree.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 20 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 21 | #include <haproxy/backend.h> |
Remi Tricot-Le Breton | 4764620 | 2021-05-19 16:40:28 +0200 | [diff] [blame] | 22 | #include <haproxy/errors.h> |
Willy Tarreau | a55c454 | 2020-06-04 22:59:39 +0200 | [diff] [blame] | 23 | #include <haproxy/queue.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 24 | #include <haproxy/server-t.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 25 | #include <haproxy/tools.h> |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 27 | /* Return next tree node after <node> which must still be in the tree, or be |
| 28 | * NULL. Lookup wraps around the end to the beginning. If the next node is the |
| 29 | * same node, return NULL. This is designed to find a valid next node before |
| 30 | * deleting one from the tree. |
| 31 | */ |
| 32 | static inline struct eb32_node *chash_skip_node(struct eb_root *root, struct eb32_node *node) |
| 33 | { |
| 34 | struct eb32_node *stop = node; |
| 35 | |
| 36 | if (!node) |
| 37 | return NULL; |
| 38 | node = eb32_next(node); |
| 39 | if (!node) |
| 40 | node = eb32_first(root); |
| 41 | if (node == stop) |
| 42 | return NULL; |
| 43 | return node; |
| 44 | } |
| 45 | |
| 46 | /* Remove all of a server's entries from its tree. This may be used when |
| 47 | * setting a server down. |
| 48 | */ |
| 49 | static inline void chash_dequeue_srv(struct server *s) |
| 50 | { |
| 51 | while (s->lb_nodes_now > 0) { |
| 52 | if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway |
| 53 | s->lb_nodes_now = s->lb_nodes_tot; |
| 54 | s->lb_nodes_now--; |
| 55 | if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node) |
| 56 | s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last); |
| 57 | eb32_delete(&s->lb_nodes[s->lb_nodes_now].node); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* Adjust the number of entries of a server in its tree. The server must appear |
| 62 | * as many times as its weight indicates it. If it's there too often, we remove |
| 63 | * the last occurrences. If it's not there enough, we add more occurrences. To |
| 64 | * remove a server from the tree, normally call this with eweight=0. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 65 | * |
| 66 | * The server's lock and the lbprm's lock must be held. |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 67 | */ |
| 68 | static inline void chash_queue_dequeue_srv(struct server *s) |
| 69 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 70 | while (s->lb_nodes_now > s->next_eweight) { |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 71 | if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway |
| 72 | s->lb_nodes_now = s->lb_nodes_tot; |
| 73 | s->lb_nodes_now--; |
| 74 | if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node) |
| 75 | s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last); |
| 76 | eb32_delete(&s->lb_nodes[s->lb_nodes_now].node); |
| 77 | } |
| 78 | |
Olivier Houchard | f8eb8d5 | 2017-10-17 15:52:59 +0200 | [diff] [blame] | 79 | /* Attempt to increase the total number of nodes, if the user |
| 80 | * increased the weight beyond the original weight |
| 81 | */ |
| 82 | if (s->lb_nodes_tot < s->next_eweight) { |
Christopher Faulet | 0a52c17 | 2019-08-01 10:09:29 +0200 | [diff] [blame] | 83 | struct tree_occ *new_nodes; |
Olivier Houchard | f8eb8d5 | 2017-10-17 15:52:59 +0200 | [diff] [blame] | 84 | |
Christopher Faulet | 0a52c17 | 2019-08-01 10:09:29 +0200 | [diff] [blame] | 85 | /* First we need to remove all server's entries from its tree |
| 86 | * because the realloc will change all nodes pointers */ |
| 87 | chash_dequeue_srv(s); |
| 88 | |
| 89 | new_nodes = realloc(s->lb_nodes, s->next_eweight * sizeof(*new_nodes)); |
Olivier Houchard | f8eb8d5 | 2017-10-17 15:52:59 +0200 | [diff] [blame] | 90 | if (new_nodes) { |
| 91 | unsigned int j; |
| 92 | |
| 93 | s->lb_nodes = new_nodes; |
| 94 | memset(&s->lb_nodes[s->lb_nodes_tot], 0, |
| 95 | (s->next_eweight - s->lb_nodes_tot) * sizeof(*s->lb_nodes)); |
| 96 | for (j = s->lb_nodes_tot; j < s->next_eweight; j++) { |
| 97 | s->lb_nodes[j].server = s; |
| 98 | s->lb_nodes[j].node.key = full_hash(s->puid * SRV_EWGHT_RANGE + j); |
| 99 | } |
| 100 | s->lb_nodes_tot = s->next_eweight; |
| 101 | } |
| 102 | } |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 103 | while (s->lb_nodes_now < s->next_eweight) { |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 104 | if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway |
| 105 | break; |
| 106 | if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node) |
| 107 | s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last); |
| 108 | eb32_insert(s->lb_tree, &s->lb_nodes[s->lb_nodes_now].node); |
| 109 | s->lb_nodes_now++; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* This function updates the server trees according to server <srv>'s new |
| 114 | * state. It should be called when server <srv>'s status changes to down. |
| 115 | * It is not important whether the server was already down or not. It is not |
| 116 | * important either that the new state is completely down (the caller may not |
| 117 | * know all the variables of a server's state). |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 118 | * |
| 119 | * 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] | 120 | */ |
| 121 | static void chash_set_server_status_down(struct server *srv) |
| 122 | { |
| 123 | struct proxy *p = srv->proxy; |
| 124 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 125 | if (!srv_lb_status_changed(srv)) |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 126 | return; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 127 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 128 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 129 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 130 | if (srv_willbe_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 131 | goto out_update_state; |
| 132 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 133 | if (!srv_currently_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 134 | /* server was already down */ |
| 135 | goto out_update_backend; |
| 136 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 137 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 138 | p->lbprm.tot_wbck -= srv->cur_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 139 | p->srv_bck--; |
| 140 | |
| 141 | if (srv == p->lbprm.fbck) { |
| 142 | /* we lost the first backup server in a single-backup |
| 143 | * configuration, we must search another one. |
| 144 | */ |
| 145 | struct server *srv2 = p->lbprm.fbck; |
| 146 | do { |
| 147 | srv2 = srv2->next; |
| 148 | } while (srv2 && |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 149 | !((srv2->flags & SRV_F_BACKUP) && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 150 | srv_willbe_usable(srv2))); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 151 | p->lbprm.fbck = srv2; |
| 152 | } |
| 153 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 154 | p->lbprm.tot_wact -= srv->cur_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 155 | p->srv_act--; |
| 156 | } |
| 157 | |
| 158 | chash_dequeue_srv(srv); |
| 159 | |
| 160 | out_update_backend: |
| 161 | /* check/update tot_used, tot_weight */ |
| 162 | update_backend_weight(p); |
| 163 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 164 | srv_lb_commit_status(srv); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 165 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 166 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* This function updates the server trees according to server <srv>'s new |
| 170 | * state. It should be called when server <srv>'s status changes to up. |
| 171 | * It is not important whether the server was already down or not. It is not |
| 172 | * important either that the new state is completely UP (the caller may not |
| 173 | * know all the variables of a server's state). This function will not change |
| 174 | * the weight of a server which was already up. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 175 | * |
| 176 | * 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] | 177 | */ |
| 178 | static void chash_set_server_status_up(struct server *srv) |
| 179 | { |
| 180 | struct proxy *p = srv->proxy; |
| 181 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 182 | if (!srv_lb_status_changed(srv)) |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 183 | return; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 184 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 185 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 186 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 187 | if (!srv_willbe_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 188 | goto out_update_state; |
| 189 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 190 | if (srv_currently_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 191 | /* server was already up */ |
| 192 | goto out_update_backend; |
| 193 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 194 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 195 | p->lbprm.tot_wbck += srv->next_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 196 | p->srv_bck++; |
| 197 | |
| 198 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 199 | if (!p->lbprm.fbck) { |
| 200 | /* there was no backup server anymore */ |
| 201 | p->lbprm.fbck = srv; |
| 202 | } else { |
| 203 | /* we may have restored a backup server prior to fbck, |
| 204 | * in which case it should replace it. |
| 205 | */ |
| 206 | struct server *srv2 = srv; |
| 207 | do { |
| 208 | srv2 = srv2->next; |
| 209 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 210 | if (srv2) |
| 211 | p->lbprm.fbck = srv; |
| 212 | } |
| 213 | } |
| 214 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 215 | p->lbprm.tot_wact += srv->next_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 216 | p->srv_act++; |
| 217 | } |
| 218 | |
| 219 | /* note that eweight cannot be 0 here */ |
| 220 | chash_queue_dequeue_srv(srv); |
| 221 | |
| 222 | out_update_backend: |
| 223 | /* check/update tot_used, tot_weight */ |
| 224 | update_backend_weight(p); |
| 225 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 226 | srv_lb_commit_status(srv); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 227 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 228 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* This function must be called after an update to server <srv>'s effective |
| 232 | * weight. It may be called after a state change too. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 233 | * |
| 234 | * 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] | 235 | */ |
| 236 | static void chash_update_server_weight(struct server *srv) |
| 237 | { |
| 238 | int old_state, new_state; |
| 239 | struct proxy *p = srv->proxy; |
| 240 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 241 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 242 | return; |
| 243 | |
| 244 | /* If changing the server's weight changes its state, we simply apply |
| 245 | * the procedures we already have for status change. If the state |
| 246 | * remains down, the server is not in any tree, so it's as easy as |
| 247 | * updating its values. If the state remains up with different weights, |
| 248 | * there are some computations to perform to find a new place and |
| 249 | * possibly a new tree for this server. |
| 250 | */ |
| 251 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 252 | old_state = srv_currently_usable(srv); |
| 253 | new_state = srv_willbe_usable(srv); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 254 | |
| 255 | if (!old_state && !new_state) { |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 256 | srv_lb_commit_status(srv); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | else if (!old_state && new_state) { |
| 260 | chash_set_server_status_up(srv); |
| 261 | return; |
| 262 | } |
| 263 | else if (old_state && !new_state) { |
| 264 | chash_set_server_status_down(srv); |
| 265 | return; |
| 266 | } |
| 267 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 268 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 269 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 270 | /* only adjust the server's presence in the tree */ |
| 271 | chash_queue_dequeue_srv(srv); |
| 272 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 273 | if (srv->flags & SRV_F_BACKUP) |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 274 | p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 275 | else |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 276 | p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 277 | |
| 278 | update_backend_weight(p); |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 279 | srv_lb_commit_status(srv); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 280 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 281 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 285 | * This function implements the "Consistent Hashing with Bounded Loads" algorithm |
| 286 | * of Mirrokni, Thorup, and Zadimoghaddam (arxiv:1608.01350), adapted for use with |
| 287 | * unequal server weights. |
| 288 | */ |
| 289 | int chash_server_is_eligible(struct server *s) |
| 290 | { |
| 291 | /* The total number of slots to allocate is the total number of outstanding requests |
| 292 | * (including the one we're about to make) times the load-balance-factor, rounded up. |
| 293 | */ |
Willy Tarreau | 76e84f5 | 2019-01-14 16:50:58 +0100 | [diff] [blame] | 294 | 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] | 295 | unsigned slots_per_weight = tot_slots / s->proxy->lbprm.tot_weight; |
| 296 | unsigned remainder = tot_slots % s->proxy->lbprm.tot_weight; |
| 297 | |
| 298 | /* Allocate a whole number of slots per weight unit... */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 299 | unsigned slots = s->cur_eweight * slots_per_weight; |
Andrew Rodland | 4f88c63 | 2016-10-25 12:50:37 -0400 | [diff] [blame] | 300 | |
| 301 | /* And then distribute the rest among servers proportionally to their weight. */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 302 | 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] | 303 | - (s->cumulative_weight * remainder) / s->proxy->lbprm.tot_weight; |
| 304 | |
| 305 | /* But never leave a server with 0. */ |
| 306 | if (slots == 0) |
| 307 | slots = 1; |
| 308 | |
| 309 | return s->served < slots; |
| 310 | } |
| 311 | |
| 312 | /* |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 313 | * This function returns the running server from the CHASH tree, which is at |
| 314 | * the closest distance from the value of <hash>. Doing so ensures that even |
| 315 | * with a well imbalanced hash, if some servers are close to each other, they |
| 316 | * 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] | 317 | * 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] | 318 | * If no valid server is found, NULL is returned. |
Willy Tarreau | 63b3ae7 | 2021-06-01 16:58:31 +0200 | [diff] [blame] | 319 | * |
| 320 | * The lbprm's lock will be used in R/O mode. The server's lock is not used. |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 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 | 4b6e3c2 | 2020-10-17 20:15:49 +0200 | [diff] [blame] | 330 | HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 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: |
Willy Tarreau | 4b6e3c2 | 2020-10-17 20:15:49 +0200 | [diff] [blame] | 385 | HA_RWLOCK_RDUNLOCK(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. |
Willy Tarreau | 63b3ae7 | 2021-06-01 16:58:31 +0200 | [diff] [blame] | 391 | * |
| 392 | * The lbprm's lock will be used in R/W mode. The server's lock is not used. |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 403 | HA_RWLOCK_WRLOCK(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; |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 446 | if (!s->maxconn || (!s->queue.length && s->served < srv_dynamic_maxconn(s))) { |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 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: |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 462 | HA_RWLOCK_WRUNLOCK(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. |
Remi Tricot-Le Breton | 4764620 | 2021-05-19 16:40:28 +0200 | [diff] [blame] | 470 | * Return 0 in case of success, -1 in case of allocation failure. |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 471 | */ |
Remi Tricot-Le Breton | 4764620 | 2021-05-19 16:40:28 +0200 | [diff] [blame] | 472 | int chash_init_server_tree(struct proxy *p) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 473 | { |
| 474 | struct server *srv; |
| 475 | struct eb_root init_head = EB_ROOT; |
| 476 | int node; |
| 477 | |
| 478 | p->lbprm.set_server_status_up = chash_set_server_status_up; |
| 479 | p->lbprm.set_server_status_down = chash_set_server_status_down; |
| 480 | p->lbprm.update_server_eweight = chash_update_server_weight; |
| 481 | p->lbprm.server_take_conn = NULL; |
| 482 | p->lbprm.server_drop_conn = NULL; |
| 483 | |
| 484 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 485 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 486 | 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] | 487 | srv_lb_commit_status(srv); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | recount_servers(p); |
| 491 | update_backend_weight(p); |
| 492 | |
| 493 | p->lbprm.chash.act = init_head; |
| 494 | p->lbprm.chash.bck = init_head; |
| 495 | p->lbprm.chash.last = NULL; |
| 496 | |
| 497 | /* queue active and backup servers in two distinct groups */ |
| 498 | for (srv = p->srv; srv; srv = srv->next) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 499 | 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] | 500 | srv->lb_nodes_tot = srv->uweight * BE_WEIGHT_SCALE; |
| 501 | srv->lb_nodes_now = 0; |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 502 | srv->lb_nodes = calloc(srv->lb_nodes_tot, |
| 503 | sizeof(*srv->lb_nodes)); |
Remi Tricot-Le Breton | 4764620 | 2021-05-19 16:40:28 +0200 | [diff] [blame] | 504 | if (!srv->lb_nodes) { |
| 505 | ha_alert("failed to allocate lb_nodes for server %s.\n", srv->id); |
| 506 | return -1; |
| 507 | } |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 508 | for (node = 0; node < srv->lb_nodes_tot; node++) { |
| 509 | srv->lb_nodes[node].server = srv; |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 510 | 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] | 511 | } |
| 512 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 513 | if (srv_currently_usable(srv)) |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 514 | chash_queue_dequeue_srv(srv); |
| 515 | } |
Remi Tricot-Le Breton | 4764620 | 2021-05-19 16:40:28 +0200 | [diff] [blame] | 516 | return 0; |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 517 | } |