1 /*
2 * Consistent Hash implementation
3 * Please consult this very well detailed article for more information :
4 * http://www.spiteful.com/2008/03/17/programmers-toolbox-part-3-consistent-hashing/
5 *
6 * Our implementation has to support both weighted hashing and weighted round
7 * robin because we'll use it to replace the previous map-based implementation
8 * which offered both algorithms.
9 *
10 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 */
18
19 #include <common/compat.h>
20 #include <common/config.h>
21 #include <common/debug.h>
22 #include <common/standard.h>
23 #include <eb32tree.h>
24
25 #include <types/global.h>
26 #include <types/server.h>
27
28 #include <proto/backend.h>
29 #include <proto/queue.h>
30
31 /* Return next tree node after <node> which must still be in the tree, or be
32 * NULL. Lookup wraps around the end to the beginning. If the next node is the
33 * same node, return NULL. This is designed to find a valid next node before
34 * deleting one from the tree.
35 */
chash_skip_node(struct eb_root * root,struct eb32_node * node)36 static inline struct eb32_node *chash_skip_node(struct eb_root *root, struct eb32_node *node)
37 {
38 struct eb32_node *stop = node;
39
40 if (!node)
41 return NULL;
42 node = eb32_next(node);
43 if (!node)
44 node = eb32_first(root);
45 if (node == stop)
46 return NULL;
47 return node;
48 }
49
50 /* Remove all of a server's entries from its tree. This may be used when
51 * setting a server down.
52 */
chash_dequeue_srv(struct server * s)53 static inline void chash_dequeue_srv(struct server *s)
54 {
55 while (s->lb_nodes_now > 0) {
56 if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway
57 s->lb_nodes_now = s->lb_nodes_tot;
58 s->lb_nodes_now--;
59 if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node)
60 s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last);
61 eb32_delete(&s->lb_nodes[s->lb_nodes_now].node);
62 }
63 }
64
65 /* Adjust the number of entries of a server in its tree. The server must appear
66 * as many times as its weight indicates it. If it's there too often, we remove
67 * the last occurrences. If it's not there enough, we add more occurrences. To
68 * remove a server from the tree, normally call this with eweight=0.
69 */
chash_queue_dequeue_srv(struct server * s)70 static inline void chash_queue_dequeue_srv(struct server *s)
71 {
72 while (s->lb_nodes_now > s->eweight) {
73 if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway
74 s->lb_nodes_now = s->lb_nodes_tot;
75 s->lb_nodes_now--;
76 if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node)
77 s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last);
78 eb32_delete(&s->lb_nodes[s->lb_nodes_now].node);
79 }
80
81 /* Attempt to increase the total number of nodes, if the user
82 * increased the weight beyond the original weight
83 */
84 if (s->lb_nodes_tot < s->eweight) {
85 struct tree_occ *new_nodes;
86
87 /* First we need to remove all server's entries from its tree
88 * because the realloc will change all nodes pointers */
89 chash_dequeue_srv(s);
90
91 new_nodes = realloc(s->lb_nodes, s->eweight * sizeof(*new_nodes));
92 if (new_nodes) {
93 unsigned int j;
94
95 s->lb_nodes = new_nodes;
96 memset(&s->lb_nodes[s->lb_nodes_tot], 0,
97 (s->eweight - s->lb_nodes_tot) * sizeof(*s->lb_nodes));
98 for (j = s->lb_nodes_tot; j < s->eweight; j++) {
99 s->lb_nodes[j].server = s;
100 s->lb_nodes[j].node.key = full_hash(s->puid * SRV_EWGHT_RANGE + j);
101 }
102 s->lb_nodes_tot = s->eweight;
103 }
104 }
105 while (s->lb_nodes_now < s->eweight) {
106 if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway
107 break;
108 if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node)
109 s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last);
110 eb32_insert(s->lb_tree, &s->lb_nodes[s->lb_nodes_now].node);
111 s->lb_nodes_now++;
112 }
113 }
114
115 /* This function updates the server trees according to server <srv>'s new
116 * state. It should be called when server <srv>'s status changes to down.
117 * It is not important whether the server was already down or not. It is not
118 * important either that the new state is completely down (the caller may not
119 * know all the variables of a server's state).
120 */
chash_set_server_status_down(struct server * srv)121 static void chash_set_server_status_down(struct server *srv)
122 {
123 struct proxy *p = srv->proxy;
124
125 if (!srv_lb_status_changed(srv))
126 return;
127
128 if (srv_is_usable(srv))
129 goto out_update_state;
130
131 if (!srv_was_usable(srv))
132 /* server was already down */
133 goto out_update_backend;
134
135 if (srv->flags & SRV_F_BACKUP) {
136 p->lbprm.tot_wbck -= srv->prev_eweight;
137 p->srv_bck--;
138
139 if (srv == p->lbprm.fbck) {
140 /* we lost the first backup server in a single-backup
141 * configuration, we must search another one.
142 */
143 struct server *srv2 = p->lbprm.fbck;
144 do {
145 srv2 = srv2->next;
146 } while (srv2 &&
147 !((srv2->flags & SRV_F_BACKUP) &&
148 srv_is_usable(srv2)));
149 p->lbprm.fbck = srv2;
150 }
151 } else {
152 p->lbprm.tot_wact -= srv->prev_eweight;
153 p->srv_act--;
154 }
155
156 chash_dequeue_srv(srv);
157
158 out_update_backend:
159 /* check/update tot_used, tot_weight */
160 update_backend_weight(p);
161 out_update_state:
162 srv_lb_commit_status(srv);
163 }
164
165 /* This function updates the server trees according to server <srv>'s new
166 * state. It should be called when server <srv>'s status changes to up.
167 * It is not important whether the server was already down or not. It is not
168 * important either that the new state is completely UP (the caller may not
169 * know all the variables of a server's state). This function will not change
170 * the weight of a server which was already up.
171 */
chash_set_server_status_up(struct server * srv)172 static void chash_set_server_status_up(struct server *srv)
173 {
174 struct proxy *p = srv->proxy;
175
176 if (!srv_lb_status_changed(srv))
177 return;
178
179 if (!srv_is_usable(srv))
180 goto out_update_state;
181
182 if (srv_was_usable(srv))
183 /* server was already up */
184 goto out_update_backend;
185
186 if (srv->flags & SRV_F_BACKUP) {
187 p->lbprm.tot_wbck += srv->eweight;
188 p->srv_bck++;
189
190 if (!(p->options & PR_O_USE_ALL_BK)) {
191 if (!p->lbprm.fbck) {
192 /* there was no backup server anymore */
193 p->lbprm.fbck = srv;
194 } else {
195 /* we may have restored a backup server prior to fbck,
196 * in which case it should replace it.
197 */
198 struct server *srv2 = srv;
199 do {
200 srv2 = srv2->next;
201 } while (srv2 && (srv2 != p->lbprm.fbck));
202 if (srv2)
203 p->lbprm.fbck = srv;
204 }
205 }
206 } else {
207 p->lbprm.tot_wact += srv->eweight;
208 p->srv_act++;
209 }
210
211 /* note that eweight cannot be 0 here */
212 chash_queue_dequeue_srv(srv);
213
214 out_update_backend:
215 /* check/update tot_used, tot_weight */
216 update_backend_weight(p);
217 out_update_state:
218 srv_lb_commit_status(srv);
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.
223 */
chash_update_server_weight(struct server * srv)224 static void chash_update_server_weight(struct server *srv)
225 {
226 int old_state, new_state;
227 struct proxy *p = srv->proxy;
228
229 if (!srv_lb_status_changed(srv))
230 return;
231
232 /* If changing the server's weight changes its state, we simply apply
233 * the procedures we already have for status change. If the state
234 * remains down, the server is not in any tree, so it's as easy as
235 * updating its values. If the state remains up with different weights,
236 * there are some computations to perform to find a new place and
237 * possibly a new tree for this server.
238 */
239
240 old_state = srv_was_usable(srv);
241 new_state = srv_is_usable(srv);
242
243 if (!old_state && !new_state) {
244 srv_lb_commit_status(srv);
245 return;
246 }
247 else if (!old_state && new_state) {
248 chash_set_server_status_up(srv);
249 return;
250 }
251 else if (old_state && !new_state) {
252 chash_set_server_status_down(srv);
253 return;
254 }
255
256 /* only adjust the server's presence in the tree */
257 chash_queue_dequeue_srv(srv);
258
259 if (srv->flags & SRV_F_BACKUP)
260 p->lbprm.tot_wbck += srv->eweight - srv->prev_eweight;
261 else
262 p->lbprm.tot_wact += srv->eweight - srv->prev_eweight;
263
264 update_backend_weight(p);
265 srv_lb_commit_status(srv);
266 }
267
268 /*
269 * This function implements the "Consistent Hashing with Bounded Loads" algorithm
270 * of Mirrokni, Thorup, and Zadimoghaddam (arxiv:1608.01350), adapted for use with
271 * unequal server weights.
272 */
chash_server_is_eligible(struct server * s)273 int chash_server_is_eligible(struct server *s)
274 {
275 /* The total number of slots to allocate is the total number of outstanding requests
276 * (including the one we're about to make) times the load-balance-factor, rounded up.
277 */
278 unsigned tot_slots = ((s->proxy->served + 1) * s->proxy->lbprm.chash.balance_factor + 99) / 100;
279 unsigned slots_per_weight = tot_slots / s->proxy->lbprm.tot_weight;
280 unsigned remainder = tot_slots % s->proxy->lbprm.tot_weight;
281
282 /* Allocate a whole number of slots per weight unit... */
283 unsigned slots = s->eweight * slots_per_weight;
284
285 /* And then distribute the rest among servers proportionally to their weight. */
286 slots += ((s->cumulative_weight + s->eweight) * remainder) / s->proxy->lbprm.tot_weight
287 - (s->cumulative_weight * remainder) / s->proxy->lbprm.tot_weight;
288
289 /* But never leave a server with 0. */
290 if (slots == 0)
291 slots = 1;
292
293 return s->served < slots;
294 }
295
296 /*
297 * This function returns the running server from the CHASH tree, which is at
298 * the closest distance from the value of <hash>. Doing so ensures that even
299 * with a well imbalanced hash, if some servers are close to each other, they
300 * will still both receive traffic. If any server is found, it will be returned.
301 * If no valid server is found, NULL is returned.
302 */
chash_get_server_hash(struct proxy * p,unsigned int hash)303 struct server *chash_get_server_hash(struct proxy *p, unsigned int hash)
304 {
305 struct eb32_node *next, *prev;
306 struct server *nsrv, *psrv;
307 struct eb_root *root;
308 unsigned int dn, dp;
309 int loop;
310
311 if (p->srv_act)
312 root = &p->lbprm.chash.act;
313 else if (p->lbprm.fbck)
314 return p->lbprm.fbck;
315 else if (p->srv_bck)
316 root = &p->lbprm.chash.bck;
317 else
318 return NULL;
319
320 /* find the node after and the node before */
321 next = eb32_lookup_ge(root, hash);
322 if (!next)
323 next = eb32_first(root);
324 if (!next)
325 return NULL; /* tree is empty */
326
327 prev = eb32_prev(next);
328 if (!prev)
329 prev = eb32_last(root);
330
331 nsrv = eb32_entry(next, struct tree_occ, node)->server;
332 psrv = eb32_entry(prev, struct tree_occ, node)->server;
333
334 /* OK we're located between two servers, let's
335 * compare distances between hash and the two servers
336 * and select the closest server.
337 */
338 dp = hash - prev->key;
339 dn = next->key - hash;
340
341 if (dp <= dn) {
342 next = prev;
343 nsrv = psrv;
344 }
345
346 loop = 0;
347 while (p->lbprm.chash.balance_factor && !chash_server_is_eligible(nsrv)) {
348 next = eb32_next(next);
349 if (!next) {
350 next = eb32_first(root);
351 if (++loop > 1) // protection against accidental loop
352 break;
353 }
354 nsrv = eb32_entry(next, struct tree_occ, node)->server;
355 }
356
357 return nsrv;
358 }
359
360 /* Return next server from the CHASH tree in backend <p>. If the tree is empty,
361 * return NULL. Saturated servers are skipped.
362 */
chash_get_next_server(struct proxy * p,struct server * srvtoavoid)363 struct server *chash_get_next_server(struct proxy *p, struct server *srvtoavoid)
364 {
365 struct server *srv, *avoided;
366 struct eb32_node *node, *stop, *avoided_node;
367 struct eb_root *root;
368
369 srv = avoided = NULL;
370 avoided_node = NULL;
371
372 if (p->srv_act)
373 root = &p->lbprm.chash.act;
374 else if (p->lbprm.fbck)
375 return p->lbprm.fbck;
376 else if (p->srv_bck)
377 root = &p->lbprm.chash.bck;
378 else
379 return NULL;
380
381 stop = node = p->lbprm.chash.last;
382 do {
383 struct server *s;
384
385 if (node)
386 node = eb32_next(node);
387 if (!node)
388 node = eb32_first(root);
389
390 p->lbprm.chash.last = node;
391 if (!node)
392 /* no node is available */
393 return NULL;
394
395 /* Note: if we came here after a down/up cycle with no last
396 * pointer, and after a redispatch (srvtoavoid is set), we
397 * must set stop to non-null otherwise we can loop forever.
398 */
399 if (!stop)
400 stop = node;
401
402 /* OK, we have a server. However, it may be saturated, in which
403 * case we don't want to reconsider it for now, so we'll simply
404 * skip it. Same if it's the server we try to avoid, in which
405 * case we simply remember it for later use if needed.
406 */
407 s = eb32_entry(node, struct tree_occ, node)->server;
408 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
409 if (s != srvtoavoid) {
410 srv = s;
411 break;
412 }
413 avoided = s;
414 avoided_node = node;
415 }
416 } while (node != stop);
417
418 if (!srv) {
419 srv = avoided;
420 p->lbprm.chash.last = avoided_node;
421 }
422
423 return srv;
424 }
425
426 /* This function is responsible for building the active and backup trees for
427 * constistent hashing. The servers receive an array of initialized nodes
428 * with their assigned keys. It also sets p->lbprm.wdiv to the eweight to
429 * uweight ratio.
430 */
chash_init_server_tree(struct proxy * p)431 void chash_init_server_tree(struct proxy *p)
432 {
433 struct server *srv;
434 struct eb_root init_head = EB_ROOT;
435 int node;
436
437 p->lbprm.set_server_status_up = chash_set_server_status_up;
438 p->lbprm.set_server_status_down = chash_set_server_status_down;
439 p->lbprm.update_server_eweight = chash_update_server_weight;
440 p->lbprm.server_take_conn = NULL;
441 p->lbprm.server_drop_conn = NULL;
442
443 p->lbprm.wdiv = BE_WEIGHT_SCALE;
444 for (srv = p->srv; srv; srv = srv->next) {
445 srv->eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
446 srv_lb_commit_status(srv);
447 }
448
449 recount_servers(p);
450 update_backend_weight(p);
451
452 p->lbprm.chash.act = init_head;
453 p->lbprm.chash.bck = init_head;
454 p->lbprm.chash.last = NULL;
455
456 /* queue active and backup servers in two distinct groups */
457 for (srv = p->srv; srv; srv = srv->next) {
458 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.chash.bck : &p->lbprm.chash.act;
459 srv->lb_nodes_tot = srv->uweight * BE_WEIGHT_SCALE;
460 srv->lb_nodes_now = 0;
461 srv->lb_nodes = calloc(srv->lb_nodes_tot, sizeof(struct tree_occ));
462 for (node = 0; node < srv->lb_nodes_tot; node++) {
463 srv->lb_nodes[node].server = srv;
464 srv->lb_nodes[node].node.key = full_hash(srv->puid * SRV_EWGHT_RANGE + node);
465 }
466
467 if (srv_is_usable(srv))
468 chash_queue_dequeue_srv(srv);
469 }
470 }
471