Mercurial > hg > nginx
annotate src/http/ngx_http_upstream_round_robin.c @ 2473:afba93b8bf06 stable-0.6
r2414 merge:
fix segfault
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 26 Jan 2009 15:17:50 +0000 |
parents | 50be8dd53d5e |
children |
rev | line source |
---|---|
884 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
1956
cb8c0c8e0c27
use ngx_int_t in ngx_sort() callback
Igor Sysoev <igor@sysoev.ru>
parents:
1887
diff
changeset
|
12 static ngx_int_t ngx_http_upstream_cmp_servers(const void *one, |
cb8c0c8e0c27
use ngx_int_t in ngx_sort() callback
Igor Sysoev <igor@sysoev.ru>
parents:
1887
diff
changeset
|
13 const void *two); |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
14 static ngx_uint_t |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
15 ngx_http_upstream_get_peer(ngx_http_upstream_rr_peers_t *peers); |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
16 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
17 |
884 | 18 ngx_int_t |
19 ngx_http_upstream_init_round_robin(ngx_conf_t *cf, | |
20 ngx_http_upstream_srv_conf_t *us) | |
21 { | |
22 ngx_url_t u; | |
23 ngx_uint_t i, j, n; | |
24 ngx_http_upstream_server_t *server; | |
1378 | 25 ngx_http_upstream_rr_peers_t *peers, *backup; |
884 | 26 |
27 us->peer.init = ngx_http_upstream_init_round_robin_peer; | |
28 | |
29 if (us->servers) { | |
30 server = us->servers->elts; | |
31 | |
1378 | 32 n = 0; |
33 | |
884 | 34 for (i = 0; i < us->servers->nelts; i++) { |
1378 | 35 if (server[i].backup) { |
36 continue; | |
37 } | |
38 | |
884 | 39 n += server[i].naddrs; |
40 } | |
41 | |
42 peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) | |
43 + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1)); | |
44 if (peers == NULL) { | |
45 return NGX_ERROR; | |
46 } | |
47 | |
1378 | 48 peers->single = (n == 1); |
884 | 49 peers->number = n; |
50 peers->name = &us->host; | |
51 | |
52 n = 0; | |
53 | |
54 for (i = 0; i < us->servers->nelts; i++) { | |
55 for (j = 0; j < server[i].naddrs; j++) { | |
1378 | 56 if (server[i].backup) { |
57 continue; | |
58 } | |
59 | |
884 | 60 peers->peer[n].sockaddr = server[i].addrs[j].sockaddr; |
61 peers->peer[n].socklen = server[i].addrs[j].socklen; | |
62 peers->peer[n].name = server[i].addrs[j].name; | |
63 peers->peer[n].max_fails = server[i].max_fails; | |
64 peers->peer[n].fail_timeout = server[i].fail_timeout; | |
65 peers->peer[n].down = server[i].down; | |
1376
6ff8c0c1e194
"down" server caused endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1344
diff
changeset
|
66 peers->peer[n].weight = server[i].down ? 0 : server[i].weight; |
6ff8c0c1e194
"down" server caused endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1344
diff
changeset
|
67 peers->peer[n].current_weight = peers->peer[n].weight; |
884 | 68 n++; |
69 } | |
70 } | |
71 | |
72 us->peer.data = peers; | |
73 | |
1379 | 74 ngx_sort(&peers->peer[0], (size_t) n, |
75 sizeof(ngx_http_upstream_rr_peer_t), | |
76 ngx_http_upstream_cmp_servers); | |
77 | |
1378 | 78 /* backup servers */ |
79 | |
80 n = 0; | |
81 | |
82 for (i = 0; i < us->servers->nelts; i++) { | |
83 if (!server[i].backup) { | |
84 continue; | |
85 } | |
86 | |
87 n += server[i].naddrs; | |
88 } | |
89 | |
90 if (n == 0) { | |
91 return NGX_OK; | |
92 } | |
93 | |
94 backup = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) | |
95 + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1)); | |
96 if (backup == NULL) { | |
97 return NGX_ERROR; | |
98 } | |
99 | |
100 peers->single = 0; | |
101 backup->single = 0; | |
102 backup->number = n; | |
103 backup->name = &us->host; | |
104 | |
105 n = 0; | |
106 | |
107 for (i = 0; i < us->servers->nelts; i++) { | |
108 for (j = 0; j < server[i].naddrs; j++) { | |
109 if (!server[i].backup) { | |
110 continue; | |
111 } | |
112 | |
113 backup->peer[n].sockaddr = server[i].addrs[j].sockaddr; | |
114 backup->peer[n].socklen = server[i].addrs[j].socklen; | |
115 backup->peer[n].name = server[i].addrs[j].name; | |
116 backup->peer[n].weight = server[i].weight; | |
117 backup->peer[n].current_weight = server[i].weight; | |
118 backup->peer[n].max_fails = server[i].max_fails; | |
119 backup->peer[n].fail_timeout = server[i].fail_timeout; | |
120 backup->peer[n].down = server[i].down; | |
121 n++; | |
122 } | |
123 } | |
124 | |
125 peers->next = backup; | |
126 | |
1379 | 127 ngx_sort(&backup->peer[0], (size_t) n, |
128 sizeof(ngx_http_upstream_rr_peer_t), | |
129 ngx_http_upstream_cmp_servers); | |
130 | |
884 | 131 return NGX_OK; |
132 } | |
133 | |
134 | |
135 /* an upstream implicitly defined by proxy_pass, etc. */ | |
136 | |
906 | 137 if (us->port == 0 && us->default_port == 0) { |
138 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
139 "no port in upstream \"%V\" in %s:%ui", | |
1489
56f1ea5baabb
u_char* is enough to keep file name
Igor Sysoev <igor@sysoev.ru>
parents:
1465
diff
changeset
|
140 &us->host, us->file_name, us->line); |
906 | 141 return NGX_ERROR; |
142 } | |
143 | |
884 | 144 ngx_memzero(&u, sizeof(ngx_url_t)); |
145 | |
146 u.host = us->host; | |
916 | 147 u.port = (in_port_t) (us->port ? us->port : us->default_port); |
884 | 148 |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1556
diff
changeset
|
149 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) { |
884 | 150 if (u.err) { |
151 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
896
f247db60fc85
fix fastcgi and memcached upstreams
Igor Sysoev <igor@sysoev.ru>
parents:
884
diff
changeset
|
152 "%s in upstream \"%V\" in %s:%ui", |
1489
56f1ea5baabb
u_char* is enough to keep file name
Igor Sysoev <igor@sysoev.ru>
parents:
1465
diff
changeset
|
153 u.err, &us->host, us->file_name, us->line); |
884 | 154 } |
155 | |
156 return NGX_ERROR; | |
157 } | |
158 | |
159 n = u.naddrs; | |
160 | |
161 peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) | |
162 + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1)); | |
163 if (peers == NULL) { | |
164 return NGX_ERROR; | |
165 } | |
166 | |
1378 | 167 peers->single = (n == 1); |
884 | 168 peers->number = n; |
169 peers->name = &us->host; | |
170 | |
171 for (i = 0; i < u.naddrs; i++) { | |
1564 | 172 peers->peer[i].sockaddr = u.addrs[i].sockaddr; |
173 peers->peer[i].socklen = u.addrs[i].socklen; | |
174 peers->peer[i].name = u.addrs[i].name; | |
175 peers->peer[i].weight = 1; | |
176 peers->peer[i].current_weight = 1; | |
177 peers->peer[i].max_fails = 1; | |
178 peers->peer[i].fail_timeout = 10; | |
884 | 179 } |
180 | |
181 us->peer.data = peers; | |
182 | |
1378 | 183 /* implicitly defined upstream has no backup servers */ |
184 | |
884 | 185 return NGX_OK; |
186 } | |
187 | |
188 | |
1956
cb8c0c8e0c27
use ngx_int_t in ngx_sort() callback
Igor Sysoev <igor@sysoev.ru>
parents:
1887
diff
changeset
|
189 static ngx_int_t |
1379 | 190 ngx_http_upstream_cmp_servers(const void *one, const void *two) |
191 { | |
192 ngx_http_upstream_rr_peer_t *first, *second; | |
193 | |
194 first = (ngx_http_upstream_rr_peer_t *) one; | |
195 second = (ngx_http_upstream_rr_peer_t *) two; | |
196 | |
197 return (first->weight < second->weight); | |
198 } | |
199 | |
200 | |
884 | 201 ngx_int_t |
202 ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r, | |
203 ngx_http_upstream_srv_conf_t *us) | |
204 { | |
205 ngx_uint_t n; | |
206 ngx_http_upstream_rr_peer_data_t *rrp; | |
207 | |
208 rrp = r->upstream->peer.data; | |
209 | |
210 if (rrp == NULL) { | |
211 rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t)); | |
212 if (rrp == NULL) { | |
213 return NGX_ERROR; | |
214 } | |
215 | |
216 r->upstream->peer.data = rrp; | |
217 } | |
218 | |
219 rrp->peers = us->peer.data; | |
220 rrp->current = 0; | |
221 | |
222 if (rrp->peers->number <= 8 * sizeof(uintptr_t)) { | |
223 rrp->tried = &rrp->data; | |
224 rrp->data = 0; | |
225 | |
226 } else { | |
227 n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) | |
228 / (8 * sizeof(uintptr_t)); | |
229 | |
230 rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t)); | |
231 if (rrp->tried == NULL) { | |
232 return NGX_ERROR; | |
233 } | |
234 } | |
235 | |
236 r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer; | |
237 r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer; | |
238 r->upstream->peer.tries = rrp->peers->number; | |
239 #if (NGX_HTTP_SSL) | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
240 r->upstream->peer.set_session = |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
241 ngx_http_upstream_set_round_robin_peer_session; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
242 r->upstream->peer.save_session = |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
243 ngx_http_upstream_save_round_robin_peer_session; |
884 | 244 #endif |
245 | |
246 return NGX_OK; | |
247 } | |
248 | |
249 | |
250 ngx_int_t | |
1658 | 251 ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r, |
252 ngx_http_upstream_resolved_t *ur) | |
253 { | |
254 u_char *p; | |
255 size_t len; | |
256 ngx_uint_t i, n; | |
257 struct sockaddr_in *sin; | |
258 ngx_http_upstream_rr_peers_t *peers; | |
259 ngx_http_upstream_rr_peer_data_t *rrp; | |
260 | |
261 rrp = r->upstream->peer.data; | |
262 | |
263 if (rrp == NULL) { | |
264 rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t)); | |
265 if (rrp == NULL) { | |
266 return NGX_ERROR; | |
267 } | |
268 | |
269 r->upstream->peer.data = rrp; | |
270 } | |
271 | |
272 peers = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peers_t) | |
273 + sizeof(ngx_http_upstream_rr_peer_t) * (ur->naddrs - 1)); | |
274 if (peers == NULL) { | |
275 return NGX_ERROR; | |
276 } | |
277 | |
278 peers->single = (ur->naddrs == 1); | |
279 peers->number = ur->naddrs; | |
280 peers->name = &ur->host; | |
281 | |
282 for (i = 0; i < ur->naddrs; i++) { | |
283 | |
284 len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1; | |
285 | |
286 p = ngx_palloc(r->pool, len); | |
287 if (p == NULL) { | |
288 return NGX_ERROR; | |
289 } | |
290 | |
291 len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, INET_ADDRSTRLEN); | |
292 len = ngx_sprintf(&p[len], ":%d", ur->port) - p; | |
293 | |
294 sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in)); | |
295 if (sin == NULL) { | |
296 return NGX_ERROR; | |
297 } | |
298 | |
299 sin->sin_family = AF_INET; | |
300 sin->sin_port = htons(ur->port); | |
301 sin->sin_addr.s_addr = ur->addrs[i]; | |
302 | |
303 peers->peer[i].sockaddr = (struct sockaddr *) sin; | |
304 peers->peer[i].socklen = sizeof(struct sockaddr_in); | |
305 peers->peer[i].name.len = len; | |
306 peers->peer[i].name.data = p; | |
307 peers->peer[i].weight = 1; | |
308 peers->peer[i].current_weight = 1; | |
309 peers->peer[i].max_fails = 1; | |
310 peers->peer[i].fail_timeout = 10; | |
311 } | |
312 | |
313 rrp->peers = peers; | |
314 rrp->current = 0; | |
315 | |
316 if (rrp->peers->number <= 8 * sizeof(uintptr_t)) { | |
317 rrp->tried = &rrp->data; | |
318 rrp->data = 0; | |
319 | |
320 } else { | |
321 n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) | |
322 / (8 * sizeof(uintptr_t)); | |
323 | |
324 rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t)); | |
325 if (rrp->tried == NULL) { | |
326 return NGX_ERROR; | |
327 } | |
328 } | |
329 | |
330 r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer; | |
331 r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer; | |
332 r->upstream->peer.tries = rrp->peers->number; | |
333 #if (NGX_HTTP_SSL) | |
334 r->upstream->peer.set_session = | |
335 ngx_http_upstream_set_round_robin_peer_session; | |
336 r->upstream->peer.save_session = | |
337 ngx_http_upstream_save_round_robin_peer_session; | |
338 #endif | |
339 | |
340 return NGX_OK; | |
341 } | |
342 | |
343 | |
344 ngx_int_t | |
884 | 345 ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data) |
346 { | |
347 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
348 | |
1378 | 349 time_t now; |
350 uintptr_t m; | |
351 ngx_int_t rc; | |
352 ngx_uint_t i, n; | |
353 ngx_connection_t *c; | |
354 ngx_http_upstream_rr_peer_t *peer; | |
355 ngx_http_upstream_rr_peers_t *peers; | |
884 | 356 |
357 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
358 "get rr peer, try: %ui", pc->tries); | |
359 | |
360 now = ngx_time(); | |
361 | |
362 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
363 | |
364 if (rrp->peers->last_cached) { | |
365 | |
366 /* cached connection */ | |
367 | |
368 c = rrp->peers->cached[rrp->peers->last_cached]; | |
369 rrp->peers->last_cached--; | |
370 | |
371 /* ngx_unlock_mutex(ppr->peers->mutex); */ | |
372 | |
373 #if (NGX_THREADS) | |
374 c->read->lock = c->read->own_lock; | |
375 c->write->lock = c->write->own_lock; | |
376 #endif | |
377 | |
378 pc->connection = c; | |
379 pc->cached = 1; | |
380 | |
381 return NGX_OK; | |
382 } | |
383 | |
384 pc->cached = 0; | |
385 pc->connection = NULL; | |
386 | |
1378 | 387 if (rrp->peers->single) { |
884 | 388 peer = &rrp->peers->peer[0]; |
389 | |
390 } else { | |
391 | |
392 /* there are several peers */ | |
393 | |
394 if (pc->tries == rrp->peers->number) { | |
395 | |
396 /* it's a first try - get a current peer */ | |
397 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
398 i = pc->tries; |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
399 |
884 | 400 for ( ;; ) { |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
401 rrp->current = ngx_http_upstream_get_peer(rrp->peers); |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
402 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
403 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
404 "get rr peer, current: %ui %i", |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
405 rrp->current, |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
406 rrp->peers->peer[rrp->current].current_weight); |
884 | 407 |
408 n = rrp->current / (8 * sizeof(uintptr_t)); | |
1206
451e34738078
fix worker endless loop on 64-bit platform if 33+ backend has fall
Igor Sysoev <igor@sysoev.ru>
parents:
916
diff
changeset
|
409 m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t)); |
884 | 410 |
411 if (!(rrp->tried[n] & m)) { | |
412 peer = &rrp->peers->peer[rrp->current]; | |
413 | |
414 if (!peer->down) { | |
415 | |
416 if (peer->max_fails == 0 | |
417 || peer->fails < peer->max_fails) | |
418 { | |
419 break; | |
420 } | |
421 | |
422 if (now - peer->accessed > peer->fail_timeout) { | |
423 peer->fails = 0; | |
424 break; | |
425 } | |
426 | |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
427 peer->current_weight = 0; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
428 |
884 | 429 } else { |
430 rrp->tried[n] |= m; | |
431 } | |
432 | |
433 pc->tries--; | |
434 } | |
435 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
436 if (pc->tries == 0) { |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
437 goto failed; |
884 | 438 } |
439 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
440 if (--i == 0) { |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
441 ngx_log_error(NGX_LOG_ALERT, pc->log, 0, |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
442 "round robin upstream stuck on %ui tries", |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
443 pc->tries); |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
444 goto failed; |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
445 } |
884 | 446 } |
447 | |
448 peer->current_weight--; | |
449 | |
450 } else { | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
451 |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
452 i = pc->tries; |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
453 |
884 | 454 for ( ;; ) { |
455 n = rrp->current / (8 * sizeof(uintptr_t)); | |
1206
451e34738078
fix worker endless loop on 64-bit platform if 33+ backend has fall
Igor Sysoev <igor@sysoev.ru>
parents:
916
diff
changeset
|
456 m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t)); |
884 | 457 |
458 if (!(rrp->tried[n] & m)) { | |
459 | |
460 peer = &rrp->peers->peer[rrp->current]; | |
461 | |
462 if (!peer->down) { | |
463 | |
464 if (peer->max_fails == 0 | |
465 || peer->fails < peer->max_fails) | |
466 { | |
467 break; | |
468 } | |
469 | |
470 if (now - peer->accessed > peer->fail_timeout) { | |
471 peer->fails = 0; | |
472 break; | |
473 } | |
474 | |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
475 peer->current_weight = 0; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
476 |
884 | 477 } else { |
478 rrp->tried[n] |= m; | |
479 } | |
480 | |
481 pc->tries--; | |
482 } | |
483 | |
484 rrp->current++; | |
485 | |
486 if (rrp->current >= rrp->peers->number) { | |
487 rrp->current = 0; | |
488 } | |
489 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
490 if (pc->tries == 0) { |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
491 goto failed; |
884 | 492 } |
493 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
494 if (--i == 0) { |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
495 ngx_log_error(NGX_LOG_ALERT, pc->log, 0, |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
496 "round robin upstream stuck on %ui tries", |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
497 pc->tries); |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
498 goto failed; |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
499 } |
884 | 500 } |
501 | |
502 peer->current_weight--; | |
503 } | |
504 | |
505 rrp->tried[n] |= m; | |
506 } | |
507 | |
508 pc->sockaddr = peer->sockaddr; | |
509 pc->socklen = peer->socklen; | |
510 pc->name = &peer->name; | |
511 | |
512 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
513 | |
1378 | 514 if (pc->tries == 1 && rrp->peers->next) { |
515 pc->tries += rrp->peers->next->number; | |
516 | |
517 n = rrp->peers->next->number / (8 * sizeof(uintptr_t)) + 1; | |
1389 | 518 for (i = 0; i < n; i++) { |
1378 | 519 rrp->tried[i] = 0; |
520 } | |
521 } | |
522 | |
884 | 523 return NGX_OK; |
524 | |
525 failed: | |
526 | |
1378 | 527 peers = rrp->peers; |
528 | |
529 if (peers->next) { | |
530 | |
531 /* ngx_unlock_mutex(peers->mutex); */ | |
532 | |
533 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, "backup servers"); | |
534 | |
535 rrp->peers = peers->next; | |
536 pc->tries = rrp->peers->number; | |
537 | |
538 n = rrp->peers->number / (8 * sizeof(uintptr_t)) + 1; | |
1389 | 539 for (i = 0; i < n; i++) { |
1378 | 540 rrp->tried[i] = 0; |
541 } | |
542 | |
543 rc = ngx_http_upstream_get_round_robin_peer(pc, rrp); | |
544 | |
545 if (rc != NGX_BUSY) { | |
546 return rc; | |
547 } | |
548 | |
549 /* ngx_lock_mutex(peers->mutex); */ | |
550 } | |
551 | |
884 | 552 /* all peers failed, mark them as live for quick recovery */ |
553 | |
1378 | 554 for (i = 0; i < peers->number; i++) { |
555 peers->peer[i].fails = 0; | |
884 | 556 } |
557 | |
1378 | 558 /* ngx_unlock_mutex(peers->mutex); */ |
884 | 559 |
1378 | 560 pc->name = peers->name; |
884 | 561 |
562 return NGX_BUSY; | |
563 } | |
564 | |
565 | |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
566 static ngx_uint_t |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
567 ngx_http_upstream_get_peer(ngx_http_upstream_rr_peers_t *peers) |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
568 { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
569 ngx_uint_t i, n; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
570 ngx_http_upstream_rr_peer_t *peer; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
571 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
572 peer = &peers->peer[0]; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
573 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
574 for ( ;; ) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
575 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
576 for (i = 0; i < peers->number; i++) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
577 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
578 if (peer[i].current_weight <= 0) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
579 continue; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
580 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
581 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
582 n = i; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
583 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
584 while (i < peers->number - 1) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
585 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
586 i++; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
587 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
588 if (peer[i].current_weight <= 0) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
589 continue; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
590 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
591 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
592 if (peer[n].current_weight * 1000 / peer[i].current_weight |
1465
a9132739bb57
balance more fair when there are several servers with equal weights,
Igor Sysoev <igor@sysoev.ru>
parents:
1463
diff
changeset
|
593 > peer[n].weight * 1000 / peer[i].weight) |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
594 { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
595 return n; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
596 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
597 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
598 n = i; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
599 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
600 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
601 if (peer[i].current_weight > 0) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
602 n = i; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
603 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
604 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
605 return n; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
606 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
607 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
608 for (i = 0; i < peers->number; i++) { |
1887
abbef7b5dab8
set absolute weight, this fixes bogus "no live upstream" case when
Igor Sysoev <igor@sysoev.ru>
parents:
1658
diff
changeset
|
609 peer[i].current_weight = peer[i].weight; |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
610 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
611 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
612 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
613 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
614 |
884 | 615 void |
616 ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data, | |
617 ngx_uint_t state) | |
618 { | |
619 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
620 | |
621 time_t now; | |
622 ngx_http_upstream_rr_peer_t *peer; | |
623 | |
624 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
625 "free rr peer %ui %ui", pc->tries, state); | |
626 | |
627 if (state == 0 && pc->tries == 0) { | |
628 return; | |
629 } | |
630 | |
631 /* TODO: NGX_PEER_KEEPALIVE */ | |
632 | |
1378 | 633 if (rrp->peers->single) { |
884 | 634 pc->tries = 0; |
635 return; | |
636 } | |
637 | |
638 if (state & NGX_PEER_FAILED) { | |
639 now = ngx_time(); | |
640 | |
641 peer = &rrp->peers->peer[rrp->current]; | |
642 | |
643 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
644 | |
645 peer->fails++; | |
646 peer->accessed = now; | |
647 | |
2325 | 648 if (peer->max_fails) { |
649 peer->current_weight -= peer->weight / peer->max_fails; | |
650 } | |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
651 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
652 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
653 "free rr peer failed: %ui %i", |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
654 rrp->current, peer->current_weight); |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
655 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
656 if (peer->current_weight < 0) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
657 peer->current_weight = 0; |
884 | 658 } |
659 | |
660 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
661 } | |
662 | |
663 rrp->current++; | |
664 | |
665 if (rrp->current >= rrp->peers->number) { | |
666 rrp->current = 0; | |
667 } | |
668 | |
669 if (pc->tries) { | |
670 pc->tries--; | |
671 } | |
672 | |
673 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
674 } | |
675 | |
676 | |
677 #if (NGX_HTTP_SSL) | |
678 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
679 ngx_int_t |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
680 ngx_http_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc, |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
681 void *data) |
884 | 682 { |
683 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
684 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
685 ngx_int_t rc; |
884 | 686 ngx_ssl_session_t *ssl_session; |
687 ngx_http_upstream_rr_peer_t *peer; | |
688 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
689 peer = &rrp->peers->peer[rrp->current]; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
690 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
691 /* TODO: threads only mutex */ |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
692 /* ngx_lock_mutex(rrp->peers->mutex); */ |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
693 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
694 ssl_session = peer->ssl_session; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
695 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
696 rc = ngx_ssl_set_session(pc->connection, ssl_session); |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
697 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
698 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1443 | 699 "set session: %p:%d", |
700 ssl_session, ssl_session ? ssl_session->references : 0); | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
701 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
702 /* ngx_unlock_mutex(rrp->peers->mutex); */ |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
703 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
704 return rc; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
705 } |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
706 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
707 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
708 void |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
709 ngx_http_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc, |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
710 void *data) |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
711 { |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
712 ngx_http_upstream_rr_peer_data_t *rrp = data; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
713 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
714 ngx_ssl_session_t *old_ssl_session, *ssl_session; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
715 ngx_http_upstream_rr_peer_t *peer; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
716 |
884 | 717 ssl_session = ngx_ssl_get_session(pc->connection); |
718 | |
719 if (ssl_session == NULL) { | |
720 return; | |
721 } | |
722 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
723 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1443 | 724 "save session: %p:%d", ssl_session, ssl_session->references); |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
725 |
884 | 726 peer = &rrp->peers->peer[rrp->current]; |
727 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
728 /* TODO: threads only mutex */ |
884 | 729 /* ngx_lock_mutex(rrp->peers->mutex); */ |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
730 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
731 old_ssl_session = peer->ssl_session; |
884 | 732 peer->ssl_session = ssl_session; |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
733 |
884 | 734 /* ngx_unlock_mutex(rrp->peers->mutex); */ |
735 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
736 if (old_ssl_session) { |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
737 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
738 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1443 | 739 "old session: %p:%d", |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
740 old_ssl_session, old_ssl_session->references); |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
741 |
884 | 742 /* TODO: may block */ |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
743 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
744 ngx_ssl_free_session(old_ssl_session); |
884 | 745 } |
746 } | |
747 | |
748 #endif |