blob: 9f310ac602a44c43b638c4ecc1a8a8d3e8317667 [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 Tarreau85b2fb02021-02-17 16:14:00 +010060 unsigned int inflight = _HA_ATOMIC_LOAD(&s->served) + _HA_ATOMIC_LOAD(&s->nbpend);
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 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 Tarreau5064ab62021-02-17 16:26:55 +010075 unsigned int inflight = _HA_ATOMIC_LOAD(&s->served) + _HA_ATOMIC_LOAD(&s->nbpend);
Willy Tarreau80314642021-09-22 07:15:57 +020076 unsigned int eweight = _HA_ATOMIC_LOAD(&s->cur_eweight);
77 unsigned int new_key = inflight ? (inflight + 1) * SRV_EWGHT_MAX / (eweight ? eweight : 1) : 0;
Willy Tarreau5064ab62021-02-17 16:26:55 +010078
79 /* some calls will be made for no change (e.g connect_server() after
80 * assign_server(). Let's check that first.
81 */
Willy Tarreau80314642021-09-22 07:15:57 +020082 if (s->lb_node.node.leaf_p && eweight && s->lb_node.key == new_key)
Willy Tarreau5064ab62021-02-17 16:26:55 +010083 return;
84
Willy Tarreaucd10def2020-10-17 18:48:47 +020085 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Christopher Faulet1ae2a882019-06-19 10:50:38 +020086 if (s->lb_tree) {
Willy Tarreau5064ab62021-02-17 16:26:55 +010087 /* we might have been waiting for a while on the lock above
88 * so it's worth testing again because other threads are very
89 * likely to have released a connection or taken one leading
90 * to our target value (50% of the case in measurements).
91 */
92 inflight = _HA_ATOMIC_LOAD(&s->served) + _HA_ATOMIC_LOAD(&s->nbpend);
Willy Tarreau80314642021-09-22 07:15:57 +020093 eweight = _HA_ATOMIC_LOAD(&s->cur_eweight);
94 new_key = inflight ? (inflight + 1) * SRV_EWGHT_MAX / (eweight ? eweight : 1) : 0;
Willy Tarreau5064ab62021-02-17 16:26:55 +010095 if (!s->lb_node.node.leaf_p || s->lb_node.key != new_key) {
96 eb32_delete(&s->lb_node);
97 s->lb_node.key = new_key;
98 eb32_insert(s->lb_tree, &s->lb_node);
99 }
Christopher Faulet1ae2a882019-06-19 10:50:38 +0200100 }
Willy Tarreaucd10def2020-10-17 18:48:47 +0200101 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200102}
103
104/* This function updates the server trees according to server <srv>'s new
105 * state. It should be called when server <srv>'s status changes to down.
106 * It is not important whether the server was already down or not. It is not
107 * important either that the new state is completely down (the caller may not
108 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +0200109 *
110 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200111 */
112static void fwlc_set_server_status_down(struct server *srv)
113{
114 struct proxy *p = srv->proxy;
115
Willy Tarreauc5150da2014-05-13 19:27:31 +0200116 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200117 return;
118
Emeric Brun52a91d32017-08-31 14:41:55 +0200119 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200120 goto out_update_state;
Willy Tarreaucd10def2020-10-17 18:48:47 +0200121 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200122
Willy Tarreauf89c1872009-10-01 11:19:37 +0200123
Emeric Brun52a91d32017-08-31 14:41:55 +0200124 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200125 /* server was already down */
126 goto out_update_backend;
127
Willy Tarreauc93cd162014-05-13 15:54:22 +0200128 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200129 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200130 p->srv_bck--;
131
132 if (srv == p->lbprm.fbck) {
133 /* we lost the first backup server in a single-backup
134 * configuration, we must search another one.
135 */
136 struct server *srv2 = p->lbprm.fbck;
137 do {
138 srv2 = srv2->next;
139 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200140 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200141 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +0200142 p->lbprm.fbck = srv2;
143 }
144 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200145 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200146 p->srv_act--;
147 }
148
149 fwlc_dequeue_srv(srv);
150 fwlc_remove_from_tree(srv);
151
152out_update_backend:
153 /* check/update tot_used, tot_weight */
154 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200155 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200156
Willy Tarreauf89c1872009-10-01 11:19:37 +0200157 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200158 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200159}
160
161/* This function updates the server trees according to server <srv>'s new
162 * state. It should be called when server <srv>'s status changes to up.
163 * It is not important whether the server was already down or not. It is not
164 * important either that the new state is completely UP (the caller may not
165 * know all the variables of a server's state). This function will not change
166 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200167 *
168 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200169 */
170static void fwlc_set_server_status_up(struct server *srv)
171{
172 struct proxy *p = srv->proxy;
173
Willy Tarreauc5150da2014-05-13 19:27:31 +0200174 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200175 return;
176
Emeric Brun52a91d32017-08-31 14:41:55 +0200177 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200178 goto out_update_state;
179
Willy Tarreaucd10def2020-10-17 18:48:47 +0200180 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200181
Emeric Brun52a91d32017-08-31 14:41:55 +0200182 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200183 /* server was already up */
184 goto out_update_backend;
185
Willy Tarreauc93cd162014-05-13 15:54:22 +0200186 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200187 srv->lb_tree = &p->lbprm.fwlc.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200188 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200189 p->srv_bck++;
190
191 if (!(p->options & PR_O_USE_ALL_BK)) {
192 if (!p->lbprm.fbck) {
193 /* there was no backup server anymore */
194 p->lbprm.fbck = srv;
195 } else {
196 /* we may have restored a backup server prior to fbck,
197 * in which case it should replace it.
198 */
199 struct server *srv2 = srv;
200 do {
201 srv2 = srv2->next;
202 } while (srv2 && (srv2 != p->lbprm.fbck));
203 if (srv2)
204 p->lbprm.fbck = srv;
205 }
206 }
207 } else {
208 srv->lb_tree = &p->lbprm.fwlc.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200209 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200210 p->srv_act++;
211 }
212
213 /* note that eweight cannot be 0 here */
Christopher Fauletcb33d3a2020-12-11 15:36:01 +0100214 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200215
216 out_update_backend:
217 /* check/update tot_used, tot_weight */
218 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200219 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200220
Willy Tarreauf89c1872009-10-01 11:19:37 +0200221 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200222 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200223}
224
225/* This function must be called after an update to server <srv>'s effective
226 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200227 *
228 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200229 */
230static void fwlc_update_server_weight(struct server *srv)
231{
232 int old_state, new_state;
233 struct proxy *p = srv->proxy;
234
Willy Tarreauc5150da2014-05-13 19:27:31 +0200235 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200236 return;
237
238 /* If changing the server's weight changes its state, we simply apply
239 * the procedures we already have for status change. If the state
240 * remains down, the server is not in any tree, so it's as easy as
241 * updating its values. If the state remains up with different weights,
242 * there are some computations to perform to find a new place and
243 * possibly a new tree for this server.
244 */
245
Emeric Brun52a91d32017-08-31 14:41:55 +0200246 old_state = srv_currently_usable(srv);
247 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200248
249 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200250 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200251 return;
252 }
253 else if (!old_state && new_state) {
254 fwlc_set_server_status_up(srv);
255 return;
256 }
257 else if (old_state && !new_state) {
258 fwlc_set_server_status_down(srv);
259 return;
260 }
261
Willy Tarreaucd10def2020-10-17 18:48:47 +0200262 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200263
Willy Tarreauf89c1872009-10-01 11:19:37 +0200264 if (srv->lb_tree)
265 fwlc_dequeue_srv(srv);
266
Willy Tarreauc93cd162014-05-13 15:54:22 +0200267 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200268 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200269 srv->lb_tree = &p->lbprm.fwlc.bck;
270 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200271 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200272 srv->lb_tree = &p->lbprm.fwlc.act;
273 }
274
Christopher Fauletcb33d3a2020-12-11 15:36:01 +0100275 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200276
277 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200278 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200279
Willy Tarreauc5150da2014-05-13 19:27:31 +0200280 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200281}
282
283/* This function is responsible for building the trees in case of fast
284 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
285 * uweight ratio. Both active and backup groups are initialized.
286 */
287void fwlc_init_server_tree(struct proxy *p)
288{
289 struct server *srv;
290 struct eb_root init_head = EB_ROOT;
291
292 p->lbprm.set_server_status_up = fwlc_set_server_status_up;
293 p->lbprm.set_server_status_down = fwlc_set_server_status_down;
294 p->lbprm.update_server_eweight = fwlc_update_server_weight;
295 p->lbprm.server_take_conn = fwlc_srv_reposition;
296 p->lbprm.server_drop_conn = fwlc_srv_reposition;
297
298 p->lbprm.wdiv = BE_WEIGHT_SCALE;
299 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200300 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200301 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200302 }
303
304 recount_servers(p);
305 update_backend_weight(p);
306
307 p->lbprm.fwlc.act = init_head;
308 p->lbprm.fwlc.bck = init_head;
309
310 /* queue active and backup servers in two distinct groups */
311 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200312 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200313 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200314 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act;
Christopher Fauletcb33d3a2020-12-11 15:36:01 +0100315 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200316 }
317}
318
319/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
320 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200321 *
322 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200323 */
324struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
325{
326 struct server *srv, *avoided;
327 struct eb32_node *node;
328
329 srv = avoided = NULL;
330
Willy Tarreau58bc9c12020-10-17 19:32:09 +0200331 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200332 if (p->srv_act)
333 node = eb32_first(&p->lbprm.fwlc.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200334 else if (p->lbprm.fbck) {
335 srv = p->lbprm.fbck;
336 goto out;
337 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200338 else if (p->srv_bck)
339 node = eb32_first(&p->lbprm.fwlc.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200340 else {
341 srv = NULL;
342 goto out;
343 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200344
345 while (node) {
346 /* OK, we have a server. However, it may be saturated, in which
347 * case we don't want to reconsider it for now, so we'll simply
348 * skip it. Same if it's the server we try to avoid, in which
349 * case we simply remember it for later use if needed.
350 */
351 struct server *s;
352
353 s = eb32_entry(node, struct server, lb_node);
Willy Tarreau8ae8c482020-10-22 17:19:07 +0200354 if (!s->maxconn || s->served + s->nbpend < srv_dynamic_maxconn(s) + s->maxqueue) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200355 if (s != srvtoavoid) {
356 srv = s;
357 break;
358 }
359 avoided = s;
360 }
361 node = eb32_next(node);
362 }
363
364 if (!srv)
365 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200366 out:
Willy Tarreau58bc9c12020-10-17 19:32:09 +0200367 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200368 return srv;
369}
370
371
372/*
373 * Local variables:
374 * c-indent-level: 8
375 * c-basic-offset: 8
376 * End:
377 */