blob: 6b72099f3da2aa7b329f0c197169de72586bd27c [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
19#include <common/compat.h>
20#include <common/config.h>
21#include <common/debug.h>
22#include <eb32tree.h>
23
24#include <types/global.h>
25#include <types/server.h>
26
27#include <proto/backend.h>
28#include <proto/queue.h>
29
30
31/* Remove a server from a tree. It must have previously been dequeued. This
32 * function is meant to be called when a server is going down or has its
33 * weight disabled.
Willy Tarreau1b877482018-08-21 19:44:53 +020034 *
35 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf09c6602012-02-13 17:12:08 +010036 */
37static inline void fas_remove_from_tree(struct server *s)
38{
39 s->lb_tree = NULL;
40}
41
Willy Tarreau1b877482018-08-21 19:44:53 +020042/* simply removes a server from a tree.
43 *
44 * The server's lock and the lbprm's lock must be held.
45 */
Willy Tarreauf09c6602012-02-13 17:12:08 +010046static inline void fas_dequeue_srv(struct server *s)
47{
48 eb32_delete(&s->lb_node);
49}
50
51/* Queue a server in its associated tree, assuming the weight is >0.
52 * Servers are sorted by unique ID so that we send all connections to the first
53 * available server in declaration order (or ID order) until its maxconn is
54 * reached. It is important to understand that the server weight is not used
55 * here.
Willy Tarreau1b877482018-08-21 19:44:53 +020056 *
57 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf09c6602012-02-13 17:12:08 +010058 */
59static inline void fas_queue_srv(struct server *s)
60{
61 s->lb_node.key = s->puid;
62 eb32_insert(s->lb_tree, &s->lb_node);
63}
64
65/* Re-position the server in the FS tree after it has been assigned one
66 * connection or after it has released one. Note that it is possible that
67 * the server has been moved out of the tree due to failed health-checks.
Willy Tarreau1b877482018-08-21 19:44:53 +020068 *
69 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +010070 */
71static void fas_srv_reposition(struct server *s)
72{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010073 HA_SPIN_LOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Christopher Faulet45fdc6f2019-07-04 11:59:42 +020074 if (s->lb_tree) {
75 fas_dequeue_srv(s);
76 fas_queue_srv(s);
77 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +010078 HA_SPIN_UNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +010079}
80
81/* This function updates the server trees according to server <srv>'s new
82 * state. It should be called when server <srv>'s status changes to down.
83 * It is not important whether the server was already down or not. It is not
84 * important either that the new state is completely down (the caller may not
85 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020086 *
87 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +010088 */
89static void fas_set_server_status_down(struct server *srv)
90{
91 struct proxy *p = srv->proxy;
92
Willy Tarreauc5150da2014-05-13 19:27:31 +020093 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +010094 return;
95
Emeric Brun52a91d32017-08-31 14:41:55 +020096 if (srv_willbe_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +010097 goto out_update_state;
98
Willy Tarreau1b877482018-08-21 19:44:53 +020099 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
100
Emeric Brun52a91d32017-08-31 14:41:55 +0200101 if (!srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100102 /* server was already down */
103 goto out_update_backend;
104
Willy Tarreauc93cd162014-05-13 15:54:22 +0200105 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200106 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100107 p->srv_bck--;
108
109 if (srv == p->lbprm.fbck) {
110 /* we lost the first backup server in a single-backup
111 * configuration, we must search another one.
112 */
113 struct server *srv2 = p->lbprm.fbck;
114 do {
115 srv2 = srv2->next;
116 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200117 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200118 srv_willbe_usable(srv2)));
Willy Tarreauf09c6602012-02-13 17:12:08 +0100119 p->lbprm.fbck = srv2;
120 }
121 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200122 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100123 p->srv_act--;
124 }
125
126 fas_dequeue_srv(srv);
127 fas_remove_from_tree(srv);
128
Christopher Faulet5b517552017-06-09 14:17:53 +0200129 out_update_backend:
Willy Tarreauf09c6602012-02-13 17:12:08 +0100130 /* check/update tot_used, tot_weight */
131 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200132 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
133
Willy Tarreauf09c6602012-02-13 17:12:08 +0100134 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200135 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100136}
137
138/* This function updates the server trees according to server <srv>'s new
139 * state. It should be called when server <srv>'s status changes to up.
140 * It is not important whether the server was already down or not. It is not
141 * important either that the new state is completely UP (the caller may not
142 * know all the variables of a server's state). This function will not change
143 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200144 *
145 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100146 */
147static void fas_set_server_status_up(struct server *srv)
148{
149 struct proxy *p = srv->proxy;
150
Willy Tarreauc5150da2014-05-13 19:27:31 +0200151 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100152 return;
153
Emeric Brun52a91d32017-08-31 14:41:55 +0200154 if (!srv_willbe_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100155 goto out_update_state;
156
Willy Tarreau1b877482018-08-21 19:44:53 +0200157 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
158
Emeric Brun52a91d32017-08-31 14:41:55 +0200159 if (srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100160 /* server was already up */
161 goto out_update_backend;
162
Willy Tarreauc93cd162014-05-13 15:54:22 +0200163 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf09c6602012-02-13 17:12:08 +0100164 srv->lb_tree = &p->lbprm.fas.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200165 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100166 p->srv_bck++;
167
168 if (!(p->options & PR_O_USE_ALL_BK)) {
169 if (!p->lbprm.fbck) {
170 /* there was no backup server anymore */
171 p->lbprm.fbck = srv;
172 } else {
173 /* we may have restored a backup server prior to fbck,
174 * in which case it should replace it.
175 */
176 struct server *srv2 = srv;
177 do {
178 srv2 = srv2->next;
179 } while (srv2 && (srv2 != p->lbprm.fbck));
180 if (srv2)
181 p->lbprm.fbck = srv;
182 }
183 }
184 } else {
185 srv->lb_tree = &p->lbprm.fas.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200186 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100187 p->srv_act++;
188 }
189
190 /* note that eweight cannot be 0 here */
191 fas_queue_srv(srv);
192
193 out_update_backend:
194 /* check/update tot_used, tot_weight */
195 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200196 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
197
Willy Tarreauf09c6602012-02-13 17:12:08 +0100198 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200199 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100200}
201
202/* This function must be called after an update to server <srv>'s effective
203 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200204 *
205 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100206 */
207static void fas_update_server_weight(struct server *srv)
208{
209 int old_state, new_state;
210 struct proxy *p = srv->proxy;
211
Willy Tarreauc5150da2014-05-13 19:27:31 +0200212 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100213 return;
214
215 /* If changing the server's weight changes its state, we simply apply
216 * the procedures we already have for status change. If the state
217 * remains down, the server is not in any tree, so it's as easy as
218 * updating its values. If the state remains up with different weights,
219 * there are some computations to perform to find a new place and
220 * possibly a new tree for this server.
221 */
222
Emeric Brun52a91d32017-08-31 14:41:55 +0200223 old_state = srv_currently_usable(srv);
224 new_state = srv_willbe_usable(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100225
226 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200227 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100228 return;
229 }
230 else if (!old_state && new_state) {
231 fas_set_server_status_up(srv);
232 return;
233 }
234 else if (old_state && !new_state) {
235 fas_set_server_status_down(srv);
236 return;
237 }
238
Willy Tarreau1b877482018-08-21 19:44:53 +0200239 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
240
Willy Tarreauf09c6602012-02-13 17:12:08 +0100241 if (srv->lb_tree)
242 fas_dequeue_srv(srv);
243
Willy Tarreauc93cd162014-05-13 15:54:22 +0200244 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200245 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100246 srv->lb_tree = &p->lbprm.fas.bck;
247 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200248 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100249 srv->lb_tree = &p->lbprm.fas.act;
250 }
251
252 fas_queue_srv(srv);
253
254 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200255 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
256
Willy Tarreauc5150da2014-05-13 19:27:31 +0200257 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100258}
259
260/* This function is responsible for building the trees in case of fast
261 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
262 * uweight ratio. Both active and backup groups are initialized.
263 */
264void fas_init_server_tree(struct proxy *p)
265{
266 struct server *srv;
267 struct eb_root init_head = EB_ROOT;
268
269 p->lbprm.set_server_status_up = fas_set_server_status_up;
270 p->lbprm.set_server_status_down = fas_set_server_status_down;
271 p->lbprm.update_server_eweight = fas_update_server_weight;
272 p->lbprm.server_take_conn = fas_srv_reposition;
273 p->lbprm.server_drop_conn = fas_srv_reposition;
274
275 p->lbprm.wdiv = BE_WEIGHT_SCALE;
276 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200277 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200278 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100279 }
280
281 recount_servers(p);
282 update_backend_weight(p);
283
284 p->lbprm.fas.act = init_head;
285 p->lbprm.fas.bck = init_head;
286
287 /* queue active and backup servers in two distinct groups */
288 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200289 if (!srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100290 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200291 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fas.bck : &p->lbprm.fas.act;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100292 fas_queue_srv(srv);
293 }
294}
295
296/* Return next server from the FS tree in backend <p>. If the tree is empty,
297 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200298 *
299 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100300 */
301struct server *fas_get_next_server(struct proxy *p, struct server *srvtoavoid)
302{
303 struct server *srv, *avoided;
304 struct eb32_node *node;
305
306 srv = avoided = NULL;
307
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100308 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100309 if (p->srv_act)
310 node = eb32_first(&p->lbprm.fas.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200311 else if (p->lbprm.fbck) {
312 srv = p->lbprm.fbck;
313 goto out;
314 }
Willy Tarreauf09c6602012-02-13 17:12:08 +0100315 else if (p->srv_bck)
316 node = eb32_first(&p->lbprm.fas.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200317 else {
318 srv = NULL;
319 goto out;
320 }
Willy Tarreauf09c6602012-02-13 17:12:08 +0100321
322 while (node) {
323 /* OK, we have a server. However, it may be saturated, in which
324 * case we don't want to reconsider it for now, so we'll simply
325 * skip it. Same if it's the server we try to avoid, in which
326 * case we simply remember it for later use if needed.
327 */
328 struct server *s;
329
330 s = eb32_entry(node, struct server, lb_node);
331 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
332 if (s != srvtoavoid) {
333 srv = s;
334 break;
335 }
336 avoided = s;
337 }
338 node = eb32_next(node);
339 }
340
341 if (!srv)
342 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200343 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100345 return srv;
346}
347
348
349/*
350 * Local variables:
351 * c-indent-level: 8
352 * c-basic-offset: 8
353 * End:
354 */