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 | 7a41340 | 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 | 7a41340 | 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 | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 63 | * |
Willy Tarreau | 2b43700 | 2021-02-17 16:01:37 +0100 | [diff] [blame] | 64 | * <locked> must reflect the server's lock ownership. The lbprm's lock will |
| 65 | * be used. |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 66 | */ |
Willy Tarreau | 2b43700 | 2021-02-17 16:01:37 +0100 | [diff] [blame] | 67 | static void fas_srv_reposition(struct server *s, int locked) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 68 | { |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 69 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Christopher Faulet | 16b2be9 | 2019-07-04 11:59:42 +0200 | [diff] [blame] | 70 | if (s->lb_tree) { |
| 71 | fas_dequeue_srv(s); |
| 72 | fas_queue_srv(s); |
| 73 | } |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 74 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* This function updates the server trees according to server <srv>'s new |
| 78 | * state. It should be called when server <srv>'s status changes to down. |
| 79 | * It is not important whether the server was already down or not. It is not |
| 80 | * important either that the new state is completely down (the caller may not |
| 81 | * know all the variables of a server's state). |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 82 | * |
| 83 | * 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] | 84 | */ |
| 85 | static void fas_set_server_status_down(struct server *srv) |
| 86 | { |
| 87 | struct proxy *p = srv->proxy; |
| 88 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 89 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 90 | return; |
| 91 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 92 | if (srv_willbe_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 93 | goto out_update_state; |
| 94 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 95 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 96 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 97 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 98 | /* server was already down */ |
| 99 | goto out_update_backend; |
| 100 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 101 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 102 | p->lbprm.tot_wbck -= srv->cur_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 103 | p->srv_bck--; |
| 104 | |
| 105 | if (srv == p->lbprm.fbck) { |
| 106 | /* we lost the first backup server in a single-backup |
| 107 | * configuration, we must search another one. |
| 108 | */ |
| 109 | struct server *srv2 = p->lbprm.fbck; |
| 110 | do { |
| 111 | srv2 = srv2->next; |
| 112 | } while (srv2 && |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 113 | !((srv2->flags & SRV_F_BACKUP) && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 114 | srv_willbe_usable(srv2))); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 115 | p->lbprm.fbck = srv2; |
| 116 | } |
| 117 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 118 | p->lbprm.tot_wact -= srv->cur_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 119 | p->srv_act--; |
| 120 | } |
| 121 | |
| 122 | fas_dequeue_srv(srv); |
| 123 | fas_remove_from_tree(srv); |
| 124 | |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 125 | out_update_backend: |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 126 | /* check/update tot_used, tot_weight */ |
| 127 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 128 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 130 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 131 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* This function updates the server trees according to server <srv>'s new |
| 135 | * state. It should be called when server <srv>'s status changes to up. |
| 136 | * It is not important whether the server was already down or not. It is not |
| 137 | * important either that the new state is completely UP (the caller may not |
| 138 | * know all the variables of a server's state). This function will not change |
| 139 | * the weight of a server which was already up. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 140 | * |
| 141 | * 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] | 142 | */ |
| 143 | static void fas_set_server_status_up(struct server *srv) |
| 144 | { |
| 145 | struct proxy *p = srv->proxy; |
| 146 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 147 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 148 | return; |
| 149 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 150 | if (!srv_willbe_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 151 | goto out_update_state; |
| 152 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 153 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 154 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 155 | if (srv_currently_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 156 | /* server was already up */ |
| 157 | goto out_update_backend; |
| 158 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 159 | if (srv->flags & SRV_F_BACKUP) { |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 160 | srv->lb_tree = &p->lbprm.fas.bck; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 161 | p->lbprm.tot_wbck += srv->next_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 162 | p->srv_bck++; |
| 163 | |
| 164 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 165 | if (!p->lbprm.fbck) { |
| 166 | /* there was no backup server anymore */ |
| 167 | p->lbprm.fbck = srv; |
| 168 | } else { |
| 169 | /* we may have restored a backup server prior to fbck, |
| 170 | * in which case it should replace it. |
| 171 | */ |
| 172 | struct server *srv2 = srv; |
| 173 | do { |
| 174 | srv2 = srv2->next; |
| 175 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 176 | if (srv2) |
| 177 | p->lbprm.fbck = srv; |
| 178 | } |
| 179 | } |
| 180 | } else { |
| 181 | srv->lb_tree = &p->lbprm.fas.act; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 182 | p->lbprm.tot_wact += srv->next_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 183 | p->srv_act++; |
| 184 | } |
| 185 | |
| 186 | /* note that eweight cannot be 0 here */ |
| 187 | fas_queue_srv(srv); |
| 188 | |
| 189 | out_update_backend: |
| 190 | /* check/update tot_used, tot_weight */ |
| 191 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 192 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 193 | |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 194 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 195 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* This function must be called after an update to server <srv>'s effective |
| 199 | * weight. It may be called after a state change too. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 200 | * |
| 201 | * 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] | 202 | */ |
| 203 | static void fas_update_server_weight(struct server *srv) |
| 204 | { |
| 205 | int old_state, new_state; |
| 206 | struct proxy *p = srv->proxy; |
| 207 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 208 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 209 | return; |
| 210 | |
| 211 | /* If changing the server's weight changes its state, we simply apply |
| 212 | * the procedures we already have for status change. If the state |
| 213 | * remains down, the server is not in any tree, so it's as easy as |
| 214 | * updating its values. If the state remains up with different weights, |
| 215 | * there are some computations to perform to find a new place and |
| 216 | * possibly a new tree for this server. |
| 217 | */ |
| 218 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 219 | old_state = srv_currently_usable(srv); |
| 220 | new_state = srv_willbe_usable(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 221 | |
| 222 | if (!old_state && !new_state) { |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 223 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | else if (!old_state && new_state) { |
| 227 | fas_set_server_status_up(srv); |
| 228 | return; |
| 229 | } |
| 230 | else if (old_state && !new_state) { |
| 231 | fas_set_server_status_down(srv); |
| 232 | return; |
| 233 | } |
| 234 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 235 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 236 | |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 237 | if (srv->lb_tree) |
| 238 | fas_dequeue_srv(srv); |
| 239 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 240 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 241 | p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 242 | srv->lb_tree = &p->lbprm.fas.bck; |
| 243 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 244 | p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 245 | srv->lb_tree = &p->lbprm.fas.act; |
| 246 | } |
| 247 | |
| 248 | fas_queue_srv(srv); |
| 249 | |
| 250 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 251 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 252 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 253 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* This function is responsible for building the trees in case of fast |
| 257 | * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to |
| 258 | * uweight ratio. Both active and backup groups are initialized. |
| 259 | */ |
| 260 | void fas_init_server_tree(struct proxy *p) |
| 261 | { |
| 262 | struct server *srv; |
| 263 | struct eb_root init_head = EB_ROOT; |
| 264 | |
| 265 | p->lbprm.set_server_status_up = fas_set_server_status_up; |
| 266 | p->lbprm.set_server_status_down = fas_set_server_status_down; |
| 267 | p->lbprm.update_server_eweight = fas_update_server_weight; |
| 268 | p->lbprm.server_take_conn = fas_srv_reposition; |
| 269 | p->lbprm.server_drop_conn = fas_srv_reposition; |
| 270 | |
| 271 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 272 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 273 | 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] | 274 | srv_lb_commit_status(srv); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | recount_servers(p); |
| 278 | update_backend_weight(p); |
| 279 | |
| 280 | p->lbprm.fas.act = init_head; |
| 281 | p->lbprm.fas.bck = init_head; |
| 282 | |
| 283 | /* queue active and backup servers in two distinct groups */ |
| 284 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 285 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 286 | continue; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 287 | 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] | 288 | fas_queue_srv(srv); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* Return next server from the FS tree in backend <p>. If the tree is empty, |
| 293 | * return NULL. Saturated servers are skipped. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 294 | * |
| 295 | * 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] | 296 | */ |
| 297 | struct server *fas_get_next_server(struct proxy *p, struct server *srvtoavoid) |
| 298 | { |
| 299 | struct server *srv, *avoided; |
| 300 | struct eb32_node *node; |
| 301 | |
| 302 | srv = avoided = NULL; |
| 303 | |
Willy Tarreau | f76a21f | 2020-10-17 19:45:42 +0200 | [diff] [blame] | 304 | HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 305 | if (p->srv_act) |
| 306 | node = eb32_first(&p->lbprm.fas.act); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 307 | else if (p->lbprm.fbck) { |
| 308 | srv = p->lbprm.fbck; |
| 309 | goto out; |
| 310 | } |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 311 | else if (p->srv_bck) |
| 312 | node = eb32_first(&p->lbprm.fas.bck); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 313 | else { |
| 314 | srv = NULL; |
| 315 | goto out; |
| 316 | } |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 317 | |
| 318 | while (node) { |
| 319 | /* OK, we have a server. However, it may be saturated, in which |
| 320 | * case we don't want to reconsider it for now, so we'll simply |
| 321 | * skip it. Same if it's the server we try to avoid, in which |
| 322 | * case we simply remember it for later use if needed. |
| 323 | */ |
| 324 | struct server *s; |
| 325 | |
| 326 | s = eb32_entry(node, struct server, lb_node); |
| 327 | if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) { |
| 328 | if (s != srvtoavoid) { |
| 329 | srv = s; |
| 330 | break; |
| 331 | } |
| 332 | avoided = s; |
| 333 | } |
| 334 | node = eb32_next(node); |
| 335 | } |
| 336 | |
| 337 | if (!srv) |
| 338 | srv = avoided; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 339 | out: |
Willy Tarreau | f76a21f | 2020-10-17 19:45:42 +0200 | [diff] [blame] | 340 | HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 341 | return srv; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | /* |
| 346 | * Local variables: |
| 347 | * c-indent-level: 8 |
| 348 | * c-basic-offset: 8 |
| 349 | * End: |
| 350 | */ |