blob: ef507b7cb38f0b04976ace372654e69bd983e4ef [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 Tarreau4c7e4b72020-05-27 12:58:42 +020019#include <haproxy/api.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010020#include <common/debug.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020021#include <import/eb32tree.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010022
23#include <types/global.h>
24#include <types/server.h>
25
26#include <proto/backend.h>
27#include <proto/queue.h>
28
29
30/* Remove a server from a tree. It must have previously been dequeued. This
31 * function is meant to be called when a server is going down or has its
32 * weight disabled.
Willy Tarreau1b877482018-08-21 19:44:53 +020033 *
34 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf09c6602012-02-13 17:12:08 +010035 */
36static inline void fas_remove_from_tree(struct server *s)
37{
38 s->lb_tree = NULL;
39}
40
Willy Tarreau1b877482018-08-21 19:44:53 +020041/* simply removes a server from a tree.
42 *
43 * The server's lock and the lbprm's lock must be held.
44 */
Willy Tarreauf09c6602012-02-13 17:12:08 +010045static inline void fas_dequeue_srv(struct server *s)
46{
47 eb32_delete(&s->lb_node);
48}
49
50/* Queue a server in its associated tree, assuming the weight is >0.
51 * Servers are sorted by unique ID so that we send all connections to the first
52 * available server in declaration order (or ID order) until its maxconn is
53 * reached. It is important to understand that the server weight is not used
54 * here.
Willy Tarreau1b877482018-08-21 19:44:53 +020055 *
56 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf09c6602012-02-13 17:12:08 +010057 */
58static inline void fas_queue_srv(struct server *s)
59{
60 s->lb_node.key = s->puid;
61 eb32_insert(s->lb_tree, &s->lb_node);
62}
63
64/* Re-position the server in the FS tree after it has been assigned one
65 * connection or after it has released one. Note that it is possible that
66 * the server has been moved out of the tree due to failed health-checks.
Willy Tarreau1b877482018-08-21 19:44:53 +020067 *
68 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +010069 */
70static void fas_srv_reposition(struct server *s)
71{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010072 HA_SPIN_LOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Christopher Faulet16b2be92019-07-04 11:59:42 +020073 if (s->lb_tree) {
74 fas_dequeue_srv(s);
75 fas_queue_srv(s);
76 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +010077 HA_SPIN_UNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +010078}
79
80/* This function updates the server trees according to server <srv>'s new
81 * state. It should be called when server <srv>'s status changes to down.
82 * It is not important whether the server was already down or not. It is not
83 * important either that the new state is completely down (the caller may not
84 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020085 *
86 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +010087 */
88static void fas_set_server_status_down(struct server *srv)
89{
90 struct proxy *p = srv->proxy;
91
Willy Tarreauc5150da2014-05-13 19:27:31 +020092 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +010093 return;
94
Emeric Brun52a91d32017-08-31 14:41:55 +020095 if (srv_willbe_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +010096 goto out_update_state;
97
Willy Tarreau1b877482018-08-21 19:44:53 +020098 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
99
Emeric Brun52a91d32017-08-31 14:41:55 +0200100 if (!srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100101 /* server was already down */
102 goto out_update_backend;
103
Willy Tarreauc93cd162014-05-13 15:54:22 +0200104 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200105 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100106 p->srv_bck--;
107
108 if (srv == p->lbprm.fbck) {
109 /* we lost the first backup server in a single-backup
110 * configuration, we must search another one.
111 */
112 struct server *srv2 = p->lbprm.fbck;
113 do {
114 srv2 = srv2->next;
115 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200116 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200117 srv_willbe_usable(srv2)));
Willy Tarreauf09c6602012-02-13 17:12:08 +0100118 p->lbprm.fbck = srv2;
119 }
120 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200121 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100122 p->srv_act--;
123 }
124
125 fas_dequeue_srv(srv);
126 fas_remove_from_tree(srv);
127
Christopher Faulet5b517552017-06-09 14:17:53 +0200128 out_update_backend:
Willy Tarreauf09c6602012-02-13 17:12:08 +0100129 /* check/update tot_used, tot_weight */
130 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200131 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
132
Willy Tarreauf09c6602012-02-13 17:12:08 +0100133 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200134 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100135}
136
137/* This function updates the server trees according to server <srv>'s new
138 * state. It should be called when server <srv>'s status changes to up.
139 * It is not important whether the server was already down or not. It is not
140 * important either that the new state is completely UP (the caller may not
141 * know all the variables of a server's state). This function will not change
142 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200143 *
144 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100145 */
146static void fas_set_server_status_up(struct server *srv)
147{
148 struct proxy *p = srv->proxy;
149
Willy Tarreauc5150da2014-05-13 19:27:31 +0200150 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100151 return;
152
Emeric Brun52a91d32017-08-31 14:41:55 +0200153 if (!srv_willbe_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100154 goto out_update_state;
155
Willy Tarreau1b877482018-08-21 19:44:53 +0200156 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
157
Emeric Brun52a91d32017-08-31 14:41:55 +0200158 if (srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100159 /* server was already up */
160 goto out_update_backend;
161
Willy Tarreauc93cd162014-05-13 15:54:22 +0200162 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf09c6602012-02-13 17:12:08 +0100163 srv->lb_tree = &p->lbprm.fas.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200164 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100165 p->srv_bck++;
166
167 if (!(p->options & PR_O_USE_ALL_BK)) {
168 if (!p->lbprm.fbck) {
169 /* there was no backup server anymore */
170 p->lbprm.fbck = srv;
171 } else {
172 /* we may have restored a backup server prior to fbck,
173 * in which case it should replace it.
174 */
175 struct server *srv2 = srv;
176 do {
177 srv2 = srv2->next;
178 } while (srv2 && (srv2 != p->lbprm.fbck));
179 if (srv2)
180 p->lbprm.fbck = srv;
181 }
182 }
183 } else {
184 srv->lb_tree = &p->lbprm.fas.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200185 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100186 p->srv_act++;
187 }
188
189 /* note that eweight cannot be 0 here */
190 fas_queue_srv(srv);
191
192 out_update_backend:
193 /* check/update tot_used, tot_weight */
194 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200195 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
196
Willy Tarreauf09c6602012-02-13 17:12:08 +0100197 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200198 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100199}
200
201/* This function must be called after an update to server <srv>'s effective
202 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200203 *
204 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100205 */
206static void fas_update_server_weight(struct server *srv)
207{
208 int old_state, new_state;
209 struct proxy *p = srv->proxy;
210
Willy Tarreauc5150da2014-05-13 19:27:31 +0200211 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100212 return;
213
214 /* If changing the server's weight changes its state, we simply apply
215 * the procedures we already have for status change. If the state
216 * remains down, the server is not in any tree, so it's as easy as
217 * updating its values. If the state remains up with different weights,
218 * there are some computations to perform to find a new place and
219 * possibly a new tree for this server.
220 */
221
Emeric Brun52a91d32017-08-31 14:41:55 +0200222 old_state = srv_currently_usable(srv);
223 new_state = srv_willbe_usable(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100224
225 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200226 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100227 return;
228 }
229 else if (!old_state && new_state) {
230 fas_set_server_status_up(srv);
231 return;
232 }
233 else if (old_state && !new_state) {
234 fas_set_server_status_down(srv);
235 return;
236 }
237
Willy Tarreau1b877482018-08-21 19:44:53 +0200238 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
239
Willy Tarreauf09c6602012-02-13 17:12:08 +0100240 if (srv->lb_tree)
241 fas_dequeue_srv(srv);
242
Willy Tarreauc93cd162014-05-13 15:54:22 +0200243 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200244 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100245 srv->lb_tree = &p->lbprm.fas.bck;
246 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200247 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100248 srv->lb_tree = &p->lbprm.fas.act;
249 }
250
251 fas_queue_srv(srv);
252
253 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200254 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
255
Willy Tarreauc5150da2014-05-13 19:27:31 +0200256 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100257}
258
259/* This function is responsible for building the trees in case of fast
260 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
261 * uweight ratio. Both active and backup groups are initialized.
262 */
263void fas_init_server_tree(struct proxy *p)
264{
265 struct server *srv;
266 struct eb_root init_head = EB_ROOT;
267
268 p->lbprm.set_server_status_up = fas_set_server_status_up;
269 p->lbprm.set_server_status_down = fas_set_server_status_down;
270 p->lbprm.update_server_eweight = fas_update_server_weight;
271 p->lbprm.server_take_conn = fas_srv_reposition;
272 p->lbprm.server_drop_conn = fas_srv_reposition;
273
274 p->lbprm.wdiv = BE_WEIGHT_SCALE;
275 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200276 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200277 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100278 }
279
280 recount_servers(p);
281 update_backend_weight(p);
282
283 p->lbprm.fas.act = init_head;
284 p->lbprm.fas.bck = init_head;
285
286 /* queue active and backup servers in two distinct groups */
287 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200288 if (!srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100289 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200290 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fas.bck : &p->lbprm.fas.act;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100291 fas_queue_srv(srv);
292 }
293}
294
295/* Return next server from the FS tree in backend <p>. If the tree is empty,
296 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200297 *
298 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100299 */
300struct server *fas_get_next_server(struct proxy *p, struct server *srvtoavoid)
301{
302 struct server *srv, *avoided;
303 struct eb32_node *node;
304
305 srv = avoided = NULL;
306
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100307 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100308 if (p->srv_act)
309 node = eb32_first(&p->lbprm.fas.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200310 else if (p->lbprm.fbck) {
311 srv = p->lbprm.fbck;
312 goto out;
313 }
Willy Tarreauf09c6602012-02-13 17:12:08 +0100314 else if (p->srv_bck)
315 node = eb32_first(&p->lbprm.fas.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200316 else {
317 srv = NULL;
318 goto out;
319 }
Willy Tarreauf09c6602012-02-13 17:12:08 +0100320
321 while (node) {
322 /* OK, we have a server. However, it may be saturated, in which
323 * case we don't want to reconsider it for now, so we'll simply
324 * skip it. Same if it's the server we try to avoid, in which
325 * case we simply remember it for later use if needed.
326 */
327 struct server *s;
328
329 s = eb32_entry(node, struct server, lb_node);
330 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
331 if (s != srvtoavoid) {
332 srv = s;
333 break;
334 }
335 avoided = s;
336 }
337 node = eb32_next(node);
338 }
339
340 if (!srv)
341 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200342 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100343 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100344 return srv;
345}
346
347
348/*
349 * Local variables:
350 * c-indent-level: 8
351 * c-basic-offset: 8
352 * End:
353 */