Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Fast Weighted Least Connection load balancing algorithm. |
| 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 | a55c454 | 2020-06-04 22:59:39 +0200 | [diff] [blame] | 16 | #include <haproxy/queue.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 17 | #include <haproxy/server-t.h> |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 18 | |
| 19 | |
| 20 | /* Remove a server from a tree. It must have previously been dequeued. This |
| 21 | * function is meant to be called when a server is going down or has its |
| 22 | * weight disabled. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 23 | * |
| 24 | * The server's lock and the lbprm's lock must be held. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 25 | */ |
| 26 | static inline void fwlc_remove_from_tree(struct server *s) |
| 27 | { |
| 28 | s->lb_tree = NULL; |
| 29 | } |
| 30 | |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 31 | /* simply removes a server from a tree. |
| 32 | * |
Willy Tarreau | 85b2fb0 | 2021-02-17 16:14:00 +0100 | [diff] [blame] | 33 | * The lbprm's lock must be held. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 34 | */ |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 35 | static inline void fwlc_dequeue_srv(struct server *s) |
| 36 | { |
| 37 | eb32_delete(&s->lb_node); |
| 38 | } |
| 39 | |
Christopher Faulet | cb33d3a | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 40 | /* Queue a server in its associated tree, assuming the <eweight> is >0. |
Willy Tarreau | 1eb6c55 | 2018-12-14 08:33:28 +0100 | [diff] [blame] | 41 | * Servers are sorted by (#conns+1)/weight. To ensure maximum accuracy, |
| 42 | * we use (#conns+1)*SRV_EWGHT_MAX/eweight as the sorting key. The reason |
| 43 | * for using #conns+1 is to sort by weights in case the server is picked |
| 44 | * and not before it is picked. This provides a better load accuracy for |
| 45 | * low connection counts when weights differ and makes sure the round-robin |
Willy Tarreau | ed5ac9c | 2019-09-06 17:04:04 +0200 | [diff] [blame] | 46 | * applies between servers of highest weight first. However servers with no |
| 47 | * connection are always picked first so that under low loads, it's not |
| 48 | * always the single server with the highest weight that gets picked. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 49 | * |
Christopher Faulet | cb33d3a | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 50 | * NOTE: Depending on the calling context, we use s->next_eweight or |
| 51 | * s->cur_eweight. The next value is used when the server state is updated |
| 52 | * (because the weight changed for instance). During this step, the server |
| 53 | * state is not yet committed. The current value is used to reposition the |
| 54 | * server in the tree. This happens when the server is used. |
| 55 | * |
Willy Tarreau | 85b2fb0 | 2021-02-17 16:14:00 +0100 | [diff] [blame] | 56 | * The lbprm's lock must be held. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 57 | */ |
Christopher Faulet | cb33d3a | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 58 | static inline void fwlc_queue_srv(struct server *s, unsigned int eweight) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 59 | { |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame^] | 60 | unsigned int inflight = _HA_ATOMIC_LOAD(&s->served) + _HA_ATOMIC_LOAD(&s->queue.length); |
Willy Tarreau | 8c855f6 | 2020-10-22 17:41:45 +0200 | [diff] [blame] | 61 | |
Christopher Faulet | cb33d3a | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 62 | s->lb_node.key = inflight ? (inflight + 1) * SRV_EWGHT_MAX / eweight : 0; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 63 | eb32_insert(s->lb_tree, &s->lb_node); |
| 64 | } |
| 65 | |
| 66 | /* Re-position the server in the FWLC tree after it has been assigned one |
| 67 | * connection or after it has released one. Note that it is possible that |
| 68 | * the server has been moved out of the tree due to failed health-checks. |
Willy Tarreau | 5941ef0 | 2021-06-18 18:29:25 +0200 | [diff] [blame] | 69 | * The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 70 | */ |
Willy Tarreau | 5941ef0 | 2021-06-18 18:29:25 +0200 | [diff] [blame] | 71 | static void fwlc_srv_reposition(struct server *s) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 72 | { |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame^] | 73 | unsigned int inflight = _HA_ATOMIC_LOAD(&s->served) + _HA_ATOMIC_LOAD(&s->queue.length); |
Willy Tarreau | 5064ab6 | 2021-02-17 16:26:55 +0100 | [diff] [blame] | 74 | unsigned int new_key = inflight ? (inflight + 1) * SRV_EWGHT_MAX / s->cur_eweight : 0; |
| 75 | |
| 76 | /* some calls will be made for no change (e.g connect_server() after |
| 77 | * assign_server(). Let's check that first. |
| 78 | */ |
| 79 | if (s->lb_node.node.leaf_p && s->lb_node.key == new_key) |
| 80 | return; |
| 81 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 82 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Christopher Faulet | 1ae2a88 | 2019-06-19 10:50:38 +0200 | [diff] [blame] | 83 | if (s->lb_tree) { |
Willy Tarreau | 5064ab6 | 2021-02-17 16:26:55 +0100 | [diff] [blame] | 84 | /* we might have been waiting for a while on the lock above |
| 85 | * so it's worth testing again because other threads are very |
| 86 | * likely to have released a connection or taken one leading |
| 87 | * to our target value (50% of the case in measurements). |
| 88 | */ |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame^] | 89 | inflight = _HA_ATOMIC_LOAD(&s->served) + _HA_ATOMIC_LOAD(&s->queue.length); |
Willy Tarreau | 5064ab6 | 2021-02-17 16:26:55 +0100 | [diff] [blame] | 90 | new_key = inflight ? (inflight + 1) * SRV_EWGHT_MAX / s->cur_eweight : 0; |
| 91 | if (!s->lb_node.node.leaf_p || s->lb_node.key != new_key) { |
| 92 | eb32_delete(&s->lb_node); |
| 93 | s->lb_node.key = new_key; |
| 94 | eb32_insert(s->lb_tree, &s->lb_node); |
| 95 | } |
Christopher Faulet | 1ae2a88 | 2019-06-19 10:50:38 +0200 | [diff] [blame] | 96 | } |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 97 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* This function updates the server trees according to server <srv>'s new |
| 101 | * state. It should be called when server <srv>'s status changes to down. |
| 102 | * It is not important whether the server was already down or not. It is not |
| 103 | * important either that the new state is completely down (the caller may not |
| 104 | * know all the variables of a server's state). |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 105 | * |
| 106 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 107 | */ |
| 108 | static void fwlc_set_server_status_down(struct server *srv) |
| 109 | { |
| 110 | struct proxy *p = srv->proxy; |
| 111 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 112 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 113 | return; |
| 114 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 115 | if (srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 116 | goto out_update_state; |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 117 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 118 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 119 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 120 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 121 | /* server was already down */ |
| 122 | goto out_update_backend; |
| 123 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 124 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 125 | p->lbprm.tot_wbck -= srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 126 | p->srv_bck--; |
| 127 | |
| 128 | if (srv == p->lbprm.fbck) { |
| 129 | /* we lost the first backup server in a single-backup |
| 130 | * configuration, we must search another one. |
| 131 | */ |
| 132 | struct server *srv2 = p->lbprm.fbck; |
| 133 | do { |
| 134 | srv2 = srv2->next; |
| 135 | } while (srv2 && |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 136 | !((srv2->flags & SRV_F_BACKUP) && |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 137 | srv_willbe_usable(srv2))); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 138 | p->lbprm.fbck = srv2; |
| 139 | } |
| 140 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 141 | p->lbprm.tot_wact -= srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 142 | p->srv_act--; |
| 143 | } |
| 144 | |
| 145 | fwlc_dequeue_srv(srv); |
| 146 | fwlc_remove_from_tree(srv); |
| 147 | |
| 148 | out_update_backend: |
| 149 | /* check/update tot_used, tot_weight */ |
| 150 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 151 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 152 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 153 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 154 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* This function updates the server trees according to server <srv>'s new |
| 158 | * state. It should be called when server <srv>'s status changes to up. |
| 159 | * It is not important whether the server was already down or not. It is not |
| 160 | * important either that the new state is completely UP (the caller may not |
| 161 | * know all the variables of a server's state). This function will not change |
| 162 | * the weight of a server which was already up. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 163 | * |
| 164 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 165 | */ |
| 166 | static void fwlc_set_server_status_up(struct server *srv) |
| 167 | { |
| 168 | struct proxy *p = srv->proxy; |
| 169 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 170 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 171 | return; |
| 172 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 173 | if (!srv_willbe_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 174 | goto out_update_state; |
| 175 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 176 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 177 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 178 | if (srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 179 | /* server was already up */ |
| 180 | goto out_update_backend; |
| 181 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 182 | if (srv->flags & SRV_F_BACKUP) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 183 | srv->lb_tree = &p->lbprm.fwlc.bck; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 184 | p->lbprm.tot_wbck += srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 185 | p->srv_bck++; |
| 186 | |
| 187 | if (!(p->options & PR_O_USE_ALL_BK)) { |
| 188 | if (!p->lbprm.fbck) { |
| 189 | /* there was no backup server anymore */ |
| 190 | p->lbprm.fbck = srv; |
| 191 | } else { |
| 192 | /* we may have restored a backup server prior to fbck, |
| 193 | * in which case it should replace it. |
| 194 | */ |
| 195 | struct server *srv2 = srv; |
| 196 | do { |
| 197 | srv2 = srv2->next; |
| 198 | } while (srv2 && (srv2 != p->lbprm.fbck)); |
| 199 | if (srv2) |
| 200 | p->lbprm.fbck = srv; |
| 201 | } |
| 202 | } |
| 203 | } else { |
| 204 | srv->lb_tree = &p->lbprm.fwlc.act; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 205 | p->lbprm.tot_wact += srv->next_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 206 | p->srv_act++; |
| 207 | } |
| 208 | |
| 209 | /* note that eweight cannot be 0 here */ |
Christopher Faulet | cb33d3a | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 210 | fwlc_queue_srv(srv, srv->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 211 | |
| 212 | out_update_backend: |
| 213 | /* check/update tot_used, tot_weight */ |
| 214 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 215 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 216 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 217 | out_update_state: |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 218 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* This function must be called after an update to server <srv>'s effective |
| 222 | * weight. It may be called after a state change too. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 223 | * |
| 224 | * The server's lock must be held. The lbprm's lock will be used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 225 | */ |
| 226 | static void fwlc_update_server_weight(struct server *srv) |
| 227 | { |
| 228 | int old_state, new_state; |
| 229 | struct proxy *p = srv->proxy; |
| 230 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 231 | if (!srv_lb_status_changed(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 232 | return; |
| 233 | |
| 234 | /* If changing the server's weight changes its state, we simply apply |
| 235 | * the procedures we already have for status change. If the state |
| 236 | * remains down, the server is not in any tree, so it's as easy as |
| 237 | * updating its values. If the state remains up with different weights, |
| 238 | * there are some computations to perform to find a new place and |
| 239 | * possibly a new tree for this server. |
| 240 | */ |
| 241 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 242 | old_state = srv_currently_usable(srv); |
| 243 | new_state = srv_willbe_usable(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 244 | |
| 245 | if (!old_state && !new_state) { |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 246 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | else if (!old_state && new_state) { |
| 250 | fwlc_set_server_status_up(srv); |
| 251 | return; |
| 252 | } |
| 253 | else if (old_state && !new_state) { |
| 254 | fwlc_set_server_status_down(srv); |
| 255 | return; |
| 256 | } |
| 257 | |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 258 | HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 259 | |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 260 | if (srv->lb_tree) |
| 261 | fwlc_dequeue_srv(srv); |
| 262 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 263 | if (srv->flags & SRV_F_BACKUP) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 264 | p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 265 | srv->lb_tree = &p->lbprm.fwlc.bck; |
| 266 | } else { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 267 | p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight; |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 268 | srv->lb_tree = &p->lbprm.fwlc.act; |
| 269 | } |
| 270 | |
Christopher Faulet | cb33d3a | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 271 | fwlc_queue_srv(srv, srv->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 272 | |
| 273 | update_backend_weight(p); |
Willy Tarreau | cd10def | 2020-10-17 18:48:47 +0200 | [diff] [blame] | 274 | HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 275 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 276 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* This function is responsible for building the trees in case of fast |
| 280 | * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to |
| 281 | * uweight ratio. Both active and backup groups are initialized. |
| 282 | */ |
| 283 | void fwlc_init_server_tree(struct proxy *p) |
| 284 | { |
| 285 | struct server *srv; |
| 286 | struct eb_root init_head = EB_ROOT; |
| 287 | |
| 288 | p->lbprm.set_server_status_up = fwlc_set_server_status_up; |
| 289 | p->lbprm.set_server_status_down = fwlc_set_server_status_down; |
| 290 | p->lbprm.update_server_eweight = fwlc_update_server_weight; |
| 291 | p->lbprm.server_take_conn = fwlc_srv_reposition; |
| 292 | p->lbprm.server_drop_conn = fwlc_srv_reposition; |
| 293 | |
| 294 | p->lbprm.wdiv = BE_WEIGHT_SCALE; |
| 295 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 296 | 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] | 297 | srv_lb_commit_status(srv); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | recount_servers(p); |
| 301 | update_backend_weight(p); |
| 302 | |
| 303 | p->lbprm.fwlc.act = init_head; |
| 304 | p->lbprm.fwlc.bck = init_head; |
| 305 | |
| 306 | /* queue active and backup servers in two distinct groups */ |
| 307 | for (srv = p->srv; srv; srv = srv->next) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 308 | if (!srv_currently_usable(srv)) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 309 | continue; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 310 | srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwlc.bck : &p->lbprm.fwlc.act; |
Christopher Faulet | cb33d3a | 2020-12-11 15:36:01 +0100 | [diff] [blame] | 311 | fwlc_queue_srv(srv, srv->next_eweight); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | /* Return next server from the FWLC tree in backend <p>. If the tree is empty, |
| 316 | * return NULL. Saturated servers are skipped. |
Willy Tarreau | 1b87748 | 2018-08-21 19:44:53 +0200 | [diff] [blame] | 317 | * |
Willy Tarreau | 63b3ae7 | 2021-06-01 16:58:31 +0200 | [diff] [blame] | 318 | * The lbprm's lock will be used in R/O mode. The server's lock is not used. |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 319 | */ |
| 320 | struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid) |
| 321 | { |
| 322 | struct server *srv, *avoided; |
| 323 | struct eb32_node *node; |
| 324 | |
| 325 | srv = avoided = NULL; |
| 326 | |
Willy Tarreau | 58bc9c1 | 2020-10-17 19:32:09 +0200 | [diff] [blame] | 327 | HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 328 | if (p->srv_act) |
| 329 | node = eb32_first(&p->lbprm.fwlc.act); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 330 | else if (p->lbprm.fbck) { |
| 331 | srv = p->lbprm.fbck; |
| 332 | goto out; |
| 333 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 334 | else if (p->srv_bck) |
| 335 | node = eb32_first(&p->lbprm.fwlc.bck); |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 336 | else { |
| 337 | srv = NULL; |
| 338 | goto out; |
| 339 | } |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 340 | |
| 341 | while (node) { |
| 342 | /* OK, we have a server. However, it may be saturated, in which |
| 343 | * case we don't want to reconsider it for now, so we'll simply |
| 344 | * skip it. Same if it's the server we try to avoid, in which |
| 345 | * case we simply remember it for later use if needed. |
| 346 | */ |
| 347 | struct server *s; |
| 348 | |
| 349 | s = eb32_entry(node, struct server, lb_node); |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame^] | 350 | if (!s->maxconn || s->served + s->queue.length < srv_dynamic_maxconn(s) + s->maxqueue) { |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 351 | if (s != srvtoavoid) { |
| 352 | srv = s; |
| 353 | break; |
| 354 | } |
| 355 | avoided = s; |
| 356 | } |
| 357 | node = eb32_next(node); |
| 358 | } |
| 359 | |
| 360 | if (!srv) |
| 361 | srv = avoided; |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 362 | out: |
Willy Tarreau | 58bc9c1 | 2020-10-17 19:32:09 +0200 | [diff] [blame] | 363 | HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &p->lbprm.lock); |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 364 | return srv; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /* |
| 369 | * Local variables: |
| 370 | * c-indent-level: 8 |
| 371 | * c-basic-offset: 8 |
| 372 | * End: |
| 373 | */ |