blob: c2fcb4efe7830cb35c42eea4784e382af636ea84 [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{
46 s->lb_node.key = s->served * SRV_EWGHT_MAX / s->eweight;
47 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;
58 fwlc_dequeue_srv(s);
59 fwlc_queue_srv(s);
60}
61
62/* This function updates the server trees according to server <srv>'s new
63 * state. It should be called when server <srv>'s status changes to down.
64 * It is not important whether the server was already down or not. It is not
65 * important either that the new state is completely down (the caller may not
66 * know all the variables of a server's state).
67 */
68static void fwlc_set_server_status_down(struct server *srv)
69{
70 struct proxy *p = srv->proxy;
71
72 if (srv->state == srv->prev_state &&
73 srv->eweight == srv->prev_eweight)
74 return;
75
76 if (srv_is_usable(srv->state, srv->eweight))
77 goto out_update_state;
78
79 if (!srv_is_usable(srv->prev_state, srv->prev_eweight))
80 /* server was already down */
81 goto out_update_backend;
82
83 if (srv->state & SRV_BACKUP) {
84 p->lbprm.tot_wbck -= srv->prev_eweight;
85 p->srv_bck--;
86
87 if (srv == p->lbprm.fbck) {
88 /* we lost the first backup server in a single-backup
89 * configuration, we must search another one.
90 */
91 struct server *srv2 = p->lbprm.fbck;
92 do {
93 srv2 = srv2->next;
94 } while (srv2 &&
95 !((srv2->state & SRV_BACKUP) &&
96 srv_is_usable(srv2->state, srv2->eweight)));
97 p->lbprm.fbck = srv2;
98 }
99 } else {
100 p->lbprm.tot_wact -= srv->prev_eweight;
101 p->srv_act--;
102 }
103
104 fwlc_dequeue_srv(srv);
105 fwlc_remove_from_tree(srv);
106
107out_update_backend:
108 /* check/update tot_used, tot_weight */
109 update_backend_weight(p);
110 out_update_state:
111 srv->prev_state = srv->state;
112 srv->prev_eweight = srv->eweight;
113}
114
115/* This function updates the server trees according to server <srv>'s new
116 * state. It should be called when server <srv>'s status changes to up.
117 * It is not important whether the server was already down or not. It is not
118 * important either that the new state is completely UP (the caller may not
119 * know all the variables of a server's state). This function will not change
120 * the weight of a server which was already up.
121 */
122static void fwlc_set_server_status_up(struct server *srv)
123{
124 struct proxy *p = srv->proxy;
125
126 if (srv->state == srv->prev_state &&
127 srv->eweight == srv->prev_eweight)
128 return;
129
130 if (!srv_is_usable(srv->state, srv->eweight))
131 goto out_update_state;
132
133 if (srv_is_usable(srv->prev_state, srv->prev_eweight))
134 /* server was already up */
135 goto out_update_backend;
136
137 if (srv->state & SRV_BACKUP) {
138 srv->lb_tree = &p->lbprm.fwlc.bck;
139 p->lbprm.tot_wbck += srv->eweight;
140 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;
160 p->lbprm.tot_wact += srv->eweight;
161 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:
171 srv->prev_state = srv->state;
172 srv->prev_eweight = srv->eweight;
173}
174
175/* This function must be called after an update to server <srv>'s effective
176 * weight. It may be called after a state change too.
177 */
178static void fwlc_update_server_weight(struct server *srv)
179{
180 int old_state, new_state;
181 struct proxy *p = srv->proxy;
182
183 if (srv->state == srv->prev_state &&
184 srv->eweight == srv->prev_eweight)
185 return;
186
187 /* If changing the server's weight changes its state, we simply apply
188 * the procedures we already have for status change. If the state
189 * remains down, the server is not in any tree, so it's as easy as
190 * updating its values. If the state remains up with different weights,
191 * there are some computations to perform to find a new place and
192 * possibly a new tree for this server.
193 */
194
195 old_state = srv_is_usable(srv->prev_state, srv->prev_eweight);
196 new_state = srv_is_usable(srv->state, srv->eweight);
197
198 if (!old_state && !new_state) {
199 srv->prev_state = srv->state;
200 srv->prev_eweight = srv->eweight;
201 return;
202 }
203 else if (!old_state && new_state) {
204 fwlc_set_server_status_up(srv);
205 return;
206 }
207 else if (old_state && !new_state) {
208 fwlc_set_server_status_down(srv);
209 return;
210 }
211
212 if (srv->lb_tree)
213 fwlc_dequeue_srv(srv);
214
215 if (srv->state & SRV_BACKUP) {
216 p->lbprm.tot_wbck += srv->eweight - srv->prev_eweight;
217 srv->lb_tree = &p->lbprm.fwlc.bck;
218 } else {
219 p->lbprm.tot_wact += srv->eweight - srv->prev_eweight;
220 srv->lb_tree = &p->lbprm.fwlc.act;
221 }
222
223 fwlc_queue_srv(srv);
224
225 update_backend_weight(p);
226 srv->prev_state = srv->state;
227 srv->prev_eweight = srv->eweight;
228}
229
230/* This function is responsible for building the trees in case of fast
231 * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
232 * uweight ratio. Both active and backup groups are initialized.
233 */
234void fwlc_init_server_tree(struct proxy *p)
235{
236 struct server *srv;
237 struct eb_root init_head = EB_ROOT;
238
239 p->lbprm.set_server_status_up = fwlc_set_server_status_up;
240 p->lbprm.set_server_status_down = fwlc_set_server_status_down;
241 p->lbprm.update_server_eweight = fwlc_update_server_weight;
242 p->lbprm.server_take_conn = fwlc_srv_reposition;
243 p->lbprm.server_drop_conn = fwlc_srv_reposition;
244
245 p->lbprm.wdiv = BE_WEIGHT_SCALE;
246 for (srv = p->srv; srv; srv = srv->next) {
247 srv->prev_eweight = srv->eweight = srv->uweight * BE_WEIGHT_SCALE;
248 srv->prev_state = srv->state;
249 }
250
251 recount_servers(p);
252 update_backend_weight(p);
253
254 p->lbprm.fwlc.act = init_head;
255 p->lbprm.fwlc.bck = init_head;
256
257 /* queue active and backup servers in two distinct groups */
258 for (srv = p->srv; srv; srv = srv->next) {
259 if (!srv_is_usable(srv->state, srv->eweight))
260 continue;
261 srv->lb_tree = (srv->state & SRV_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act;
262 fwlc_queue_srv(srv);
263 }
264}
265
266/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
267 * return NULL. Saturated servers are skipped.
268 */
269struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
270{
271 struct server *srv, *avoided;
272 struct eb32_node *node;
273
274 srv = avoided = NULL;
275
276 if (p->srv_act)
277 node = eb32_first(&p->lbprm.fwlc.act);
278 else if (p->lbprm.fbck)
279 return p->lbprm.fbck;
280 else if (p->srv_bck)
281 node = eb32_first(&p->lbprm.fwlc.bck);
282 else
283 return NULL;
284
285 while (node) {
286 /* OK, we have a server. However, it may be saturated, in which
287 * case we don't want to reconsider it for now, so we'll simply
288 * skip it. Same if it's the server we try to avoid, in which
289 * case we simply remember it for later use if needed.
290 */
291 struct server *s;
292
293 s = eb32_entry(node, struct server, lb_node);
294 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
295 if (s != srvtoavoid) {
296 srv = s;
297 break;
298 }
299 avoided = s;
300 }
301 node = eb32_next(node);
302 }
303
304 if (!srv)
305 srv = avoided;
306
307 return srv;
308}
309
310
311/*
312 * Local variables:
313 * c-indent-level: 8
314 * c-basic-offset: 8
315 * End:
316 */