blob: 7cf0cc749bbb808ea3ab133bb8bca1cc55409a04 [file] [log] [blame]
Willy Tarreauf89c1872009-10-01 11:19:37 +02001/*
2 * Fast Weighted Round Robin 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
24static inline void fwrr_remove_from_tree(struct server *s);
25static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s);
26static inline void fwrr_dequeue_srv(struct server *s);
27static void fwrr_get_srv(struct server *s);
28static void fwrr_queue_srv(struct server *s);
29
30
31/* This function updates the server trees according to server <srv>'s new
32 * state. It should be called when server <srv>'s status changes to down.
33 * It is not important whether the server was already down or not. It is not
34 * important either that the new state is completely down (the caller may not
35 * know all the variables of a server's state).
36 */
37static void fwrr_set_server_status_down(struct server *srv)
38{
39 struct proxy *p = srv->proxy;
40 struct fwrr_group *grp;
41
42 if (srv->state == srv->prev_state &&
43 srv->eweight == srv->prev_eweight)
44 return;
45
46 if (srv_is_usable(srv->state, srv->eweight))
47 goto out_update_state;
48
49 if (!srv_is_usable(srv->prev_state, srv->prev_eweight))
50 /* server was already down */
51 goto out_update_backend;
52
53 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
54 grp->next_weight -= srv->prev_eweight;
55
56 if (srv->state & SRV_BACKUP) {
57 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
58 p->srv_bck--;
59
60 if (srv == p->lbprm.fbck) {
61 /* we lost the first backup server in a single-backup
62 * configuration, we must search another one.
63 */
64 struct server *srv2 = p->lbprm.fbck;
65 do {
66 srv2 = srv2->next;
67 } while (srv2 &&
68 !((srv2->state & SRV_BACKUP) &&
69 srv_is_usable(srv2->state, srv2->eweight)));
70 p->lbprm.fbck = srv2;
71 }
72 } else {
73 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
74 p->srv_act--;
75 }
76
77 fwrr_dequeue_srv(srv);
78 fwrr_remove_from_tree(srv);
79
80out_update_backend:
81 /* check/update tot_used, tot_weight */
82 update_backend_weight(p);
83 out_update_state:
84 srv->prev_state = srv->state;
85 srv->prev_eweight = srv->eweight;
86}
87
88/* This function updates the server trees according to server <srv>'s new
89 * state. It should be called when server <srv>'s status changes to up.
90 * It is not important whether the server was already down or not. It is not
91 * important either that the new state is completely UP (the caller may not
92 * know all the variables of a server's state). This function will not change
93 * the weight of a server which was already up.
94 */
95static void fwrr_set_server_status_up(struct server *srv)
96{
97 struct proxy *p = srv->proxy;
98 struct fwrr_group *grp;
99
100 if (srv->state == srv->prev_state &&
101 srv->eweight == srv->prev_eweight)
102 return;
103
104 if (!srv_is_usable(srv->state, srv->eweight))
105 goto out_update_state;
106
107 if (srv_is_usable(srv->prev_state, srv->prev_eweight))
108 /* server was already up */
109 goto out_update_backend;
110
111 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
112 grp->next_weight += srv->eweight;
113
114 if (srv->state & SRV_BACKUP) {
115 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
116 p->srv_bck++;
117
118 if (!(p->options & PR_O_USE_ALL_BK)) {
119 if (!p->lbprm.fbck) {
120 /* there was no backup server anymore */
121 p->lbprm.fbck = srv;
122 } else {
123 /* we may have restored a backup server prior to fbck,
124 * in which case it should replace it.
125 */
126 struct server *srv2 = srv;
127 do {
128 srv2 = srv2->next;
129 } while (srv2 && (srv2 != p->lbprm.fbck));
130 if (srv2)
131 p->lbprm.fbck = srv;
132 }
133 }
134 } else {
135 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
136 p->srv_act++;
137 }
138
139 /* note that eweight cannot be 0 here */
140 fwrr_get_srv(srv);
141 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight;
142 fwrr_queue_srv(srv);
143
144out_update_backend:
145 /* check/update tot_used, tot_weight */
146 update_backend_weight(p);
147 out_update_state:
148 srv->prev_state = srv->state;
149 srv->prev_eweight = srv->eweight;
150}
151
152/* This function must be called after an update to server <srv>'s effective
153 * weight. It may be called after a state change too.
154 */
155static void fwrr_update_server_weight(struct server *srv)
156{
157 int old_state, new_state;
158 struct proxy *p = srv->proxy;
159 struct fwrr_group *grp;
160
161 if (srv->state == srv->prev_state &&
162 srv->eweight == srv->prev_eweight)
163 return;
164
165 /* If changing the server's weight changes its state, we simply apply
166 * the procedures we already have for status change. If the state
167 * remains down, the server is not in any tree, so it's as easy as
168 * updating its values. If the state remains up with different weights,
169 * there are some computations to perform to find a new place and
170 * possibly a new tree for this server.
171 */
172
173 old_state = srv_is_usable(srv->prev_state, srv->prev_eweight);
174 new_state = srv_is_usable(srv->state, srv->eweight);
175
176 if (!old_state && !new_state) {
177 srv->prev_state = srv->state;
178 srv->prev_eweight = srv->eweight;
179 return;
180 }
181 else if (!old_state && new_state) {
182 fwrr_set_server_status_up(srv);
183 return;
184 }
185 else if (old_state && !new_state) {
186 fwrr_set_server_status_down(srv);
187 return;
188 }
189
190 grp = (srv->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
191 grp->next_weight = grp->next_weight - srv->prev_eweight + srv->eweight;
192
193 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
194 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
195
196 if (srv->lb_tree == grp->init) {
197 fwrr_dequeue_srv(srv);
198 fwrr_queue_by_weight(grp->init, srv);
199 }
200 else if (!srv->lb_tree) {
201 /* FIXME: server was down. This is not possible right now but
202 * may be needed soon for slowstart or graceful shutdown.
203 */
204 fwrr_dequeue_srv(srv);
205 fwrr_get_srv(srv);
206 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->eweight;
207 fwrr_queue_srv(srv);
208 } else {
209 /* The server is either active or in the next queue. If it's
210 * still in the active queue and it has not consumed all of its
211 * places, let's adjust its next position.
212 */
213 fwrr_get_srv(srv);
214
215 if (srv->eweight > 0) {
216 int prev_next = srv->npos;
217 int step = grp->next_weight / srv->eweight;
218
219 srv->npos = srv->lpos + step;
220 srv->rweight = 0;
221
222 if (srv->npos > prev_next)
223 srv->npos = prev_next;
224 if (srv->npos < grp->curr_pos + 2)
225 srv->npos = grp->curr_pos + step;
226 } else {
227 /* push it into the next tree */
228 srv->npos = grp->curr_pos + grp->curr_weight;
229 }
230
231 fwrr_dequeue_srv(srv);
232 fwrr_queue_srv(srv);
233 }
234
235 update_backend_weight(p);
236 srv->prev_state = srv->state;
237 srv->prev_eweight = srv->eweight;
238}
239
240/* Remove a server from a tree. It must have previously been dequeued. This
241 * function is meant to be called when a server is going down or has its
242 * weight disabled.
243 */
244static inline void fwrr_remove_from_tree(struct server *s)
245{
246 s->lb_tree = NULL;
247}
248
249/* Queue a server in the weight tree <root>, assuming the weight is >0.
250 * We want to sort them by inverted weights, because we need to place
251 * heavy servers first in order to get a smooth distribution.
252 */
253static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s)
254{
255 s->lb_node.key = SRV_EWGHT_MAX - s->eweight;
256 eb32_insert(root, &s->lb_node);
257 s->lb_tree = root;
258}
259
260/* This function is responsible for building the weight trees in case of fast
261 * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight
262 * ratio. Both active and backup groups are initialized.
263 */
264void fwrr_init_server_groups(struct proxy *p)
265{
266 struct server *srv;
267 struct eb_root init_head = EB_ROOT;
268
269 p->lbprm.set_server_status_up = fwrr_set_server_status_up;
270 p->lbprm.set_server_status_down = fwrr_set_server_status_down;
271 p->lbprm.update_server_eweight = fwrr_update_server_weight;
272
273 p->lbprm.wdiv = BE_WEIGHT_SCALE;
274 for (srv = p->srv; srv; srv = srv->next) {
Willy Tarreau004e0452013-11-21 11:22:01 +0100275 srv->eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
276 srv->prev_eweight = srv->eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200277 srv->prev_state = srv->state;
278 }
279
280 recount_servers(p);
281 update_backend_weight(p);
282
283 /* prepare the active servers group */
284 p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight =
285 p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact;
286 p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 =
287 p->lbprm.fwrr.act.t1 = init_head;
288 p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0;
289 p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1;
290
291 /* prepare the backup servers group */
292 p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight =
293 p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck;
294 p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 =
295 p->lbprm.fwrr.bck.t1 = init_head;
296 p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0;
297 p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1;
298
299 /* queue active and backup servers in two distinct groups */
300 for (srv = p->srv; srv; srv = srv->next) {
301 if (!srv_is_usable(srv->state, srv->eweight))
302 continue;
303 fwrr_queue_by_weight((srv->state & SRV_BACKUP) ?
304 p->lbprm.fwrr.bck.init :
305 p->lbprm.fwrr.act.init,
306 srv);
307 }
308}
309
310/* simply removes a server from a weight tree */
311static inline void fwrr_dequeue_srv(struct server *s)
312{
313 eb32_delete(&s->lb_node);
314}
315
316/* queues a server into the appropriate group and tree depending on its
317 * backup status, and ->npos. If the server is disabled, simply assign
318 * it to the NULL tree.
319 */
320static void fwrr_queue_srv(struct server *s)
321{
322 struct proxy *p = s->proxy;
323 struct fwrr_group *grp;
324
325 grp = (s->state & SRV_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
326
327 /* Delay everything which does not fit into the window and everything
328 * which does not fit into the theorical new window.
329 */
330 if (!srv_is_usable(s->state, s->eweight)) {
331 fwrr_remove_from_tree(s);
332 }
333 else if (s->eweight <= 0 ||
334 s->npos >= 2 * grp->curr_weight ||
335 s->npos >= grp->curr_weight + grp->next_weight) {
336 /* put into next tree, and readjust npos in case we could
337 * finally take this back to current. */
338 s->npos -= grp->curr_weight;
339 fwrr_queue_by_weight(grp->next, s);
340 }
341 else {
342 /* The sorting key is stored in units of s->npos * user_weight
343 * in order to avoid overflows. As stated in backend.h, the
344 * lower the scale, the rougher the weights modulation, and the
345 * higher the scale, the lower the number of servers without
346 * overflow. With this formula, the result is always positive,
Godbacha34bdc02013-07-22 07:44:53 +0800347 * so we can use eb32_insert().
Willy Tarreauf89c1872009-10-01 11:19:37 +0200348 */
349 s->lb_node.key = SRV_UWGHT_RANGE * s->npos +
350 (unsigned)(SRV_EWGHT_MAX + s->rweight - s->eweight) / BE_WEIGHT_SCALE;
351
352 eb32_insert(&grp->curr, &s->lb_node);
353 s->lb_tree = &grp->curr;
354 }
355}
356
357/* prepares a server when extracting it from the "init" tree */
358static inline void fwrr_get_srv_init(struct server *s)
359{
360 s->npos = s->rweight = 0;
361}
362
363/* prepares a server when extracting it from the "next" tree */
364static inline void fwrr_get_srv_next(struct server *s)
365{
366 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
367 &s->proxy->lbprm.fwrr.bck :
368 &s->proxy->lbprm.fwrr.act;
369
370 s->npos += grp->curr_weight;
371}
372
373/* prepares a server when it was marked down */
374static inline void fwrr_get_srv_down(struct server *s)
375{
376 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
377 &s->proxy->lbprm.fwrr.bck :
378 &s->proxy->lbprm.fwrr.act;
379
380 s->npos = grp->curr_pos;
381}
382
383/* prepares a server when extracting it from its tree */
384static void fwrr_get_srv(struct server *s)
385{
386 struct proxy *p = s->proxy;
387 struct fwrr_group *grp = (s->state & SRV_BACKUP) ?
388 &p->lbprm.fwrr.bck :
389 &p->lbprm.fwrr.act;
390
391 if (s->lb_tree == grp->init) {
392 fwrr_get_srv_init(s);
393 }
394 else if (s->lb_tree == grp->next) {
395 fwrr_get_srv_next(s);
396 }
397 else if (s->lb_tree == NULL) {
398 fwrr_get_srv_down(s);
399 }
400}
401
402/* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty
403 * when this happens, and "next" filled with servers sorted by weights.
404 */
405static inline void fwrr_switch_trees(struct fwrr_group *grp)
406{
407 struct eb_root *swap;
408 swap = grp->init;
409 grp->init = grp->next;
410 grp->next = swap;
411 grp->curr_weight = grp->next_weight;
412 grp->curr_pos = grp->curr_weight;
413}
414
415/* return next server from the current tree in FWRR group <grp>, or a server
416 * from the "init" tree if appropriate. If both trees are empty, return NULL.
417 */
418static struct server *fwrr_get_server_from_group(struct fwrr_group *grp)
419{
420 struct eb32_node *node;
421 struct server *s;
422
423 node = eb32_first(&grp->curr);
424 s = eb32_entry(node, struct server, lb_node);
425
426 if (!node || s->npos > grp->curr_pos) {
427 /* either we have no server left, or we have a hole */
428 struct eb32_node *node2;
429 node2 = eb32_first(grp->init);
430 if (node2) {
431 node = node2;
432 s = eb32_entry(node, struct server, lb_node);
433 fwrr_get_srv_init(s);
434 if (s->eweight == 0) /* FIXME: is it possible at all ? */
435 node = NULL;
436 }
437 }
438 if (node)
439 return s;
440 else
441 return NULL;
442}
443
444/* Computes next position of server <s> in the group. It is mandatory for <s>
445 * to have a non-zero, positive eweight.
446*/
447static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s)
448{
449 if (!s->npos) {
450 /* first time ever for this server */
451 s->lpos = grp->curr_pos;
452 s->npos = grp->curr_pos + grp->next_weight / s->eweight;
453 s->rweight += grp->next_weight % s->eweight;
454
455 if (s->rweight >= s->eweight) {
456 s->rweight -= s->eweight;
457 s->npos++;
458 }
459 } else {
460 s->lpos = s->npos;
461 s->npos += grp->next_weight / s->eweight;
462 s->rweight += grp->next_weight % s->eweight;
463
464 if (s->rweight >= s->eweight) {
465 s->rweight -= s->eweight;
466 s->npos++;
467 }
468 }
469}
470
471/* Return next server from the current tree in backend <p>, or a server from
472 * the init tree if appropriate. If both trees are empty, return NULL.
473 * Saturated servers are skipped and requeued.
474 */
475struct server *fwrr_get_next_server(struct proxy *p, struct server *srvtoavoid)
476{
477 struct server *srv, *full, *avoided;
478 struct fwrr_group *grp;
479 int switched;
480
481 if (p->srv_act)
482 grp = &p->lbprm.fwrr.act;
483 else if (p->lbprm.fbck)
484 return p->lbprm.fbck;
485 else if (p->srv_bck)
486 grp = &p->lbprm.fwrr.bck;
487 else
488 return NULL;
489
490 switched = 0;
491 avoided = NULL;
492 full = NULL; /* NULL-terminated list of saturated servers */
493 while (1) {
494 /* if we see an empty group, let's first try to collect weights
495 * which might have recently changed.
496 */
497 if (!grp->curr_weight)
498 grp->curr_pos = grp->curr_weight = grp->next_weight;
499
500 /* get first server from the "current" tree. When the end of
501 * the tree is reached, we may have to switch, but only once.
502 */
503 while (1) {
504 srv = fwrr_get_server_from_group(grp);
505 if (srv)
506 break;
507 if (switched) {
508 if (avoided) {
509 srv = avoided;
510 break;
511 }
512 goto requeue_servers;
513 }
514 switched = 1;
515 fwrr_switch_trees(grp);
516
517 }
518
519 /* OK, we have a server. However, it may be saturated, in which
520 * case we don't want to reconsider it for now. We'll update
521 * its position and dequeue it anyway, so that we can move it
522 * to a better place afterwards.
523 */
524 fwrr_update_position(grp, srv);
525 fwrr_dequeue_srv(srv);
526 grp->curr_pos++;
527 if (!srv->maxconn || (!srv->nbpend && srv->served < srv_dynamic_maxconn(srv))) {
528 /* make sure it is not the server we are trying to exclude... */
529 if (srv != srvtoavoid || avoided)
530 break;
531
532 avoided = srv; /* ...but remember that is was selected yet avoided */
533 }
534
535 /* the server is saturated or avoided, let's chain it for later reinsertion */
536 srv->next_full = full;
537 full = srv;
538 }
539
540 /* OK, we got the best server, let's update it */
541 fwrr_queue_srv(srv);
542
543 requeue_servers:
544 /* Requeue all extracted servers. If full==srv then it was
545 * avoided (unsucessfully) and chained, omit it now.
546 */
547 if (unlikely(full != NULL)) {
548 if (switched) {
549 /* the tree has switched, requeue all extracted servers
550 * into "init", because their place was lost, and only
551 * their weight matters.
552 */
553 do {
554 if (likely(full != srv))
555 fwrr_queue_by_weight(grp->init, full);
556 full = full->next_full;
557 } while (full);
558 } else {
559 /* requeue all extracted servers just as if they were consumed
560 * so that they regain their expected place.
561 */
562 do {
563 if (likely(full != srv))
564 fwrr_queue_srv(full);
565 full = full->next_full;
566 } while (full);
567 }
568 }
569 return srv;
570}
571
572/*
573 * Local variables:
574 * c-indent-level: 8
575 * c-basic-offset: 8
576 * End:
577 */