blob: 6f87697416b682c37d1553684658b7517682178c [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 Tarreau8d2b7772020-05-27 10:58:19 +020014#include <import/eb32tree.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020015
16#include <types/global.h>
17#include <types/server.h>
18
19#include <proto/backend.h>
20#include <proto/queue.h>
21
22static inline void fwrr_remove_from_tree(struct server *s);
23static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s);
24static inline void fwrr_dequeue_srv(struct server *s);
25static void fwrr_get_srv(struct server *s);
26static void fwrr_queue_srv(struct server *s);
27
28
29/* This function updates the server trees according to server <srv>'s new
30 * state. It should be called when server <srv>'s status changes to down.
31 * It is not important whether the server was already down or not. It is not
32 * important either that the new state is completely down (the caller may not
33 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020034 *
35 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020036 */
37static void fwrr_set_server_status_down(struct server *srv)
38{
39 struct proxy *p = srv->proxy;
40 struct fwrr_group *grp;
41
Willy Tarreauc5150da2014-05-13 19:27:31 +020042 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020043 return;
44
Emeric Brun52a91d32017-08-31 14:41:55 +020045 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020046 goto out_update_state;
47
Willy Tarreau1b877482018-08-21 19:44:53 +020048 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
49
Emeric Brun52a91d32017-08-31 14:41:55 +020050 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020051 /* server was already down */
52 goto out_update_backend;
53
Willy Tarreauc93cd162014-05-13 15:54:22 +020054 grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Emeric Brun52a91d32017-08-31 14:41:55 +020055 grp->next_weight -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +020056
Willy Tarreauc93cd162014-05-13 15:54:22 +020057 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +020058 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
59 p->srv_bck--;
60
61 if (srv == p->lbprm.fbck) {
62 /* we lost the first backup server in a single-backup
63 * configuration, we must search another one.
64 */
65 struct server *srv2 = p->lbprm.fbck;
66 do {
67 srv2 = srv2->next;
68 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +020069 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +020070 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +020071 p->lbprm.fbck = srv2;
72 }
73 } else {
74 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
75 p->srv_act--;
76 }
77
78 fwrr_dequeue_srv(srv);
79 fwrr_remove_from_tree(srv);
80
81out_update_backend:
82 /* check/update tot_used, tot_weight */
83 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +020084 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
85
Willy Tarreauf89c1872009-10-01 11:19:37 +020086 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +020087 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +020088}
89
90/* This function updates the server trees according to server <srv>'s new
91 * state. It should be called when server <srv>'s status changes to up.
92 * It is not important whether the server was already down or not. It is not
93 * important either that the new state is completely UP (the caller may not
94 * know all the variables of a server's state). This function will not change
95 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +020096 *
97 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020098 */
99static void fwrr_set_server_status_up(struct server *srv)
100{
101 struct proxy *p = srv->proxy;
102 struct fwrr_group *grp;
103
Willy Tarreauc5150da2014-05-13 19:27:31 +0200104 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200105 return;
106
Emeric Brun52a91d32017-08-31 14:41:55 +0200107 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200108 goto out_update_state;
109
Willy Tarreau1b877482018-08-21 19:44:53 +0200110 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
111
Emeric Brun52a91d32017-08-31 14:41:55 +0200112 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200113 /* server was already up */
114 goto out_update_backend;
115
Willy Tarreauc93cd162014-05-13 15:54:22 +0200116 grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200117 grp->next_weight += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200118
Willy Tarreauc93cd162014-05-13 15:54:22 +0200119 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200120 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
121 p->srv_bck++;
122
123 if (!(p->options & PR_O_USE_ALL_BK)) {
124 if (!p->lbprm.fbck) {
125 /* there was no backup server anymore */
126 p->lbprm.fbck = srv;
127 } else {
128 /* we may have restored a backup server prior to fbck,
129 * in which case it should replace it.
130 */
131 struct server *srv2 = srv;
132 do {
133 srv2 = srv2->next;
134 } while (srv2 && (srv2 != p->lbprm.fbck));
135 if (srv2)
136 p->lbprm.fbck = srv;
137 }
138 }
139 } else {
140 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
141 p->srv_act++;
142 }
143
144 /* note that eweight cannot be 0 here */
145 fwrr_get_srv(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +0200146 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200147 fwrr_queue_srv(srv);
148
149out_update_backend:
150 /* check/update tot_used, tot_weight */
151 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200152 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
153
Willy Tarreauf89c1872009-10-01 11:19:37 +0200154 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200155 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200156}
157
158/* This function must be called after an update to server <srv>'s effective
159 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200160 *
161 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200162 */
163static void fwrr_update_server_weight(struct server *srv)
164{
165 int old_state, new_state;
166 struct proxy *p = srv->proxy;
167 struct fwrr_group *grp;
168
Willy Tarreauc5150da2014-05-13 19:27:31 +0200169 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200170 return;
171
172 /* If changing the server's weight changes its state, we simply apply
173 * the procedures we already have for status change. If the state
174 * remains down, the server is not in any tree, so it's as easy as
175 * updating its values. If the state remains up with different weights,
176 * there are some computations to perform to find a new place and
177 * possibly a new tree for this server.
178 */
179
Emeric Brun52a91d32017-08-31 14:41:55 +0200180 old_state = srv_currently_usable(srv);
181 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200182
183 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200184 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200185 return;
186 }
187 else if (!old_state && new_state) {
188 fwrr_set_server_status_up(srv);
189 return;
190 }
191 else if (old_state && !new_state) {
192 fwrr_set_server_status_down(srv);
193 return;
194 }
195
Willy Tarreau1b877482018-08-21 19:44:53 +0200196 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
197
Willy Tarreauc93cd162014-05-13 15:54:22 +0200198 grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200199 grp->next_weight = grp->next_weight - srv->cur_eweight + srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200200
201 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
202 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
203
204 if (srv->lb_tree == grp->init) {
205 fwrr_dequeue_srv(srv);
206 fwrr_queue_by_weight(grp->init, srv);
207 }
208 else if (!srv->lb_tree) {
209 /* FIXME: server was down. This is not possible right now but
210 * may be needed soon for slowstart or graceful shutdown.
211 */
212 fwrr_dequeue_srv(srv);
213 fwrr_get_srv(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +0200214 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200215 fwrr_queue_srv(srv);
216 } else {
217 /* The server is either active or in the next queue. If it's
218 * still in the active queue and it has not consumed all of its
219 * places, let's adjust its next position.
220 */
221 fwrr_get_srv(srv);
222
Emeric Brun52a91d32017-08-31 14:41:55 +0200223 if (srv->next_eweight > 0) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200224 int prev_next = srv->npos;
Emeric Brun52a91d32017-08-31 14:41:55 +0200225 int step = grp->next_weight / srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200226
227 srv->npos = srv->lpos + step;
228 srv->rweight = 0;
229
230 if (srv->npos > prev_next)
231 srv->npos = prev_next;
232 if (srv->npos < grp->curr_pos + 2)
233 srv->npos = grp->curr_pos + step;
234 } else {
235 /* push it into the next tree */
236 srv->npos = grp->curr_pos + grp->curr_weight;
237 }
238
239 fwrr_dequeue_srv(srv);
240 fwrr_queue_srv(srv);
241 }
242
243 update_backend_weight(p);
Willy Tarreau1b877482018-08-21 19:44:53 +0200244 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
245
Willy Tarreauc5150da2014-05-13 19:27:31 +0200246 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200247}
248
249/* Remove a server from a tree. It must have previously been dequeued. This
250 * function is meant to be called when a server is going down or has its
251 * weight disabled.
Willy Tarreau1b877482018-08-21 19:44:53 +0200252 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200253 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200254 */
255static inline void fwrr_remove_from_tree(struct server *s)
256{
257 s->lb_tree = NULL;
258}
259
260/* Queue a server in the weight tree <root>, assuming the weight is >0.
261 * We want to sort them by inverted weights, because we need to place
262 * heavy servers first in order to get a smooth distribution.
Willy Tarreau1b877482018-08-21 19:44:53 +0200263 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200264 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200265 */
266static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s)
267{
Emeric Brun52a91d32017-08-31 14:41:55 +0200268 s->lb_node.key = SRV_EWGHT_MAX - s->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200269 eb32_insert(root, &s->lb_node);
270 s->lb_tree = root;
271}
272
273/* This function is responsible for building the weight trees in case of fast
274 * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight
275 * ratio. Both active and backup groups are initialized.
276 */
277void fwrr_init_server_groups(struct proxy *p)
278{
279 struct server *srv;
280 struct eb_root init_head = EB_ROOT;
281
282 p->lbprm.set_server_status_up = fwrr_set_server_status_up;
283 p->lbprm.set_server_status_down = fwrr_set_server_status_down;
284 p->lbprm.update_server_eweight = fwrr_update_server_weight;
285
286 p->lbprm.wdiv = BE_WEIGHT_SCALE;
287 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200288 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200289 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200290 }
291
292 recount_servers(p);
293 update_backend_weight(p);
294
295 /* prepare the active servers group */
296 p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight =
297 p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact;
298 p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 =
299 p->lbprm.fwrr.act.t1 = init_head;
300 p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0;
301 p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1;
302
303 /* prepare the backup servers group */
304 p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight =
305 p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck;
306 p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 =
307 p->lbprm.fwrr.bck.t1 = init_head;
308 p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0;
309 p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1;
310
311 /* queue active and backup servers in two distinct groups */
312 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200313 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200314 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200315 fwrr_queue_by_weight((srv->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200316 p->lbprm.fwrr.bck.init :
317 p->lbprm.fwrr.act.init,
318 srv);
319 }
320}
321
Willy Tarreau1b877482018-08-21 19:44:53 +0200322/* simply removes a server from a weight tree.
323 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200324 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200325 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200326static inline void fwrr_dequeue_srv(struct server *s)
327{
328 eb32_delete(&s->lb_node);
329}
330
331/* queues a server into the appropriate group and tree depending on its
332 * backup status, and ->npos. If the server is disabled, simply assign
333 * it to the NULL tree.
Willy Tarreau1b877482018-08-21 19:44:53 +0200334 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200335 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200336 */
337static void fwrr_queue_srv(struct server *s)
338{
339 struct proxy *p = s->proxy;
340 struct fwrr_group *grp;
341
Willy Tarreauc93cd162014-05-13 15:54:22 +0200342 grp = (s->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Christopher Faulet5b517552017-06-09 14:17:53 +0200343
Willy Tarreauf89c1872009-10-01 11:19:37 +0200344 /* Delay everything which does not fit into the window and everything
345 * which does not fit into the theorical new window.
346 */
Emeric Brun52a91d32017-08-31 14:41:55 +0200347 if (!srv_willbe_usable(s)) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200348 fwrr_remove_from_tree(s);
349 }
Emeric Brun52a91d32017-08-31 14:41:55 +0200350 else if (s->next_eweight <= 0 ||
Willy Tarreauf89c1872009-10-01 11:19:37 +0200351 s->npos >= 2 * grp->curr_weight ||
352 s->npos >= grp->curr_weight + grp->next_weight) {
353 /* put into next tree, and readjust npos in case we could
354 * finally take this back to current. */
Willy Tarreau274ba672019-04-24 10:48:00 +0200355 s->npos -= grp->curr_weight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200356 fwrr_queue_by_weight(grp->next, s);
357 }
358 else {
359 /* The sorting key is stored in units of s->npos * user_weight
360 * in order to avoid overflows. As stated in backend.h, the
361 * lower the scale, the rougher the weights modulation, and the
362 * higher the scale, the lower the number of servers without
363 * overflow. With this formula, the result is always positive,
Godbacha34bdc02013-07-22 07:44:53 +0800364 * so we can use eb32_insert().
Willy Tarreauf89c1872009-10-01 11:19:37 +0200365 */
366 s->lb_node.key = SRV_UWGHT_RANGE * s->npos +
Emeric Brun52a91d32017-08-31 14:41:55 +0200367 (unsigned)(SRV_EWGHT_MAX + s->rweight - s->next_eweight) / BE_WEIGHT_SCALE;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200368
369 eb32_insert(&grp->curr, &s->lb_node);
370 s->lb_tree = &grp->curr;
371 }
372}
373
Willy Tarreau1b877482018-08-21 19:44:53 +0200374/* prepares a server when extracting it from the "init" tree.
375 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200376 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200377 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200378static inline void fwrr_get_srv_init(struct server *s)
379{
380 s->npos = s->rweight = 0;
381}
382
Willy Tarreau1b877482018-08-21 19:44:53 +0200383/* prepares a server when extracting it from the "next" tree.
384 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200385 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200386 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200387static inline void fwrr_get_srv_next(struct server *s)
388{
Willy Tarreauc93cd162014-05-13 15:54:22 +0200389 struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200390 &s->proxy->lbprm.fwrr.bck :
391 &s->proxy->lbprm.fwrr.act;
392
Willy Tarreau274ba672019-04-24 10:48:00 +0200393 s->npos += grp->curr_weight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200394}
395
Willy Tarreau1b877482018-08-21 19:44:53 +0200396/* prepares a server when it was marked down.
397 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200398 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200399 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200400static inline void fwrr_get_srv_down(struct server *s)
401{
Willy Tarreauc93cd162014-05-13 15:54:22 +0200402 struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200403 &s->proxy->lbprm.fwrr.bck :
404 &s->proxy->lbprm.fwrr.act;
405
406 s->npos = grp->curr_pos;
407}
408
Willy Tarreau1b877482018-08-21 19:44:53 +0200409/* prepares a server when extracting it from its tree.
410 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200411 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200412 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200413static void fwrr_get_srv(struct server *s)
414{
415 struct proxy *p = s->proxy;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200416 struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200417 &p->lbprm.fwrr.bck :
418 &p->lbprm.fwrr.act;
419
420 if (s->lb_tree == grp->init) {
421 fwrr_get_srv_init(s);
422 }
423 else if (s->lb_tree == grp->next) {
424 fwrr_get_srv_next(s);
425 }
426 else if (s->lb_tree == NULL) {
427 fwrr_get_srv_down(s);
428 }
429}
430
431/* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty
432 * when this happens, and "next" filled with servers sorted by weights.
Willy Tarreau1b877482018-08-21 19:44:53 +0200433 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200434 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200435 */
436static inline void fwrr_switch_trees(struct fwrr_group *grp)
437{
438 struct eb_root *swap;
439 swap = grp->init;
440 grp->init = grp->next;
441 grp->next = swap;
442 grp->curr_weight = grp->next_weight;
443 grp->curr_pos = grp->curr_weight;
444}
445
446/* return next server from the current tree in FWRR group <grp>, or a server
447 * from the "init" tree if appropriate. If both trees are empty, return NULL.
Willy Tarreau1b877482018-08-21 19:44:53 +0200448 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200449 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200450 */
451static struct server *fwrr_get_server_from_group(struct fwrr_group *grp)
452{
Willy Tarreau9df86f92019-04-16 11:21:14 +0200453 struct eb32_node *node1;
454 struct eb32_node *node2;
455 struct server *s1 = NULL;
456 struct server *s2 = NULL;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200457
Willy Tarreau9df86f92019-04-16 11:21:14 +0200458 node1 = eb32_first(&grp->curr);
459 if (node1) {
460 s1 = eb32_entry(node1, struct server, lb_node);
Willy Tarreau9df86f92019-04-16 11:21:14 +0200461 if (s1->cur_eweight && s1->npos <= grp->curr_pos)
462 return s1;
463 }
Christopher Faulet5b517552017-06-09 14:17:53 +0200464
Willy Tarreau9df86f92019-04-16 11:21:14 +0200465 /* Either we have no server left, or we have a hole. We'll look in the
466 * init tree or a better proposal. At this point, if <s1> is non-null,
Willy Tarreau274ba672019-04-24 10:48:00 +0200467 * it is guaranteed to remain available as the tree is locked.
Willy Tarreau9df86f92019-04-16 11:21:14 +0200468 */
469 node2 = eb32_first(grp->init);
470 if (node2) {
471 s2 = eb32_entry(node2, struct server, lb_node);
Willy Tarreau9df86f92019-04-16 11:21:14 +0200472 if (s2->cur_eweight) {
Willy Tarreau9df86f92019-04-16 11:21:14 +0200473 fwrr_get_srv_init(s2);
474 return s2;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200475 }
476 }
Willy Tarreau9df86f92019-04-16 11:21:14 +0200477 return s1;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200478}
479
Willy Tarreau274ba672019-04-24 10:48:00 +0200480/* Computes next position of server <s> in the group. Nothing is done if <s>
481 * has a zero weight.
Willy Tarreau1b877482018-08-21 19:44:53 +0200482 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200483 * The lbprm's lock must be held to protect lpos/npos/rweight.
Willy Tarreau1b877482018-08-21 19:44:53 +0200484 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200485static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s)
486{
Willy Tarreau274ba672019-04-24 10:48:00 +0200487 unsigned int eweight = *(volatile unsigned int *)&s->cur_eweight;
488
489 if (!eweight)
490 return;
491
Willy Tarreauf89c1872009-10-01 11:19:37 +0200492 if (!s->npos) {
493 /* first time ever for this server */
Willy Tarreau274ba672019-04-24 10:48:00 +0200494 s->npos = grp->curr_pos;
495 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200496
Willy Tarreau274ba672019-04-24 10:48:00 +0200497 s->lpos = s->npos;
498 s->npos += grp->next_weight / eweight;
499 s->rweight += grp->next_weight % eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200500
Willy Tarreau274ba672019-04-24 10:48:00 +0200501 if (s->rweight >= eweight) {
502 s->rweight -= eweight;
503 s->npos++;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200504 }
505}
506
507/* Return next server from the current tree in backend <p>, or a server from
508 * the init tree if appropriate. If both trees are empty, return NULL.
509 * Saturated servers are skipped and requeued.
Willy Tarreau1b877482018-08-21 19:44:53 +0200510 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200511 * The lbprm's lock will be used. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200512 */
513struct server *fwrr_get_next_server(struct proxy *p, struct server *srvtoavoid)
514{
515 struct server *srv, *full, *avoided;
516 struct fwrr_group *grp;
517 int switched;
518
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100519 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200520 if (p->srv_act)
521 grp = &p->lbprm.fwrr.act;
Christopher Faulet5b517552017-06-09 14:17:53 +0200522 else if (p->lbprm.fbck) {
523 srv = p->lbprm.fbck;
524 goto out;
525 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200526 else if (p->srv_bck)
527 grp = &p->lbprm.fwrr.bck;
Christopher Faulet5b517552017-06-09 14:17:53 +0200528 else {
529 srv = NULL;
530 goto out;
531 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200532
533 switched = 0;
534 avoided = NULL;
535 full = NULL; /* NULL-terminated list of saturated servers */
536 while (1) {
537 /* if we see an empty group, let's first try to collect weights
538 * which might have recently changed.
539 */
540 if (!grp->curr_weight)
541 grp->curr_pos = grp->curr_weight = grp->next_weight;
542
543 /* get first server from the "current" tree. When the end of
544 * the tree is reached, we may have to switch, but only once.
545 */
546 while (1) {
547 srv = fwrr_get_server_from_group(grp);
548 if (srv)
549 break;
550 if (switched) {
551 if (avoided) {
552 srv = avoided;
Willy Tarreaub6195ef2019-05-27 10:17:05 +0200553 goto take_this_one;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200554 }
555 goto requeue_servers;
556 }
557 switched = 1;
558 fwrr_switch_trees(grp);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200559 }
560
Willy Tarreau274ba672019-04-24 10:48:00 +0200561 /* OK, we have a server. However, it may be saturated, in which
562 * case we don't want to reconsider it for now. We'll update
563 * its position and dequeue it anyway, so that we can move it
564 * to a better place afterwards.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200565 */
566 fwrr_update_position(grp, srv);
567 fwrr_dequeue_srv(srv);
568 grp->curr_pos++;
569 if (!srv->maxconn || (!srv->nbpend && srv->served < srv_dynamic_maxconn(srv))) {
570 /* make sure it is not the server we are trying to exclude... */
571 if (srv != srvtoavoid || avoided)
572 break;
573
574 avoided = srv; /* ...but remember that is was selected yet avoided */
575 }
576
Willy Tarreau9df86f92019-04-16 11:21:14 +0200577 /* the server is saturated or avoided, let's chain it for later reinsertion.
Willy Tarreau9df86f92019-04-16 11:21:14 +0200578 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200579 srv->next_full = full;
580 full = srv;
581 }
582
Willy Tarreaub6195ef2019-05-27 10:17:05 +0200583 take_this_one:
Willy Tarreauf89c1872009-10-01 11:19:37 +0200584 /* OK, we got the best server, let's update it */
585 fwrr_queue_srv(srv);
586
587 requeue_servers:
588 /* Requeue all extracted servers. If full==srv then it was
Willy Tarreau9df86f92019-04-16 11:21:14 +0200589 * avoided (unsuccessfully) and chained, omit it now. The
590 * only way to get there is by having <avoided>==NULL or
591 * <avoided>==<srv>.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200592 */
593 if (unlikely(full != NULL)) {
594 if (switched) {
595 /* the tree has switched, requeue all extracted servers
596 * into "init", because their place was lost, and only
597 * their weight matters.
598 */
599 do {
Willy Tarreau274ba672019-04-24 10:48:00 +0200600 if (likely(full != srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200601 fwrr_queue_by_weight(grp->init, full);
602 full = full->next_full;
603 } while (full);
604 } else {
605 /* requeue all extracted servers just as if they were consumed
606 * so that they regain their expected place.
607 */
608 do {
Willy Tarreau274ba672019-04-24 10:48:00 +0200609 if (likely(full != srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200610 fwrr_queue_srv(full);
611 full = full->next_full;
612 } while (full);
613 }
614 }
Christopher Faulet5b517552017-06-09 14:17:53 +0200615 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100616 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200617 return srv;
618}
619
620/*
621 * Local variables:
622 * c-indent-level: 8
623 * c-basic-offset: 8
624 * End:
625 */