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