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