Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Fast Weighted Least Connection 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 | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 13 | #include <import/eb32tree.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 14 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 15 | #include <haproxy/backend.h> |
Willy Tarreau | a55c454 | 2020-06-04 22:59:39 +0200 | [diff] [blame] | 16 | #include <haproxy/queue.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 17 | #include <haproxy/server-t.h> |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 18 | |
| 19 | |
| 20 | /* Remove a server from a tree. It must have previously been dequeued. This |
| 21 | * function is meant to be called when a server is going down or has its |
| 22 | * weight disabled. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 23 | * |
| 24 | * The server's lock and the lbprm's lock must be held. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 25 | */ |
| 26 | static inline void fwlc_remove_from_tree(struct server *s) |
| 27 | { |
| 28 | s->lb_tree = NULL; |
| 29 | } |
| 30 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 31 | /* simply removes a server from a tree. |
| 32 | * |
| 33 | * The server's lock and the lbprm's lock must be held. |
| 34 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 35 | static inline void fwlc_dequeue_srv(struct server *s) |
| 36 | { |
| 37 | eb32_delete(&s->lb_node); |
| 38 | } |
| 39 | |
Christopher Faulet | 0881842 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 40 | /* Queue a server in its associated tree, assuming the <eweight> is >0. |
Willy Tarreau | 1eb6c55 | 2018-12-14 08:33:28 +0100 | [diff] [blame] | 41 | * Servers are sorted by (#conns+1)/weight. To ensure maximum accuracy, |
| 42 | * we use (#conns+1)*SRV_EWGHT_MAX/eweight as the sorting key. The reason |
| 43 | * for using #conns+1 is to sort by weights in case the server is picked |
| 44 | * and not before it is picked. This provides a better load accuracy for |
| 45 | * low connection counts when weights differ and makes sure the round-robin |
Willy Tarreau | ed5ac9c | 2019-09-06 17:04:04 +0200 | [diff] [blame] | 46 | * applies between servers of highest weight first. However servers with no |
| 47 | * connection are always picked first so that under low loads, it's not |
| 48 | * always the single server with the highest weight that gets picked. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 49 | * |
Christopher Faulet | 0881842 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 50 | * NOTE: Depending on the calling context, we use s->next_eweight or |
| 51 | * s->cur_eweight. The next value is used when the server state is updated |
| 52 | * (because the weight changed for instance). During this step, the server |
| 53 | * state is not yet committed. The current value is used to reposition the |
| 54 | * server in the tree. This happens when the server is used. |
| 55 | * |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 56 | * The server's lock and the lbprm's lock must be held. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 57 | */ |
Christopher Faulet | 0881842 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 58 | static inline void fwlc_queue_srv(struct server *s, unsigned int eweight) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 59 | { |
Willy Tarreau | 8c855f6 | 2020-10-22 17:41:45 +0200 | [diff] [blame] | 60 | unsigned int inflight = s->served + s->nbpend; |
| 61 | |
Christopher Faulet | 0881842 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 62 | s->lb_node.key = inflight ? (inflight + 1) * SRV_EWGHT_MAX / eweight : 0; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 63 | eb32_insert(s->lb_tree, &s->lb_node); |
| 64 | } |
| 65 | |
| 66 | /* Re-position the server in the FWLC tree after it has been assigned one |
| 67 | * connection or after it has released one. Note that it is possible that |
| 68 | * 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] | 69 | * |
| 70 | * 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] | 71 | */ |
| 72 | static void fwlc_srv_reposition(struct server *s) |
| 73 | { |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 74 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Christopher Faulet | 1ae2a88 | 2019-06-19 10:50:38 +0200 | [diff] [blame] | 75 | if (s->lb_tree) { |
| 76 | fwlc_dequeue_srv(s); |
Christopher Faulet | 0881842 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 77 | fwlc_queue_srv(s, s->cur_eweight); |
Christopher Faulet | 1ae2a88 | 2019-06-19 10:50:38 +0200 | [diff] [blame] | 78 | } |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 79 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* This function updates the server trees according to server <srv>'s new |
| 83 | * state. It should be called when server <srv>'s status changes to down. |
| 84 | * It is not important whether the server was already down or not. It is not |
| 85 | * important either that the new state is completely down (the caller may not |
| 86 | * know all the variables of a server's state). |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 87 | * |
| 88 | * 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] | 89 | */ |
| 90 | static void fwlc_set_server_status_down(struct server *srv) |
| 91 | { |
| 92 | struct proxy *p = srv->proxy; |
| 93 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 94 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 95 | return; |
| 96 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 97 | if (srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 98 | goto out_update_state; |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 99 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 100 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 101 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 102 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 103 | /* server was already down */ |
| 104 | goto out_update_backend; |
| 105 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 106 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 107 | p->lbprm.tot_wbck -= srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 108 | p->srv_bck--; |
| 109 | |
| 110 | if (srv == p->lbprm.fbck) { |
| 111 | /* we lost the first backup server in a single-backup |
| 112 | * configuration, we must search another one. |
| 113 | */ |
| 114 | struct server *srv2 = p->lbprm.fbck; |
| 115 | do { |
| 116 | srv2 = srv2->next; |
| 117 | } while (srv2 && |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 118 | !((srv2->flags & SRV_F_BACKUP) && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 119 | srv_willbe_usable(srv2))); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 120 | p->lbprm.fbck = srv2; |
| 121 | } |
| 122 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 123 | p->lbprm.tot_wact -= srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 124 | p->srv_act--; |
| 125 | } |
| 126 | |
| 127 | fwlc_dequeue_srv(srv); |
| 128 | fwlc_remove_from_tree(srv); |
| 129 | |
| 130 | out_update_backend: |
| 131 | /* check/update tot_used, tot_weight */ |
| 132 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 133 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 134 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 135 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 136 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* This function updates the server trees according to server <srv>'s new |
| 140 | * state. It should be called when server <srv>'s status changes to up. |
| 141 | * It is not important whether the server was already down or not. It is not |
| 142 | * important either that the new state is completely UP (the caller may not |
| 143 | * know all the variables of a server's state). This function will not change |
| 144 | * the weight of a server which was already up. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 145 | * |
| 146 | * 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] | 147 | */ |
| 148 | static void fwlc_set_server_status_up(struct server *srv) |
| 149 | { |
| 150 | struct proxy *p = srv->proxy; |
| 151 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 152 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 153 | return; |
| 154 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 155 | if (!srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 156 | goto out_update_state; |
| 157 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 158 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 159 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 160 | if (srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 161 | /* server was already up */ |
| 162 | goto out_update_backend; |
| 163 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 164 | if (srv->flags & SRV_F_BACKUP) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 165 | srv->lb_tree = &p->lbprm.fwlc.bck; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 166 | p->lbprm.tot_wbck += srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 167 | p->srv_bck++; |
| 168 | |
| 169 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 170 | if (!p->lbprm.fbck) { |
| 171 | /* there was no backup server anymore */ |
| 172 | p->lbprm.fbck = srv; |
| 173 | } else { |
| 174 | /* we may have restored a backup server prior to fbck, |
| 175 | * in which case it should replace it. |
| 176 | */ |
| 177 | struct server *srv2 = srv; |
| 178 | do { |
| 179 | srv2 = srv2->next; |
| 180 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 181 | if (srv2) |
| 182 | p->lbprm.fbck = srv; |
| 183 | } |
| 184 | } |
| 185 | } else { |
| 186 | srv->lb_tree = &p->lbprm.fwlc.act; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 187 | p->lbprm.tot_wact += srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 188 | p->srv_act++; |
| 189 | } |
| 190 | |
| 191 | /* note that eweight cannot be 0 here */ |
Christopher Faulet | 0881842 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 192 | fwlc_queue_srv(srv, srv->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 193 | |
| 194 | out_update_backend: |
| 195 | /* check/update tot_used, tot_weight */ |
| 196 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 197 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 198 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 199 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 200 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* This function must be called after an update to server <srv>'s effective |
| 204 | * weight. It may be called after a state change too. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 205 | * |
| 206 | * 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] | 207 | */ |
| 208 | static void fwlc_update_server_weight(struct server *srv) |
| 209 | { |
| 210 | int old_state, new_state; |
| 211 | struct proxy *p = srv->proxy; |
| 212 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 213 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 214 | return; |
| 215 | |
| 216 | /* If changing the server's weight changes its state, we simply apply |
| 217 | * the procedures we already have for status change. If the state |
| 218 | * remains down, the server is not in any tree, so it's as easy as |
| 219 | * updating its values. If the state remains up with different weights, |
| 220 | * there are some computations to perform to find a new place and |
| 221 | * possibly a new tree for this server. |
| 222 | */ |
| 223 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 224 | old_state = srv_currently_usable(srv); |
| 225 | new_state = srv_willbe_usable(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 226 | |
| 227 | if (!old_state && !new_state) { |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 228 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | else if (!old_state && new_state) { |
| 232 | fwlc_set_server_status_up(srv); |
| 233 | return; |
| 234 | } |
| 235 | else if (old_state && !new_state) { |
| 236 | fwlc_set_server_status_down(srv); |
| 237 | return; |
| 238 | } |
| 239 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 240 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 241 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 242 | if (srv->lb_tree) |
| 243 | fwlc_dequeue_srv(srv); |
| 244 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 245 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 246 | p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 247 | srv->lb_tree = &p->lbprm.fwlc.bck; |
| 248 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 249 | p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 250 | srv->lb_tree = &p->lbprm.fwlc.act; |
| 251 | } |
| 252 | |
Christopher Faulet | 0881842 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 253 | fwlc_queue_srv(srv, srv->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 254 | |
| 255 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 256 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 257 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 258 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* This function is responsible for building the trees in case of fast |
| 262 | * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to |
| 263 | * uweight ratio. Both active and backup groups are initialized. |
| 264 | */ |
| 265 | void fwlc_init_server_tree(struct proxy *p) |
| 266 | { |
| 267 | struct server *srv; |
| 268 | struct eb_root init_head = EB_ROOT; |
| 269 | |
| 270 | p->lbprm.set_server_status_up = fwlc_set_server_status_up; |
| 271 | p->lbprm.set_server_status_down = fwlc_set_server_status_down; |
| 272 | p->lbprm.update_server_eweight = fwlc_update_server_weight; |
| 273 | p->lbprm.server_take_conn = fwlc_srv_reposition; |
| 274 | p->lbprm.server_drop_conn = fwlc_srv_reposition; |
| 275 | |
| 276 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 277 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 278 | 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] | 279 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | recount_servers(p); |
| 283 | update_backend_weight(p); |
| 284 | |
| 285 | p->lbprm.fwlc.act = init_head; |
| 286 | p->lbprm.fwlc.bck = init_head; |
| 287 | |
| 288 | /* queue active and backup servers in two distinct groups */ |
| 289 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 290 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 291 | continue; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 292 | srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act; |
Christopher Faulet | 0881842 | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 293 | fwlc_queue_srv(srv, srv->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | /* Return next server from the FWLC tree in backend <p>. If the tree is empty, |
| 298 | * return NULL. Saturated servers are skipped. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 299 | * |
| 300 | * 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] | 301 | */ |
| 302 | struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid) |
| 303 | { |
| 304 | struct server *srv, *avoided; |
| 305 | struct eb32_node *node; |
| 306 | |
| 307 | srv = avoided = NULL; |
| 308 | |
Willy Tarreau | 58bc9c1 | 2020-10-17 19:32:09 +0200 | [diff] [blame] | 309 | HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 310 | if (p->srv_act) |
| 311 | node = eb32_first(&p->lbprm.fwlc.act); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 312 | else if (p->lbprm.fbck) { |
| 313 | srv = p->lbprm.fbck; |
| 314 | goto out; |
| 315 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 316 | else if (p->srv_bck) |
| 317 | node = eb32_first(&p->lbprm.fwlc.bck); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 318 | else { |
| 319 | srv = NULL; |
| 320 | goto out; |
| 321 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 322 | |
| 323 | while (node) { |
| 324 | /* OK, we have a server. However, it may be saturated, in which |
| 325 | * case we don't want to reconsider it for now, so we'll simply |
| 326 | * skip it. Same if it's the server we try to avoid, in which |
| 327 | * case we simply remember it for later use if needed. |
| 328 | */ |
| 329 | struct server *s; |
| 330 | |
| 331 | s = eb32_entry(node, struct server, lb_node); |
Willy Tarreau | 8ae8c48 | 2020-10-22 17:19:07 +0200 | [diff] [blame] | 332 | if (!s->maxconn || s->served + s->nbpend < srv_dynamic_maxconn(s) + s->maxqueue) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 333 | if (s != srvtoavoid) { |
| 334 | srv = s; |
| 335 | break; |
| 336 | } |
| 337 | avoided = s; |
| 338 | } |
| 339 | node = eb32_next(node); |
| 340 | } |
| 341 | |
| 342 | if (!srv) |
| 343 | srv = avoided; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 344 | out: |
Willy Tarreau | 58bc9c1 | 2020-10-17 19:32:09 +0200 | [diff] [blame] | 345 | HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 346 | return srv; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | /* |
| 351 | * Local variables: |
| 352 | * c-indent-level: 8 |
| 353 | * c-basic-offset: 8 |
| 354 | * End: |
| 355 | */ |