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