blob: c0f74bf7a776bb168c29bcc63ef8a69684045d65 [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
13#include <common/compat.h>
14#include <common/config.h>
15#include <common/debug.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010016#include <eb32tree.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020017
18#include <types/global.h>
19#include <types/server.h>
20
21#include <proto/backend.h>
22#include <proto/queue.h>
23
24
25/* Remove a server from a tree. It must have previously been dequeued. This
26 * function is meant to be called when a server is going down or has its
27 * weight disabled.
Willy Tarreau1b877482018-08-21 19:44:53 +020028 *
29 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf89c1872009-10-01 11:19:37 +020030 */
31static inline void fwlc_remove_from_tree(struct server *s)
32{
33 s->lb_tree = NULL;
34}
35
Willy Tarreau1b877482018-08-21 19:44:53 +020036/* simply removes a server from a tree.
37 *
38 * The server's lock and the lbprm's lock must be held.
39 */
Willy Tarreauf89c1872009-10-01 11:19:37 +020040static inline void fwlc_dequeue_srv(struct server *s)
41{
42 eb32_delete(&s->lb_node);
43}
44
Christopher Faulet2ecaf842020-12-11 15:36:01 +010045/* Queue a server in its associated tree, assuming the <eweight> is >0.
Willy Tarreau1eb6c552018-12-14 08:33:28 +010046 * Servers are sorted by (#conns+1)/weight. To ensure maximum accuracy,
47 * we use (#conns+1)*SRV_EWGHT_MAX/eweight as the sorting key. The reason
48 * for using #conns+1 is to sort by weights in case the server is picked
49 * and not before it is picked. This provides a better load accuracy for
50 * low connection counts when weights differ and makes sure the round-robin
Willy Tarreau345ddf42019-09-06 17:04:04 +020051 * applies between servers of highest weight first. However servers with no
52 * connection are always picked first so that under low loads, it's not
53 * always the single server with the highest weight that gets picked.
Willy Tarreau1b877482018-08-21 19:44:53 +020054 *
Christopher Faulet2ecaf842020-12-11 15:36:01 +010055 * NOTE: Depending on the calling context, we use s->next_eweight or
56 * s->cur_eweight. The next value is used when the server state is updated
57 * (because the weight changed for instance). During this step, the server
58 * state is not yet committed. The current value is used to reposition the
59 * server in the tree. This happens when the server is used.
60 *
Willy Tarreau1b877482018-08-21 19:44:53 +020061 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf89c1872009-10-01 11:19:37 +020062 */
Christopher Faulet2ecaf842020-12-11 15:36:01 +010063static inline void fwlc_queue_srv(struct server *s, unsigned int eweight)
Willy Tarreauf89c1872009-10-01 11:19:37 +020064{
Christopher Faulet2ecaf842020-12-11 15:36:01 +010065 s->lb_node.key = s->served ? (s->served + 1) * SRV_EWGHT_MAX / eweight : 0;
Willy Tarreauf89c1872009-10-01 11:19:37 +020066 eb32_insert(s->lb_tree, &s->lb_node);
67}
68
69/* Re-position the server in the FWLC tree after it has been assigned one
70 * connection or after it has released one. Note that it is possible that
71 * the server has been moved out of the tree due to failed health-checks.
Willy Tarreau1b877482018-08-21 19:44:53 +020072 *
73 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020074 */
75static void fwlc_srv_reposition(struct server *s)
76{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010077 HA_SPIN_LOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Christopher Faulet3f0b1de2019-06-19 10:50:38 +020078 if (s->lb_tree) {
79 fwlc_dequeue_srv(s);
Christopher Faulet2ecaf842020-12-11 15:36:01 +010080 fwlc_queue_srv(s, s->cur_eweight);
Christopher Faulet3f0b1de2019-06-19 10:50:38 +020081 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +010082 HA_SPIN_UNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020083}
84
85/* This function updates the server trees according to server <srv>'s new
86 * state. It should be called when server <srv>'s status changes to down.
87 * It is not important whether the server was already down or not. It is not
88 * important either that the new state is completely down (the caller may not
89 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020090 *
91 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020092 */
93static void fwlc_set_server_status_down(struct server *srv)
94{
95 struct proxy *p = srv->proxy;
96
Willy Tarreauc5150da2014-05-13 19:27:31 +020097 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020098 return;
99
Emeric Brun52a91d32017-08-31 14:41:55 +0200100 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200101 goto out_update_state;
Willy Tarreau1b877482018-08-21 19:44:53 +0200102 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
103
Willy Tarreauf89c1872009-10-01 11:19:37 +0200104
Emeric Brun52a91d32017-08-31 14:41:55 +0200105 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200106 /* server was already down */
107 goto out_update_backend;
108
Willy Tarreauc93cd162014-05-13 15:54:22 +0200109 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200110 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200111 p->srv_bck--;
112
113 if (srv == p->lbprm.fbck) {
114 /* we lost the first backup server in a single-backup
115 * configuration, we must search another one.
116 */
117 struct server *srv2 = p->lbprm.fbck;
118 do {
119 srv2 = srv2->next;
120 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200121 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200122 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +0200123 p->lbprm.fbck = srv2;
124 }
125 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200126 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200127 p->srv_act--;
128 }
129
130 fwlc_dequeue_srv(srv);
131 fwlc_remove_from_tree(srv);
132
133out_update_backend:
134 /* check/update tot_used, tot_weight */
135 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200136 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
137
Willy Tarreauf89c1872009-10-01 11:19:37 +0200138 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200139 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200140}
141
142/* This function updates the server trees according to server <srv>'s new
143 * state. It should be called when server <srv>'s status changes to up.
144 * It is not important whether the server was already down or not. It is not
145 * important either that the new state is completely UP (the caller may not
146 * know all the variables of a server's state). This function will not change
147 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200148 *
149 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200150 */
151static void fwlc_set_server_status_up(struct server *srv)
152{
153 struct proxy *p = srv->proxy;
154
Willy Tarreauc5150da2014-05-13 19:27:31 +0200155 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200156 return;
157
Emeric Brun52a91d32017-08-31 14:41:55 +0200158 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200159 goto out_update_state;
160
Willy Tarreau1b877482018-08-21 19:44:53 +0200161 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
162
Emeric Brun52a91d32017-08-31 14:41:55 +0200163 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200164 /* server was already up */
165 goto out_update_backend;
166
Willy Tarreauc93cd162014-05-13 15:54:22 +0200167 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200168 srv->lb_tree = &p->lbprm.fwlc.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200169 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200170 p->srv_bck++;
171
172 if (!(p->options & PR_O_USE_ALL_BK)) {
173 if (!p->lbprm.fbck) {
174 /* there was no backup server anymore */
175 p->lbprm.fbck = srv;
176 } else {
177 /* we may have restored a backup server prior to fbck,
178 * in which case it should replace it.
179 */
180 struct server *srv2 = srv;
181 do {
182 srv2 = srv2->next;
183 } while (srv2 && (srv2 != p->lbprm.fbck));
184 if (srv2)
185 p->lbprm.fbck = srv;
186 }
187 }
188 } else {
189 srv->lb_tree = &p->lbprm.fwlc.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200190 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200191 p->srv_act++;
192 }
193
194 /* note that eweight cannot be 0 here */
Christopher Faulet2ecaf842020-12-11 15:36:01 +0100195 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200196
197 out_update_backend:
198 /* check/update tot_used, tot_weight */
199 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200200 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
201
Willy Tarreauf89c1872009-10-01 11:19:37 +0200202 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200203 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200204}
205
206/* This function must be called after an update to server <srv>'s effective
207 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200208 *
209 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200210 */
211static void fwlc_update_server_weight(struct server *srv)
212{
213 int old_state, new_state;
214 struct proxy *p = srv->proxy;
215
Willy Tarreauc5150da2014-05-13 19:27:31 +0200216 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200217 return;
218
219 /* If changing the server's weight changes its state, we simply apply
220 * the procedures we already have for status change. If the state
221 * remains down, the server is not in any tree, so it's as easy as
222 * updating its values. If the state remains up with different weights,
223 * there are some computations to perform to find a new place and
224 * possibly a new tree for this server.
225 */
226
Emeric Brun52a91d32017-08-31 14:41:55 +0200227 old_state = srv_currently_usable(srv);
228 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200229
230 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200231 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200232 return;
233 }
234 else if (!old_state && new_state) {
235 fwlc_set_server_status_up(srv);
236 return;
237 }
238 else if (old_state && !new_state) {
239 fwlc_set_server_status_down(srv);
240 return;
241 }
242
Willy Tarreau1b877482018-08-21 19:44:53 +0200243 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
244
Willy Tarreauf89c1872009-10-01 11:19:37 +0200245 if (srv->lb_tree)
246 fwlc_dequeue_srv(srv);
247
Willy Tarreauc93cd162014-05-13 15:54:22 +0200248 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200249 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200250 srv->lb_tree = &p->lbprm.fwlc.bck;
251 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200252 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200253 srv->lb_tree = &p->lbprm.fwlc.act;
254 }
255
Christopher Faulet2ecaf842020-12-11 15:36:01 +0100256 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200257
258 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200259 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
260
Willy Tarreauc5150da2014-05-13 19:27:31 +0200261 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200262}
263
264/* This function is responsible for building the trees in case of fast
265 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
266 * uweight ratio. Both active and backup groups are initialized.
267 */
268void fwlc_init_server_tree(struct proxy *p)
269{
270 struct server *srv;
271 struct eb_root init_head = EB_ROOT;
272
273 p->lbprm.set_server_status_up = fwlc_set_server_status_up;
274 p->lbprm.set_server_status_down = fwlc_set_server_status_down;
275 p->lbprm.update_server_eweight = fwlc_update_server_weight;
276 p->lbprm.server_take_conn = fwlc_srv_reposition;
277 p->lbprm.server_drop_conn = fwlc_srv_reposition;
278
279 p->lbprm.wdiv = BE_WEIGHT_SCALE;
280 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200281 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200282 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200283 }
284
285 recount_servers(p);
286 update_backend_weight(p);
287
288 p->lbprm.fwlc.act = init_head;
289 p->lbprm.fwlc.bck = init_head;
290
291 /* queue active and backup servers in two distinct groups */
292 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200293 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200294 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200295 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act;
Christopher Faulet2ecaf842020-12-11 15:36:01 +0100296 fwlc_queue_srv(srv, srv->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200297 }
298}
299
300/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
301 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200302 *
303 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200304 */
305struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
306{
307 struct server *srv, *avoided;
308 struct eb32_node *node;
309
310 srv = avoided = NULL;
311
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100312 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200313 if (p->srv_act)
314 node = eb32_first(&p->lbprm.fwlc.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200315 else if (p->lbprm.fbck) {
316 srv = p->lbprm.fbck;
317 goto out;
318 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200319 else if (p->srv_bck)
320 node = eb32_first(&p->lbprm.fwlc.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200321 else {
322 srv = NULL;
323 goto out;
324 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200325
326 while (node) {
327 /* OK, we have a server. However, it may be saturated, in which
328 * case we don't want to reconsider it for now, so we'll simply
329 * skip it. Same if it's the server we try to avoid, in which
330 * case we simply remember it for later use if needed.
331 */
332 struct server *s;
333
334 s = eb32_entry(node, struct server, lb_node);
335 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
336 if (s != srvtoavoid) {
337 srv = s;
338 break;
339 }
340 avoided = s;
341 }
342 node = eb32_next(node);
343 }
344
345 if (!srv)
346 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200347 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100348 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200349 return srv;
350}
351
352
353/*
354 * Local variables:
355 * c-indent-level: 8
356 * c-basic-offset: 8
357 * End:
358 */