blob: 4239a4c63a20d64a35c2e6e361fa9d72e5d757f7 [file] [log] [blame]
Willy Tarreauf89c1872009-10-01 11:19:37 +02001/*
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 Tarreau1e56f922020-06-04 23:20:13 +020013#include <import/eb32tree.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <haproxy/backend.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020016#include <haproxy/queue.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020017#include <haproxy/server-t.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020018
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 Tarreau1b877482018-08-21 19:44:53 +020023 *
24 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf89c1872009-10-01 11:19:37 +020025 */
26static inline void fwlc_remove_from_tree(struct server *s)
27{
28 s->lb_tree = NULL;
29}
30
Willy Tarreau1b877482018-08-21 19:44:53 +020031/* simply removes a server from a tree.
32 *
33 * The server's lock and the lbprm's lock must be held.
34 */
Willy Tarreauf89c1872009-10-01 11:19:37 +020035static inline void fwlc_dequeue_srv(struct server *s)
36{
37 eb32_delete(&s->lb_node);
38}
39
Christopher Fauletcb33d3a2020-12-11 15:36:01 +010040/* Queue a server in its associated tree, assuming the <eweight> is >0.
Willy Tarreau1eb6c552018-12-14 08:33:28 +010041 * 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 Tarreaued5ac9c2019-09-06 17:04:04 +020046 * 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 Tarreau1b877482018-08-21 19:44:53 +020049 *
Christopher Fauletcb33d3a2020-12-11 15:36:01 +010050 * 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 Tarreau1b877482018-08-21 19:44:53 +020056 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf89c1872009-10-01 11:19:37 +020057 */
Christopher Fauletcb33d3a2020-12-11 15:36:01 +010058static inline void fwlc_queue_srv(struct server *s, unsigned int eweight)
Willy Tarreauf89c1872009-10-01 11:19:37 +020059{
Willy Tarreau8c855f62020-10-22 17:41:45 +020060 unsigned int inflight = s->served + s->nbpend;
61
Christopher Fauletcb33d3a2020-12-11 15:36:01 +010062 s->lb_node.key = inflight ? (inflight + 1) * SRV_EWGHT_MAX / eweight : 0;
Willy Tarreauf89c1872009-10-01 11:19:37 +020063 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 Tarreau1b877482018-08-21 19:44:53 +020069 *
Willy Tarreau59b0fec2021-02-17 16:01:37 +010070 * <locked> must reflect the server's lock ownership. The lbprm's lock will
71 * be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020072 */
Willy Tarreau59b0fec2021-02-17 16:01:37 +010073static void fwlc_srv_reposition(struct server *s, int locked)
Willy Tarreauf89c1872009-10-01 11:19:37 +020074{
Willy Tarreau59b0fec2021-02-17 16:01:37 +010075 if (!locked)
76 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
77
Willy Tarreaucd10def2020-10-17 18:48:47 +020078 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Christopher Faulet1ae2a882019-06-19 10:50:38 +020079 if (s->lb_tree) {
80 fwlc_dequeue_srv(s);
Christopher Fauletcb33d3a2020-12-11 15:36:01 +010081 fwlc_queue_srv(s, s->cur_eweight);
Christopher Faulet1ae2a882019-06-19 10:50:38 +020082 }
Willy Tarreaucd10def2020-10-17 18:48:47 +020083 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreau59b0fec2021-02-17 16:01:37 +010084
85 if (!locked)
86 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020087}
88
89/* This function updates the server trees according to server <srv>'s new
90 * state. It should be called when server <srv>'s status changes to down.
91 * It is not important whether the server was already down or not. It is not
92 * important either that the new state is completely down (the caller may not
93 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020094 *
95 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020096 */
97static void fwlc_set_server_status_down(struct server *srv)
98{
99 struct proxy *p = srv->proxy;
100
Willy Tarreauc5150da2014-05-13 19:27:31 +0200101 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200102 return;
103
Emeric Brun52a91d32017-08-31 14:41:55 +0200104 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200105 goto out_update_state;
Willy Tarreaucd10def2020-10-17 18:48:47 +0200106 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200107
Willy Tarreauf89c1872009-10-01 11:19:37 +0200108
Emeric Brun52a91d32017-08-31 14:41:55 +0200109 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200110 /* server was already down */
111 goto out_update_backend;
112
Willy Tarreauc93cd162014-05-13 15:54:22 +0200113 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200114 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200115 p->srv_bck--;
116
117 if (srv == p->lbprm.fbck) {
118 /* we lost the first backup server in a single-backup
119 * configuration, we must search another one.
120 */
121 struct server *srv2 = p->lbprm.fbck;
122 do {
123 srv2 = srv2->next;
124 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200125 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200126 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +0200127 p->lbprm.fbck = srv2;
128 }
129 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200130 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200131 p->srv_act--;
132 }
133
134 fwlc_dequeue_srv(srv);
135 fwlc_remove_from_tree(srv);
136
137out_update_backend:
138 /* check/update tot_used, tot_weight */
139 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200140 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200141
Willy Tarreauf89c1872009-10-01 11:19:37 +0200142 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200143 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200144}
145
146/* This function updates the server trees according to server <srv>'s new
147 * state. It should be called when server <srv>'s status changes to up.
148 * It is not important whether the server was already down or not. It is not
149 * important either that the new state is completely UP (the caller may not
150 * know all the variables of a server's state). This function will not change
151 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200152 *
153 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200154 */
155static void fwlc_set_server_status_up(struct server *srv)
156{
157 struct proxy *p = srv->proxy;
158
Willy Tarreauc5150da2014-05-13 19:27:31 +0200159 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200160 return;
161
Emeric Brun52a91d32017-08-31 14:41:55 +0200162 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200163 goto out_update_state;
164
Willy Tarreaucd10def2020-10-17 18:48:47 +0200165 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200166
Emeric Brun52a91d32017-08-31 14:41:55 +0200167 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200168 /* server was already up */
169 goto out_update_backend;
170
Willy Tarreauc93cd162014-05-13 15:54:22 +0200171 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200172 srv->lb_tree = &p->lbprm.fwlc.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200173 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200174 p->srv_bck++;
175
176 if (!(p->options & PR_O_USE_ALL_BK)) {
177 if (!p->lbprm.fbck) {
178 /* there was no backup server anymore */
179 p->lbprm.fbck = srv;
180 } else {
181 /* we may have restored a backup server prior to fbck,
182 * in which case it should replace it.
183 */
184 struct server *srv2 = srv;
185 do {
186 srv2 = srv2->next;
187 } while (srv2 && (srv2 != p->lbprm.fbck));
188 if (srv2)
189 p->lbprm.fbck = srv;
190 }
191 }
192 } else {
193 srv->lb_tree = &p->lbprm.fwlc.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200194 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200195 p->srv_act++;
196 }
197
198 /* note that eweight cannot be 0 here */
Christopher Fauletcb33d3a2020-12-11 15:36:01 +0100199 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200200
201 out_update_backend:
202 /* check/update tot_used, tot_weight */
203 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200204 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200205
Willy Tarreauf89c1872009-10-01 11:19:37 +0200206 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200207 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200208}
209
210/* This function must be called after an update to server <srv>'s effective
211 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200212 *
213 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200214 */
215static void fwlc_update_server_weight(struct server *srv)
216{
217 int old_state, new_state;
218 struct proxy *p = srv->proxy;
219
Willy Tarreauc5150da2014-05-13 19:27:31 +0200220 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200221 return;
222
223 /* If changing the server's weight changes its state, we simply apply
224 * the procedures we already have for status change. If the state
225 * remains down, the server is not in any tree, so it's as easy as
226 * updating its values. If the state remains up with different weights,
227 * there are some computations to perform to find a new place and
228 * possibly a new tree for this server.
229 */
230
Emeric Brun52a91d32017-08-31 14:41:55 +0200231 old_state = srv_currently_usable(srv);
232 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200233
234 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200235 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200236 return;
237 }
238 else if (!old_state && new_state) {
239 fwlc_set_server_status_up(srv);
240 return;
241 }
242 else if (old_state && !new_state) {
243 fwlc_set_server_status_down(srv);
244 return;
245 }
246
Willy Tarreaucd10def2020-10-17 18:48:47 +0200247 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200248
Willy Tarreauf89c1872009-10-01 11:19:37 +0200249 if (srv->lb_tree)
250 fwlc_dequeue_srv(srv);
251
Willy Tarreauc93cd162014-05-13 15:54:22 +0200252 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200253 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200254 srv->lb_tree = &p->lbprm.fwlc.bck;
255 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200256 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200257 srv->lb_tree = &p->lbprm.fwlc.act;
258 }
259
Christopher Fauletcb33d3a2020-12-11 15:36:01 +0100260 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200261
262 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200263 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200264
Willy Tarreauc5150da2014-05-13 19:27:31 +0200265 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200266}
267
268/* This function is responsible for building the trees in case of fast
269 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
270 * uweight ratio. Both active and backup groups are initialized.
271 */
272void fwlc_init_server_tree(struct proxy *p)
273{
274 struct server *srv;
275 struct eb_root init_head = EB_ROOT;
276
277 p->lbprm.set_server_status_up = fwlc_set_server_status_up;
278 p->lbprm.set_server_status_down = fwlc_set_server_status_down;
279 p->lbprm.update_server_eweight = fwlc_update_server_weight;
280 p->lbprm.server_take_conn = fwlc_srv_reposition;
281 p->lbprm.server_drop_conn = fwlc_srv_reposition;
282
283 p->lbprm.wdiv = BE_WEIGHT_SCALE;
284 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200285 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200286 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200287 }
288
289 recount_servers(p);
290 update_backend_weight(p);
291
292 p->lbprm.fwlc.act = init_head;
293 p->lbprm.fwlc.bck = init_head;
294
295 /* queue active and backup servers in two distinct groups */
296 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200297 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200298 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200299 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act;
Christopher Fauletcb33d3a2020-12-11 15:36:01 +0100300 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200301 }
302}
303
304/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
305 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200306 *
307 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200308 */
309struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
310{
311 struct server *srv, *avoided;
312 struct eb32_node *node;
313
314 srv = avoided = NULL;
315
Willy Tarreau58bc9c12020-10-17 19:32:09 +0200316 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200317 if (p->srv_act)
318 node = eb32_first(&p->lbprm.fwlc.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200319 else if (p->lbprm.fbck) {
320 srv = p->lbprm.fbck;
321 goto out;
322 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200323 else if (p->srv_bck)
324 node = eb32_first(&p->lbprm.fwlc.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200325 else {
326 srv = NULL;
327 goto out;
328 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200329
330 while (node) {
331 /* OK, we have a server. However, it may be saturated, in which
332 * case we don't want to reconsider it for now, so we'll simply
333 * skip it. Same if it's the server we try to avoid, in which
334 * case we simply remember it for later use if needed.
335 */
336 struct server *s;
337
338 s = eb32_entry(node, struct server, lb_node);
Willy Tarreau8ae8c482020-10-22 17:19:07 +0200339 if (!s->maxconn || s->served + s->nbpend < srv_dynamic_maxconn(s) + s->maxqueue) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200340 if (s != srvtoavoid) {
341 srv = s;
342 break;
343 }
344 avoided = s;
345 }
346 node = eb32_next(node);
347 }
348
349 if (!srv)
350 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200351 out:
Willy Tarreau58bc9c12020-10-17 19:32:09 +0200352 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200353 return srv;
354}
355
356
357/*
358 * Local variables:
359 * c-indent-level: 8
360 * c-basic-offset: 8
361 * End:
362 */