Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 13 | #include <import/eb32tree.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 14 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 15 | #include <haproxy/backend.h> |
Willy Tarreau | 2867159 | 2020-06-04 20:22:59 +0200 | [diff] [blame] | 16 | #include <haproxy/lb_map.h> |
Willy Tarreau | a55c454 | 2020-06-04 22:59:39 +0200 | [diff] [blame] | 17 | #include <haproxy/queue.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 18 | #include <haproxy/server-t.h> |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 19 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 20 | /* this function updates the map according to server <srv>'s new state. |
| 21 | * |
| 22 | * The server's lock must be held. The lbprm's lock will be used. |
| 23 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 24 | static void map_set_server_status_down(struct server *srv) |
| 25 | { |
| 26 | struct proxy *p = srv->proxy; |
| 27 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 28 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 29 | return; |
| 30 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 31 | if (srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 32 | goto out_update_state; |
| 33 | |
| 34 | /* FIXME: could be optimized since we know what changed */ |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 35 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 36 | recount_servers(p); |
| 37 | update_backend_weight(p); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 38 | recalc_server_map(p); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 39 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 40 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 41 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 44 | /* This function updates the map according to server <srv>'s new state. |
| 45 | * |
| 46 | * The server's lock must be held. The lbprm's lock will be used. |
| 47 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 48 | static void map_set_server_status_up(struct server *srv) |
| 49 | { |
| 50 | struct proxy *p = srv->proxy; |
| 51 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 52 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 53 | return; |
| 54 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 55 | if (!srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 56 | goto out_update_state; |
| 57 | |
| 58 | /* FIXME: could be optimized since we know what changed */ |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 59 | HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 60 | recount_servers(p); |
| 61 | update_backend_weight(p); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 62 | recalc_server_map(p); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 63 | HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 64 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 65 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* This function recomputes the server map for proxy px. It relies on |
| 69 | * px->lbprm.tot_wact, tot_wbck, tot_used, tot_weight, so it must be |
| 70 | * called after recount_servers(). It also expects px->lbprm.map.srv |
| 71 | * to be allocated with the largest size needed. It updates tot_weight. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 72 | * |
| 73 | * The lbprm's lock must be held. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 74 | */ |
| 75 | void recalc_server_map(struct proxy *px) |
| 76 | { |
| 77 | int o, tot, flag; |
| 78 | struct server *cur, *best; |
| 79 | |
| 80 | switch (px->lbprm.tot_used) { |
| 81 | case 0: /* no server */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 82 | return; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 83 | default: |
| 84 | tot = px->lbprm.tot_weight; |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | /* here we *know* that we have some servers */ |
| 89 | if (px->srv_act) |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 90 | flag = 0; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 91 | else |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 92 | flag = SRV_F_BACKUP; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 93 | |
| 94 | /* this algorithm gives priority to the first server, which means that |
| 95 | * it will respect the declaration order for equivalent weights, and |
| 96 | * that whatever the weights, the first server called will always be |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 97 | * the first declared. This is an important assumption for the backup |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 98 | * case, where we want the first server only. |
| 99 | */ |
| 100 | for (cur = px->srv; cur; cur = cur->next) |
| 101 | cur->wscore = 0; |
| 102 | |
| 103 | for (o = 0; o < tot; o++) { |
| 104 | int max = 0; |
| 105 | best = NULL; |
| 106 | for (cur = px->srv; cur; cur = cur->next) { |
Willy Tarreau | 9943d31 | 2014-05-22 16:20:59 +0200 | [diff] [blame] | 107 | if ((cur->flags & SRV_F_BACKUP) == flag && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 108 | srv_willbe_usable(cur)) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 109 | int v; |
| 110 | |
| 111 | /* If we are forced to return only one server, we don't want to |
| 112 | * go further, because we would return the wrong one due to |
| 113 | * divide overflow. |
| 114 | */ |
| 115 | if (tot == 1) { |
| 116 | best = cur; |
| 117 | /* note that best->wscore will be wrong but we don't care */ |
| 118 | break; |
| 119 | } |
| 120 | |
Olivier Houchard | 36a8e6f | 2019-03-08 18:52:46 +0100 | [diff] [blame] | 121 | _HA_ATOMIC_ADD(&cur->wscore, cur->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 122 | v = (cur->wscore + tot) / tot; /* result between 0 and 3 */ |
| 123 | if (best == NULL || v > max) { |
| 124 | max = v; |
| 125 | best = cur; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | px->lbprm.map.srv[o] = best; |
Willy Tarreau | c8b476d | 2018-12-02 19:22:55 +0100 | [diff] [blame] | 130 | if (best) |
Olivier Houchard | 36a8e6f | 2019-03-08 18:52:46 +0100 | [diff] [blame] | 131 | _HA_ATOMIC_SUB(&best->wscore, tot); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 132 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* This function is responsible of building the server MAP for map-based LB |
| 136 | * algorithms, allocating the map, and setting p->lbprm.wmult to the GCD of the |
| 137 | * weights if applicable. It should be called only once per proxy, at config |
| 138 | * time. |
| 139 | */ |
| 140 | void init_server_map(struct proxy *p) |
| 141 | { |
| 142 | struct server *srv; |
| 143 | int pgcd; |
| 144 | int act, bck; |
| 145 | |
| 146 | p->lbprm.set_server_status_up = map_set_server_status_up; |
| 147 | p->lbprm.set_server_status_down = map_set_server_status_down; |
| 148 | p->lbprm.update_server_eweight = NULL; |
| 149 | |
| 150 | if (!p->srv) |
| 151 | return; |
| 152 | |
| 153 | /* We will factor the weights to reduce the table, |
| 154 | * using Euclide's largest common divisor algorithm. |
| 155 | * Since we may have zero weights, we have to first |
| 156 | * find a non-zero weight server. |
| 157 | */ |
| 158 | pgcd = 1; |
| 159 | srv = p->srv; |
| 160 | while (srv && !srv->uweight) |
| 161 | srv = srv->next; |
| 162 | |
| 163 | if (srv) { |
| 164 | pgcd = srv->uweight; /* note: cannot be zero */ |
| 165 | while (pgcd > 1 && (srv = srv->next)) { |
| 166 | int w = srv->uweight; |
| 167 | while (w) { |
| 168 | int t = pgcd % w; |
| 169 | pgcd = w; |
| 170 | w = t; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* It is sometimes useful to know what factor to apply |
| 176 | * to the backend's effective weight to know its real |
| 177 | * weight. |
| 178 | */ |
| 179 | p->lbprm.wmult = pgcd; |
| 180 | |
| 181 | act = bck = 0; |
| 182 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 183 | srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult; |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 184 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 185 | if (srv->flags & SRV_F_BACKUP) |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 186 | bck += srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 187 | else |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 188 | act += srv->next_eweight; |
| 189 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* this is the largest map we will ever need for this servers list */ |
| 193 | if (act < bck) |
| 194 | act = bck; |
| 195 | |
| 196 | if (!act) |
| 197 | act = 1; |
| 198 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 199 | p->lbprm.map.srv = calloc(act, sizeof(struct server *)); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 200 | /* recounts servers and their weights */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 201 | recount_servers(p); |
| 202 | update_backend_weight(p); |
| 203 | recalc_server_map(p); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * This function tries to find a running server with free connection slots for |
| 208 | * the proxy <px> following the round-robin method. |
| 209 | * If any server is found, it will be returned and px->lbprm.map.rr_idx will be updated |
| 210 | * to point to the next server. If no valid server is found, NULL is returned. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 211 | * |
| 212 | * The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 213 | */ |
| 214 | struct server *map_get_server_rr(struct proxy *px, struct server *srvtoavoid) |
| 215 | { |
| 216 | int newidx, avoididx; |
| 217 | struct server *srv, *avoided; |
| 218 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 219 | HA_SPIN_LOCK(LBPRM_LOCK, &px->lbprm.lock); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 220 | if (px->lbprm.tot_weight == 0) { |
| 221 | avoided = NULL; |
| 222 | goto out; |
| 223 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 224 | |
| 225 | if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight) |
| 226 | px->lbprm.map.rr_idx = 0; |
| 227 | newidx = px->lbprm.map.rr_idx; |
| 228 | |
| 229 | avoided = NULL; |
| 230 | avoididx = 0; /* shut a gcc warning */ |
| 231 | do { |
| 232 | srv = px->lbprm.map.srv[newidx++]; |
Godbach | 8f9fd2f | 2013-08-07 09:48:23 +0800 | [diff] [blame] | 233 | if (!srv->maxconn || (!srv->nbpend && srv->served < srv_dynamic_maxconn(srv))) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 234 | /* make sure it is not the server we are try to exclude... */ |
Willy Tarreau | 03071f6 | 2017-11-05 10:59:12 +0100 | [diff] [blame] | 235 | /* ...but remember that is was selected yet avoided */ |
| 236 | avoided = srv; |
| 237 | avoididx = newidx; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 238 | if (srv != srvtoavoid) { |
| 239 | px->lbprm.map.rr_idx = newidx; |
Willy Tarreau | 03071f6 | 2017-11-05 10:59:12 +0100 | [diff] [blame] | 240 | goto out; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 241 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 242 | } |
| 243 | if (newidx == px->lbprm.tot_weight) |
| 244 | newidx = 0; |
| 245 | } while (newidx != px->lbprm.map.rr_idx); |
| 246 | |
| 247 | if (avoided) |
| 248 | px->lbprm.map.rr_idx = avoididx; |
| 249 | |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 250 | out: |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 251 | HA_SPIN_UNLOCK(LBPRM_LOCK, &px->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 252 | /* return NULL or srvtoavoid if found */ |
| 253 | return avoided; |
| 254 | } |
| 255 | |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 256 | /* |
| 257 | * This function returns the running server from the map at the location |
| 258 | * pointed to by the result of a modulo operation on <hash>. The server map may |
| 259 | * be recomputed if required before being looked up. If any server is found, it |
| 260 | * will be returned. If no valid server is found, NULL is returned. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 261 | * |
| 262 | * The lbprm's lock will be used. |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 263 | */ |
| 264 | struct server *map_get_server_hash(struct proxy *px, unsigned int hash) |
| 265 | { |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 266 | struct server *srv = NULL; |
| 267 | |
| 268 | HA_SPIN_LOCK(LBPRM_LOCK, &px->lbprm.lock); |
| 269 | if (px->lbprm.tot_weight) |
| 270 | srv = px->lbprm.map.srv[hash % px->lbprm.tot_weight]; |
| 271 | HA_SPIN_UNLOCK(LBPRM_LOCK, &px->lbprm.lock); |
| 272 | return srv; |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * Local variables: |
| 278 | * c-indent-level: 8 |
| 279 | * c-basic-offset: 8 |
| 280 | * End: |
| 281 | */ |