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