blob: 091241cc5b8704866b7179c24cc3fcb40dbab274 [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 *
Willy Tarreau85b2fb02021-02-17 16:14:00 +010033 * The lbprm's lock must be held.
Willy Tarreau1b877482018-08-21 19:44:53 +020034 */
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 Tarreau85b2fb02021-02-17 16:14:00 +010056 * 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 Tarreaua0570452021-06-18 09:30:30 +020060 unsigned int inflight = _HA_ATOMIC_LOAD(&s->served) + _HA_ATOMIC_LOAD(&s->queue.length);
Willy Tarreau8c855f62020-10-22 17:41:45 +020061
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 Tarreau5941ef02021-06-18 18:29:25 +020069 * The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020070 */
Willy Tarreau5941ef02021-06-18 18:29:25 +020071static void fwlc_srv_reposition(struct server *s)
Willy Tarreauf89c1872009-10-01 11:19:37 +020072{
Willy Tarreaua0570452021-06-18 09:30:30 +020073 unsigned int inflight = _HA_ATOMIC_LOAD(&s->served) + _HA_ATOMIC_LOAD(&s->queue.length);
Willy Tarreau5064ab62021-02-17 16:26:55 +010074 unsigned int new_key = inflight ? (inflight + 1) * SRV_EWGHT_MAX / s->cur_eweight : 0;
75
76 /* some calls will be made for no change (e.g connect_server() after
77 * assign_server(). Let's check that first.
78 */
79 if (s->lb_node.node.leaf_p && s->lb_node.key == new_key)
80 return;
81
Willy Tarreaucd10def2020-10-17 18:48:47 +020082 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Christopher Faulet1ae2a882019-06-19 10:50:38 +020083 if (s->lb_tree) {
Willy Tarreau5064ab62021-02-17 16:26:55 +010084 /* we might have been waiting for a while on the lock above
85 * so it's worth testing again because other threads are very
86 * likely to have released a connection or taken one leading
87 * to our target value (50% of the case in measurements).
88 */
Willy Tarreaua0570452021-06-18 09:30:30 +020089 inflight = _HA_ATOMIC_LOAD(&s->served) + _HA_ATOMIC_LOAD(&s->queue.length);
Willy Tarreau5064ab62021-02-17 16:26:55 +010090 new_key = inflight ? (inflight + 1) * SRV_EWGHT_MAX / s->cur_eweight : 0;
91 if (!s->lb_node.node.leaf_p || s->lb_node.key != new_key) {
92 eb32_delete(&s->lb_node);
93 s->lb_node.key = new_key;
94 eb32_insert(s->lb_tree, &s->lb_node);
95 }
Christopher Faulet1ae2a882019-06-19 10:50:38 +020096 }
Willy Tarreaucd10def2020-10-17 18:48:47 +020097 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020098}
99
100/* This function updates the server trees according to server <srv>'s new
101 * state. It should be called when server <srv>'s status changes to down.
102 * It is not important whether the server was already down or not. It is not
103 * important either that the new state is completely down (the caller may not
104 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +0200105 *
106 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200107 */
108static void fwlc_set_server_status_down(struct server *srv)
109{
110 struct proxy *p = srv->proxy;
111
Willy Tarreauc5150da2014-05-13 19:27:31 +0200112 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200113 return;
114
Emeric Brun52a91d32017-08-31 14:41:55 +0200115 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200116 goto out_update_state;
Willy Tarreaucd10def2020-10-17 18:48:47 +0200117 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200118
Willy Tarreauf89c1872009-10-01 11:19:37 +0200119
Emeric Brun52a91d32017-08-31 14:41:55 +0200120 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200121 /* server was already down */
122 goto out_update_backend;
123
Willy Tarreauc93cd162014-05-13 15:54:22 +0200124 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200125 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200126 p->srv_bck--;
127
128 if (srv == p->lbprm.fbck) {
129 /* we lost the first backup server in a single-backup
130 * configuration, we must search another one.
131 */
132 struct server *srv2 = p->lbprm.fbck;
133 do {
134 srv2 = srv2->next;
135 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200136 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200137 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +0200138 p->lbprm.fbck = srv2;
139 }
140 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200141 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200142 p->srv_act--;
143 }
144
145 fwlc_dequeue_srv(srv);
146 fwlc_remove_from_tree(srv);
147
148out_update_backend:
149 /* check/update tot_used, tot_weight */
150 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200151 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200152
Willy Tarreauf89c1872009-10-01 11:19:37 +0200153 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200154 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200155}
156
157/* This function updates the server trees according to server <srv>'s new
158 * state. It should be called when server <srv>'s status changes to up.
159 * It is not important whether the server was already down or not. It is not
160 * important either that the new state is completely UP (the caller may not
161 * know all the variables of a server's state). This function will not change
162 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200163 *
164 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200165 */
166static void fwlc_set_server_status_up(struct server *srv)
167{
168 struct proxy *p = srv->proxy;
169
Willy Tarreauc5150da2014-05-13 19:27:31 +0200170 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200171 return;
172
Emeric Brun52a91d32017-08-31 14:41:55 +0200173 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200174 goto out_update_state;
175
Willy Tarreaucd10def2020-10-17 18:48:47 +0200176 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200177
Emeric Brun52a91d32017-08-31 14:41:55 +0200178 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200179 /* server was already up */
180 goto out_update_backend;
181
Willy Tarreauc93cd162014-05-13 15:54:22 +0200182 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200183 srv->lb_tree = &p->lbprm.fwlc.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200184 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200185 p->srv_bck++;
186
187 if (!(p->options & PR_O_USE_ALL_BK)) {
188 if (!p->lbprm.fbck) {
189 /* there was no backup server anymore */
190 p->lbprm.fbck = srv;
191 } else {
192 /* we may have restored a backup server prior to fbck,
193 * in which case it should replace it.
194 */
195 struct server *srv2 = srv;
196 do {
197 srv2 = srv2->next;
198 } while (srv2 && (srv2 != p->lbprm.fbck));
199 if (srv2)
200 p->lbprm.fbck = srv;
201 }
202 }
203 } else {
204 srv->lb_tree = &p->lbprm.fwlc.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200205 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200206 p->srv_act++;
207 }
208
209 /* note that eweight cannot be 0 here */
Christopher Fauletcb33d3a2020-12-11 15:36:01 +0100210 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200211
212 out_update_backend:
213 /* check/update tot_used, tot_weight */
214 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200215 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200216
Willy Tarreauf89c1872009-10-01 11:19:37 +0200217 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200218 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200219}
220
221/* This function must be called after an update to server <srv>'s effective
222 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200223 *
224 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200225 */
226static void fwlc_update_server_weight(struct server *srv)
227{
228 int old_state, new_state;
229 struct proxy *p = srv->proxy;
230
Willy Tarreauc5150da2014-05-13 19:27:31 +0200231 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200232 return;
233
234 /* If changing the server's weight changes its state, we simply apply
235 * the procedures we already have for status change. If the state
236 * remains down, the server is not in any tree, so it's as easy as
237 * updating its values. If the state remains up with different weights,
238 * there are some computations to perform to find a new place and
239 * possibly a new tree for this server.
240 */
241
Emeric Brun52a91d32017-08-31 14:41:55 +0200242 old_state = srv_currently_usable(srv);
243 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200244
245 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200246 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200247 return;
248 }
249 else if (!old_state && new_state) {
250 fwlc_set_server_status_up(srv);
251 return;
252 }
253 else if (old_state && !new_state) {
254 fwlc_set_server_status_down(srv);
255 return;
256 }
257
Willy Tarreaucd10def2020-10-17 18:48:47 +0200258 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200259
Willy Tarreauf89c1872009-10-01 11:19:37 +0200260 if (srv->lb_tree)
261 fwlc_dequeue_srv(srv);
262
Willy Tarreauc93cd162014-05-13 15:54:22 +0200263 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200264 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200265 srv->lb_tree = &p->lbprm.fwlc.bck;
266 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200267 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200268 srv->lb_tree = &p->lbprm.fwlc.act;
269 }
270
Christopher Fauletcb33d3a2020-12-11 15:36:01 +0100271 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200272
273 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200274 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200275
Willy Tarreauc5150da2014-05-13 19:27:31 +0200276 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200277}
278
279/* This function is responsible for building the trees in case of fast
280 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
281 * uweight ratio. Both active and backup groups are initialized.
282 */
283void fwlc_init_server_tree(struct proxy *p)
284{
285 struct server *srv;
286 struct eb_root init_head = EB_ROOT;
287
288 p->lbprm.set_server_status_up = fwlc_set_server_status_up;
289 p->lbprm.set_server_status_down = fwlc_set_server_status_down;
290 p->lbprm.update_server_eweight = fwlc_update_server_weight;
291 p->lbprm.server_take_conn = fwlc_srv_reposition;
292 p->lbprm.server_drop_conn = fwlc_srv_reposition;
293
294 p->lbprm.wdiv = BE_WEIGHT_SCALE;
295 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200296 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200297 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200298 }
299
300 recount_servers(p);
301 update_backend_weight(p);
302
303 p->lbprm.fwlc.act = init_head;
304 p->lbprm.fwlc.bck = init_head;
305
306 /* queue active and backup servers in two distinct groups */
307 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200308 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200309 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200310 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act;
Christopher Fauletcb33d3a2020-12-11 15:36:01 +0100311 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200312 }
313}
314
315/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
316 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200317 *
Willy Tarreau63b3ae72021-06-01 16:58:31 +0200318 * The lbprm's lock will be used in R/O mode. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200319 */
320struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
321{
322 struct server *srv, *avoided;
323 struct eb32_node *node;
324
325 srv = avoided = NULL;
326
Willy Tarreau58bc9c12020-10-17 19:32:09 +0200327 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200328 if (p->srv_act)
329 node = eb32_first(&p->lbprm.fwlc.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200330 else if (p->lbprm.fbck) {
331 srv = p->lbprm.fbck;
332 goto out;
333 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200334 else if (p->srv_bck)
335 node = eb32_first(&p->lbprm.fwlc.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200336 else {
337 srv = NULL;
338 goto out;
339 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200340
341 while (node) {
342 /* OK, we have a server. However, it may be saturated, in which
343 * case we don't want to reconsider it for now, so we'll simply
344 * skip it. Same if it's the server we try to avoid, in which
345 * case we simply remember it for later use if needed.
346 */
347 struct server *s;
348
349 s = eb32_entry(node, struct server, lb_node);
Willy Tarreaua0570452021-06-18 09:30:30 +0200350 if (!s->maxconn || s->served + s->queue.length < srv_dynamic_maxconn(s) + s->maxqueue) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200351 if (s != srvtoavoid) {
352 srv = s;
353 break;
354 }
355 avoided = s;
356 }
357 node = eb32_next(node);
358 }
359
360 if (!srv)
361 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200362 out:
Willy Tarreau58bc9c12020-10-17 19:32:09 +0200363 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200364 return srv;
365}
366
367
368/*
369 * Local variables:
370 * c-indent-level: 8
371 * c-basic-offset: 8
372 * End:
373 */