blob: 5fa8173998e24fe00740c0909e7be2d3cebf9f3a [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
45/* Queue a server in its associated tree, assuming the weight 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
51 * applies between servers of highest weight first.
Willy Tarreau1b877482018-08-21 19:44:53 +020052 *
53 * The server's lock and the lbprm's lock must be held.
Willy Tarreauf89c1872009-10-01 11:19:37 +020054 */
55static inline void fwlc_queue_srv(struct server *s)
56{
Willy Tarreau1eb6c552018-12-14 08:33:28 +010057 s->lb_node.key = (s->served + 1) * SRV_EWGHT_MAX / s->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +020058 eb32_insert(s->lb_tree, &s->lb_node);
59}
60
61/* Re-position the server in the FWLC tree after it has been assigned one
62 * connection or after it has released one. Note that it is possible that
63 * the server has been moved out of the tree due to failed health-checks.
Willy Tarreau1b877482018-08-21 19:44:53 +020064 *
65 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020066 */
67static void fwlc_srv_reposition(struct server *s)
68{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010069 HA_SPIN_LOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Christopher Faulet3f0b1de2019-06-19 10:50:38 +020070 if (s->lb_tree) {
71 fwlc_dequeue_srv(s);
72 fwlc_queue_srv(s);
73 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +010074 HA_SPIN_UNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020075}
76
77/* This function updates the server trees according to server <srv>'s new
78 * state. It should be called when server <srv>'s status changes to down.
79 * It is not important whether the server was already down or not. It is not
80 * important either that the new state is completely down (the caller may not
81 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020082 *
83 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020084 */
85static void fwlc_set_server_status_down(struct server *srv)
86{
87 struct proxy *p = srv->proxy;
88
Willy Tarreauc5150da2014-05-13 19:27:31 +020089 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020090 return;
91
Emeric Brun52a91d32017-08-31 14:41:55 +020092 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020093 goto out_update_state;
Willy Tarreau1b877482018-08-21 19:44:53 +020094 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
95
Willy Tarreauf89c1872009-10-01 11:19:37 +020096
Emeric Brun52a91d32017-08-31 14:41:55 +020097 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020098 /* server was already down */
99 goto out_update_backend;
100
Willy Tarreauc93cd162014-05-13 15:54:22 +0200101 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200102 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200103 p->srv_bck--;
104
105 if (srv == p->lbprm.fbck) {
106 /* we lost the first backup server in a single-backup
107 * configuration, we must search another one.
108 */
109 struct server *srv2 = p->lbprm.fbck;
110 do {
111 srv2 = srv2->next;
112 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200113 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200114 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +0200115 p->lbprm.fbck = srv2;
116 }
117 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200118 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200119 p->srv_act--;
120 }
121
122 fwlc_dequeue_srv(srv);
123 fwlc_remove_from_tree(srv);
124
125out_update_backend:
126 /* check/update tot_used, tot_weight */
127 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200128 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
129
Willy Tarreauf89c1872009-10-01 11:19:37 +0200130 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200131 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200132}
133
134/* This function updates the server trees according to server <srv>'s new
135 * state. It should be called when server <srv>'s status changes to up.
136 * It is not important whether the server was already down or not. It is not
137 * important either that the new state is completely UP (the caller may not
138 * know all the variables of a server's state). This function will not change
139 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200140 *
141 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200142 */
143static void fwlc_set_server_status_up(struct server *srv)
144{
145 struct proxy *p = srv->proxy;
146
Willy Tarreauc5150da2014-05-13 19:27:31 +0200147 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200148 return;
149
Emeric Brun52a91d32017-08-31 14:41:55 +0200150 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200151 goto out_update_state;
152
Willy Tarreau1b877482018-08-21 19:44:53 +0200153 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
154
Emeric Brun52a91d32017-08-31 14:41:55 +0200155 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200156 /* server was already up */
157 goto out_update_backend;
158
Willy Tarreauc93cd162014-05-13 15:54:22 +0200159 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200160 srv->lb_tree = &p->lbprm.fwlc.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200161 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200162 p->srv_bck++;
163
164 if (!(p->options & PR_O_USE_ALL_BK)) {
165 if (!p->lbprm.fbck) {
166 /* there was no backup server anymore */
167 p->lbprm.fbck = srv;
168 } else {
169 /* we may have restored a backup server prior to fbck,
170 * in which case it should replace it.
171 */
172 struct server *srv2 = srv;
173 do {
174 srv2 = srv2->next;
175 } while (srv2 && (srv2 != p->lbprm.fbck));
176 if (srv2)
177 p->lbprm.fbck = srv;
178 }
179 }
180 } else {
181 srv->lb_tree = &p->lbprm.fwlc.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200182 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200183 p->srv_act++;
184 }
185
186 /* note that eweight cannot be 0 here */
187 fwlc_queue_srv(srv);
188
189 out_update_backend:
190 /* check/update tot_used, tot_weight */
191 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200192 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
193
Willy Tarreauf89c1872009-10-01 11:19:37 +0200194 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200195 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200196}
197
198/* This function must be called after an update to server <srv>'s effective
199 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200200 *
201 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200202 */
203static void fwlc_update_server_weight(struct server *srv)
204{
205 int old_state, new_state;
206 struct proxy *p = srv->proxy;
207
Willy Tarreauc5150da2014-05-13 19:27:31 +0200208 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200209 return;
210
211 /* If changing the server's weight changes its state, we simply apply
212 * the procedures we already have for status change. If the state
213 * remains down, the server is not in any tree, so it's as easy as
214 * updating its values. If the state remains up with different weights,
215 * there are some computations to perform to find a new place and
216 * possibly a new tree for this server.
217 */
218
Emeric Brun52a91d32017-08-31 14:41:55 +0200219 old_state = srv_currently_usable(srv);
220 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200221
222 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200223 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200224 return;
225 }
226 else if (!old_state && new_state) {
227 fwlc_set_server_status_up(srv);
228 return;
229 }
230 else if (old_state && !new_state) {
231 fwlc_set_server_status_down(srv);
232 return;
233 }
234
Willy Tarreau1b877482018-08-21 19:44:53 +0200235 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
236
Willy Tarreauf89c1872009-10-01 11:19:37 +0200237 if (srv->lb_tree)
238 fwlc_dequeue_srv(srv);
239
Willy Tarreauc93cd162014-05-13 15:54:22 +0200240 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200241 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200242 srv->lb_tree = &p->lbprm.fwlc.bck;
243 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200244 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200245 srv->lb_tree = &p->lbprm.fwlc.act;
246 }
247
248 fwlc_queue_srv(srv);
249
250 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200251 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
252
Willy Tarreauc5150da2014-05-13 19:27:31 +0200253 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200254}
255
256/* This function is responsible for building the trees in case of fast
257 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
258 * uweight ratio. Both active and backup groups are initialized.
259 */
260void fwlc_init_server_tree(struct proxy *p)
261{
262 struct server *srv;
263 struct eb_root init_head = EB_ROOT;
264
265 p->lbprm.set_server_status_up = fwlc_set_server_status_up;
266 p->lbprm.set_server_status_down = fwlc_set_server_status_down;
267 p->lbprm.update_server_eweight = fwlc_update_server_weight;
268 p->lbprm.server_take_conn = fwlc_srv_reposition;
269 p->lbprm.server_drop_conn = fwlc_srv_reposition;
270
271 p->lbprm.wdiv = BE_WEIGHT_SCALE;
272 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200273 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200274 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200275 }
276
277 recount_servers(p);
278 update_backend_weight(p);
279
280 p->lbprm.fwlc.act = init_head;
281 p->lbprm.fwlc.bck = init_head;
282
283 /* queue active and backup servers in two distinct groups */
284 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200285 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200286 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200287 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200288 fwlc_queue_srv(srv);
289 }
290}
291
292/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
293 * return NULL. Saturated servers are skipped.
Willy Tarreau1b877482018-08-21 19:44:53 +0200294 *
295 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200296 */
297struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
298{
299 struct server *srv, *avoided;
300 struct eb32_node *node;
301
302 srv = avoided = NULL;
303
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100304 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200305 if (p->srv_act)
306 node = eb32_first(&p->lbprm.fwlc.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200307 else if (p->lbprm.fbck) {
308 srv = p->lbprm.fbck;
309 goto out;
310 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200311 else if (p->srv_bck)
312 node = eb32_first(&p->lbprm.fwlc.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200313 else {
314 srv = NULL;
315 goto out;
316 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200317
318 while (node) {
319 /* OK, we have a server. However, it may be saturated, in which
320 * case we don't want to reconsider it for now, so we'll simply
321 * skip it. Same if it's the server we try to avoid, in which
322 * case we simply remember it for later use if needed.
323 */
324 struct server *s;
325
326 s = eb32_entry(node, struct server, lb_node);
327 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
328 if (s != srvtoavoid) {
329 srv = s;
330 break;
331 }
332 avoided = s;
333 }
334 node = eb32_next(node);
335 }
336
337 if (!srv)
338 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200339 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100340 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200341 return srv;
342}
343
344
345/*
346 * Local variables:
347 * c-indent-level: 8
348 * c-basic-offset: 8
349 * End:
350 */