blob: 69b85d72df1577eb9508abd8fd17abe899ebcad9 [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{
73 if (!s->lb_tree)
74 return;
Christopher Faulet5b517552017-06-09 14:17:53 +020075
Christopher Faulet2a944ee2017-11-07 10:42:54 +010076 HA_SPIN_LOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +010077 fas_dequeue_srv(s);
78 fas_queue_srv(s);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010079 HA_SPIN_UNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +010080}
81
82/* This function updates the server trees according to server <srv>'s new
83 * state. It should be called when server <srv>'s status changes to down.
84 * It is not important whether the server was already down or not. It is not
85 * important either that the new state is completely down (the caller may not
86 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020087 *
88 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +010089 */
90static void fas_set_server_status_down(struct server *srv)
91{
92 struct proxy *p = srv->proxy;
93
Willy Tarreauc5150da2014-05-13 19:27:31 +020094 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +010095 return;
96
Emeric Brun52a91d32017-08-31 14:41:55 +020097 if (srv_willbe_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +010098 goto out_update_state;
99
Willy Tarreau1b877482018-08-21 19:44:53 +0200100 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
101
Emeric Brun52a91d32017-08-31 14:41:55 +0200102 if (!srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100103 /* server was already down */
104 goto out_update_backend;
105
Willy Tarreauc93cd162014-05-13 15:54:22 +0200106 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200107 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100108 p->srv_bck--;
109
110 if (srv == p->lbprm.fbck) {
111 /* we lost the first backup server in a single-backup
112 * configuration, we must search another one.
113 */
114 struct server *srv2 = p->lbprm.fbck;
115 do {
116 srv2 = srv2->next;
117 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200118 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200119 srv_willbe_usable(srv2)));
Willy Tarreauf09c6602012-02-13 17:12:08 +0100120 p->lbprm.fbck = srv2;
121 }
122 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200123 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100124 p->srv_act--;
125 }
126
127 fas_dequeue_srv(srv);
128 fas_remove_from_tree(srv);
129
Christopher Faulet5b517552017-06-09 14:17:53 +0200130 out_update_backend:
Willy Tarreauf09c6602012-02-13 17:12:08 +0100131 /* check/update tot_used, tot_weight */
132 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200133 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
134
Willy Tarreauf09c6602012-02-13 17:12:08 +0100135 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200136 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100137}
138
139/* This function updates the server trees according to server <srv>'s new
140 * state. It should be called when server <srv>'s status changes to up.
141 * It is not important whether the server was already down or not. It is not
142 * important either that the new state is completely UP (the caller may not
143 * know all the variables of a server's state). This function will not change
144 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200145 *
146 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100147 */
148static void fas_set_server_status_up(struct server *srv)
149{
150 struct proxy *p = srv->proxy;
151
Willy Tarreauc5150da2014-05-13 19:27:31 +0200152 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100153 return;
154
Emeric Brun52a91d32017-08-31 14:41:55 +0200155 if (!srv_willbe_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100156 goto out_update_state;
157
Willy Tarreau1b877482018-08-21 19:44:53 +0200158 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
159
Emeric Brun52a91d32017-08-31 14:41:55 +0200160 if (srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100161 /* server was already up */
162 goto out_update_backend;
163
Willy Tarreauc93cd162014-05-13 15:54:22 +0200164 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf09c6602012-02-13 17:12:08 +0100165 srv->lb_tree = &p->lbprm.fas.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200166 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100167 p->srv_bck++;
168
169 if (!(p->options & PR_O_USE_ALL_BK)) {
170 if (!p->lbprm.fbck) {
171 /* there was no backup server anymore */
172 p->lbprm.fbck = srv;
173 } else {
174 /* we may have restored a backup server prior to fbck,
175 * in which case it should replace it.
176 */
177 struct server *srv2 = srv;
178 do {
179 srv2 = srv2->next;
180 } while (srv2 && (srv2 != p->lbprm.fbck));
181 if (srv2)
182 p->lbprm.fbck = srv;
183 }
184 }
185 } else {
186 srv->lb_tree = &p->lbprm.fas.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200187 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100188 p->srv_act++;
189 }
190
191 /* note that eweight cannot be 0 here */
192 fas_queue_srv(srv);
193
194 out_update_backend:
195 /* check/update tot_used, tot_weight */
196 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200197 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
198
Willy Tarreauf09c6602012-02-13 17:12:08 +0100199 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200200 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100201}
202
203/* This function must be called after an update to server <srv>'s effective
204 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200205 *
206 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100207 */
208static void fas_update_server_weight(struct server *srv)
209{
210 int old_state, new_state;
211 struct proxy *p = srv->proxy;
212
Willy Tarreauc5150da2014-05-13 19:27:31 +0200213 if (!srv_lb_status_changed(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100214 return;
215
216 /* If changing the server's weight changes its state, we simply apply
217 * the procedures we already have for status change. If the state
218 * remains down, the server is not in any tree, so it's as easy as
219 * updating its values. If the state remains up with different weights,
220 * there are some computations to perform to find a new place and
221 * possibly a new tree for this server.
222 */
223
Emeric Brun52a91d32017-08-31 14:41:55 +0200224 old_state = srv_currently_usable(srv);
225 new_state = srv_willbe_usable(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100226
227 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200228 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100229 return;
230 }
231 else if (!old_state && new_state) {
232 fas_set_server_status_up(srv);
233 return;
234 }
235 else if (old_state && !new_state) {
236 fas_set_server_status_down(srv);
237 return;
238 }
239
Willy Tarreau1b877482018-08-21 19:44:53 +0200240 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
241
Willy Tarreauf09c6602012-02-13 17:12:08 +0100242 if (srv->lb_tree)
243 fas_dequeue_srv(srv);
244
Willy Tarreauc93cd162014-05-13 15:54:22 +0200245 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200246 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100247 srv->lb_tree = &p->lbprm.fas.bck;
248 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200249 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100250 srv->lb_tree = &p->lbprm.fas.act;
251 }
252
253 fas_queue_srv(srv);
254
255 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200256 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
257
Willy Tarreauc5150da2014-05-13 19:27:31 +0200258 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100259}
260
261/* This function is responsible for building the trees in case of fast
262 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
263 * uweight ratio. Both active and backup groups are initialized.
264 */
265void fas_init_server_tree(struct proxy *p)
266{
267 struct server *srv;
268 struct eb_root init_head = EB_ROOT;
269
270 p->lbprm.set_server_status_up = fas_set_server_status_up;
271 p->lbprm.set_server_status_down = fas_set_server_status_down;
272 p->lbprm.update_server_eweight = fas_update_server_weight;
273 p->lbprm.server_take_conn = fas_srv_reposition;
274 p->lbprm.server_drop_conn = fas_srv_reposition;
275
276 p->lbprm.wdiv = BE_WEIGHT_SCALE;
277 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200278 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200279 srv_lb_commit_status(srv);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100280 }
281
282 recount_servers(p);
283 update_backend_weight(p);
284
285 p->lbprm.fas.act = init_head;
286 p->lbprm.fas.bck = init_head;
287
288 /* queue active and backup servers in two distinct groups */
289 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200290 if (!srv_currently_usable(srv))
Willy Tarreauf09c6602012-02-13 17:12:08 +0100291 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200292 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fas.bck : &p->lbprm.fas.act;
Willy Tarreauf09c6602012-02-13 17:12:08 +0100293 fas_queue_srv(srv);
294 }
295}
296
297/* Return next server from the FS tree in backend <p>. If the tree is empty,
298 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200299 *
300 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf09c6602012-02-13 17:12:08 +0100301 */
302struct server *fas_get_next_server(struct proxy *p, struct server *srvtoavoid)
303{
304 struct server *srv, *avoided;
305 struct eb32_node *node;
306
307 srv = avoided = NULL;
308
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100309 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100310 if (p->srv_act)
311 node = eb32_first(&p->lbprm.fas.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200312 else if (p->lbprm.fbck) {
313 srv = p->lbprm.fbck;
314 goto out;
315 }
Willy Tarreauf09c6602012-02-13 17:12:08 +0100316 else if (p->srv_bck)
317 node = eb32_first(&p->lbprm.fas.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200318 else {
319 srv = NULL;
320 goto out;
321 }
Willy Tarreauf09c6602012-02-13 17:12:08 +0100322
323 while (node) {
324 /* OK, we have a server. However, it may be saturated, in which
325 * case we don't want to reconsider it for now, so we'll simply
326 * skip it. Same if it's the server we try to avoid, in which
327 * case we simply remember it for later use if needed.
328 */
329 struct server *s;
330
331 s = eb32_entry(node, struct server, lb_node);
332 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
333 if (s != srvtoavoid) {
334 srv = s;
335 break;
336 }
337 avoided = s;
338 }
339 node = eb32_next(node);
340 }
341
342 if (!srv)
343 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200344 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100345 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf09c6602012-02-13 17:12:08 +0100346 return srv;
347}
348
349
350/*
351 * Local variables:
352 * c-indent-level: 8
353 * c-basic-offset: 8
354 * End:
355 */