blob: fd9b437c11b9a3ca50c88609dec30dd25b42f8a9 [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.
28 */
29static inline void fwlc_remove_from_tree(struct server *s)
30{
31 s->lb_tree = NULL;
32}
33
34/* simply removes a server from a tree */
35static 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.
41 * Servers are sorted by #conns/weight. To ensure maximum accuracy,
42 * we use #conns*SRV_EWGHT_MAX/eweight as the sorting key.
43 */
44static inline void fwlc_queue_srv(struct server *s)
45{
Emeric Brun52a91d32017-08-31 14:41:55 +020046 s->lb_node.key = s->served * SRV_EWGHT_MAX / s->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +020047 eb32_insert(s->lb_tree, &s->lb_node);
48}
49
50/* Re-position the server in the FWLC tree after it has been assigned one
51 * connection or after it has released one. Note that it is possible that
52 * the server has been moved out of the tree due to failed health-checks.
53 */
54static void fwlc_srv_reposition(struct server *s)
55{
56 if (!s->lb_tree)
57 return;
Christopher Faulet5b517552017-06-09 14:17:53 +020058
Christopher Faulet2a944ee2017-11-07 10:42:54 +010059 HA_SPIN_LOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020060 fwlc_dequeue_srv(s);
61 fwlc_queue_srv(s);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010062 HA_SPIN_UNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020063}
64
65/* This function updates the server trees according to server <srv>'s new
66 * state. It should be called when server <srv>'s status changes to down.
67 * It is not important whether the server was already down or not. It is not
68 * important either that the new state is completely down (the caller may not
69 * know all the variables of a server's state).
70 */
71static void fwlc_set_server_status_down(struct server *srv)
72{
73 struct proxy *p = srv->proxy;
74
Willy Tarreauc5150da2014-05-13 19:27:31 +020075 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020076 return;
77
Emeric Brun52a91d32017-08-31 14:41:55 +020078 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020079 goto out_update_state;
80
Emeric Brun52a91d32017-08-31 14:41:55 +020081 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020082 /* server was already down */
83 goto out_update_backend;
84
Willy Tarreauc93cd162014-05-13 15:54:22 +020085 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +020086 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +020087 p->srv_bck--;
88
89 if (srv == p->lbprm.fbck) {
90 /* we lost the first backup server in a single-backup
91 * configuration, we must search another one.
92 */
93 struct server *srv2 = p->lbprm.fbck;
94 do {
95 srv2 = srv2->next;
96 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +020097 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +020098 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +020099 p->lbprm.fbck = srv2;
100 }
101 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200102 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200103 p->srv_act--;
104 }
105
106 fwlc_dequeue_srv(srv);
107 fwlc_remove_from_tree(srv);
108
109out_update_backend:
110 /* check/update tot_used, tot_weight */
111 update_backend_weight(p);
112 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200113 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200114}
115
116/* This function updates the server trees according to server <srv>'s new
117 * state. It should be called when server <srv>'s status changes to up.
118 * It is not important whether the server was already down or not. It is not
119 * important either that the new state is completely UP (the caller may not
120 * know all the variables of a server's state). This function will not change
121 * the weight of a server which was already up.
122 */
123static void fwlc_set_server_status_up(struct server *srv)
124{
125 struct proxy *p = srv->proxy;
126
Willy Tarreauc5150da2014-05-13 19:27:31 +0200127 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200128 return;
129
Emeric Brun52a91d32017-08-31 14:41:55 +0200130 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200131 goto out_update_state;
132
Emeric Brun52a91d32017-08-31 14:41:55 +0200133 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200134 /* server was already up */
135 goto out_update_backend;
136
Willy Tarreauc93cd162014-05-13 15:54:22 +0200137 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200138 srv->lb_tree = &p->lbprm.fwlc.bck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200139 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200140 p->srv_bck++;
141
142 if (!(p->options & PR_O_USE_ALL_BK)) {
143 if (!p->lbprm.fbck) {
144 /* there was no backup server anymore */
145 p->lbprm.fbck = srv;
146 } else {
147 /* we may have restored a backup server prior to fbck,
148 * in which case it should replace it.
149 */
150 struct server *srv2 = srv;
151 do {
152 srv2 = srv2->next;
153 } while (srv2 && (srv2 != p->lbprm.fbck));
154 if (srv2)
155 p->lbprm.fbck = srv;
156 }
157 }
158 } else {
159 srv->lb_tree = &p->lbprm.fwlc.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200160 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200161 p->srv_act++;
162 }
163
164 /* note that eweight cannot be 0 here */
165 fwlc_queue_srv(srv);
166
167 out_update_backend:
168 /* check/update tot_used, tot_weight */
169 update_backend_weight(p);
170 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200171 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200172}
173
174/* This function must be called after an update to server <srv>'s effective
175 * weight. It may be called after a state change too.
176 */
177static void fwlc_update_server_weight(struct server *srv)
178{
179 int old_state, new_state;
180 struct proxy *p = srv->proxy;
181
Willy Tarreauc5150da2014-05-13 19:27:31 +0200182 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200183 return;
184
185 /* If changing the server's weight changes its state, we simply apply
186 * the procedures we already have for status change. If the state
187 * remains down, the server is not in any tree, so it's as easy as
188 * updating its values. If the state remains up with different weights,
189 * there are some computations to perform to find a new place and
190 * possibly a new tree for this server.
191 */
192
Emeric Brun52a91d32017-08-31 14:41:55 +0200193 old_state = srv_currently_usable(srv);
194 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200195
196 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200197 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200198 return;
199 }
200 else if (!old_state && new_state) {
201 fwlc_set_server_status_up(srv);
202 return;
203 }
204 else if (old_state && !new_state) {
205 fwlc_set_server_status_down(srv);
206 return;
207 }
208
209 if (srv->lb_tree)
210 fwlc_dequeue_srv(srv);
211
Willy Tarreauc93cd162014-05-13 15:54:22 +0200212 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200213 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200214 srv->lb_tree = &p->lbprm.fwlc.bck;
215 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200216 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200217 srv->lb_tree = &p->lbprm.fwlc.act;
218 }
219
220 fwlc_queue_srv(srv);
221
222 update_backend_weight(p);
Willy Tarreauc5150da2014-05-13 19:27:31 +0200223 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200224}
225
226/* This function is responsible for building the trees in case of fast
227 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
228 * uweight ratio. Both active and backup groups are initialized.
229 */
230void fwlc_init_server_tree(struct proxy *p)
231{
232 struct server *srv;
233 struct eb_root init_head = EB_ROOT;
234
235 p->lbprm.set_server_status_up = fwlc_set_server_status_up;
236 p->lbprm.set_server_status_down = fwlc_set_server_status_down;
237 p->lbprm.update_server_eweight = fwlc_update_server_weight;
238 p->lbprm.server_take_conn = fwlc_srv_reposition;
239 p->lbprm.server_drop_conn = fwlc_srv_reposition;
240
241 p->lbprm.wdiv = BE_WEIGHT_SCALE;
242 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200243 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200244 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200245 }
246
247 recount_servers(p);
248 update_backend_weight(p);
249
250 p->lbprm.fwlc.act = init_head;
251 p->lbprm.fwlc.bck = init_head;
252
253 /* queue active and backup servers in two distinct groups */
254 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200255 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200256 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200257 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200258 fwlc_queue_srv(srv);
259 }
260}
261
262/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
263 * return NULL. Saturated servers are skipped.
264 */
265struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
266{
267 struct server *srv, *avoided;
268 struct eb32_node *node;
269
270 srv = avoided = NULL;
271
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100272 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200273 if (p->srv_act)
274 node = eb32_first(&p->lbprm.fwlc.act);
Christopher Faulet5b517552017-06-09 14:17:53 +0200275 else if (p->lbprm.fbck) {
276 srv = p->lbprm.fbck;
277 goto out;
278 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200279 else if (p->srv_bck)
280 node = eb32_first(&p->lbprm.fwlc.bck);
Christopher Faulet5b517552017-06-09 14:17:53 +0200281 else {
282 srv = NULL;
283 goto out;
284 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200285
286 while (node) {
287 /* OK, we have a server. However, it may be saturated, in which
288 * case we don't want to reconsider it for now, so we'll simply
289 * skip it. Same if it's the server we try to avoid, in which
290 * case we simply remember it for later use if needed.
291 */
292 struct server *s;
293
294 s = eb32_entry(node, struct server, lb_node);
295 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
296 if (s != srvtoavoid) {
297 srv = s;
298 break;
299 }
300 avoided = s;
301 }
302 node = eb32_next(node);
303 }
304
305 if (!srv)
306 srv = avoided;
Christopher Faulet5b517552017-06-09 14:17:53 +0200307 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100308 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200309 return srv;
310}
311
312
313/*
314 * Local variables:
315 * c-indent-level: 8
316 * c-basic-offset: 8
317 * End:
318 */