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