blob: 31e8bb2d10e39671e45f17dcfc00170a67c1e8d1 [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020014#include <common/debug.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020015#include <import/eb32tree.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020016
17#include <types/global.h>
18#include <types/server.h>
19
20#include <proto/backend.h>
21#include <proto/queue.h>
22
23static inline void fwrr_remove_from_tree(struct server *s);
24static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s);
25static inline void fwrr_dequeue_srv(struct server *s);
26static void fwrr_get_srv(struct server *s);
27static void fwrr_queue_srv(struct server *s);
28
29
30/* This function updates the server trees according to server <srv>'s new
31 * state. It should be called when server <srv>'s status changes to down.
32 * It is not important whether the server was already down or not. It is not
33 * important either that the new state is completely down (the caller may not
34 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020035 *
36 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020037 */
38static void fwrr_set_server_status_down(struct server *srv)
39{
40 struct proxy *p = srv->proxy;
41 struct fwrr_group *grp;
42
Willy Tarreauc5150da2014-05-13 19:27:31 +020043 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020044 return;
45
Emeric Brun52a91d32017-08-31 14:41:55 +020046 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020047 goto out_update_state;
48
Willy Tarreau1b877482018-08-21 19:44:53 +020049 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
50
Emeric Brun52a91d32017-08-31 14:41:55 +020051 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020052 /* server was already down */
53 goto out_update_backend;
54
Willy Tarreauc93cd162014-05-13 15:54:22 +020055 grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Emeric Brun52a91d32017-08-31 14:41:55 +020056 grp->next_weight -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +020057
Willy Tarreauc93cd162014-05-13 15:54:22 +020058 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +020059 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
60 p->srv_bck--;
61
62 if (srv == p->lbprm.fbck) {
63 /* we lost the first backup server in a single-backup
64 * configuration, we must search another one.
65 */
66 struct server *srv2 = p->lbprm.fbck;
67 do {
68 srv2 = srv2->next;
69 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +020070 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +020071 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +020072 p->lbprm.fbck = srv2;
73 }
74 } else {
75 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
76 p->srv_act--;
77 }
78
79 fwrr_dequeue_srv(srv);
80 fwrr_remove_from_tree(srv);
81
82out_update_backend:
83 /* check/update tot_used, tot_weight */
84 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +020085 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
86
Willy Tarreauf89c1872009-10-01 11:19:37 +020087 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +020088 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +020089}
90
91/* This function updates the server trees according to server <srv>'s new
92 * state. It should be called when server <srv>'s status changes to up.
93 * It is not important whether the server was already down or not. It is not
94 * important either that the new state is completely UP (the caller may not
95 * know all the variables of a server's state). This function will not change
96 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +020097 *
98 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020099 */
100static void fwrr_set_server_status_up(struct server *srv)
101{
102 struct proxy *p = srv->proxy;
103 struct fwrr_group *grp;
104
Willy Tarreauc5150da2014-05-13 19:27:31 +0200105 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200106 return;
107
Emeric Brun52a91d32017-08-31 14:41:55 +0200108 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200109 goto out_update_state;
110
Willy Tarreau1b877482018-08-21 19:44:53 +0200111 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
112
Emeric Brun52a91d32017-08-31 14:41:55 +0200113 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200114 /* server was already up */
115 goto out_update_backend;
116
Willy Tarreauc93cd162014-05-13 15:54:22 +0200117 grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200118 grp->next_weight += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200119
Willy Tarreauc93cd162014-05-13 15:54:22 +0200120 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200121 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
122 p->srv_bck++;
123
124 if (!(p->options & PR_O_USE_ALL_BK)) {
125 if (!p->lbprm.fbck) {
126 /* there was no backup server anymore */
127 p->lbprm.fbck = srv;
128 } else {
129 /* we may have restored a backup server prior to fbck,
130 * in which case it should replace it.
131 */
132 struct server *srv2 = srv;
133 do {
134 srv2 = srv2->next;
135 } while (srv2 && (srv2 != p->lbprm.fbck));
136 if (srv2)
137 p->lbprm.fbck = srv;
138 }
139 }
140 } else {
141 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
142 p->srv_act++;
143 }
144
145 /* note that eweight cannot be 0 here */
146 fwrr_get_srv(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +0200147 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200148 fwrr_queue_srv(srv);
149
150out_update_backend:
151 /* check/update tot_used, tot_weight */
152 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200153 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
154
Willy Tarreauf89c1872009-10-01 11:19:37 +0200155 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200156 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200157}
158
159/* This function must be called after an update to server <srv>'s effective
160 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200161 *
162 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200163 */
164static void fwrr_update_server_weight(struct server *srv)
165{
166 int old_state, new_state;
167 struct proxy *p = srv->proxy;
168 struct fwrr_group *grp;
169
Willy Tarreauc5150da2014-05-13 19:27:31 +0200170 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200171 return;
172
173 /* If changing the server's weight changes its state, we simply apply
174 * the procedures we already have for status change. If the state
175 * remains down, the server is not in any tree, so it's as easy as
176 * updating its values. If the state remains up with different weights,
177 * there are some computations to perform to find a new place and
178 * possibly a new tree for this server.
179 */
180
Emeric Brun52a91d32017-08-31 14:41:55 +0200181 old_state = srv_currently_usable(srv);
182 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200183
184 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200185 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200186 return;
187 }
188 else if (!old_state && new_state) {
189 fwrr_set_server_status_up(srv);
190 return;
191 }
192 else if (old_state && !new_state) {
193 fwrr_set_server_status_down(srv);
194 return;
195 }
196
Willy Tarreau1b877482018-08-21 19:44:53 +0200197 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
198
Willy Tarreauc93cd162014-05-13 15:54:22 +0200199 grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200200 grp->next_weight = grp->next_weight - srv->cur_eweight + srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200201
202 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
203 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
204
205 if (srv->lb_tree == grp->init) {
206 fwrr_dequeue_srv(srv);
207 fwrr_queue_by_weight(grp->init, srv);
208 }
209 else if (!srv->lb_tree) {
210 /* FIXME: server was down. This is not possible right now but
211 * may be needed soon for slowstart or graceful shutdown.
212 */
213 fwrr_dequeue_srv(srv);
214 fwrr_get_srv(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +0200215 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200216 fwrr_queue_srv(srv);
217 } else {
218 /* The server is either active or in the next queue. If it's
219 * still in the active queue and it has not consumed all of its
220 * places, let's adjust its next position.
221 */
222 fwrr_get_srv(srv);
223
Emeric Brun52a91d32017-08-31 14:41:55 +0200224 if (srv->next_eweight > 0) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200225 int prev_next = srv->npos;
Emeric Brun52a91d32017-08-31 14:41:55 +0200226 int step = grp->next_weight / srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200227
228 srv->npos = srv->lpos + step;
229 srv->rweight = 0;
230
231 if (srv->npos > prev_next)
232 srv->npos = prev_next;
233 if (srv->npos < grp->curr_pos + 2)
234 srv->npos = grp->curr_pos + step;
235 } else {
236 /* push it into the next tree */
237 srv->npos = grp->curr_pos + grp->curr_weight;
238 }
239
240 fwrr_dequeue_srv(srv);
241 fwrr_queue_srv(srv);
242 }
243
244 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200245 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
246
Willy Tarreauc5150da2014-05-13 19:27:31 +0200247 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200248}
249
250/* Remove a server from a tree. It must have previously been dequeued. This
251 * function is meant to be called when a server is going down or has its
252 * weight disabled.
Willy Tarreau1b877482018-08-21 19:44:53 +0200253 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200254 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200255 */
256static inline void fwrr_remove_from_tree(struct server *s)
257{
258 s->lb_tree = NULL;
259}
260
261/* Queue a server in the weight tree <root>, assuming the weight is >0.
262 * We want to sort them by inverted weights, because we need to place
263 * heavy servers first in order to get a smooth distribution.
Willy Tarreau1b877482018-08-21 19:44:53 +0200264 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200265 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200266 */
267static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s)
268{
Emeric Brun52a91d32017-08-31 14:41:55 +0200269 s->lb_node.key = SRV_EWGHT_MAX - s->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200270 eb32_insert(root, &s->lb_node);
271 s->lb_tree = root;
272}
273
274/* This function is responsible for building the weight trees in case of fast
275 * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight
276 * ratio. Both active and backup groups are initialized.
277 */
278void fwrr_init_server_groups(struct proxy *p)
279{
280 struct server *srv;
281 struct eb_root init_head = EB_ROOT;
282
283 p->lbprm.set_server_status_up = fwrr_set_server_status_up;
284 p->lbprm.set_server_status_down = fwrr_set_server_status_down;
285 p->lbprm.update_server_eweight = fwrr_update_server_weight;
286
287 p->lbprm.wdiv = BE_WEIGHT_SCALE;
288 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200289 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200290 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200291 }
292
293 recount_servers(p);
294 update_backend_weight(p);
295
296 /* prepare the active servers group */
297 p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight =
298 p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact;
299 p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 =
300 p->lbprm.fwrr.act.t1 = init_head;
301 p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0;
302 p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1;
303
304 /* prepare the backup servers group */
305 p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight =
306 p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck;
307 p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 =
308 p->lbprm.fwrr.bck.t1 = init_head;
309 p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0;
310 p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1;
311
312 /* queue active and backup servers in two distinct groups */
313 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200314 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200315 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200316 fwrr_queue_by_weight((srv->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200317 p->lbprm.fwrr.bck.init :
318 p->lbprm.fwrr.act.init,
319 srv);
320 }
321}
322
Willy Tarreau1b877482018-08-21 19:44:53 +0200323/* simply removes a server from a weight tree.
324 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200325 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200326 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200327static inline void fwrr_dequeue_srv(struct server *s)
328{
329 eb32_delete(&s->lb_node);
330}
331
332/* queues a server into the appropriate group and tree depending on its
333 * backup status, and ->npos. If the server is disabled, simply assign
334 * it to the NULL tree.
Willy Tarreau1b877482018-08-21 19:44:53 +0200335 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200336 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200337 */
338static void fwrr_queue_srv(struct server *s)
339{
340 struct proxy *p = s->proxy;
341 struct fwrr_group *grp;
342
Willy Tarreauc93cd162014-05-13 15:54:22 +0200343 grp = (s->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Christopher Faulet5b517552017-06-09 14:17:53 +0200344
Willy Tarreauf89c1872009-10-01 11:19:37 +0200345 /* Delay everything which does not fit into the window and everything
346 * which does not fit into the theorical new window.
347 */
Emeric Brun52a91d32017-08-31 14:41:55 +0200348 if (!srv_willbe_usable(s)) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200349 fwrr_remove_from_tree(s);
350 }
Emeric Brun52a91d32017-08-31 14:41:55 +0200351 else if (s->next_eweight <= 0 ||
Willy Tarreauf89c1872009-10-01 11:19:37 +0200352 s->npos >= 2 * grp->curr_weight ||
353 s->npos >= grp->curr_weight + grp->next_weight) {
354 /* put into next tree, and readjust npos in case we could
355 * finally take this back to current. */
Willy Tarreau274ba672019-04-24 10:48:00 +0200356 s->npos -= grp->curr_weight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200357 fwrr_queue_by_weight(grp->next, s);
358 }
359 else {
360 /* The sorting key is stored in units of s->npos * user_weight
361 * in order to avoid overflows. As stated in backend.h, the
362 * lower the scale, the rougher the weights modulation, and the
363 * higher the scale, the lower the number of servers without
364 * overflow. With this formula, the result is always positive,
Godbacha34bdc02013-07-22 07:44:53 +0800365 * so we can use eb32_insert().
Willy Tarreauf89c1872009-10-01 11:19:37 +0200366 */
367 s->lb_node.key = SRV_UWGHT_RANGE * s->npos +
Emeric Brun52a91d32017-08-31 14:41:55 +0200368 (unsigned)(SRV_EWGHT_MAX + s->rweight - s->next_eweight) / BE_WEIGHT_SCALE;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200369
370 eb32_insert(&grp->curr, &s->lb_node);
371 s->lb_tree = &grp->curr;
372 }
373}
374
Willy Tarreau1b877482018-08-21 19:44:53 +0200375/* prepares a server when extracting it from the "init" tree.
376 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200377 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200378 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200379static inline void fwrr_get_srv_init(struct server *s)
380{
381 s->npos = s->rweight = 0;
382}
383
Willy Tarreau1b877482018-08-21 19:44:53 +0200384/* prepares a server when extracting it from the "next" tree.
385 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200386 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200387 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200388static inline void fwrr_get_srv_next(struct server *s)
389{
Willy Tarreauc93cd162014-05-13 15:54:22 +0200390 struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200391 &s->proxy->lbprm.fwrr.bck :
392 &s->proxy->lbprm.fwrr.act;
393
Willy Tarreau274ba672019-04-24 10:48:00 +0200394 s->npos += grp->curr_weight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200395}
396
Willy Tarreau1b877482018-08-21 19:44:53 +0200397/* prepares a server when it was marked down.
398 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200399 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200400 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200401static inline void fwrr_get_srv_down(struct server *s)
402{
Willy Tarreauc93cd162014-05-13 15:54:22 +0200403 struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200404 &s->proxy->lbprm.fwrr.bck :
405 &s->proxy->lbprm.fwrr.act;
406
407 s->npos = grp->curr_pos;
408}
409
Willy Tarreau1b877482018-08-21 19:44:53 +0200410/* prepares a server when extracting it from its tree.
411 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200412 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200413 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200414static void fwrr_get_srv(struct server *s)
415{
416 struct proxy *p = s->proxy;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200417 struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200418 &p->lbprm.fwrr.bck :
419 &p->lbprm.fwrr.act;
420
421 if (s->lb_tree == grp->init) {
422 fwrr_get_srv_init(s);
423 }
424 else if (s->lb_tree == grp->next) {
425 fwrr_get_srv_next(s);
426 }
427 else if (s->lb_tree == NULL) {
428 fwrr_get_srv_down(s);
429 }
430}
431
432/* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty
433 * when this happens, and "next" filled with servers sorted by weights.
Willy Tarreau1b877482018-08-21 19:44:53 +0200434 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200435 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200436 */
437static inline void fwrr_switch_trees(struct fwrr_group *grp)
438{
439 struct eb_root *swap;
440 swap = grp->init;
441 grp->init = grp->next;
442 grp->next = swap;
443 grp->curr_weight = grp->next_weight;
444 grp->curr_pos = grp->curr_weight;
445}
446
447/* return next server from the current tree in FWRR group <grp>, or a server
448 * from the "init" tree if appropriate. If both trees are empty, return NULL.
Willy Tarreau1b877482018-08-21 19:44:53 +0200449 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200450 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200451 */
452static struct server *fwrr_get_server_from_group(struct fwrr_group *grp)
453{
Willy Tarreau9df86f92019-04-16 11:21:14 +0200454 struct eb32_node *node1;
455 struct eb32_node *node2;
456 struct server *s1 = NULL;
457 struct server *s2 = NULL;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200458
Willy Tarreau9df86f92019-04-16 11:21:14 +0200459 node1 = eb32_first(&grp->curr);
460 if (node1) {
461 s1 = eb32_entry(node1, struct server, lb_node);
Willy Tarreau9df86f92019-04-16 11:21:14 +0200462 if (s1->cur_eweight && s1->npos <= grp->curr_pos)
463 return s1;
464 }
Christopher Faulet5b517552017-06-09 14:17:53 +0200465
Willy Tarreau9df86f92019-04-16 11:21:14 +0200466 /* Either we have no server left, or we have a hole. We'll look in the
467 * init tree or a better proposal. At this point, if <s1> is non-null,
Willy Tarreau274ba672019-04-24 10:48:00 +0200468 * it is guaranteed to remain available as the tree is locked.
Willy Tarreau9df86f92019-04-16 11:21:14 +0200469 */
470 node2 = eb32_first(grp->init);
471 if (node2) {
472 s2 = eb32_entry(node2, struct server, lb_node);
Willy Tarreau9df86f92019-04-16 11:21:14 +0200473 if (s2->cur_eweight) {
Willy Tarreau9df86f92019-04-16 11:21:14 +0200474 fwrr_get_srv_init(s2);
475 return s2;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200476 }
477 }
Willy Tarreau9df86f92019-04-16 11:21:14 +0200478 return s1;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200479}
480
Willy Tarreau274ba672019-04-24 10:48:00 +0200481/* Computes next position of server <s> in the group. Nothing is done if <s>
482 * has a zero weight.
Willy Tarreau1b877482018-08-21 19:44:53 +0200483 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200484 * The lbprm's lock must be held to protect lpos/npos/rweight.
Willy Tarreau1b877482018-08-21 19:44:53 +0200485 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200486static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s)
487{
Willy Tarreau274ba672019-04-24 10:48:00 +0200488 unsigned int eweight = *(volatile unsigned int *)&s->cur_eweight;
489
490 if (!eweight)
491 return;
492
Willy Tarreauf89c1872009-10-01 11:19:37 +0200493 if (!s->npos) {
494 /* first time ever for this server */
Willy Tarreau274ba672019-04-24 10:48:00 +0200495 s->npos = grp->curr_pos;
496 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200497
Willy Tarreau274ba672019-04-24 10:48:00 +0200498 s->lpos = s->npos;
499 s->npos += grp->next_weight / eweight;
500 s->rweight += grp->next_weight % eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200501
Willy Tarreau274ba672019-04-24 10:48:00 +0200502 if (s->rweight >= eweight) {
503 s->rweight -= eweight;
504 s->npos++;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200505 }
506}
507
508/* Return next server from the current tree in backend <p>, or a server from
509 * the init tree if appropriate. If both trees are empty, return NULL.
510 * Saturated servers are skipped and requeued.
Willy Tarreau1b877482018-08-21 19:44:53 +0200511 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200512 * The lbprm's lock will be used. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200513 */
514struct server *fwrr_get_next_server(struct proxy *p, struct server *srvtoavoid)
515{
516 struct server *srv, *full, *avoided;
517 struct fwrr_group *grp;
518 int switched;
519
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100520 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200521 if (p->srv_act)
522 grp = &p->lbprm.fwrr.act;
Christopher Faulet5b517552017-06-09 14:17:53 +0200523 else if (p->lbprm.fbck) {
524 srv = p->lbprm.fbck;
525 goto out;
526 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200527 else if (p->srv_bck)
528 grp = &p->lbprm.fwrr.bck;
Christopher Faulet5b517552017-06-09 14:17:53 +0200529 else {
530 srv = NULL;
531 goto out;
532 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200533
534 switched = 0;
535 avoided = NULL;
536 full = NULL; /* NULL-terminated list of saturated servers */
537 while (1) {
538 /* if we see an empty group, let's first try to collect weights
539 * which might have recently changed.
540 */
541 if (!grp->curr_weight)
542 grp->curr_pos = grp->curr_weight = grp->next_weight;
543
544 /* get first server from the "current" tree. When the end of
545 * the tree is reached, we may have to switch, but only once.
546 */
547 while (1) {
548 srv = fwrr_get_server_from_group(grp);
549 if (srv)
550 break;
551 if (switched) {
552 if (avoided) {
553 srv = avoided;
Willy Tarreaub6195ef2019-05-27 10:17:05 +0200554 goto take_this_one;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200555 }
556 goto requeue_servers;
557 }
558 switched = 1;
559 fwrr_switch_trees(grp);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200560 }
561
Willy Tarreau274ba672019-04-24 10:48:00 +0200562 /* OK, we have a server. However, it may be saturated, in which
563 * case we don't want to reconsider it for now. We'll update
564 * its position and dequeue it anyway, so that we can move it
565 * to a better place afterwards.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200566 */
567 fwrr_update_position(grp, srv);
568 fwrr_dequeue_srv(srv);
569 grp->curr_pos++;
570 if (!srv->maxconn || (!srv->nbpend && srv->served < srv_dynamic_maxconn(srv))) {
571 /* make sure it is not the server we are trying to exclude... */
572 if (srv != srvtoavoid || avoided)
573 break;
574
575 avoided = srv; /* ...but remember that is was selected yet avoided */
576 }
577
Willy Tarreau9df86f92019-04-16 11:21:14 +0200578 /* the server is saturated or avoided, let's chain it for later reinsertion.
Willy Tarreau9df86f92019-04-16 11:21:14 +0200579 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200580 srv->next_full = full;
581 full = srv;
582 }
583
Willy Tarreaub6195ef2019-05-27 10:17:05 +0200584 take_this_one:
Willy Tarreauf89c1872009-10-01 11:19:37 +0200585 /* OK, we got the best server, let's update it */
586 fwrr_queue_srv(srv);
587
588 requeue_servers:
589 /* Requeue all extracted servers. If full==srv then it was
Willy Tarreau9df86f92019-04-16 11:21:14 +0200590 * avoided (unsuccessfully) and chained, omit it now. The
591 * only way to get there is by having <avoided>==NULL or
592 * <avoided>==<srv>.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200593 */
594 if (unlikely(full != NULL)) {
595 if (switched) {
596 /* the tree has switched, requeue all extracted servers
597 * into "init", because their place was lost, and only
598 * their weight matters.
599 */
600 do {
Willy Tarreau274ba672019-04-24 10:48:00 +0200601 if (likely(full != srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200602 fwrr_queue_by_weight(grp->init, full);
603 full = full->next_full;
604 } while (full);
605 } else {
606 /* requeue all extracted servers just as if they were consumed
607 * so that they regain their expected place.
608 */
609 do {
Willy Tarreau274ba672019-04-24 10:48:00 +0200610 if (likely(full != srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200611 fwrr_queue_srv(full);
612 full = full->next_full;
613 } while (full);
614 }
615 }
Christopher Faulet5b517552017-06-09 14:17:53 +0200616 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100617 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200618 return srv;
619}
620
621/*
622 * Local variables:
623 * c-indent-level: 8
624 * c-basic-offset: 8
625 * End:
626 */