blob: 8d2461fcf0430889a3672cac9c48503da449f0d7 [file] [log] [blame]
Willy Tarreauf89c1872009-10-01 11:19:37 +02001/*
2 * Map-based load-balancing (RR and HASH)
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>
Christopher Faulet5b517552017-06-09 14:17:53 +020020#include <proto/lb_map.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020021#include <proto/queue.h>
22
Willy Tarreau1b877482018-08-21 19:44:53 +020023/* this function updates the map according to server <srv>'s new state.
24 *
25 * The server's lock must be held. The lbprm's lock will be used.
26 */
Willy Tarreauf89c1872009-10-01 11:19:37 +020027static void map_set_server_status_down(struct server *srv)
28{
29 struct proxy *p = srv->proxy;
30
Willy Tarreauc5150da2014-05-13 19:27:31 +020031 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020032 return;
33
Emeric Brun52a91d32017-08-31 14:41:55 +020034 if (srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020035 goto out_update_state;
36
37 /* FIXME: could be optimized since we know what changed */
Willy Tarreau1b877482018-08-21 19:44:53 +020038 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020039 recount_servers(p);
40 update_backend_weight(p);
Christopher Faulet5b517552017-06-09 14:17:53 +020041 recalc_server_map(p);
Willy Tarreau1b877482018-08-21 19:44:53 +020042 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020043 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +020044 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +020045}
46
Willy Tarreau1b877482018-08-21 19:44:53 +020047/* This function updates the map according to server <srv>'s new state.
48 *
49 * The server's lock must be held. The lbprm's lock will be used.
50 */
Willy Tarreauf89c1872009-10-01 11:19:37 +020051static void map_set_server_status_up(struct server *srv)
52{
53 struct proxy *p = srv->proxy;
54
Willy Tarreauc5150da2014-05-13 19:27:31 +020055 if (!srv_lb_status_changed(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020056 return;
57
Emeric Brun52a91d32017-08-31 14:41:55 +020058 if (!srv_willbe_usable(srv))
Willy Tarreauf89c1872009-10-01 11:19:37 +020059 goto out_update_state;
60
61 /* FIXME: could be optimized since we know what changed */
Willy Tarreau1b877482018-08-21 19:44:53 +020062 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020063 recount_servers(p);
64 update_backend_weight(p);
Christopher Faulet5b517552017-06-09 14:17:53 +020065 recalc_server_map(p);
Willy Tarreau1b877482018-08-21 19:44:53 +020066 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +020067 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +020068 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +020069}
70
71/* This function recomputes the server map for proxy px. It relies on
72 * px->lbprm.tot_wact, tot_wbck, tot_used, tot_weight, so it must be
73 * called after recount_servers(). It also expects px->lbprm.map.srv
74 * to be allocated with the largest size needed. It updates tot_weight.
Willy Tarreau1b877482018-08-21 19:44:53 +020075 *
76 * The lbprm's lock must be held.
Willy Tarreauf89c1872009-10-01 11:19:37 +020077 */
78void recalc_server_map(struct proxy *px)
79{
80 int o, tot, flag;
81 struct server *cur, *best;
82
83 switch (px->lbprm.tot_used) {
84 case 0: /* no server */
Willy Tarreauf89c1872009-10-01 11:19:37 +020085 return;
Willy Tarreauf89c1872009-10-01 11:19:37 +020086 default:
87 tot = px->lbprm.tot_weight;
88 break;
89 }
90
91 /* here we *know* that we have some servers */
92 if (px->srv_act)
Willy Tarreauc93cd162014-05-13 15:54:22 +020093 flag = 0;
Willy Tarreauf89c1872009-10-01 11:19:37 +020094 else
Willy Tarreauc93cd162014-05-13 15:54:22 +020095 flag = SRV_F_BACKUP;
Willy Tarreauf89c1872009-10-01 11:19:37 +020096
97 /* this algorithm gives priority to the first server, which means that
98 * it will respect the declaration order for equivalent weights, and
99 * that whatever the weights, the first server called will always be
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500100 * the first declared. This is an important assumption for the backup
Willy Tarreauf89c1872009-10-01 11:19:37 +0200101 * case, where we want the first server only.
102 */
103 for (cur = px->srv; cur; cur = cur->next)
104 cur->wscore = 0;
105
106 for (o = 0; o < tot; o++) {
107 int max = 0;
108 best = NULL;
109 for (cur = px->srv; cur; cur = cur->next) {
Willy Tarreau9943d312014-05-22 16:20:59 +0200110 if ((cur->flags & SRV_F_BACKUP) == flag &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200111 srv_willbe_usable(cur)) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200112 int v;
113
114 /* If we are forced to return only one server, we don't want to
115 * go further, because we would return the wrong one due to
116 * divide overflow.
117 */
118 if (tot == 1) {
119 best = cur;
120 /* note that best->wscore will be wrong but we don't care */
121 break;
122 }
123
Olivier Houchard36a8e6f2019-03-08 18:52:46 +0100124 _HA_ATOMIC_ADD(&cur->wscore, cur->next_eweight);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200125 v = (cur->wscore + tot) / tot; /* result between 0 and 3 */
126 if (best == NULL || v > max) {
127 max = v;
128 best = cur;
129 }
130 }
131 }
132 px->lbprm.map.srv[o] = best;
Willy Tarreauc8b476d2018-12-02 19:22:55 +0100133 if (best)
Olivier Houchard36a8e6f2019-03-08 18:52:46 +0100134 _HA_ATOMIC_SUB(&best->wscore, tot);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200135 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200136}
137
138/* This function is responsible of building the server MAP for map-based LB
139 * algorithms, allocating the map, and setting p->lbprm.wmult to the GCD of the
140 * weights if applicable. It should be called only once per proxy, at config
141 * time.
142 */
143void init_server_map(struct proxy *p)
144{
145 struct server *srv;
146 int pgcd;
147 int act, bck;
148
149 p->lbprm.set_server_status_up = map_set_server_status_up;
150 p->lbprm.set_server_status_down = map_set_server_status_down;
151 p->lbprm.update_server_eweight = NULL;
152
153 if (!p->srv)
154 return;
155
156 /* We will factor the weights to reduce the table,
157 * using Euclide's largest common divisor algorithm.
158 * Since we may have zero weights, we have to first
159 * find a non-zero weight server.
160 */
161 pgcd = 1;
162 srv = p->srv;
163 while (srv && !srv->uweight)
164 srv = srv->next;
165
166 if (srv) {
167 pgcd = srv->uweight; /* note: cannot be zero */
168 while (pgcd > 1 && (srv = srv->next)) {
169 int w = srv->uweight;
170 while (w) {
171 int t = pgcd % w;
172 pgcd = w;
173 w = t;
174 }
175 }
176 }
177
178 /* It is sometimes useful to know what factor to apply
179 * to the backend's effective weight to know its real
180 * weight.
181 */
182 p->lbprm.wmult = pgcd;
183
184 act = bck = 0;
185 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200186 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200187
Willy Tarreauc93cd162014-05-13 15:54:22 +0200188 if (srv->flags & SRV_F_BACKUP)
Emeric Brun52a91d32017-08-31 14:41:55 +0200189 bck += srv->next_eweight;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200190 else
Emeric Brun52a91d32017-08-31 14:41:55 +0200191 act += srv->next_eweight;
192 srv_lb_commit_status(srv);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200193 }
194
195 /* this is the largest map we will ever need for this servers list */
196 if (act < bck)
197 act = bck;
198
199 if (!act)
200 act = 1;
201
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200202 p->lbprm.map.srv = calloc(act, sizeof(struct server *));
Willy Tarreauf89c1872009-10-01 11:19:37 +0200203 /* recounts servers and their weights */
Willy Tarreauf89c1872009-10-01 11:19:37 +0200204 recount_servers(p);
205 update_backend_weight(p);
206 recalc_server_map(p);
207}
208
209/*
210 * This function tries to find a running server with free connection slots for
211 * the proxy <px> following the round-robin method.
212 * If any server is found, it will be returned and px->lbprm.map.rr_idx will be updated
213 * to point to the next server. If no valid server is found, NULL is returned.
Willy Tarreau1b877482018-08-21 19:44:53 +0200214 *
215 * The lbprm's lock will be used.
Willy Tarreauf89c1872009-10-01 11:19:37 +0200216 */
217struct server *map_get_server_rr(struct proxy *px, struct server *srvtoavoid)
218{
219 int newidx, avoididx;
220 struct server *srv, *avoided;
221
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100222 HA_SPIN_LOCK(LBPRM_LOCK, &px->lbprm.lock);
Christopher Faulet5b517552017-06-09 14:17:53 +0200223 if (px->lbprm.tot_weight == 0) {
224 avoided = NULL;
225 goto out;
226 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200227
228 if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight)
229 px->lbprm.map.rr_idx = 0;
230 newidx = px->lbprm.map.rr_idx;
231
232 avoided = NULL;
233 avoididx = 0; /* shut a gcc warning */
234 do {
235 srv = px->lbprm.map.srv[newidx++];
Godbach8f9fd2f2013-08-07 09:48:23 +0800236 if (!srv->maxconn || (!srv->nbpend && srv->served < srv_dynamic_maxconn(srv))) {
Willy Tarreauf89c1872009-10-01 11:19:37 +0200237 /* make sure it is not the server we are try to exclude... */
Willy Tarreau03071f62017-11-05 10:59:12 +0100238 /* ...but remember that is was selected yet avoided */
239 avoided = srv;
240 avoididx = newidx;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200241 if (srv != srvtoavoid) {
242 px->lbprm.map.rr_idx = newidx;
Willy Tarreau03071f62017-11-05 10:59:12 +0100243 goto out;
Willy Tarreauf89c1872009-10-01 11:19:37 +0200244 }
Willy Tarreauf89c1872009-10-01 11:19:37 +0200245 }
246 if (newidx == px->lbprm.tot_weight)
247 newidx = 0;
248 } while (newidx != px->lbprm.map.rr_idx);
249
250 if (avoided)
251 px->lbprm.map.rr_idx = avoididx;
252
Christopher Faulet5b517552017-06-09 14:17:53 +0200253 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100254 HA_SPIN_UNLOCK(LBPRM_LOCK, &px->lbprm.lock);
Willy Tarreauf89c1872009-10-01 11:19:37 +0200255 /* return NULL or srvtoavoid if found */
256 return avoided;
257}
258
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200259/*
260 * This function returns the running server from the map at the location
261 * pointed to by the result of a modulo operation on <hash>. The server map may
262 * be recomputed if required before being looked up. If any server is found, it
263 * will be returned. If no valid server is found, NULL is returned.
Willy Tarreau1b877482018-08-21 19:44:53 +0200264 *
265 * The lbprm's lock will be used.
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200266 */
267struct server *map_get_server_hash(struct proxy *px, unsigned int hash)
268{
Willy Tarreau1b877482018-08-21 19:44:53 +0200269 struct server *srv = NULL;
270
271 HA_SPIN_LOCK(LBPRM_LOCK, &px->lbprm.lock);
272 if (px->lbprm.tot_weight)
273 srv = px->lbprm.map.srv[hash % px->lbprm.tot_weight];
274 HA_SPIN_UNLOCK(LBPRM_LOCK, &px->lbprm.lock);
275 return srv;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200276}
277
Willy Tarreauf89c1872009-10-01 11:19:37 +0200278
279/*
280 * Local variables:
281 * c-indent-level: 8
282 * c-basic-offset: 8
283 * End:
284 */