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