blob: 0fc18d92d3a9f9e5fc11d9f64e816abc8a67a657 [file] [log] [blame]
Willy Tarreau6b2e11b2009-10-01 07:52:15 +02001/*
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 *
Willy Tarreau4c14eaa2010-11-24 14:01:45 +010010 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020011 *
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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020019#include <haproxy/api.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020020#include <haproxy/tools.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020021#include <import/eb32tree.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020022
23#include <types/global.h>
24#include <types/server.h>
25
26#include <proto/backend.h>
27#include <proto/queue.h>
28
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020029/* Return next tree node after <node> which must still be in the tree, or be
30 * NULL. Lookup wraps around the end to the beginning. If the next node is the
31 * same node, return NULL. This is designed to find a valid next node before
32 * deleting one from the tree.
33 */
34static inline struct eb32_node *chash_skip_node(struct eb_root *root, struct eb32_node *node)
35{
36 struct eb32_node *stop = node;
37
38 if (!node)
39 return NULL;
40 node = eb32_next(node);
41 if (!node)
42 node = eb32_first(root);
43 if (node == stop)
44 return NULL;
45 return node;
46}
47
48/* Remove all of a server's entries from its tree. This may be used when
49 * setting a server down.
50 */
51static inline void chash_dequeue_srv(struct server *s)
52{
53 while (s->lb_nodes_now > 0) {
54 if (s->lb_nodes_now >= s->lb_nodes_tot) // should always be false anyway
55 s->lb_nodes_now = s->lb_nodes_tot;
56 s->lb_nodes_now--;
57 if (s->proxy->lbprm.chash.last == &s->lb_nodes[s->lb_nodes_now].node)
58 s->proxy->lbprm.chash.last = chash_skip_node(s->lb_tree, s->proxy->lbprm.chash.last);
59 eb32_delete(&s->lb_nodes[s->lb_nodes_now].node);
60 }
61}
62
63/* Adjust the number of entries of a server in its tree. The server must appear
64 * as many times as its weight indicates it. If it's there too often, we remove
65 * the last occurrences. If it's not there enough, we add more occurrences. To
66 * remove a server from the tree, normally call this with eweight=0.
Willy Tarreau1b877482018-08-21 19:44:53 +020067 *
68 * The server's lock and the lbprm's lock must be held.
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020069 */
70static inline void chash_queue_dequeue_srv(struct server *s)
71{
Emeric Brun52a91d32017-08-31 14:41:55 +020072 while (s->lb_nodes_now > s->next_eweight) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020073 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
Olivier Houchardf8eb8d52017-10-17 15:52:59 +020081 /* 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->next_eweight) {
Christopher Faulet0a52c172019-08-01 10:09:29 +020085 struct tree_occ *new_nodes;
Olivier Houchardf8eb8d52017-10-17 15:52:59 +020086
Christopher Faulet0a52c172019-08-01 10:09:29 +020087 /* 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->next_eweight * sizeof(*new_nodes));
Olivier Houchardf8eb8d52017-10-17 15:52:59 +020092 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->next_eweight - s->lb_nodes_tot) * sizeof(*s->lb_nodes));
98 for (j = s->lb_nodes_tot; j < s->next_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->next_eweight;
103 }
104 }
Emeric Brun52a91d32017-08-31 14:41:55 +0200105 while (s->lb_nodes_now < s->next_eweight) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200106 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).
Willy Tarreau1b877482018-08-21 19:44:53 +0200120 *
121 * The server's lock must be held. The lbprm lock will be used.
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200122 */
123static void chash_set_server_status_down(struct server *srv)
124{
125 struct proxy *p = srv->proxy;
126
Willy Tarreauc5150da2014-05-13 19:27:31 +0200127 if (!srv_lb_status_changed(srv))
Christopher Faulet5b517552017-06-09 14:17:53 +0200128 return;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200129
Willy Tarreau1b877482018-08-21 19:44:53 +0200130 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
131
Emeric Brun52a91d32017-08-31 14:41:55 +0200132 if (srv_willbe_usable(srv))
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200133 goto out_update_state;
134
Emeric Brun52a91d32017-08-31 14:41:55 +0200135 if (!srv_currently_usable(srv))
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200136 /* server was already down */
137 goto out_update_backend;
138
Willy Tarreauc93cd162014-05-13 15:54:22 +0200139 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200140 p->lbprm.tot_wbck -= srv->cur_eweight;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200141 p->srv_bck--;
142
143 if (srv == p->lbprm.fbck) {
144 /* we lost the first backup server in a single-backup
145 * configuration, we must search another one.
146 */
147 struct server *srv2 = p->lbprm.fbck;
148 do {
149 srv2 = srv2->next;
150 } while (srv2 &&
Willy Tarreauc93cd162014-05-13 15:54:22 +0200151 !((srv2->flags & SRV_F_BACKUP) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200152 srv_willbe_usable(srv2)));
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200153 p->lbprm.fbck = srv2;
154 }
155 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200156 p->lbprm.tot_wact -= srv->cur_eweight;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200157 p->srv_act--;
158 }
159
160 chash_dequeue_srv(srv);
161
162out_update_backend:
163 /* check/update tot_used, tot_weight */
164 update_backend_weight(p);
165 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200166 srv_lb_commit_status(srv);
Willy Tarreau1b877482018-08-21 19:44:53 +0200167
168 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200169}
170
171/* This function updates the server trees according to server <srv>'s new
172 * state. It should be called when server <srv>'s status changes to up.
173 * It is not important whether the server was already down or not. It is not
174 * important either that the new state is completely UP (the caller may not
175 * know all the variables of a server's state). This function will not change
176 * the weight of a server which was already up.
Willy Tarreau1b877482018-08-21 19:44:53 +0200177 *
178 * The server's lock must be held. The lbprm lock will be used.
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200179 */
180static void chash_set_server_status_up(struct server *srv)
181{
182 struct proxy *p = srv->proxy;
183
Willy Tarreauc5150da2014-05-13 19:27:31 +0200184 if (!srv_lb_status_changed(srv))
Christopher Faulet5b517552017-06-09 14:17:53 +0200185 return;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200186
Willy Tarreau1b877482018-08-21 19:44:53 +0200187 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
188
Emeric Brun52a91d32017-08-31 14:41:55 +0200189 if (!srv_willbe_usable(srv))
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200190 goto out_update_state;
191
Emeric Brun52a91d32017-08-31 14:41:55 +0200192 if (srv_currently_usable(srv))
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200193 /* server was already up */
194 goto out_update_backend;
195
Willy Tarreauc93cd162014-05-13 15:54:22 +0200196 if (srv->flags & SRV_F_BACKUP) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200197 p->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200198 p->srv_bck++;
199
200 if (!(p->options & PR_O_USE_ALL_BK)) {
201 if (!p->lbprm.fbck) {
202 /* there was no backup server anymore */
203 p->lbprm.fbck = srv;
204 } else {
205 /* we may have restored a backup server prior to fbck,
206 * in which case it should replace it.
207 */
208 struct server *srv2 = srv;
209 do {
210 srv2 = srv2->next;
211 } while (srv2 && (srv2 != p->lbprm.fbck));
212 if (srv2)
213 p->lbprm.fbck = srv;
214 }
215 }
216 } else {
Emeric Brun52a91d32017-08-31 14:41:55 +0200217 p->lbprm.tot_wact += srv->next_eweight;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200218 p->srv_act++;
219 }
220
221 /* note that eweight cannot be 0 here */
222 chash_queue_dequeue_srv(srv);
223
224 out_update_backend:
225 /* check/update tot_used, tot_weight */
226 update_backend_weight(p);
227 out_update_state:
Willy Tarreauc5150da2014-05-13 19:27:31 +0200228 srv_lb_commit_status(srv);
Willy Tarreau1b877482018-08-21 19:44:53 +0200229
230 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200231}
232
233/* This function must be called after an update to server <srv>'s effective
234 * weight. It may be called after a state change too.
Willy Tarreau1b877482018-08-21 19:44:53 +0200235 *
236 * The server's lock must be held. The lbprm lock may be used.
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200237 */
238static void chash_update_server_weight(struct server *srv)
239{
240 int old_state, new_state;
241 struct proxy *p = srv->proxy;
242
Willy Tarreauc5150da2014-05-13 19:27:31 +0200243 if (!srv_lb_status_changed(srv))
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200244 return;
245
246 /* If changing the server's weight changes its state, we simply apply
247 * the procedures we already have for status change. If the state
248 * remains down, the server is not in any tree, so it's as easy as
249 * updating its values. If the state remains up with different weights,
250 * there are some computations to perform to find a new place and
251 * possibly a new tree for this server.
252 */
253
Emeric Brun52a91d32017-08-31 14:41:55 +0200254 old_state = srv_currently_usable(srv);
255 new_state = srv_willbe_usable(srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200256
257 if (!old_state && !new_state) {
Willy Tarreauc5150da2014-05-13 19:27:31 +0200258 srv_lb_commit_status(srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200259 return;
260 }
261 else if (!old_state && new_state) {
262 chash_set_server_status_up(srv);
263 return;
264 }
265 else if (old_state && !new_state) {
266 chash_set_server_status_down(srv);
267 return;
268 }
269
Willy Tarreau1b877482018-08-21 19:44:53 +0200270 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
271
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200272 /* only adjust the server's presence in the tree */
273 chash_queue_dequeue_srv(srv);
274
Willy Tarreauc93cd162014-05-13 15:54:22 +0200275 if (srv->flags & SRV_F_BACKUP)
Emeric Brun52a91d32017-08-31 14:41:55 +0200276 p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200277 else
Emeric Brun52a91d32017-08-31 14:41:55 +0200278 p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200279
280 update_backend_weight(p);
Willy Tarreauc5150da2014-05-13 19:27:31 +0200281 srv_lb_commit_status(srv);
Willy Tarreau1b877482018-08-21 19:44:53 +0200282
283 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200284}
285
286/*
Andrew Rodland4f88c632016-10-25 12:50:37 -0400287 * This function implements the "Consistent Hashing with Bounded Loads" algorithm
288 * of Mirrokni, Thorup, and Zadimoghaddam (arxiv:1608.01350), adapted for use with
289 * unequal server weights.
290 */
291int chash_server_is_eligible(struct server *s)
292{
293 /* The total number of slots to allocate is the total number of outstanding requests
294 * (including the one we're about to make) times the load-balance-factor, rounded up.
295 */
Willy Tarreau76e84f52019-01-14 16:50:58 +0100296 unsigned tot_slots = ((s->proxy->served + 1) * s->proxy->lbprm.hash_balance_factor + 99) / 100;
Andrew Rodland4f88c632016-10-25 12:50:37 -0400297 unsigned slots_per_weight = tot_slots / s->proxy->lbprm.tot_weight;
298 unsigned remainder = tot_slots % s->proxy->lbprm.tot_weight;
299
300 /* Allocate a whole number of slots per weight unit... */
Emeric Brun52a91d32017-08-31 14:41:55 +0200301 unsigned slots = s->cur_eweight * slots_per_weight;
Andrew Rodland4f88c632016-10-25 12:50:37 -0400302
303 /* And then distribute the rest among servers proportionally to their weight. */
Emeric Brun52a91d32017-08-31 14:41:55 +0200304 slots += ((s->cumulative_weight + s->cur_eweight) * remainder) / s->proxy->lbprm.tot_weight
Andrew Rodland4f88c632016-10-25 12:50:37 -0400305 - (s->cumulative_weight * remainder) / s->proxy->lbprm.tot_weight;
306
307 /* But never leave a server with 0. */
308 if (slots == 0)
309 slots = 1;
310
311 return s->served < slots;
312}
313
314/*
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200315 * This function returns the running server from the CHASH tree, which is at
316 * the closest distance from the value of <hash>. Doing so ensures that even
317 * with a well imbalanced hash, if some servers are close to each other, they
318 * will still both receive traffic. If any server is found, it will be returned.
Willy Tarreau59884a62019-01-02 14:48:31 +0100319 * It will also skip server <avoid> if the hash result ends on this one.
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200320 * If no valid server is found, NULL is returned.
321 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100322struct server *chash_get_server_hash(struct proxy *p, unsigned int hash, const struct server *avoid)
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200323{
324 struct eb32_node *next, *prev;
325 struct server *nsrv, *psrv;
326 struct eb_root *root;
327 unsigned int dn, dp;
Andrew Rodland4f88c632016-10-25 12:50:37 -0400328 int loop;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200329
Willy Tarreau1b877482018-08-21 19:44:53 +0200330 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
331
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200332 if (p->srv_act)
333 root = &p->lbprm.chash.act;
Willy Tarreau1b877482018-08-21 19:44:53 +0200334 else if (p->lbprm.fbck) {
335 nsrv = p->lbprm.fbck;
336 goto out;
337 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200338 else if (p->srv_bck)
339 root = &p->lbprm.chash.bck;
Willy Tarreau1b877482018-08-21 19:44:53 +0200340 else {
341 nsrv = NULL;
342 goto out;
343 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200344
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200345 /* find the node after and the node before */
346 next = eb32_lookup_ge(root, hash);
347 if (!next)
348 next = eb32_first(root);
Willy Tarreau1b877482018-08-21 19:44:53 +0200349 if (!next) {
350 nsrv = NULL; /* tree is empty */
351 goto out;
352 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200353
354 prev = eb32_prev(next);
355 if (!prev)
356 prev = eb32_last(root);
357
358 nsrv = eb32_entry(next, struct tree_occ, node)->server;
359 psrv = eb32_entry(prev, struct tree_occ, node)->server;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200360
Andrew Rodland18330ab2017-04-26 02:57:03 -0400361 /* OK we're located between two servers, let's
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200362 * compare distances between hash and the two servers
363 * and select the closest server.
364 */
365 dp = hash - prev->key;
366 dn = next->key - hash;
367
Andrew Rodland4f88c632016-10-25 12:50:37 -0400368 if (dp <= dn) {
369 next = prev;
370 nsrv = psrv;
371 }
372
373 loop = 0;
Willy Tarreau76e84f52019-01-14 16:50:58 +0100374 while (nsrv == avoid || (p->lbprm.hash_balance_factor && !chash_server_is_eligible(nsrv))) {
Andrew Rodland4f88c632016-10-25 12:50:37 -0400375 next = eb32_next(next);
376 if (!next) {
377 next = eb32_first(root);
378 if (++loop > 1) // protection against accidental loop
379 break;
380 }
381 nsrv = eb32_entry(next, struct tree_occ, node)->server;
382 }
383
Willy Tarreau1b877482018-08-21 19:44:53 +0200384 out:
385 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Andrew Rodland4f88c632016-10-25 12:50:37 -0400386 return nsrv;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200387}
388
389/* Return next server from the CHASH tree in backend <p>. If the tree is empty,
390 * return NULL. Saturated servers are skipped.
391 */
392struct server *chash_get_next_server(struct proxy *p, struct server *srvtoavoid)
393{
394 struct server *srv, *avoided;
395 struct eb32_node *node, *stop, *avoided_node;
396 struct eb_root *root;
397
398 srv = avoided = NULL;
399 avoided_node = NULL;
400
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100401 HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200402 if (p->srv_act)
403 root = &p->lbprm.chash.act;
Christopher Faulet5b517552017-06-09 14:17:53 +0200404 else if (p->lbprm.fbck) {
405 srv = p->lbprm.fbck;
406 goto out;
407 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200408 else if (p->srv_bck)
409 root = &p->lbprm.chash.bck;
Christopher Faulet5b517552017-06-09 14:17:53 +0200410 else {
411 srv = NULL;
412 goto out;
413 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200414
415 stop = node = p->lbprm.chash.last;
416 do {
417 struct server *s;
418
419 if (node)
420 node = eb32_next(node);
421 if (!node)
422 node = eb32_first(root);
423
424 p->lbprm.chash.last = node;
Willy Tarreau1ed90ac2017-11-05 10:54:50 +0100425 if (!node) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200426 /* no node is available */
Willy Tarreau1ed90ac2017-11-05 10:54:50 +0100427 srv = NULL;
428 goto out;
429 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200430
Willy Tarreaud16a1b22013-04-12 14:46:51 +0200431 /* Note: if we came here after a down/up cycle with no last
432 * pointer, and after a redispatch (srvtoavoid is set), we
433 * must set stop to non-null otherwise we can loop forever.
434 */
435 if (!stop)
436 stop = node;
437
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200438 /* OK, we have a server. However, it may be saturated, in which
439 * case we don't want to reconsider it for now, so we'll simply
440 * skip it. Same if it's the server we try to avoid, in which
441 * case we simply remember it for later use if needed.
442 */
443 s = eb32_entry(node, struct tree_occ, node)->server;
444 if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
445 if (s != srvtoavoid) {
446 srv = s;
447 break;
448 }
449 avoided = s;
450 avoided_node = node;
451 }
452 } while (node != stop);
453
454 if (!srv) {
455 srv = avoided;
456 p->lbprm.chash.last = avoided_node;
457 }
458
Christopher Faulet5b517552017-06-09 14:17:53 +0200459 out:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100460 HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200461 return srv;
462}
463
464/* This function is responsible for building the active and backup trees for
465 * constistent hashing. The servers receive an array of initialized nodes
466 * with their assigned keys. It also sets p->lbprm.wdiv to the eweight to
467 * uweight ratio.
468 */
469void chash_init_server_tree(struct proxy *p)
470{
471 struct server *srv;
472 struct eb_root init_head = EB_ROOT;
473 int node;
474
475 p->lbprm.set_server_status_up = chash_set_server_status_up;
476 p->lbprm.set_server_status_down = chash_set_server_status_down;
477 p->lbprm.update_server_eweight = chash_update_server_weight;
478 p->lbprm.server_take_conn = NULL;
479 p->lbprm.server_drop_conn = NULL;
480
481 p->lbprm.wdiv = BE_WEIGHT_SCALE;
482 for (srv = p->srv; srv; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200483 srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200484 srv_lb_commit_status(srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200485 }
486
487 recount_servers(p);
488 update_backend_weight(p);
489
490 p->lbprm.chash.act = init_head;
491 p->lbprm.chash.bck = init_head;
492 p->lbprm.chash.last = NULL;
493
494 /* queue active and backup servers in two distinct groups */
495 for (srv = p->srv; srv; srv = srv->next) {
Willy Tarreauc93cd162014-05-13 15:54:22 +0200496 srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.chash.bck : &p->lbprm.chash.act;
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200497 srv->lb_nodes_tot = srv->uweight * BE_WEIGHT_SCALE;
498 srv->lb_nodes_now = 0;
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200499 srv->lb_nodes = calloc(srv->lb_nodes_tot, sizeof(struct tree_occ));
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200500 for (node = 0; node < srv->lb_nodes_tot; node++) {
501 srv->lb_nodes[node].server = srv;
Willy Tarreau4c14eaa2010-11-24 14:01:45 +0100502 srv->lb_nodes[node].node.key = full_hash(srv->puid * SRV_EWGHT_RANGE + node);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200503 }
504
Emeric Brun52a91d32017-08-31 14:41:55 +0200505 if (srv_currently_usable(srv))
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200506 chash_queue_dequeue_srv(srv);
507 }
508}