blob: a762623f331527ab795525961447644a7545c13f [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 Tarreau1e56f922020-06-04 23:20:13 +020013#include <import/eb32tree.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <haproxy/backend.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020016#include <haproxy/queue.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020017#include <haproxy/server-t.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020018
Willy Tarreauf89c1872009-10-01 11:19:37 +020019
20static inline void fwrr_remove_from_tree(struct server *s);
21static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s);
22static inline void fwrr_dequeue_srv(struct server *s);
23static void fwrr_get_srv(struct server *s);
24static void fwrr_queue_srv(struct server *s);
25
26
27/* This function updates the server trees according to server <srv>'s new
28 * state. It should be called when server <srv>'s status changes to down.
29 * It is not important whether the server was already down or not. It is not
30 * important either that the new state is completely down (the caller may not
31 * know all the variables of a server's state).
Willy Tarreau1b877482018-08-21 19:44:53 +020032 *
33 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020034 */
35static void fwrr_set_server_status_down(struct server *srv)
36{
37 struct proxy *p = srv->proxy;
38 struct fwrr_group *grp;
39
Willy Tarreauc5150da2014-05-13 19:27:31 +020040 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020041 return;
42
Emeric Brun52a91d32017-08-31 14:41:55 +020043 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020044 goto out_update_state;
45
Willy Tarreaucd10def2020-10-17 18:48:47 +020046 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +020047
Emeric Brun52a91d32017-08-31 14:41:55 +020048 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020049 /* server was already down */
50 goto out_update_backend;
51
Willy Tarreauc93cd162014-05-13 15:54:22 +020052 grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Emeric Brun52a91d32017-08-31 14:41:55 +020053 grp->next_weight -= srv->cur_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +020054
Willy Tarreauc93cd162014-05-13 15:54:22 +020055 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +020056 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
57 p->srv_bck--;
58
59 if (srv == p->lbprm.fbck) {
60 /* we lost the first backup server in a single-backup
61 * configuration, we must search another one.
62 */
63 struct server *srv2 = p->lbprm.fbck;
64 do {
65 srv2 = srv2->next;
66 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +020067 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +020068 srv_willbe_usable(srv2)));
Willy Tarreauf89c1872009-10-01 11:19:37 +020069 p->lbprm.fbck = srv2;
70 }
71 } else {
72 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
73 p->srv_act--;
74 }
75
76 fwrr_dequeue_srv(srv);
77 fwrr_remove_from_tree(srv);
78
79out_update_backend:
80 /* check/update tot_used, tot_weight */
81 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +020082 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +020083
Willy Tarreauf89c1872009-10-01 11:19:37 +020084 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +020085 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +020086}
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.
Willy Tarreau1b877482018-08-21 19:44:53 +020094 *
95 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +020096 */
97static void fwrr_set_server_status_up(struct server *srv)
98{
99 struct proxy *p = srv->proxy;
100 struct fwrr_group *grp;
101
Willy Tarreauc5150da2014-05-13 19:27:31 +0200102 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200103 return;
104
Emeric Brun52a91d32017-08-31 14:41:55 +0200105 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200106 goto out_update_state;
107
Willy Tarreaucd10def2020-10-17 18:48:47 +0200108 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200109
Emeric Brun52a91d32017-08-31 14:41:55 +0200110 if (srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200111 /* server was already up */
112 goto out_update_backend;
113
Willy Tarreauc93cd162014-05-13 15:54:22 +0200114 grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200115 grp->next_weight += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200116
Willy Tarreauc93cd162014-05-13 15:54:22 +0200117 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200118 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
119 p->srv_bck++;
120
121 if (!(p->options & PR_O_USE_ALL_BK)) {
122 if (!p->lbprm.fbck) {
123 /* there was no backup server anymore */
124 p->lbprm.fbck = srv;
125 } else {
126 /* we may have restored a backup server prior to fbck,
127 * in which case it should replace it.
128 */
129 struct server *srv2 = srv;
130 do {
131 srv2 = srv2->next;
132 } while (srv2 && (srv2 != p->lbprm.fbck));
133 if (srv2)
134 p->lbprm.fbck = srv;
135 }
136 }
137 } else {
138 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
139 p->srv_act++;
140 }
141
142 /* note that eweight cannot be 0 here */
143 fwrr_get_srv(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +0200144 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200145 fwrr_queue_srv(srv);
146
147out_update_backend:
148 /* check/update tot_used, tot_weight */
149 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200150 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200151
Willy Tarreauf89c1872009-10-01 11:19:37 +0200152 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200153 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200154}
155
156/* This function must be called after an update to server <srv>'s effective
157 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200158 *
159 * The server's lock must be held. The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200160 */
161static void fwrr_update_server_weight(struct server *srv)
162{
163 int old_state, new_state;
164 struct proxy *p = srv->proxy;
165 struct fwrr_group *grp;
166
Willy Tarreauc5150da2014-05-13 19:27:31 +0200167 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200168 return;
169
170 /* If changing the server's weight changes its state, we simply apply
171 * the procedures we already have for status change. If the state
172 * remains down, the server is not in any tree, so it's as easy as
173 * updating its values. If the state remains up with different weights,
174 * there are some computations to perform to find a new place and
175 * possibly a new tree for this server.
176 */
177
Emeric Brun52a91d32017-08-31 14:41:55 +0200178 old_state = srv_currently_usable(srv);
179 new_state = srv_willbe_usable(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200180
181 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200182 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200183 return;
184 }
185 else if (!old_state && new_state) {
186 fwrr_set_server_status_up(srv);
187 return;
188 }
189 else if (old_state && !new_state) {
190 fwrr_set_server_status_down(srv);
191 return;
192 }
193
Willy Tarreaucd10def2020-10-17 18:48:47 +0200194 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200195
Willy Tarreauc93cd162014-05-13 15:54:22 +0200196 grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Emeric Brun52a91d32017-08-31 14:41:55 +0200197 grp->next_weight = grp->next_weight - srv->cur_eweight + srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200198
199 p->lbprm.tot_wact = p->lbprm.fwrr.act.next_weight;
200 p->lbprm.tot_wbck = p->lbprm.fwrr.bck.next_weight;
201
202 if (srv->lb_tree == grp->init) {
203 fwrr_dequeue_srv(srv);
204 fwrr_queue_by_weight(grp->init, srv);
205 }
206 else if (!srv->lb_tree) {
207 /* FIXME: server was down. This is not possible right now but
208 * may be needed soon for slowstart or graceful shutdown.
209 */
210 fwrr_dequeue_srv(srv);
211 fwrr_get_srv(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +0200212 srv->npos = grp->curr_pos + (grp->next_weight + grp->curr_weight - grp->curr_pos) / srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200213 fwrr_queue_srv(srv);
214 } else {
215 /* The server is either active or in the next queue. If it's
216 * still in the active queue and it has not consumed all of its
217 * places, let's adjust its next position.
218 */
219 fwrr_get_srv(srv);
220
Emeric Brun52a91d32017-08-31 14:41:55 +0200221 if (srv->next_eweight > 0) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200222 int prev_next = srv->npos;
Emeric Brun52a91d32017-08-31 14:41:55 +0200223 int step = grp->next_weight / srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200224
225 srv->npos = srv->lpos + step;
226 srv->rweight = 0;
227
228 if (srv->npos > prev_next)
229 srv->npos = prev_next;
230 if (srv->npos < grp->curr_pos + 2)
231 srv->npos = grp->curr_pos + step;
232 } else {
233 /* push it into the next tree */
234 srv->npos = grp->curr_pos + grp->curr_weight;
235 }
236
237 fwrr_dequeue_srv(srv);
238 fwrr_queue_srv(srv);
239 }
240
241 update_backend_weight(p);
Willy Tarreaucd10def2020-10-17 18:48:47 +0200242 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau1b877482018-08-21 19:44:53 +0200243
Willy Tarreauc5150da2014-05-13 19:27:31 +0200244 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200245}
246
247/* Remove a server from a tree. It must have previously been dequeued. This
248 * function is meant to be called when a server is going down or has its
249 * weight disabled.
Willy Tarreau1b877482018-08-21 19:44:53 +0200250 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200251 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200252 */
253static inline void fwrr_remove_from_tree(struct server *s)
254{
255 s->lb_tree = NULL;
256}
257
258/* Queue a server in the weight tree <root>, assuming the weight is >0.
259 * We want to sort them by inverted weights, because we need to place
260 * heavy servers first in order to get a smooth distribution.
Willy Tarreau1b877482018-08-21 19:44:53 +0200261 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200262 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200263 */
264static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s)
265{
Emeric Brun52a91d32017-08-31 14:41:55 +0200266 s->lb_node.key = SRV_EWGHT_MAX - s->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200267 eb32_insert(root, &s->lb_node);
268 s->lb_tree = root;
269}
270
271/* This function is responsible for building the weight trees in case of fast
272 * weighted round-robin. It also sets p->lbprm.wdiv to the eweight to uweight
273 * ratio. Both active and backup groups are initialized.
274 */
275void fwrr_init_server_groups(struct proxy *p)
276{
277 struct server *srv;
278 struct eb_root init_head = EB_ROOT;
279
280 p->lbprm.set_server_status_up = fwrr_set_server_status_up;
281 p->lbprm.set_server_status_down = fwrr_set_server_status_down;
282 p->lbprm.update_server_eweight = fwrr_update_server_weight;
283
284 p->lbprm.wdiv = BE_WEIGHT_SCALE;
285 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200286 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200287 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200288 }
289
290 recount_servers(p);
291 update_backend_weight(p);
292
293 /* prepare the active servers group */
294 p->lbprm.fwrr.act.curr_pos = p->lbprm.fwrr.act.curr_weight =
295 p->lbprm.fwrr.act.next_weight = p->lbprm.tot_wact;
296 p->lbprm.fwrr.act.curr = p->lbprm.fwrr.act.t0 =
297 p->lbprm.fwrr.act.t1 = init_head;
298 p->lbprm.fwrr.act.init = &p->lbprm.fwrr.act.t0;
299 p->lbprm.fwrr.act.next = &p->lbprm.fwrr.act.t1;
300
301 /* prepare the backup servers group */
302 p->lbprm.fwrr.bck.curr_pos = p->lbprm.fwrr.bck.curr_weight =
303 p->lbprm.fwrr.bck.next_weight = p->lbprm.tot_wbck;
304 p->lbprm.fwrr.bck.curr = p->lbprm.fwrr.bck.t0 =
305 p->lbprm.fwrr.bck.t1 = init_head;
306 p->lbprm.fwrr.bck.init = &p->lbprm.fwrr.bck.t0;
307 p->lbprm.fwrr.bck.next = &p->lbprm.fwrr.bck.t1;
308
309 /* queue active and backup servers in two distinct groups */
310 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200311 if (!srv_currently_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200312 continue;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200313 fwrr_queue_by_weight((srv->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200314 p->lbprm.fwrr.bck.init :
315 p->lbprm.fwrr.act.init,
316 srv);
317 }
318}
319
Willy Tarreau1b877482018-08-21 19:44:53 +0200320/* simply removes a server from a weight tree.
321 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200322 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200323 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200324static inline void fwrr_dequeue_srv(struct server *s)
325{
326 eb32_delete(&s->lb_node);
327}
328
329/* queues a server into the appropriate group and tree depending on its
330 * backup status, and ->npos. If the server is disabled, simply assign
331 * it to the NULL tree.
Willy Tarreau1b877482018-08-21 19:44:53 +0200332 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200333 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200334 */
335static void fwrr_queue_srv(struct server *s)
336{
337 struct proxy *p = s->proxy;
338 struct fwrr_group *grp;
339
Willy Tarreauc93cd162014-05-13 15:54:22 +0200340 grp = (s->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
Christopher Faulet5b517552017-06-09 14:17:53 +0200341
Willy Tarreauf89c1872009-10-01 11:19:37 +0200342 /* Delay everything which does not fit into the window and everything
Ilya Shipitsin01881082021-08-07 14:41:56 +0500343 * which does not fit into the theoretical new window.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200344 */
Emeric Brun52a91d32017-08-31 14:41:55 +0200345 if (!srv_willbe_usable(s)) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200346 fwrr_remove_from_tree(s);
347 }
Emeric Brun52a91d32017-08-31 14:41:55 +0200348 else if (s->next_eweight <= 0 ||
Willy Tarreauf89c1872009-10-01 11:19:37 +0200349 s->npos >= 2 * grp->curr_weight ||
350 s->npos >= grp->curr_weight + grp->next_weight) {
351 /* put into next tree, and readjust npos in case we could
352 * finally take this back to current. */
Willy Tarreau274ba672019-04-24 10:48:00 +0200353 s->npos -= grp->curr_weight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200354 fwrr_queue_by_weight(grp->next, s);
355 }
356 else {
357 /* The sorting key is stored in units of s->npos * user_weight
358 * in order to avoid overflows. As stated in backend.h, the
359 * lower the scale, the rougher the weights modulation, and the
360 * higher the scale, the lower the number of servers without
361 * overflow. With this formula, the result is always positive,
Godbacha34bdc02013-07-22 07:44:53 +0800362 * so we can use eb32_insert().
Willy Tarreauf89c1872009-10-01 11:19:37 +0200363 */
364 s->lb_node.key = SRV_UWGHT_RANGE * s->npos +
Emeric Brun52a91d32017-08-31 14:41:55 +0200365 (unsigned)(SRV_EWGHT_MAX + s->rweight - s->next_eweight) / BE_WEIGHT_SCALE;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200366
367 eb32_insert(&grp->curr, &s->lb_node);
368 s->lb_tree = &grp->curr;
369 }
370}
371
Willy Tarreau1b877482018-08-21 19:44:53 +0200372/* prepares a server when extracting it from the "init" tree.
373 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200374 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200375 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200376static inline void fwrr_get_srv_init(struct server *s)
377{
378 s->npos = s->rweight = 0;
379}
380
Willy Tarreau1b877482018-08-21 19:44:53 +0200381/* prepares a server when extracting it from the "next" tree.
382 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200383 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200384 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200385static inline void fwrr_get_srv_next(struct server *s)
386{
Willy Tarreauc93cd162014-05-13 15:54:22 +0200387 struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200388 &s->proxy->lbprm.fwrr.bck :
389 &s->proxy->lbprm.fwrr.act;
390
Willy Tarreau274ba672019-04-24 10:48:00 +0200391 s->npos += grp->curr_weight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200392}
393
Willy Tarreau1b877482018-08-21 19:44:53 +0200394/* prepares a server when it was marked down.
395 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200396 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200397 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200398static inline void fwrr_get_srv_down(struct server *s)
399{
Willy Tarreauc93cd162014-05-13 15:54:22 +0200400 struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200401 &s->proxy->lbprm.fwrr.bck :
402 &s->proxy->lbprm.fwrr.act;
403
404 s->npos = grp->curr_pos;
405}
406
Willy Tarreau1b877482018-08-21 19:44:53 +0200407/* prepares a server when extracting it from its tree.
408 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200409 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreau1b877482018-08-21 19:44:53 +0200410 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200411static void fwrr_get_srv(struct server *s)
412{
413 struct proxy *p = s->proxy;
Willy Tarreauc93cd162014-05-13 15:54:22 +0200414 struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
Willy Tarreauf89c1872009-10-01 11:19:37 +0200415 &p->lbprm.fwrr.bck :
416 &p->lbprm.fwrr.act;
417
418 if (s->lb_tree == grp->init) {
419 fwrr_get_srv_init(s);
420 }
421 else if (s->lb_tree == grp->next) {
422 fwrr_get_srv_next(s);
423 }
424 else if (s->lb_tree == NULL) {
425 fwrr_get_srv_down(s);
426 }
427}
428
429/* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty
430 * when this happens, and "next" filled with servers sorted by weights.
Willy Tarreau1b877482018-08-21 19:44:53 +0200431 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200432 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200433 */
434static inline void fwrr_switch_trees(struct fwrr_group *grp)
435{
436 struct eb_root *swap;
437 swap = grp->init;
438 grp->init = grp->next;
439 grp->next = swap;
440 grp->curr_weight = grp->next_weight;
441 grp->curr_pos = grp->curr_weight;
442}
443
444/* return next server from the current tree in FWRR group <grp>, or a server
445 * from the "init" tree if appropriate. If both trees are empty, return NULL.
Willy Tarreau1b877482018-08-21 19:44:53 +0200446 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200447 * The lbprm's lock must be held. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200448 */
449static struct server *fwrr_get_server_from_group(struct fwrr_group *grp)
450{
Willy Tarreau9df86f92019-04-16 11:21:14 +0200451 struct eb32_node *node1;
452 struct eb32_node *node2;
453 struct server *s1 = NULL;
454 struct server *s2 = NULL;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200455
Willy Tarreau9df86f92019-04-16 11:21:14 +0200456 node1 = eb32_first(&grp->curr);
457 if (node1) {
458 s1 = eb32_entry(node1, struct server, lb_node);
Willy Tarreau9df86f92019-04-16 11:21:14 +0200459 if (s1->cur_eweight && s1->npos <= grp->curr_pos)
460 return s1;
461 }
Christopher Faulet5b517552017-06-09 14:17:53 +0200462
Willy Tarreau9df86f92019-04-16 11:21:14 +0200463 /* Either we have no server left, or we have a hole. We'll look in the
464 * init tree or a better proposal. At this point, if <s1> is non-null,
Willy Tarreau274ba672019-04-24 10:48:00 +0200465 * it is guaranteed to remain available as the tree is locked.
Willy Tarreau9df86f92019-04-16 11:21:14 +0200466 */
467 node2 = eb32_first(grp->init);
468 if (node2) {
469 s2 = eb32_entry(node2, struct server, lb_node);
Willy Tarreau9df86f92019-04-16 11:21:14 +0200470 if (s2->cur_eweight) {
Willy Tarreau9df86f92019-04-16 11:21:14 +0200471 fwrr_get_srv_init(s2);
472 return s2;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200473 }
474 }
Willy Tarreau9df86f92019-04-16 11:21:14 +0200475 return s1;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200476}
477
Willy Tarreau274ba672019-04-24 10:48:00 +0200478/* Computes next position of server <s> in the group. Nothing is done if <s>
479 * has a zero weight.
Willy Tarreau1b877482018-08-21 19:44:53 +0200480 *
Willy Tarreau274ba672019-04-24 10:48:00 +0200481 * The lbprm's lock must be held to protect lpos/npos/rweight.
Willy Tarreau1b877482018-08-21 19:44:53 +0200482 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200483static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s)
484{
Willy Tarreau274ba672019-04-24 10:48:00 +0200485 unsigned int eweight = *(volatile unsigned int *)&s->cur_eweight;
486
487 if (!eweight)
488 return;
489
Willy Tarreauf89c1872009-10-01 11:19:37 +0200490 if (!s->npos) {
491 /* first time ever for this server */
Willy Tarreau274ba672019-04-24 10:48:00 +0200492 s->npos = grp->curr_pos;
493 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200494
Willy Tarreau274ba672019-04-24 10:48:00 +0200495 s->lpos = s->npos;
496 s->npos += grp->next_weight / eweight;
497 s->rweight += grp->next_weight % eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200498
Willy Tarreau274ba672019-04-24 10:48:00 +0200499 if (s->rweight >= eweight) {
500 s->rweight -= eweight;
501 s->npos++;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200502 }
503}
504
505/* Return next server from the current tree in backend <p>, or a server from
506 * the init tree if appropriate. If both trees are empty, return NULL.
507 * Saturated servers are skipped and requeued.
Willy Tarreau1b877482018-08-21 19:44:53 +0200508 *
Willy Tarreau63b3ae72021-06-01 16:58:31 +0200509 * The lbprm's lock will be used in R/W mode. The server's lock is not used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200510 */
511struct server *fwrr_get_next_server(struct proxy *p, struct server *srvtoavoid)
512{
513 struct server *srv, *full, *avoided;
514 struct fwrr_group *grp;
515 int switched;
516
Willy Tarreaucd10def2020-10-17 18:48:47 +0200517 HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200518 if (p->srv_act)
519 grp = &p->lbprm.fwrr.act;
Christopher Faulet5b517552017-06-09 14:17:53 +0200520 else if (p->lbprm.fbck) {
521 srv = p->lbprm.fbck;
522 goto out;
523 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200524 else if (p->srv_bck)
525 grp = &p->lbprm.fwrr.bck;
Christopher Faulet5b517552017-06-09 14:17:53 +0200526 else {
527 srv = NULL;
528 goto out;
529 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200530
531 switched = 0;
532 avoided = NULL;
533 full = NULL; /* NULL-terminated list of saturated servers */
534 while (1) {
535 /* if we see an empty group, let's first try to collect weights
536 * which might have recently changed.
537 */
538 if (!grp->curr_weight)
539 grp->curr_pos = grp->curr_weight = grp->next_weight;
540
541 /* get first server from the "current" tree. When the end of
542 * the tree is reached, we may have to switch, but only once.
543 */
544 while (1) {
545 srv = fwrr_get_server_from_group(grp);
546 if (srv)
547 break;
548 if (switched) {
549 if (avoided) {
550 srv = avoided;
Willy Tarreaub6195ef2019-05-27 10:17:05 +0200551 goto take_this_one;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200552 }
553 goto requeue_servers;
554 }
555 switched = 1;
556 fwrr_switch_trees(grp);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200557 }
558
Willy Tarreau274ba672019-04-24 10:48:00 +0200559 /* OK, we have a server. However, it may be saturated, in which
560 * case we don't want to reconsider it for now. We'll update
561 * its position and dequeue it anyway, so that we can move it
562 * to a better place afterwards.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200563 */
564 fwrr_update_position(grp, srv);
565 fwrr_dequeue_srv(srv);
566 grp->curr_pos++;
Willy Tarreaua0570452021-06-18 09:30:30 +0200567 if (!srv->maxconn || (!srv->queue.length && srv->served < srv_dynamic_maxconn(srv))) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200568 /* make sure it is not the server we are trying to exclude... */
569 if (srv != srvtoavoid || avoided)
570 break;
571
572 avoided = srv; /* ...but remember that is was selected yet avoided */
573 }
574
Willy Tarreau9df86f92019-04-16 11:21:14 +0200575 /* the server is saturated or avoided, let's chain it for later reinsertion.
Willy Tarreau9df86f92019-04-16 11:21:14 +0200576 */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200577 srv->next_full = full;
578 full = srv;
579 }
580
Willy Tarreaub6195ef2019-05-27 10:17:05 +0200581 take_this_one:
Willy Tarreauf89c1872009-10-01 11:19:37 +0200582 /* OK, we got the best server, let's update it */
583 fwrr_queue_srv(srv);
584
585 requeue_servers:
586 /* Requeue all extracted servers. If full==srv then it was
Willy Tarreau9df86f92019-04-16 11:21:14 +0200587 * avoided (unsuccessfully) and chained, omit it now. The
588 * only way to get there is by having <avoided>==NULL or
589 * <avoided>==<srv>.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200590 */
591 if (unlikely(full != NULL)) {
592 if (switched) {
593 /* the tree has switched, requeue all extracted servers
594 * into "init", because their place was lost, and only
595 * their weight matters.
596 */
597 do {
Willy Tarreau274ba672019-04-24 10:48:00 +0200598 if (likely(full != srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200599 fwrr_queue_by_weight(grp->init, full);
600 full = full->next_full;
601 } while (full);
602 } else {
603 /* requeue all extracted servers just as if they were consumed
604 * so that they regain their expected place.
605 */
606 do {
Willy Tarreau274ba672019-04-24 10:48:00 +0200607 if (likely(full != srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +0200608 fwrr_queue_srv(full);
609 full = full->next_full;
610 } while (full);
611 }
612 }
Christopher Faulet5b517552017-06-09 14:17:53 +0200613 out:
Willy Tarreaucd10def2020-10-17 18:48:47 +0200614 HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200615 return srv;
616}
617
618/*
619 * Local variables:
620 * c-indent-level: 8
621 * c-basic-offset: 8
622 * End:
623 */