blob: 8eac2c9304f066295d6a61b5e6af232fbc39c52a [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
Willy Tarreau1e56f922020-06-04 23:20:13 +020013#include <import/eb32tree.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <haproxy/backend.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020016#include <haproxy/queue.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020017#include <haproxy/server-t.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020018
19
20/* Remove a server from a tree. It must have previously been dequeued. This
21 * function is meant to be called when a server is going down or has its
22 * weight disabled.
Willy Tarreau1b877482018-08-21 19:44:53 +020023 *
24 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf89c1872009-10-01 11:19:37 +020025 */
26static inline void fwlc_remove_from_tree(struct server *s)
27{
28 s->lb_tree = NULL;
29}
30
Willy Tarreau1b877482018-08-21 19:44:53 +020031/* simply removes a server from a tree.
32 *
33 * The server's lock and the lbprm's lock must be held.
34 */
Willy Tarreauf89c1872009-10-01 11:19:37 +020035static inline void fwlc_dequeue_srv(struct server *s)
36{
37 eb32_delete(&s->lb_node);
38}
39
40/* Queue a server in its associated tree, assuming the weight is >0.
Willy Tarreau1eb6c552018-12-14 08:33:28 +010041 * Servers are sorted by (#conns+1)/weight. To ensure maximum accuracy,
42 * we use (#conns+1)*SRV_EWGHT_MAX/eweight as the sorting key. The reason
43 * for using #conns+1 is to sort by weights in case the server is picked
44 * and not before it is picked. This provides a better load accuracy for
45 * low connection counts when weights differ and makes sure the round-robin
Willy Tarreaued5ac9c2019-09-06 17:04:04 +020046 * applies between servers of highest weight first. However servers with no
47 * connection are always picked first so that under low loads, it's not
48 * always the single server with the highest weight that gets picked.
Willy Tarreau1b877482018-08-21 19:44:53 +020049 *
50 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf89c1872009-10-01 11:19:37 +020051 */
52static inline void fwlc_queue_srv(struct server *s)
53{
Willy Tarreaued5ac9c2019-09-06 17:04:04 +020054 s->lb_node.key = s->served ? (s->served + 1) * SRV_EWGHT_MAX / s->next_eweight : 0;
Willy Tarreauf89c1872009-10-01 11:19:37 +020055 eb32_insert(s->lb_tree, &s->lb_node);
56}
57
58/* Re-position the server in the FWLC tree after it has been assigned one
59 * connection or after it has released one. Note that it is possible that
60 * the server has been moved out of the tree due to failed health-checks.
Willy Tarreau1b877482018-08-21 19:44:53 +020061 *
62 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020063 */
64static void fwlc_srv_reposition(struct server *s)
65{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010066 HA_SPIN_LOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Christopher Faulet1ae2a882019-06-19 10:50:38 +020067 if (s->lb_tree) {
68 fwlc_dequeue_srv(s);
69 fwlc_queue_srv(s);
70 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +010071 HA_SPIN_UNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020072}
73
74/* This function updates the server trees according to server <srv>'s new
75 * state. It should be called when server <srv>'s status changes to down.
76 * It is not important whether the server was already down or not. It is not
77 * important either that the new state is completely down (the caller may not
78 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020079 *
80 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020081 */
82static void fwlc_set_server_status_down(struct server *srv)
83{
84 struct proxy *p = srv->proxy;
85
Willy Tarreauc5150da2014-05-13 19:27:31 +020086 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020087 return;
88
Emeric Brun52a91d32017-08-31 14:41:55 +020089 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020090 goto out_update_state;
Willy Tarreau1b877482018-08-21 19:44:53 +020091 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
92
Willy Tarreauf89c1872009-10-01 11:19:37 +020093
Emeric Brun52a91d32017-08-31 14:41:55 +020094 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020095 /* server was already down */
96 goto out_update_backend;
97
Willy Tarreauc93cd162014-05-13 15:54:22 +020098 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +020099 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200100 p->srv_bck--;
101
102 if (srv == p->lbprm.fbck) {
103 /* we lost the first backup server in a single-backup
104 * configuration, we must search another one.
105 */
106 struct server *srv2 = p->lbprm.fbck;
107 do {
108 srv2 = srv2->next;
109 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200110 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200111 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +0200112 p->lbprm.fbck = srv2;
113 }
114 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200115 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200116 p->srv_act--;
117 }
118
119 fwlc_dequeue_srv(srv);
120 fwlc_remove_from_tree(srv);
121
122out_update_backend:
123 /* check/update tot_used, tot_weight */
124 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200125 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
126
Willy Tarreauf89c1872009-10-01 11:19:37 +0200127 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200128 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200129}
130
131/* This function updates the server trees according to server <srv>'s new
132 * state. It should be called when server <srv>'s status changes to up.
133 * It is not important whether the server was already down or not. It is not
134 * important either that the new state is completely UP (the caller may not
135 * know all the variables of a server's state). This function will not change
136 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200137 *
138 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200139 */
140static void fwlc_set_server_status_up(struct server *srv)
141{
142 struct proxy *p = srv->proxy;
143
Willy Tarreauc5150da2014-05-13 19:27:31 +0200144 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200145 return;
146
Emeric Brun52a91d32017-08-31 14:41:55 +0200147 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200148 goto out_update_state;
149
Willy Tarreau1b877482018-08-21 19:44:53 +0200150 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
151
Emeric Brun52a91d32017-08-31 14:41:55 +0200152 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200153 /* server was already up */
154 goto out_update_backend;
155
Willy Tarreauc93cd162014-05-13 15:54:22 +0200156 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200157 srv->lb_tree = &p->lbprm.fwlc.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200158 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200159 p->srv_bck++;
160
161 if (!(p->options & PR_O_USE_ALL_BK)) {
162 if (!p->lbprm.fbck) {
163 /* there was no backup server anymore */
164 p->lbprm.fbck = srv;
165 } else {
166 /* we may have restored a backup server prior to fbck,
167 * in which case it should replace it.
168 */
169 struct server *srv2 = srv;
170 do {
171 srv2 = srv2->next;
172 } while (srv2 && (srv2 != p->lbprm.fbck));
173 if (srv2)
174 p->lbprm.fbck = srv;
175 }
176 }
177 } else {
178 srv->lb_tree = &p->lbprm.fwlc.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200179 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200180 p->srv_act++;
181 }
182
183 /* note that eweight cannot be 0 here */
184 fwlc_queue_srv(srv);
185
186 out_update_backend:
187 /* check/update tot_used, tot_weight */
188 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200189 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
190
Willy Tarreauf89c1872009-10-01 11:19:37 +0200191 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200192 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200193}
194
195/* This function must be called after an update to server <srv>'s effective
196 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200197 *
198 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200199 */
200static void fwlc_update_server_weight(struct server *srv)
201{
202 int old_state, new_state;
203 struct proxy *p = srv->proxy;
204
Willy Tarreauc5150da2014-05-13 19:27:31 +0200205 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200206 return;
207
208 /* If changing the server's weight changes its state, we simply apply
209 * the procedures we already have for status change. If the state
210 * remains down, the server is not in any tree, so it's as easy as
211 * updating its values. If the state remains up with different weights,
212 * there are some computations to perform to find a new place and
213 * possibly a new tree for this server.
214 */
215
Emeric Brun52a91d32017-08-31 14:41:55 +0200216 old_state = srv_currently_usable(srv);
217 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200218
219 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200220 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200221 return;
222 }
223 else if (!old_state && new_state) {
224 fwlc_set_server_status_up(srv);
225 return;
226 }
227 else if (old_state && !new_state) {
228 fwlc_set_server_status_down(srv);
229 return;
230 }
231
Willy Tarreau1b877482018-08-21 19:44:53 +0200232 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
233
Willy Tarreauf89c1872009-10-01 11:19:37 +0200234 if (srv->lb_tree)
235 fwlc_dequeue_srv(srv);
236
Willy Tarreauc93cd162014-05-13 15:54:22 +0200237 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200238 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200239 srv->lb_tree = &p->lbprm.fwlc.bck;
240 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200241 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200242 srv->lb_tree = &p->lbprm.fwlc.act;
243 }
244
245 fwlc_queue_srv(srv);
246
247 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200248 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
249
Willy Tarreauc5150da2014-05-13 19:27:31 +0200250 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200251}
252
253/* This function is responsible for building the trees in case of fast
254 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
255 * uweight ratio. Both active and backup groups are initialized.
256 */
257void fwlc_init_server_tree(struct proxy *p)
258{
259 struct server *srv;
260 struct eb_root init_head = EB_ROOT;
261
262 p->lbprm.set_server_status_up = fwlc_set_server_status_up;
263 p->lbprm.set_server_status_down = fwlc_set_server_status_down;
264 p->lbprm.update_server_eweight = fwlc_update_server_weight;
265 p->lbprm.server_take_conn = fwlc_srv_reposition;
266 p->lbprm.server_drop_conn = fwlc_srv_reposition;
267
268 p->lbprm.wdiv = BE_WEIGHT_SCALE;
269 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200270 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200271 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200272 }
273
274 recount_servers(p);
275 update_backend_weight(p);
276
277 p->lbprm.fwlc.act = init_head;
278 p->lbprm.fwlc.bck = init_head;
279
280 /* queue active and backup servers in two distinct groups */
281 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200282 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200283 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200284 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200285 fwlc_queue_srv(srv);
286 }
287}
288
289/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
290 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200291 *
292 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200293 */
294struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
295{
296 struct server *srv, *avoided;
297 struct eb32_node *node;
298
299 srv = avoided = NULL;
300
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100301 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200302 if (p->srv_act)
303 node = eb32_first(&p->lbprm.fwlc.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200304 else if (p->lbprm.fbck) {
305 srv = p->lbprm.fbck;
306 goto out;
307 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200308 else if (p->srv_bck)
309 node = eb32_first(&p->lbprm.fwlc.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200310 else {
311 srv = NULL;
312 goto out;
313 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200314
315 while (node) {
316 /* OK, we have a server. However, it may be saturated, in which
317 * case we don't want to reconsider it for now, so we'll simply
318 * skip it. Same if it's the server we try to avoid, in which
319 * case we simply remember it for later use if needed.
320 */
321 struct server *s;
322
323 s = eb32_entry(node, struct server, lb_node);
324 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
325 if (s != srvtoavoid) {
326 srv = s;
327 break;
328 }
329 avoided = s;
330 }
331 node = eb32_next(node);
332 }
333
334 if (!srv)
335 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200336 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100337 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200338 return srv;
339}
340
341
342/*
343 * Local variables:
344 * c-indent-level: 8
345 * c-basic-offset: 8
346 * End:
347 */