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