Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * First Available Server load balancing algorithm. |
| 3 | * |
Willy Tarreau | 64559c5 | 2012-04-07 09:08:45 +0200 | [diff] [blame] | 4 | * This file implements an algorithm which emerged during a discussion with |
| 5 | * Steen Larsen, initially inspired from Anshul Gandhi et.al.'s work now |
| 6 | * described as "packing" in section 3.5: |
| 7 | * |
| 8 | * http://reports-archive.adm.cs.cmu.edu/anon/2012/CMU-CS-12-109.pdf |
| 9 | * |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 10 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | */ |
| 18 | |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 19 | #include <import/eb32tree.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 20 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 21 | #include <haproxy/backend.h> |
Willy Tarreau | a55c454 | 2020-06-04 22:59:39 +0200 | [diff] [blame] | 22 | #include <haproxy/queue.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 23 | #include <haproxy/server-t.h> |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 24 | |
| 25 | |
| 26 | /* Remove a server from a tree. It must have previously been dequeued. This |
| 27 | * function is meant to be called when a server is going down or has its |
| 28 | * weight disabled. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 29 | * |
| 30 | * The server's lock and the lbprm's lock must be held. |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 31 | */ |
| 32 | static inline void fas_remove_from_tree(struct server *s) |
| 33 | { |
| 34 | s->lb_tree = NULL; |
| 35 | } |
| 36 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 37 | /* simply removes a server from a tree. |
| 38 | * |
Willy Tarreau | 6b96e0e | 2021-02-17 16:15:23 +0100 | [diff] [blame] | 39 | * The lbprm's lock must be held. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 40 | */ |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 41 | static inline void fas_dequeue_srv(struct server *s) |
| 42 | { |
| 43 | eb32_delete(&s->lb_node); |
| 44 | } |
| 45 | |
| 46 | /* Queue a server in its associated tree, assuming the weight is >0. |
| 47 | * Servers are sorted by unique ID so that we send all connections to the first |
| 48 | * available server in declaration order (or ID order) until its maxconn is |
| 49 | * reached. It is important to understand that the server weight is not used |
| 50 | * here. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 51 | * |
Willy Tarreau | 6b96e0e | 2021-02-17 16:15:23 +0100 | [diff] [blame] | 52 | * The lbprm's lock must be held. |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 53 | */ |
| 54 | static inline void fas_queue_srv(struct server *s) |
| 55 | { |
| 56 | s->lb_node.key = s->puid; |
| 57 | eb32_insert(s->lb_tree, &s->lb_node); |
| 58 | } |
| 59 | |
| 60 | /* Re-position the server in the FS tree after it has been assigned one |
| 61 | * connection or after it has released one. Note that it is possible that |
| 62 | * the server has been moved out of the tree due to failed health-checks. |
Willy Tarreau | 5941ef0 | 2021-06-18 18:29:25 +0200 | [diff] [blame] | 63 | * The lbprm's lock will be used. |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 64 | */ |
Willy Tarreau | 5941ef0 | 2021-06-18 18:29:25 +0200 | [diff] [blame] | 65 | static void fas_srv_reposition(struct server *s) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 66 | { |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 67 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Christopher Faulet | 16b2be9 | 2019-07-04 11:59:42 +0200 | [diff] [blame] | 68 | if (s->lb_tree) { |
| 69 | fas_dequeue_srv(s); |
| 70 | fas_queue_srv(s); |
| 71 | } |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 72 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* This function updates the server trees according to server <srv>'s new |
| 76 | * state. It should be called when server <srv>'s status changes to down. |
| 77 | * It is not important whether the server was already down or not. It is not |
| 78 | * important either that the new state is completely down (the caller may not |
| 79 | * know all the variables of a server's state). |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 80 | * |
| 81 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 82 | */ |
| 83 | static void fas_set_server_status_down(struct server *srv) |
| 84 | { |
| 85 | struct proxy *p = srv->proxy; |
| 86 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 87 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 88 | return; |
| 89 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 90 | if (srv_willbe_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 91 | goto out_update_state; |
| 92 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 93 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 94 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 95 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 96 | /* server was already down */ |
| 97 | goto out_update_backend; |
| 98 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 99 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 100 | p->lbprm.tot_wbck -= srv->cur_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 101 | p->srv_bck--; |
| 102 | |
| 103 | if (srv == p->lbprm.fbck) { |
| 104 | /* we lost the first backup server in a single-backup |
| 105 | * configuration, we must search another one. |
| 106 | */ |
| 107 | struct server *srv2 = p->lbprm.fbck; |
| 108 | do { |
| 109 | srv2 = srv2->next; |
| 110 | } while (srv2 && |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 111 | !((srv2->flags & SRV_F_BACKUP) && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 112 | srv_willbe_usable(srv2))); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 113 | p->lbprm.fbck = srv2; |
| 114 | } |
| 115 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 116 | p->lbprm.tot_wact -= srv->cur_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 117 | p->srv_act--; |
| 118 | } |
| 119 | |
| 120 | fas_dequeue_srv(srv); |
| 121 | fas_remove_from_tree(srv); |
| 122 | |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 123 | out_update_backend: |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 124 | /* check/update tot_used, tot_weight */ |
| 125 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 126 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 127 | |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 128 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 129 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* This function updates the server trees according to server <srv>'s new |
| 133 | * state. It should be called when server <srv>'s status changes to up. |
| 134 | * It is not important whether the server was already down or not. It is not |
| 135 | * important either that the new state is completely UP (the caller may not |
| 136 | * know all the variables of a server's state). This function will not change |
| 137 | * the weight of a server which was already up. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 138 | * |
| 139 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 140 | */ |
| 141 | static void fas_set_server_status_up(struct server *srv) |
| 142 | { |
| 143 | struct proxy *p = srv->proxy; |
| 144 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 145 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 146 | return; |
| 147 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 148 | if (!srv_willbe_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 149 | goto out_update_state; |
| 150 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 151 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 152 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 153 | if (srv_currently_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 154 | /* server was already up */ |
| 155 | goto out_update_backend; |
| 156 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 157 | if (srv->flags & SRV_F_BACKUP) { |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 158 | srv->lb_tree = &p->lbprm.fas.bck; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 159 | p->lbprm.tot_wbck += srv->next_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 160 | p->srv_bck++; |
| 161 | |
| 162 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 163 | if (!p->lbprm.fbck) { |
| 164 | /* there was no backup server anymore */ |
| 165 | p->lbprm.fbck = srv; |
| 166 | } else { |
| 167 | /* we may have restored a backup server prior to fbck, |
| 168 | * in which case it should replace it. |
| 169 | */ |
| 170 | struct server *srv2 = srv; |
| 171 | do { |
| 172 | srv2 = srv2->next; |
| 173 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 174 | if (srv2) |
| 175 | p->lbprm.fbck = srv; |
| 176 | } |
| 177 | } |
| 178 | } else { |
| 179 | srv->lb_tree = &p->lbprm.fas.act; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 180 | p->lbprm.tot_wact += srv->next_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 181 | p->srv_act++; |
| 182 | } |
| 183 | |
| 184 | /* note that eweight cannot be 0 here */ |
| 185 | fas_queue_srv(srv); |
| 186 | |
| 187 | out_update_backend: |
| 188 | /* check/update tot_used, tot_weight */ |
| 189 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 190 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 191 | |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 192 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 193 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* This function must be called after an update to server <srv>'s effective |
| 197 | * weight. It may be called after a state change too. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 198 | * |
| 199 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 200 | */ |
| 201 | static void fas_update_server_weight(struct server *srv) |
| 202 | { |
| 203 | int old_state, new_state; |
| 204 | struct proxy *p = srv->proxy; |
| 205 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 206 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 207 | return; |
| 208 | |
| 209 | /* If changing the server's weight changes its state, we simply apply |
| 210 | * the procedures we already have for status change. If the state |
| 211 | * remains down, the server is not in any tree, so it's as easy as |
| 212 | * updating its values. If the state remains up with different weights, |
| 213 | * there are some computations to perform to find a new place and |
| 214 | * possibly a new tree for this server. |
| 215 | */ |
| 216 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 217 | old_state = srv_currently_usable(srv); |
| 218 | new_state = srv_willbe_usable(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 219 | |
| 220 | if (!old_state && !new_state) { |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 221 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | else if (!old_state && new_state) { |
| 225 | fas_set_server_status_up(srv); |
| 226 | return; |
| 227 | } |
| 228 | else if (old_state && !new_state) { |
| 229 | fas_set_server_status_down(srv); |
| 230 | return; |
| 231 | } |
| 232 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 233 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 234 | |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 235 | if (srv->lb_tree) |
| 236 | fas_dequeue_srv(srv); |
| 237 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 238 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 239 | p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 240 | srv->lb_tree = &p->lbprm.fas.bck; |
| 241 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 242 | p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 243 | srv->lb_tree = &p->lbprm.fas.act; |
| 244 | } |
| 245 | |
| 246 | fas_queue_srv(srv); |
| 247 | |
| 248 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 249 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 250 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 251 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* This function is responsible for building the trees in case of fast |
| 255 | * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to |
| 256 | * uweight ratio. Both active and backup groups are initialized. |
| 257 | */ |
| 258 | void fas_init_server_tree(struct proxy *p) |
| 259 | { |
| 260 | struct server *srv; |
| 261 | struct eb_root init_head = EB_ROOT; |
| 262 | |
| 263 | p->lbprm.set_server_status_up = fas_set_server_status_up; |
| 264 | p->lbprm.set_server_status_down = fas_set_server_status_down; |
| 265 | p->lbprm.update_server_eweight = fas_update_server_weight; |
| 266 | p->lbprm.server_take_conn = fas_srv_reposition; |
| 267 | p->lbprm.server_drop_conn = fas_srv_reposition; |
| 268 | |
| 269 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 270 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 271 | 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] | 272 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | recount_servers(p); |
| 276 | update_backend_weight(p); |
| 277 | |
| 278 | p->lbprm.fas.act = init_head; |
| 279 | p->lbprm.fas.bck = init_head; |
| 280 | |
| 281 | /* queue active and backup servers in two distinct groups */ |
| 282 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 283 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 284 | continue; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 285 | srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fas.bck : &p->lbprm.fas.act; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 286 | fas_queue_srv(srv); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* Return next server from the FS tree in backend <p>. If the tree is empty, |
| 291 | * return NULL. Saturated servers are skipped. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 292 | * |
Willy Tarreau | 63b3ae7 | 2021-06-01 16:58:31 +0200 | [diff] [blame] | 293 | * The lbprm's lock will be used. The server's lock is not used. |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 294 | */ |
| 295 | struct server *fas_get_next_server(struct proxy *p, struct server *srvtoavoid) |
| 296 | { |
| 297 | struct server *srv, *avoided; |
| 298 | struct eb32_node *node; |
| 299 | |
| 300 | srv = avoided = NULL; |
| 301 | |
Willy Tarreau | f76a21f | 2020-10-17 19:45:42 +0200 | [diff] [blame] | 302 | HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 303 | if (p->srv_act) |
| 304 | node = eb32_first(&p->lbprm.fas.act); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 305 | else if (p->lbprm.fbck) { |
| 306 | srv = p->lbprm.fbck; |
| 307 | goto out; |
| 308 | } |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 309 | else if (p->srv_bck) |
| 310 | node = eb32_first(&p->lbprm.fas.bck); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 311 | else { |
| 312 | srv = NULL; |
| 313 | goto out; |
| 314 | } |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 315 | |
| 316 | while (node) { |
| 317 | /* OK, we have a server. However, it may be saturated, in which |
| 318 | * case we don't want to reconsider it for now, so we'll simply |
| 319 | * skip it. Same if it's the server we try to avoid, in which |
| 320 | * case we simply remember it for later use if needed. |
| 321 | */ |
| 322 | struct server *s; |
| 323 | |
| 324 | s = eb32_entry(node, struct server, lb_node); |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 325 | if (!s->maxconn || (!s->queue.length && s->served < srv_dynamic_maxconn(s))) { |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 326 | if (s != srvtoavoid) { |
| 327 | srv = s; |
| 328 | break; |
| 329 | } |
| 330 | avoided = s; |
| 331 | } |
| 332 | node = eb32_next(node); |
| 333 | } |
| 334 | |
| 335 | if (!srv) |
| 336 | srv = avoided; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 337 | out: |
Willy Tarreau | f76a21f | 2020-10-17 19:45:42 +0200 | [diff] [blame] | 338 | HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 339 | return srv; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /* |
| 344 | * Local variables: |
| 345 | * c-indent-level: 8 |
| 346 | * c-basic-offset: 8 |
| 347 | * End: |
| 348 | */ |