blob: d90388b4016978b4ae75a4bf2b108377f66d5514 [file] [log] [blame]
Willy Tarreauf09c6602012-02-13 17:12:08 +01001/*
2 * First Available Server load balancing algorithm.
3 *
Willy Tarreau64559c52012-04-07 09:08:45 +02004 * This file implements an algorithm which emerged during a discussion with
5 * Steen Larsen, initially inspired from Anshul Gandhi et.al.'s work now
6 * described as "packing" in section 3.5:
7 *
8 * http://reports-archive.adm.cs.cmu.edu/anon/2012/CMU-CS-12-109.pdf
9 *
Willy Tarreauf09c6602012-02-13 17:12:08 +010010 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 */
18
Willy Tarreau1e56f922020-06-04 23:20:13 +020019#include <import/eb32tree.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020020#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/backend.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020022#include <haproxy/queue.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020023#include <haproxy/server-t.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010024
25
26/* Remove a server from a tree. It must have previously been dequeued. This
27 * function is meant to be called when a server is going down or has its
28 * weight disabled.
Willy Tarreau1b877482018-08-21 19:44:53 +020029 *
30 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf09c6602012-02-13 17:12:08 +010031 */
32static inline void fas_remove_from_tree(struct server *s)
33{
34 s->lb_tree = NULL;
35}
36
Willy Tarreau1b877482018-08-21 19:44:53 +020037/* simply removes a server from a tree.
38 *
Willy Tarreau6b96e0e2021-02-17 16:15:23 +010039 * The lbprm's lock must be held.
Willy Tarreau1b877482018-08-21 19:44:53 +020040 */
Willy Tarreauf09c6602012-02-13 17:12:08 +010041static inline void fas_dequeue_srv(struct server *s)
42{
43 eb32_delete(&s->lb_node);
44}
45
46/* Queue a server in its associated tree, assuming the weight is >0.
47 * Servers are sorted by unique ID so that we send all connections to the first
48 * available server in declaration order (or ID order) until its maxconn is
49 * reached. It is important to understand that the server weight is not used
50 * here.
Willy Tarreau1b877482018-08-21 19:44:53 +020051 *
Willy Tarreau6b96e0e2021-02-17 16:15:23 +010052 * The lbprm's lock must be held.
Willy Tarreauf09c6602012-02-13 17:12:08 +010053 */
54static inline void fas_queue_srv(struct server *s)
55{
56 s->lb_node.key = s->puid;
57 eb32_insert(s->lb_tree, &s->lb_node);
58}
59
60/* Re-position the server in the FS tree after it has been assigned one
61 * connection or after it has released one. Note that it is possible that
62 * the server has been moved out of the tree due to failed health-checks.
Willy Tarreau5941ef02021-06-18 18:29:25 +020063 * The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +010064 */
Willy Tarreau5941ef02021-06-18 18:29:25 +020065static void fas_srv_reposition(struct server *s)
Willy Tarreauf09c6602012-02-13 17:12:08 +010066{
Willy Tarreaucd10def2020-10-17 18:48:47 +020067 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Christopher Faulet16b2be92019-07-04 11:59:42 +020068 if (s->lb_tree) {
69 fas_dequeue_srv(s);
70 fas_queue_srv(s);
71 }
Willy Tarreaucd10def2020-10-17 18:48:47 +020072 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +010073}
74
75/* This function updates the server trees according to server <srv>'s new
76 * state. It should be called when server <srv>'s status changes to down.
77 * It is not important whether the server was already down or not. It is not
78 * important either that the new state is completely down (the caller may not
79 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020080 *
81 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +010082 */
83static void fas_set_server_status_down(struct server *srv)
84{
85 struct proxy *p = srv->proxy;
86
Willy Tarreauc5150da2014-05-13 19:27:31 +020087 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +010088 return;
89
Emeric Brun52a91d32017-08-31 14:41:55 +020090 if (srv_willbe_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +010091 goto out_update_state;
92
Willy Tarreaucd10def2020-10-17 18:48:47 +020093 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +020094
Emeric Brun52a91d32017-08-31 14:41:55 +020095 if (!srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +010096 /* server was already down */
97 goto out_update_backend;
98
Willy Tarreauc93cd162014-05-13 15:54:22 +020099 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200100 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100101 p->srv_bck--;
102
103 if (srv == p->lbprm.fbck) {
104 /* we lost the first backup server in a single-backup
105 * configuration, we must search another one.
106 */
107 struct server *srv2 = p->lbprm.fbck;
108 do {
109 srv2 = srv2->next;
110 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200111 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200112 srv_willbe_usable(srv2)));
Willy Tarreauf09c6602012-02-13 17:12:08 +0100113 p->lbprm.fbck = srv2;
114 }
115 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200116 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100117 p->srv_act--;
118 }
119
120 fas_dequeue_srv(srv);
121 fas_remove_from_tree(srv);
122
Christopher Faulet5b517552017-06-09 14:17:53 +0200123 out_update_backend:
Willy Tarreauf09c6602012-02-13 17:12:08 +0100124 /* check/update tot_used, tot_weight */
125 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200126 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200127
Willy Tarreauf09c6602012-02-13 17:12:08 +0100128 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200129 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100130}
131
132/* This function updates the server trees according to server <srv>'s new
133 * state. It should be called when server <srv>'s status changes to up.
134 * It is not important whether the server was already down or not. It is not
135 * important either that the new state is completely UP (the caller may not
136 * know all the variables of a server's state). This function will not change
137 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200138 *
139 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100140 */
141static void fas_set_server_status_up(struct server *srv)
142{
143 struct proxy *p = srv->proxy;
144
Willy Tarreauc5150da2014-05-13 19:27:31 +0200145 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100146 return;
147
Emeric Brun52a91d32017-08-31 14:41:55 +0200148 if (!srv_willbe_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100149 goto out_update_state;
150
Willy Tarreaucd10def2020-10-17 18:48:47 +0200151 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200152
Emeric Brun52a91d32017-08-31 14:41:55 +0200153 if (srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100154 /* server was already up */
155 goto out_update_backend;
156
Willy Tarreauc93cd162014-05-13 15:54:22 +0200157 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf09c6602012-02-13 17:12:08 +0100158 srv->lb_tree = &p->lbprm.fas.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200159 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100160 p->srv_bck++;
161
162 if (!(p->options & PR_O_USE_ALL_BK)) {
163 if (!p->lbprm.fbck) {
164 /* there was no backup server anymore */
165 p->lbprm.fbck = srv;
166 } else {
167 /* we may have restored a backup server prior to fbck,
168 * in which case it should replace it.
169 */
170 struct server *srv2 = srv;
171 do {
172 srv2 = srv2->next;
173 } while (srv2 && (srv2 != p->lbprm.fbck));
174 if (srv2)
175 p->lbprm.fbck = srv;
176 }
177 }
178 } else {
179 srv->lb_tree = &p->lbprm.fas.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200180 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100181 p->srv_act++;
182 }
183
184 /* note that eweight cannot be 0 here */
185 fas_queue_srv(srv);
186
187 out_update_backend:
188 /* check/update tot_used, tot_weight */
189 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200190 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200191
Willy Tarreauf09c6602012-02-13 17:12:08 +0100192 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200193 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100194}
195
196/* This function must be called after an update to server <srv>'s effective
197 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200198 *
199 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100200 */
201static void fas_update_server_weight(struct server *srv)
202{
203 int old_state, new_state;
204 struct proxy *p = srv->proxy;
205
Willy Tarreauc5150da2014-05-13 19:27:31 +0200206 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100207 return;
208
209 /* If changing the server's weight changes its state, we simply apply
210 * the procedures we already have for status change. If the state
211 * remains down, the server is not in any tree, so it's as easy as
212 * updating its values. If the state remains up with different weights,
213 * there are some computations to perform to find a new place and
214 * possibly a new tree for this server.
215 */
216
Emeric Brun52a91d32017-08-31 14:41:55 +0200217 old_state = srv_currently_usable(srv);
218 new_state = srv_willbe_usable(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100219
220 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200221 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100222 return;
223 }
224 else if (!old_state && new_state) {
225 fas_set_server_status_up(srv);
226 return;
227 }
228 else if (old_state && !new_state) {
229 fas_set_server_status_down(srv);
230 return;
231 }
232
Willy Tarreaucd10def2020-10-17 18:48:47 +0200233 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200234
Willy Tarreauf09c6602012-02-13 17:12:08 +0100235 if (srv->lb_tree)
236 fas_dequeue_srv(srv);
237
Willy Tarreauc93cd162014-05-13 15:54:22 +0200238 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200239 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100240 srv->lb_tree = &p->lbprm.fas.bck;
241 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200242 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100243 srv->lb_tree = &p->lbprm.fas.act;
244 }
245
246 fas_queue_srv(srv);
247
248 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200249 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200250
Willy Tarreauc5150da2014-05-13 19:27:31 +0200251 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100252}
253
254/* This function is responsible for building the trees in case of fast
255 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
256 * uweight ratio. Both active and backup groups are initialized.
257 */
258void fas_init_server_tree(struct proxy *p)
259{
260 struct server *srv;
261 struct eb_root init_head = EB_ROOT;
262
263 p->lbprm.set_server_status_up = fas_set_server_status_up;
264 p->lbprm.set_server_status_down = fas_set_server_status_down;
265 p->lbprm.update_server_eweight = fas_update_server_weight;
266 p->lbprm.server_take_conn = fas_srv_reposition;
267 p->lbprm.server_drop_conn = fas_srv_reposition;
268
269 p->lbprm.wdiv = BE_WEIGHT_SCALE;
270 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200271 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200272 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100273 }
274
275 recount_servers(p);
276 update_backend_weight(p);
277
278 p->lbprm.fas.act = init_head;
279 p->lbprm.fas.bck = init_head;
280
281 /* queue active and backup servers in two distinct groups */
282 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200283 if (!srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100284 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200285 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fas.bck : &p->lbprm.fas.act;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100286 fas_queue_srv(srv);
287 }
288}
289
290/* Return next server from the FS tree in backend <p>. If the tree is empty,
291 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200292 *
Willy Tarreau63b3ae72021-06-01 16:58:31 +0200293 * The lbprm's lock will be used. The server's lock is not used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100294 */
295struct server *fas_get_next_server(struct proxy *p, struct server *srvtoavoid)
296{
297 struct server *srv, *avoided;
298 struct eb32_node *node;
299
300 srv = avoided = NULL;
301
Willy Tarreauf76a21f2020-10-17 19:45:42 +0200302 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100303 if (p->srv_act)
304 node = eb32_first(&p->lbprm.fas.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200305 else if (p->lbprm.fbck) {
306 srv = p->lbprm.fbck;
307 goto out;
308 }
Willy Tarreauf09c6602012-02-13 17:12:08 +0100309 else if (p->srv_bck)
310 node = eb32_first(&p->lbprm.fas.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200311 else {
312 srv = NULL;
313 goto out;
314 }
Willy Tarreauf09c6602012-02-13 17:12:08 +0100315
316 while (node) {
317 /* OK, we have a server. However, it may be saturated, in which
318 * case we don't want to reconsider it for now, so we'll simply
319 * skip it. Same if it's the server we try to avoid, in which
320 * case we simply remember it for later use if needed.
321 */
322 struct server *s;
323
324 s = eb32_entry(node, struct server, lb_node);
Willy Tarreaua0570452021-06-18 09:30:30 +0200325 if (!s->maxconn || (!s->queue.length && s->served < srv_dynamic_maxconn(s))) {
Willy Tarreauf09c6602012-02-13 17:12:08 +0100326 if (s != srvtoavoid) {
327 srv = s;
328 break;
329 }
330 avoided = s;
331 }
332 node = eb32_next(node);
333 }
334
335 if (!srv)
336 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200337 out:
Willy Tarreauf76a21f2020-10-17 19:45:42 +0200338 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100339 return srv;
340}
341
342
343/*
344 * Local variables:
345 * c-indent-level: 8
346 * c-basic-offset: 8
347 * End:
348 */