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