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