Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Fast Weighted Round Robin load balancing algorithm. |
| 3 | * |
| 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 13 | #include <import/eb32tree.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 14 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 15 | #include <haproxy/backend.h> |
Willy Tarreau | a55c454 | 2020-06-04 22:59:39 +0200 | [diff] [blame] | 16 | #include <haproxy/queue.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 17 | #include <haproxy/server-t.h> |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 19 | |
| 20 | static inline void fwrr_remove_from_tree(struct server *s); |
| 21 | static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s); |
| 22 | static inline void fwrr_dequeue_srv(struct server *s); |
| 23 | static void fwrr_get_srv(struct server *s); |
| 24 | static void fwrr_queue_srv(struct server *s); |
| 25 | |
| 26 | |
| 27 | /* This function updates the server trees according to server <srv>'s new |
| 28 | * state. It should be called when server <srv>'s status changes to down. |
| 29 | * It is not important whether the server was already down or not. It is not |
| 30 | * important either that the new state is completely down (the caller may not |
| 31 | * know all the variables of a server's state). |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 32 | * |
| 33 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 34 | */ |
| 35 | static void fwrr_set_server_status_down(struct server *srv) |
| 36 | { |
| 37 | struct proxy *p = srv->proxy; |
| 38 | struct fwrr_group *grp; |
| 39 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 40 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 41 | return; |
| 42 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 43 | if (srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 44 | goto out_update_state; |
| 45 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 46 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 47 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 48 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 49 | /* server was already down */ |
| 50 | goto out_update_backend; |
| 51 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 52 | grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 53 | grp->next_weight -= srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 54 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 55 | if (srv->flags & SRV_F_BACKUP) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 56 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 57 | p->srv_bck--; |
| 58 | |
| 59 | if (srv == p->lbprm.fbck) { |
| 60 | /* we lost the first backup server in a single-backup |
| 61 | * configuration, we must search another one. |
| 62 | */ |
| 63 | struct server *srv2 = p->lbprm.fbck; |
| 64 | do { |
| 65 | srv2 = srv2->next; |
| 66 | } while (srv2 && |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 67 | !((srv2->flags & SRV_F_BACKUP) && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 68 | srv_willbe_usable(srv2))); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 69 | p->lbprm.fbck = srv2; |
| 70 | } |
| 71 | } else { |
| 72 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 73 | p->srv_act--; |
| 74 | } |
| 75 | |
| 76 | fwrr_dequeue_srv(srv); |
| 77 | fwrr_remove_from_tree(srv); |
| 78 | |
| 79 | out_update_backend: |
| 80 | /* check/update tot_used, tot_weight */ |
| 81 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 82 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 84 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 85 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* This function updates the server trees according to server <srv>'s new |
| 89 | * state. It should be called when server <srv>'s status changes to up. |
| 90 | * It is not important whether the server was already down or not. It is not |
| 91 | * important either that the new state is completely UP (the caller may not |
| 92 | * know all the variables of a server's state). This function will not change |
| 93 | * the weight of a server which was already up. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 94 | * |
| 95 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 96 | */ |
| 97 | static void fwrr_set_server_status_up(struct server *srv) |
| 98 | { |
| 99 | struct proxy *p = srv->proxy; |
| 100 | struct fwrr_group *grp; |
| 101 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 102 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 103 | return; |
| 104 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 105 | if (!srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 106 | goto out_update_state; |
| 107 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 108 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 109 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 110 | if (srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 111 | /* server was already up */ |
| 112 | goto out_update_backend; |
| 113 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 114 | grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 115 | grp->next_weight += srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 117 | if (srv->flags & SRV_F_BACKUP) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 118 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 119 | p->srv_bck++; |
| 120 | |
| 121 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 122 | if (!p->lbprm.fbck) { |
| 123 | /* there was no backup server anymore */ |
| 124 | p->lbprm.fbck = srv; |
| 125 | } else { |
| 126 | /* we may have restored a backup server prior to fbck, |
| 127 | * in which case it should replace it. |
| 128 | */ |
| 129 | struct server *srv2 = srv; |
| 130 | do { |
| 131 | srv2 = srv2->next; |
| 132 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 133 | if (srv2) |
| 134 | p->lbprm.fbck = srv; |
| 135 | } |
| 136 | } |
| 137 | } else { |
| 138 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 139 | p->srv_act++; |
| 140 | } |
| 141 | |
| 142 | /* note that eweight cannot be 0 here */ |
| 143 | fwrr_get_srv(srv); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 144 | srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 145 | fwrr_queue_srv(srv); |
| 146 | |
| 147 | out_update_backend: |
| 148 | /* check/update tot_used, tot_weight */ |
| 149 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 150 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 151 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 152 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 153 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* This function must be called after an update to server <srv>'s effective |
| 157 | * weight. It may be called after a state change too. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 158 | * |
| 159 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 160 | */ |
| 161 | static void fwrr_update_server_weight(struct server *srv) |
| 162 | { |
| 163 | int old_state, new_state; |
| 164 | struct proxy *p = srv->proxy; |
| 165 | struct fwrr_group *grp; |
| 166 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 167 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 168 | return; |
| 169 | |
| 170 | /* If changing the server's weight changes its state, we simply apply |
| 171 | * the procedures we already have for status change. If the state |
| 172 | * remains down, the server is not in any tree, so it's as easy as |
| 173 | * updating its values. If the state remains up with different weights, |
| 174 | * there are some computations to perform to find a new place and |
| 175 | * possibly a new tree for this server. |
| 176 | */ |
| 177 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 178 | old_state = srv_currently_usable(srv); |
| 179 | new_state = srv_willbe_usable(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 180 | |
| 181 | if (!old_state && !new_state) { |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 182 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | else if (!old_state && new_state) { |
| 186 | fwrr_set_server_status_up(srv); |
| 187 | return; |
| 188 | } |
| 189 | else if (old_state && !new_state) { |
| 190 | fwrr_set_server_status_down(srv); |
| 191 | return; |
| 192 | } |
| 193 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 194 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 195 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 196 | grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 197 | grp->next_weight = grp->next_weight - srv->cur_eweight + srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 198 | |
| 199 | p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight; |
| 200 | p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight; |
| 201 | |
| 202 | if (srv->lb_tree == grp->init) { |
| 203 | fwrr_dequeue_srv(srv); |
| 204 | fwrr_queue_by_weight(grp->init, srv); |
| 205 | } |
| 206 | else if (!srv->lb_tree) { |
| 207 | /* FIXME: server was down. This is not possible right now but |
| 208 | * may be needed soon for slowstart or graceful shutdown. |
| 209 | */ |
| 210 | fwrr_dequeue_srv(srv); |
| 211 | fwrr_get_srv(srv); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 212 | srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 213 | fwrr_queue_srv(srv); |
| 214 | } else { |
| 215 | /* The server is either active or in the next queue. If it's |
| 216 | * still in the active queue and it has not consumed all of its |
| 217 | * places, let's adjust its next position. |
| 218 | */ |
| 219 | fwrr_get_srv(srv); |
| 220 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 221 | if (srv->next_eweight > 0) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 222 | int prev_next = srv->npos; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 223 | int step = grp->next_weight / srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 224 | |
| 225 | srv->npos = srv->lpos + step; |
| 226 | srv->rweight = 0; |
| 227 | |
| 228 | if (srv->npos > prev_next) |
| 229 | srv->npos = prev_next; |
| 230 | if (srv->npos < grp->curr_pos + 2) |
| 231 | srv->npos = grp->curr_pos + step; |
| 232 | } else { |
| 233 | /* push it into the next tree */ |
| 234 | srv->npos = grp->curr_pos + grp->curr_weight; |
| 235 | } |
| 236 | |
| 237 | fwrr_dequeue_srv(srv); |
| 238 | fwrr_queue_srv(srv); |
| 239 | } |
| 240 | |
| 241 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 242 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 243 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 244 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Remove a server from a tree. It must have previously been dequeued. This |
| 248 | * function is meant to be called when a server is going down or has its |
| 249 | * weight disabled. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 250 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 251 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 252 | */ |
| 253 | static inline void fwrr_remove_from_tree(struct server *s) |
| 254 | { |
| 255 | s->lb_tree = NULL; |
| 256 | } |
| 257 | |
| 258 | /* Queue a server in the weight tree <root>, assuming the weight is >0. |
| 259 | * We want to sort them by inverted weights, because we need to place |
| 260 | * heavy servers first in order to get a smooth distribution. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 261 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 262 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 263 | */ |
| 264 | static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s) |
| 265 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 266 | s->lb_node.key = SRV_EWGHT_MAX - s->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 267 | eb32_insert(root, &s->lb_node); |
| 268 | s->lb_tree = root; |
| 269 | } |
| 270 | |
| 271 | /* This function is responsible for building the weight trees in case of fast |
| 272 | * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight |
| 273 | * ratio. Both active and backup groups are initialized. |
| 274 | */ |
| 275 | void fwrr_init_server_groups(struct proxy *p) |
| 276 | { |
| 277 | struct server *srv; |
| 278 | struct eb_root init_head = EB_ROOT; |
| 279 | |
| 280 | p->lbprm.set_server_status_up = fwrr_set_server_status_up; |
| 281 | p->lbprm.set_server_status_down = fwrr_set_server_status_down; |
| 282 | p->lbprm.update_server_eweight = fwrr_update_server_weight; |
| 283 | |
| 284 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 285 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 286 | 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] | 287 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | recount_servers(p); |
| 291 | update_backend_weight(p); |
| 292 | |
| 293 | /* prepare the active servers group */ |
| 294 | p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight = |
| 295 | p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact; |
| 296 | p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 = |
| 297 | p->lbprm.fwrr.act.t1 = init_head; |
| 298 | p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0; |
| 299 | p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1; |
| 300 | |
| 301 | /* prepare the backup servers group */ |
| 302 | p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight = |
| 303 | p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck; |
| 304 | p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 = |
| 305 | p->lbprm.fwrr.bck.t1 = init_head; |
| 306 | p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0; |
| 307 | p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1; |
| 308 | |
| 309 | /* queue active and backup servers in two distinct groups */ |
| 310 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 311 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 312 | continue; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 313 | fwrr_queue_by_weight((srv->flags & SRV_F_BACKUP) ? |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 314 | p->lbprm.fwrr.bck.init : |
| 315 | p->lbprm.fwrr.act.init, |
| 316 | srv); |
| 317 | } |
| 318 | } |
| 319 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 320 | /* simply removes a server from a weight tree. |
| 321 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 322 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 323 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 324 | static inline void fwrr_dequeue_srv(struct server *s) |
| 325 | { |
| 326 | eb32_delete(&s->lb_node); |
| 327 | } |
| 328 | |
| 329 | /* queues a server into the appropriate group and tree depending on its |
| 330 | * backup status, and ->npos. If the server is disabled, simply assign |
| 331 | * it to the NULL tree. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 332 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 333 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 334 | */ |
| 335 | static void fwrr_queue_srv(struct server *s) |
| 336 | { |
| 337 | struct proxy *p = s->proxy; |
| 338 | struct fwrr_group *grp; |
| 339 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 340 | grp = (s->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 341 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 342 | /* Delay everything which does not fit into the window and everything |
| 343 | * which does not fit into the theorical new window. |
| 344 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 345 | if (!srv_willbe_usable(s)) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 346 | fwrr_remove_from_tree(s); |
| 347 | } |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 348 | else if (s->next_eweight <= 0 || |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 349 | s->npos >= 2 * grp->curr_weight || |
| 350 | s->npos >= grp->curr_weight + grp->next_weight) { |
| 351 | /* put into next tree, and readjust npos in case we could |
| 352 | * finally take this back to current. */ |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 353 | s->npos -= grp->curr_weight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 354 | fwrr_queue_by_weight(grp->next, s); |
| 355 | } |
| 356 | else { |
| 357 | /* The sorting key is stored in units of s->npos * user_weight |
| 358 | * in order to avoid overflows. As stated in backend.h, the |
| 359 | * lower the scale, the rougher the weights modulation, and the |
| 360 | * higher the scale, the lower the number of servers without |
| 361 | * overflow. With this formula, the result is always positive, |
Godbach | a34bdc0 | 2013-07-22 07:44:53 +0800 | [diff] [blame] | 362 | * so we can use eb32_insert(). |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 363 | */ |
| 364 | s->lb_node.key = SRV_UWGHT_RANGE * s->npos + |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 365 | (unsigned)(SRV_EWGHT_MAX + s->rweight - s->next_eweight) / BE_WEIGHT_SCALE; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 366 | |
| 367 | eb32_insert(&grp->curr, &s->lb_node); |
| 368 | s->lb_tree = &grp->curr; |
| 369 | } |
| 370 | } |
| 371 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 372 | /* prepares a server when extracting it from the "init" tree. |
| 373 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 374 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 375 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 376 | static inline void fwrr_get_srv_init(struct server *s) |
| 377 | { |
| 378 | s->npos = s->rweight = 0; |
| 379 | } |
| 380 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 381 | /* prepares a server when extracting it from the "next" tree. |
| 382 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 383 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 384 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 385 | static inline void fwrr_get_srv_next(struct server *s) |
| 386 | { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 387 | struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ? |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 388 | &s->proxy->lbprm.fwrr.bck : |
| 389 | &s->proxy->lbprm.fwrr.act; |
| 390 | |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 391 | s->npos += grp->curr_weight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 394 | /* prepares a server when it was marked down. |
| 395 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 396 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 397 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 398 | static inline void fwrr_get_srv_down(struct server *s) |
| 399 | { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 400 | struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ? |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 401 | &s->proxy->lbprm.fwrr.bck : |
| 402 | &s->proxy->lbprm.fwrr.act; |
| 403 | |
| 404 | s->npos = grp->curr_pos; |
| 405 | } |
| 406 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 407 | /* prepares a server when extracting it from its tree. |
| 408 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 409 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 410 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 411 | static void fwrr_get_srv(struct server *s) |
| 412 | { |
| 413 | struct proxy *p = s->proxy; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 414 | struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ? |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 415 | &p->lbprm.fwrr.bck : |
| 416 | &p->lbprm.fwrr.act; |
| 417 | |
| 418 | if (s->lb_tree == grp->init) { |
| 419 | fwrr_get_srv_init(s); |
| 420 | } |
| 421 | else if (s->lb_tree == grp->next) { |
| 422 | fwrr_get_srv_next(s); |
| 423 | } |
| 424 | else if (s->lb_tree == NULL) { |
| 425 | fwrr_get_srv_down(s); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty |
| 430 | * when this happens, and "next" filled with servers sorted by weights. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 431 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 432 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 433 | */ |
| 434 | static inline void fwrr_switch_trees(struct fwrr_group *grp) |
| 435 | { |
| 436 | struct eb_root *swap; |
| 437 | swap = grp->init; |
| 438 | grp->init = grp->next; |
| 439 | grp->next = swap; |
| 440 | grp->curr_weight = grp->next_weight; |
| 441 | grp->curr_pos = grp->curr_weight; |
| 442 | } |
| 443 | |
| 444 | /* return next server from the current tree in FWRR group <grp>, or a server |
| 445 | * from the "init" tree if appropriate. If both trees are empty, return NULL. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 446 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 447 | * The lbprm's lock must be held. The server's lock is not used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 448 | */ |
| 449 | static struct server *fwrr_get_server_from_group(struct fwrr_group *grp) |
| 450 | { |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 451 | struct eb32_node *node1; |
| 452 | struct eb32_node *node2; |
| 453 | struct server *s1 = NULL; |
| 454 | struct server *s2 = NULL; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 455 | |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 456 | node1 = eb32_first(&grp->curr); |
| 457 | if (node1) { |
| 458 | s1 = eb32_entry(node1, struct server, lb_node); |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 459 | if (s1->cur_eweight && s1->npos <= grp->curr_pos) |
| 460 | return s1; |
| 461 | } |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 462 | |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 463 | /* Either we have no server left, or we have a hole. We'll look in the |
| 464 | * init tree or a better proposal. At this point, if <s1> is non-null, |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 465 | * it is guaranteed to remain available as the tree is locked. |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 466 | */ |
| 467 | node2 = eb32_first(grp->init); |
| 468 | if (node2) { |
| 469 | s2 = eb32_entry(node2, struct server, lb_node); |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 470 | if (s2->cur_eweight) { |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 471 | fwrr_get_srv_init(s2); |
| 472 | return s2; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 473 | } |
| 474 | } |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 475 | return s1; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 478 | /* Computes next position of server <s> in the group. Nothing is done if <s> |
| 479 | * has a zero weight. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 480 | * |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 481 | * The lbprm's lock must be held to protect lpos/npos/rweight. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 482 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 483 | static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s) |
| 484 | { |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 485 | unsigned int eweight = *(volatile unsigned int *)&s->cur_eweight; |
| 486 | |
| 487 | if (!eweight) |
| 488 | return; |
| 489 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 490 | if (!s->npos) { |
| 491 | /* first time ever for this server */ |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 492 | s->npos = grp->curr_pos; |
| 493 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 494 | |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 495 | s->lpos = s->npos; |
| 496 | s->npos += grp->next_weight / eweight; |
| 497 | s->rweight += grp->next_weight % eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 498 | |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 499 | if (s->rweight >= eweight) { |
| 500 | s->rweight -= eweight; |
| 501 | s->npos++; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | /* Return next server from the current tree in backend <p>, or a server from |
| 506 | * the init tree if appropriate. If both trees are empty, return NULL. |
| 507 | * Saturated servers are skipped and requeued. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 508 | * |
Willy Tarreau | 63b3ae7 | 2021-06-01 16:58:31 +0200 | [diff] [blame] | 509 | * The lbprm's lock will be used in R/W mode. The server's lock is not used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 510 | */ |
| 511 | struct server *fwrr_get_next_server(struct proxy *p, struct server *srvtoavoid) |
| 512 | { |
| 513 | struct server *srv, *full, *avoided; |
| 514 | struct fwrr_group *grp; |
| 515 | int switched; |
| 516 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 517 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 518 | if (p->srv_act) |
| 519 | grp = &p->lbprm.fwrr.act; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 520 | else if (p->lbprm.fbck) { |
| 521 | srv = p->lbprm.fbck; |
| 522 | goto out; |
| 523 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 524 | else if (p->srv_bck) |
| 525 | grp = &p->lbprm.fwrr.bck; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 526 | else { |
| 527 | srv = NULL; |
| 528 | goto out; |
| 529 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 530 | |
| 531 | switched = 0; |
| 532 | avoided = NULL; |
| 533 | full = NULL; /* NULL-terminated list of saturated servers */ |
| 534 | while (1) { |
| 535 | /* if we see an empty group, let's first try to collect weights |
| 536 | * which might have recently changed. |
| 537 | */ |
| 538 | if (!grp->curr_weight) |
| 539 | grp->curr_pos = grp->curr_weight = grp->next_weight; |
| 540 | |
| 541 | /* get first server from the "current" tree. When the end of |
| 542 | * the tree is reached, we may have to switch, but only once. |
| 543 | */ |
| 544 | while (1) { |
| 545 | srv = fwrr_get_server_from_group(grp); |
| 546 | if (srv) |
| 547 | break; |
| 548 | if (switched) { |
| 549 | if (avoided) { |
| 550 | srv = avoided; |
Willy Tarreau | b6195ef | 2019-05-27 10:17:05 +0200 | [diff] [blame] | 551 | goto take_this_one; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 552 | } |
| 553 | goto requeue_servers; |
| 554 | } |
| 555 | switched = 1; |
| 556 | fwrr_switch_trees(grp); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 559 | /* OK, we have a server. However, it may be saturated, in which |
| 560 | * case we don't want to reconsider it for now. We'll update |
| 561 | * its position and dequeue it anyway, so that we can move it |
| 562 | * to a better place afterwards. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 563 | */ |
| 564 | fwrr_update_position(grp, srv); |
| 565 | fwrr_dequeue_srv(srv); |
| 566 | grp->curr_pos++; |
| 567 | if (!srv->maxconn || (!srv->nbpend && srv->served < srv_dynamic_maxconn(srv))) { |
| 568 | /* make sure it is not the server we are trying to exclude... */ |
| 569 | if (srv != srvtoavoid || avoided) |
| 570 | break; |
| 571 | |
| 572 | avoided = srv; /* ...but remember that is was selected yet avoided */ |
| 573 | } |
| 574 | |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 575 | /* the server is saturated or avoided, let's chain it for later reinsertion. |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 576 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 577 | srv->next_full = full; |
| 578 | full = srv; |
| 579 | } |
| 580 | |
Willy Tarreau | b6195ef | 2019-05-27 10:17:05 +0200 | [diff] [blame] | 581 | take_this_one: |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 582 | /* OK, we got the best server, let's update it */ |
| 583 | fwrr_queue_srv(srv); |
| 584 | |
| 585 | requeue_servers: |
| 586 | /* Requeue all extracted servers. If full==srv then it was |
Willy Tarreau | 9df86f9 | 2019-04-16 11:21:14 +0200 | [diff] [blame] | 587 | * avoided (unsuccessfully) and chained, omit it now. The |
| 588 | * only way to get there is by having <avoided>==NULL or |
| 589 | * <avoided>==<srv>. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 590 | */ |
| 591 | if (unlikely(full != NULL)) { |
| 592 | if (switched) { |
| 593 | /* the tree has switched, requeue all extracted servers |
| 594 | * into "init", because their place was lost, and only |
| 595 | * their weight matters. |
| 596 | */ |
| 597 | do { |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 598 | if (likely(full != srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 599 | fwrr_queue_by_weight(grp->init, full); |
| 600 | full = full->next_full; |
| 601 | } while (full); |
| 602 | } else { |
| 603 | /* requeue all extracted servers just as if they were consumed |
| 604 | * so that they regain their expected place. |
| 605 | */ |
| 606 | do { |
Willy Tarreau | 274ba67 | 2019-04-24 10:48:00 +0200 | [diff] [blame] | 607 | if (likely(full != srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 608 | fwrr_queue_srv(full); |
| 609 | full = full->next_full; |
| 610 | } while (full); |
| 611 | } |
| 612 | } |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 613 | out: |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 614 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 615 | return srv; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Local variables: |
| 620 | * c-indent-level: 8 |
| 621 | * c-basic-offset: 8 |
| 622 | * End: |
| 623 | */ |