Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_upstream_hash_module.c @ 6173:1729d8d3eb3a
Mail: listen backlog=.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Mon, 08 Jun 2015 23:13:56 +0300 |
parents | bf8b6534db3a |
children | 68c106e6fa0a |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Roman Arutyunyan | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
13 typedef struct { | |
14 uint32_t hash; | |
15 ngx_str_t *server; | |
16 } ngx_stream_upstream_chash_point_t; | |
17 | |
18 | |
19 typedef struct { | |
20 ngx_uint_t number; | |
21 ngx_stream_upstream_chash_point_t point[1]; | |
22 } ngx_stream_upstream_chash_points_t; | |
23 | |
24 | |
25 typedef struct { | |
26 ngx_stream_upstream_chash_points_t *points; | |
27 } ngx_stream_upstream_hash_srv_conf_t; | |
28 | |
29 | |
30 typedef struct { | |
31 /* the round robin data must be first */ | |
32 ngx_stream_upstream_rr_peer_data_t rrp; | |
33 ngx_stream_upstream_hash_srv_conf_t *conf; | |
34 ngx_str_t key; | |
35 ngx_uint_t tries; | |
36 ngx_uint_t rehash; | |
37 uint32_t hash; | |
38 ngx_event_get_peer_pt get_rr_peer; | |
39 } ngx_stream_upstream_hash_peer_data_t; | |
40 | |
41 | |
42 static ngx_int_t ngx_stream_upstream_init_hash(ngx_conf_t *cf, | |
43 ngx_stream_upstream_srv_conf_t *us); | |
44 static ngx_int_t ngx_stream_upstream_init_hash_peer(ngx_stream_session_t *s, | |
45 ngx_stream_upstream_srv_conf_t *us); | |
46 static ngx_int_t ngx_stream_upstream_get_hash_peer(ngx_peer_connection_t *pc, | |
47 void *data); | |
48 | |
49 static ngx_int_t ngx_stream_upstream_init_chash(ngx_conf_t *cf, | |
50 ngx_stream_upstream_srv_conf_t *us); | |
51 static int ngx_libc_cdecl | |
52 ngx_stream_upstream_chash_cmp_points(const void *one, const void *two); | |
53 static ngx_uint_t ngx_stream_upstream_find_chash_point( | |
54 ngx_stream_upstream_chash_points_t *points, uint32_t hash); | |
55 static ngx_int_t ngx_stream_upstream_init_chash_peer(ngx_stream_session_t *s, | |
56 ngx_stream_upstream_srv_conf_t *us); | |
57 static ngx_int_t ngx_stream_upstream_get_chash_peer(ngx_peer_connection_t *pc, | |
58 void *data); | |
59 | |
60 static void *ngx_stream_upstream_hash_create_conf(ngx_conf_t *cf); | |
61 static char *ngx_stream_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd, | |
62 void *conf); | |
63 | |
64 | |
65 static ngx_command_t ngx_stream_upstream_hash_commands[] = { | |
66 | |
67 { ngx_string("hash"), | |
68 NGX_STREAM_UPS_CONF|NGX_CONF_TAKE12, | |
69 ngx_stream_upstream_hash, | |
70 NGX_STREAM_SRV_CONF_OFFSET, | |
71 0, | |
72 NULL }, | |
73 | |
74 ngx_null_command | |
75 }; | |
76 | |
77 | |
78 static ngx_stream_module_t ngx_stream_upstream_hash_module_ctx = { | |
79 NULL, /* create main configuration */ | |
80 NULL, /* init main configuration */ | |
81 | |
82 ngx_stream_upstream_hash_create_conf, /* create server configuration */ | |
83 NULL, /* merge server configuration */ | |
84 }; | |
85 | |
86 | |
87 ngx_module_t ngx_stream_upstream_hash_module = { | |
88 NGX_MODULE_V1, | |
89 &ngx_stream_upstream_hash_module_ctx, /* module context */ | |
90 ngx_stream_upstream_hash_commands, /* module directives */ | |
91 NGX_STREAM_MODULE, /* module type */ | |
92 NULL, /* init master */ | |
93 NULL, /* init module */ | |
94 NULL, /* init process */ | |
95 NULL, /* init thread */ | |
96 NULL, /* exit thread */ | |
97 NULL, /* exit process */ | |
98 NULL, /* exit master */ | |
99 NGX_MODULE_V1_PADDING | |
100 }; | |
101 | |
102 | |
103 static ngx_int_t | |
104 ngx_stream_upstream_init_hash(ngx_conf_t *cf, | |
105 ngx_stream_upstream_srv_conf_t *us) | |
106 { | |
107 if (ngx_stream_upstream_init_round_robin(cf, us) != NGX_OK) { | |
108 return NGX_ERROR; | |
109 } | |
110 | |
111 us->peer.init = ngx_stream_upstream_init_hash_peer; | |
112 | |
113 return NGX_OK; | |
114 } | |
115 | |
116 | |
117 static ngx_int_t | |
118 ngx_stream_upstream_init_hash_peer(ngx_stream_session_t *s, | |
119 ngx_stream_upstream_srv_conf_t *us) | |
120 { | |
121 ngx_stream_upstream_hash_srv_conf_t *hcf; | |
122 ngx_stream_upstream_hash_peer_data_t *hp; | |
123 | |
124 hp = ngx_palloc(s->connection->pool, | |
125 sizeof(ngx_stream_upstream_hash_peer_data_t)); | |
126 if (hp == NULL) { | |
127 return NGX_ERROR; | |
128 } | |
129 | |
130 s->upstream->peer.data = &hp->rrp; | |
131 | |
132 if (ngx_stream_upstream_init_round_robin_peer(s, us) != NGX_OK) { | |
133 return NGX_ERROR; | |
134 } | |
135 | |
136 s->upstream->peer.get = ngx_stream_upstream_get_hash_peer; | |
137 | |
138 hcf = ngx_stream_conf_upstream_srv_conf(us, | |
139 ngx_stream_upstream_hash_module); | |
140 | |
141 hp->key = s->connection->addr_text; | |
142 | |
143 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
144 "upstream hash key:\"%V\"", &hp->key); | |
145 | |
146 hp->conf = hcf; | |
147 hp->tries = 0; | |
148 hp->rehash = 0; | |
149 hp->hash = 0; | |
150 hp->get_rr_peer = ngx_stream_upstream_get_round_robin_peer; | |
151 | |
152 return NGX_OK; | |
153 } | |
154 | |
155 | |
156 static ngx_int_t | |
157 ngx_stream_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data) | |
158 { | |
159 ngx_stream_upstream_hash_peer_data_t *hp = data; | |
160 | |
161 time_t now; | |
162 u_char buf[NGX_INT_T_LEN]; | |
163 size_t size; | |
164 uint32_t hash; | |
165 ngx_int_t w; | |
166 uintptr_t m; | |
6121
b6047abf5f30
Upstream: simplified ip_hash and hash peer selection code.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
167 ngx_uint_t n, p; |
6115 | 168 ngx_stream_upstream_rr_peer_t *peer; |
169 | |
170 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
171 "get hash peer, try: %ui", pc->tries); | |
172 | |
173 ngx_stream_upstream_rr_peers_wlock(hp->rrp.peers); | |
174 | |
175 if (hp->tries > 20 || hp->rrp.peers->single) { | |
176 ngx_stream_upstream_rr_peers_unlock(hp->rrp.peers); | |
177 return hp->get_rr_peer(pc, &hp->rrp); | |
178 } | |
179 | |
180 now = ngx_time(); | |
181 | |
182 pc->connection = NULL; | |
183 | |
184 for ( ;; ) { | |
185 | |
186 /* | |
187 * Hash expression is compatible with Cache::Memcached: | |
188 * ((crc32([REHASH] KEY) >> 16) & 0x7fff) + PREV_HASH | |
189 * with REHASH omitted at the first iteration. | |
190 */ | |
191 | |
192 ngx_crc32_init(hash); | |
193 | |
194 if (hp->rehash > 0) { | |
195 size = ngx_sprintf(buf, "%ui", hp->rehash) - buf; | |
196 ngx_crc32_update(&hash, buf, size); | |
197 } | |
198 | |
199 ngx_crc32_update(&hash, hp->key.data, hp->key.len); | |
200 ngx_crc32_final(hash); | |
201 | |
202 hash = (hash >> 16) & 0x7fff; | |
203 | |
204 hp->hash += hash; | |
205 hp->rehash++; | |
206 | |
6121
b6047abf5f30
Upstream: simplified ip_hash and hash peer selection code.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
207 w = hp->hash % hp->rrp.peers->total_weight; |
b6047abf5f30
Upstream: simplified ip_hash and hash peer selection code.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
208 peer = hp->rrp.peers->peer; |
b6047abf5f30
Upstream: simplified ip_hash and hash peer selection code.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
209 p = 0; |
6115 | 210 |
6121
b6047abf5f30
Upstream: simplified ip_hash and hash peer selection code.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
211 while (w >= peer->weight) { |
b6047abf5f30
Upstream: simplified ip_hash and hash peer selection code.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
212 w -= peer->weight; |
b6047abf5f30
Upstream: simplified ip_hash and hash peer selection code.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
213 peer = peer->next; |
b6047abf5f30
Upstream: simplified ip_hash and hash peer selection code.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
214 p++; |
6115 | 215 } |
216 | |
217 n = p / (8 * sizeof(uintptr_t)); | |
218 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t)); | |
219 | |
220 if (hp->rrp.tried[n] & m) { | |
221 goto next; | |
222 } | |
223 | |
224 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
225 "get hash peer, value:%uD, peer:%ui", hp->hash, p); | |
226 | |
227 if (peer->down) { | |
228 goto next; | |
229 } | |
230 | |
231 if (peer->max_fails | |
232 && peer->fails >= peer->max_fails | |
233 && now - peer->checked <= peer->fail_timeout) | |
234 { | |
235 goto next; | |
236 } | |
237 | |
238 break; | |
239 | |
240 next: | |
241 | |
242 if (++hp->tries > 20) { | |
243 ngx_stream_upstream_rr_peers_unlock(hp->rrp.peers); | |
244 return hp->get_rr_peer(pc, &hp->rrp); | |
245 } | |
246 } | |
247 | |
248 hp->rrp.current = peer; | |
249 | |
250 pc->sockaddr = peer->sockaddr; | |
251 pc->socklen = peer->socklen; | |
252 pc->name = &peer->name; | |
253 | |
254 peer->conns++; | |
255 | |
256 if (now - peer->checked > peer->fail_timeout) { | |
257 peer->checked = now; | |
258 } | |
259 | |
260 ngx_stream_upstream_rr_peers_unlock(hp->rrp.peers); | |
261 | |
262 hp->rrp.tried[n] |= m; | |
263 | |
264 return NGX_OK; | |
265 } | |
266 | |
267 | |
268 static ngx_int_t | |
269 ngx_stream_upstream_init_chash(ngx_conf_t *cf, | |
270 ngx_stream_upstream_srv_conf_t *us) | |
271 { | |
272 u_char *host, *port, c; | |
273 size_t host_len, port_len, size; | |
6148
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
274 uint32_t hash, base_hash; |
6115 | 275 ngx_str_t *server; |
276 ngx_uint_t npoints, i, j; | |
277 ngx_stream_upstream_rr_peer_t *peer; | |
278 ngx_stream_upstream_rr_peers_t *peers; | |
279 ngx_stream_upstream_chash_points_t *points; | |
280 ngx_stream_upstream_hash_srv_conf_t *hcf; | |
6148
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
281 union { |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
282 uint32_t value; |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
283 u_char byte[4]; |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
284 } prev_hash; |
6115 | 285 |
286 if (ngx_stream_upstream_init_round_robin(cf, us) != NGX_OK) { | |
287 return NGX_ERROR; | |
288 } | |
289 | |
290 us->peer.init = ngx_stream_upstream_init_chash_peer; | |
291 | |
292 peers = us->peer.data; | |
293 npoints = peers->total_weight * 160; | |
294 | |
295 size = sizeof(ngx_stream_upstream_chash_points_t) | |
296 + sizeof(ngx_stream_upstream_chash_point_t) * (npoints - 1); | |
297 | |
298 points = ngx_palloc(cf->pool, size); | |
299 if (points == NULL) { | |
300 return NGX_ERROR; | |
301 } | |
302 | |
303 points->number = 0; | |
304 | |
305 for (peer = peers->peer; peer; peer = peer->next) { | |
306 server = &peer->server; | |
307 | |
308 /* | |
309 * Hash expression is compatible with Cache::Memcached::Fast: | |
310 * crc32(HOST \0 PORT PREV_HASH). | |
311 */ | |
312 | |
313 if (server->len >= 5 | |
314 && ngx_strncasecmp(server->data, (u_char *) "unix:", 5) == 0) | |
315 { | |
316 host = server->data + 5; | |
317 host_len = server->len - 5; | |
318 port = NULL; | |
319 port_len = 0; | |
320 goto done; | |
321 } | |
322 | |
323 for (j = 0; j < server->len; j++) { | |
324 c = server->data[server->len - j - 1]; | |
325 | |
326 if (c == ':') { | |
327 host = server->data; | |
328 host_len = server->len - j - 1; | |
329 port = server->data + server->len - j; | |
330 port_len = j; | |
331 goto done; | |
332 } | |
333 | |
334 if (c < '0' || c > '9') { | |
335 break; | |
336 } | |
337 } | |
338 | |
339 host = server->data; | |
340 host_len = server->len; | |
341 port = NULL; | |
342 port_len = 0; | |
343 | |
344 done: | |
345 | |
346 ngx_crc32_init(base_hash); | |
347 ngx_crc32_update(&base_hash, host, host_len); | |
348 ngx_crc32_update(&base_hash, (u_char *) "", 1); | |
349 ngx_crc32_update(&base_hash, port, port_len); | |
350 | |
6148
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
351 prev_hash.value = 0; |
6115 | 352 npoints = peer->weight * 160; |
353 | |
354 for (j = 0; j < npoints; j++) { | |
355 hash = base_hash; | |
356 | |
6148
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
357 ngx_crc32_update(&hash, prev_hash.byte, 4); |
6115 | 358 ngx_crc32_final(hash); |
359 | |
360 points->point[points->number].hash = hash; | |
361 points->point[points->number].server = server; | |
362 points->number++; | |
363 | |
6148
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
364 #if (NGX_HAVE_LITTLE_ENDIAN) |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
365 prev_hash.value = hash; |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
366 #else |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
367 prev_hash.byte[0] = (u_char) (hash & 0xff); |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
368 prev_hash.byte[1] = (u_char) ((hash >> 8) & 0xff); |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
369 prev_hash.byte[2] = (u_char) ((hash >> 16) & 0xff); |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
370 prev_hash.byte[3] = (u_char) ((hash >> 24) & 0xff); |
bf8b6534db3a
Upstream hash: consistency across little/big endianness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
6121
diff
changeset
|
371 #endif |
6115 | 372 } |
373 } | |
374 | |
375 ngx_qsort(points->point, | |
376 points->number, | |
377 sizeof(ngx_stream_upstream_chash_point_t), | |
378 ngx_stream_upstream_chash_cmp_points); | |
379 | |
380 for (i = 0, j = 1; j < points->number; j++) { | |
381 if (points->point[i].hash != points->point[j].hash) { | |
382 points->point[++i] = points->point[j]; | |
383 } | |
384 } | |
385 | |
386 points->number = i + 1; | |
387 | |
388 hcf = ngx_stream_conf_upstream_srv_conf(us, | |
389 ngx_stream_upstream_hash_module); | |
390 hcf->points = points; | |
391 | |
392 return NGX_OK; | |
393 } | |
394 | |
395 | |
396 static int ngx_libc_cdecl | |
397 ngx_stream_upstream_chash_cmp_points(const void *one, const void *two) | |
398 { | |
399 ngx_stream_upstream_chash_point_t *first = | |
400 (ngx_stream_upstream_chash_point_t *) one; | |
401 ngx_stream_upstream_chash_point_t *second = | |
402 (ngx_stream_upstream_chash_point_t *) two; | |
403 | |
404 if (first->hash < second->hash) { | |
405 return -1; | |
406 | |
407 } else if (first->hash > second->hash) { | |
408 return 1; | |
409 | |
410 } else { | |
411 return 0; | |
412 } | |
413 } | |
414 | |
415 | |
416 static ngx_uint_t | |
417 ngx_stream_upstream_find_chash_point(ngx_stream_upstream_chash_points_t *points, | |
418 uint32_t hash) | |
419 { | |
420 ngx_uint_t i, j, k; | |
421 ngx_stream_upstream_chash_point_t *point; | |
422 | |
423 /* find first point >= hash */ | |
424 | |
425 point = &points->point[0]; | |
426 | |
427 i = 0; | |
428 j = points->number; | |
429 | |
430 while (i < j) { | |
431 k = (i + j) / 2; | |
432 | |
433 if (hash > point[k].hash) { | |
434 i = k + 1; | |
435 | |
436 } else if (hash < point[k].hash) { | |
437 j = k; | |
438 | |
439 } else { | |
440 return k; | |
441 } | |
442 } | |
443 | |
444 return i; | |
445 } | |
446 | |
447 | |
448 static ngx_int_t | |
449 ngx_stream_upstream_init_chash_peer(ngx_stream_session_t *s, | |
450 ngx_stream_upstream_srv_conf_t *us) | |
451 { | |
452 uint32_t hash; | |
453 ngx_stream_upstream_hash_srv_conf_t *hcf; | |
454 ngx_stream_upstream_hash_peer_data_t *hp; | |
455 | |
456 if (ngx_stream_upstream_init_hash_peer(s, us) != NGX_OK) { | |
457 return NGX_ERROR; | |
458 } | |
459 | |
460 s->upstream->peer.get = ngx_stream_upstream_get_chash_peer; | |
461 | |
462 hp = s->upstream->peer.data; | |
463 hcf = ngx_stream_conf_upstream_srv_conf(us, | |
464 ngx_stream_upstream_hash_module); | |
465 | |
466 hash = ngx_crc32_long(hp->key.data, hp->key.len); | |
467 | |
468 ngx_stream_upstream_rr_peers_rlock(hp->rrp.peers); | |
469 | |
470 hp->hash = ngx_stream_upstream_find_chash_point(hcf->points, hash); | |
471 | |
472 ngx_stream_upstream_rr_peers_unlock(hp->rrp.peers); | |
473 | |
474 return NGX_OK; | |
475 } | |
476 | |
477 | |
478 static ngx_int_t | |
479 ngx_stream_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data) | |
480 { | |
481 ngx_stream_upstream_hash_peer_data_t *hp = data; | |
482 | |
483 time_t now; | |
484 intptr_t m; | |
485 ngx_str_t *server; | |
486 ngx_int_t total; | |
487 ngx_uint_t i, n, best_i; | |
488 ngx_stream_upstream_rr_peer_t *peer, *best; | |
489 ngx_stream_upstream_chash_point_t *point; | |
490 ngx_stream_upstream_chash_points_t *points; | |
491 ngx_stream_upstream_hash_srv_conf_t *hcf; | |
492 | |
493 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
494 "get consistent hash peer, try: %ui", pc->tries); | |
495 | |
496 ngx_stream_upstream_rr_peers_wlock(hp->rrp.peers); | |
497 | |
498 pc->connection = NULL; | |
499 | |
500 now = ngx_time(); | |
501 hcf = hp->conf; | |
502 | |
503 points = hcf->points; | |
504 point = &points->point[0]; | |
505 | |
506 for ( ;; ) { | |
507 server = point[hp->hash % points->number].server; | |
508 | |
509 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
510 "consistent hash peer:%uD, server:\"%V\"", | |
511 hp->hash, server); | |
512 | |
513 best = NULL; | |
514 best_i = 0; | |
515 total = 0; | |
516 | |
517 for (peer = hp->rrp.peers->peer, i = 0; | |
518 peer; | |
519 peer = peer->next, i++) | |
520 { | |
521 | |
522 n = i / (8 * sizeof(uintptr_t)); | |
523 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); | |
524 | |
525 if (hp->rrp.tried[n] & m) { | |
526 continue; | |
527 } | |
528 | |
529 if (peer->down) { | |
530 continue; | |
531 } | |
532 | |
533 if (peer->server.len != server->len | |
534 || ngx_strncmp(peer->server.data, server->data, server->len) | |
535 != 0) | |
536 { | |
537 continue; | |
538 } | |
539 | |
540 if (peer->max_fails | |
541 && peer->fails >= peer->max_fails | |
542 && now - peer->checked <= peer->fail_timeout) | |
543 { | |
544 continue; | |
545 } | |
546 | |
547 peer->current_weight += peer->effective_weight; | |
548 total += peer->effective_weight; | |
549 | |
550 if (peer->effective_weight < peer->weight) { | |
551 peer->effective_weight++; | |
552 } | |
553 | |
554 if (best == NULL || peer->current_weight > best->current_weight) { | |
555 best = peer; | |
556 best_i = i; | |
557 } | |
558 } | |
559 | |
560 if (best) { | |
561 best->current_weight -= total; | |
562 break; | |
563 } | |
564 | |
565 hp->hash++; | |
566 hp->tries++; | |
567 | |
568 if (hp->tries >= points->number) { | |
569 ngx_stream_upstream_rr_peers_unlock(hp->rrp.peers); | |
570 return NGX_BUSY; | |
571 } | |
572 } | |
573 | |
574 hp->rrp.current = best; | |
575 | |
576 pc->sockaddr = best->sockaddr; | |
577 pc->socklen = best->socklen; | |
578 pc->name = &best->name; | |
579 | |
580 best->conns++; | |
581 | |
582 if (now - best->checked > best->fail_timeout) { | |
583 best->checked = now; | |
584 } | |
585 | |
586 ngx_stream_upstream_rr_peers_unlock(hp->rrp.peers); | |
587 | |
588 n = best_i / (8 * sizeof(uintptr_t)); | |
589 m = (uintptr_t) 1 << best_i % (8 * sizeof(uintptr_t)); | |
590 | |
591 hp->rrp.tried[n] |= m; | |
592 | |
593 return NGX_OK; | |
594 } | |
595 | |
596 | |
597 static void * | |
598 ngx_stream_upstream_hash_create_conf(ngx_conf_t *cf) | |
599 { | |
600 ngx_stream_upstream_hash_srv_conf_t *conf; | |
601 | |
602 conf = ngx_palloc(cf->pool, sizeof(ngx_stream_upstream_hash_srv_conf_t)); | |
603 if (conf == NULL) { | |
604 return NULL; | |
605 } | |
606 | |
607 conf->points = NULL; | |
608 | |
609 return conf; | |
610 } | |
611 | |
612 | |
613 static char * | |
614 ngx_stream_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
615 { | |
616 ngx_str_t *value; | |
617 ngx_stream_upstream_srv_conf_t *uscf; | |
618 | |
619 value = cf->args->elts; | |
620 | |
621 if (ngx_strcmp(value[1].data, "$remote_addr")) { | |
622 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
623 "unsupported hash key \"%V\", use $remote_addr", | |
624 &value[1]); | |
625 return NGX_CONF_ERROR; | |
626 } | |
627 | |
628 uscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_upstream_module); | |
629 | |
630 if (uscf->peer.init_upstream) { | |
631 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
632 "load balancing method redefined"); | |
633 } | |
634 | |
635 uscf->flags = NGX_STREAM_UPSTREAM_CREATE | |
636 |NGX_STREAM_UPSTREAM_WEIGHT | |
637 |NGX_STREAM_UPSTREAM_MAX_FAILS | |
638 |NGX_STREAM_UPSTREAM_FAIL_TIMEOUT | |
639 |NGX_STREAM_UPSTREAM_DOWN; | |
640 | |
641 if (cf->args->nelts == 2) { | |
642 uscf->peer.init_upstream = ngx_stream_upstream_init_hash; | |
643 | |
644 } else if (ngx_strcmp(value[2].data, "consistent") == 0) { | |
645 uscf->peer.init_upstream = ngx_stream_upstream_init_chash; | |
646 | |
647 } else { | |
648 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
649 "invalid parameter \"%V\"", &value[2]); | |
650 return NGX_CONF_ERROR; | |
651 } | |
652 | |
653 return NGX_CONF_OK; | |
654 } |