[nginx] Upstream: store peers as a linked list.
Ruslan Ermilov
ru at nginx.com
Thu Apr 16 12:23:12 UTC 2015
details: http://hg.nginx.org/nginx/rev/c44459611d91
branches:
changeset: 6100:c44459611d91
user: Ruslan Ermilov <ru at nginx.com>
date: Fri Apr 10 14:48:36 2015 +0300
description:
Upstream: store peers as a linked list.
This is an API change.
diffstat:
src/http/modules/ngx_http_upstream_hash_module.c | 73 ++++++-----
src/http/modules/ngx_http_upstream_ip_hash_module.c | 16 +-
src/http/modules/ngx_http_upstream_least_conn_module.c | 21 +-
src/http/ngx_http_upstream_round_robin.c | 105 +++++++++++-----
src/http/ngx_http_upstream_round_robin.h | 12 +-
5 files changed, 140 insertions(+), 87 deletions(-)
diffs (truncated from 627 to 300 lines):
diff -r 6ff0ebd6fbf4 -r c44459611d91 src/http/modules/ngx_http_upstream_hash_module.c
--- a/src/http/modules/ngx_http_upstream_hash_module.c Fri Apr 10 13:16:23 2015 +0300
+++ b/src/http/modules/ngx_http_upstream_hash_module.c Fri Apr 10 14:48:36 2015 +0300
@@ -211,11 +211,19 @@ ngx_http_upstream_get_hash_peer(ngx_peer
if (!hp->rrp.peers->weighted) {
p = hp->hash % hp->rrp.peers->number;
+ peer = hp->rrp.peers->peer;
+ for (i = 0; i < p; i++) {
+ peer = peer->next;
+ }
+
} else {
w = hp->hash % hp->rrp.peers->total_weight;
- for (i = 0; i < hp->rrp.peers->number; i++) {
- w -= hp->rrp.peers->peer[i].weight;
+ for (peer = hp->rrp.peers->peer, i = 0;
+ peer;
+ peer = peer->next, i++)
+ {
+ w -= peer->weight;
if (w < 0) {
break;
}
@@ -234,8 +242,6 @@ ngx_http_upstream_get_hash_peer(ngx_peer
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"get hash peer, value:%uD, peer:%ui", hp->hash, p);
- peer = &hp->rrp.peers->peer[p];
-
if (peer->down) {
goto next;
}
@@ -256,7 +262,7 @@ ngx_http_upstream_get_hash_peer(ngx_peer
}
}
- hp->rrp.current = p;
+ hp->rrp.current = peer;
pc->sockaddr = peer->sockaddr;
pc->socklen = peer->socklen;
@@ -306,8 +312,7 @@ ngx_http_upstream_init_chash(ngx_conf_t
points->number = 0;
- for (i = 0; i < peers->number; i++) {
- peer = &peers->peer[i];
+ for (peer = peers->peer; peer; peer = peer->next) {
server = &peer->server;
/*
@@ -475,7 +480,7 @@ ngx_http_upstream_get_chash_peer(ngx_pee
intptr_t m;
ngx_str_t *server;
ngx_int_t total;
- ngx_uint_t i, n;
+ ngx_uint_t i, n, best_i;
ngx_http_upstream_rr_peer_t *peer, *best;
ngx_http_upstream_chash_point_t *point;
ngx_http_upstream_chash_points_t *points;
@@ -501,9 +506,13 @@ ngx_http_upstream_get_chash_peer(ngx_pee
hp->hash, server);
best = NULL;
+ best_i = 0;
total = 0;
- for (i = 0; i < hp->rrp.peers->number; i++) {
+ for (peer = hp->rrp.peers->peer, i = 0;
+ peer;
+ peer = peer->next, i++)
+ {
n = i / (8 * sizeof(uintptr_t));
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
@@ -512,8 +521,6 @@ ngx_http_upstream_get_chash_peer(ngx_pee
continue;
}
- peer = &hp->rrp.peers->peer[i];
-
if (peer->down) {
continue;
}
@@ -541,32 +548,13 @@ ngx_http_upstream_get_chash_peer(ngx_pee
if (best == NULL || peer->current_weight > best->current_weight) {
best = peer;
+ best_i = i;
}
}
if (best) {
best->current_weight -= total;
-
- i = best - &hp->rrp.peers->peer[0];
-
- hp->rrp.current = i;
-
- n = i / (8 * sizeof(uintptr_t));
- m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
-
- hp->rrp.tried[n] |= m;
-
- if (now - best->checked > best->fail_timeout) {
- best->checked = now;
- }
-
- pc->sockaddr = best->sockaddr;
- pc->socklen = best->socklen;
- pc->name = &best->name;
-
- best->conns++;
-
- return NGX_OK;
+ goto found;
}
hp->hash++;
@@ -576,6 +564,27 @@ ngx_http_upstream_get_chash_peer(ngx_pee
return NGX_BUSY;
}
}
+
+found:
+
+ hp->rrp.current = best;
+
+ pc->sockaddr = best->sockaddr;
+ pc->socklen = best->socklen;
+ pc->name = &best->name;
+
+ best->conns++;
+
+ if (now - best->checked > best->fail_timeout) {
+ best->checked = now;
+ }
+
+ n = best_i / (8 * sizeof(uintptr_t));
+ m = (uintptr_t) 1 << best_i % (8 * sizeof(uintptr_t));
+
+ hp->rrp.tried[n] |= m;
+
+ return NGX_OK;
}
diff -r 6ff0ebd6fbf4 -r c44459611d91 src/http/modules/ngx_http_upstream_ip_hash_module.c
--- a/src/http/modules/ngx_http_upstream_ip_hash_module.c Fri Apr 10 13:16:23 2015 +0300
+++ b/src/http/modules/ngx_http_upstream_ip_hash_module.c Fri Apr 10 14:48:36 2015 +0300
@@ -181,11 +181,19 @@ ngx_http_upstream_get_ip_hash_peer(ngx_p
if (!iphp->rrp.peers->weighted) {
p = hash % iphp->rrp.peers->number;
+ peer = iphp->rrp.peers->peer;
+ for (i = 0; i < p; i++) {
+ peer = peer->next;
+ }
+
} else {
w = hash % iphp->rrp.peers->total_weight;
- for (i = 0; i < iphp->rrp.peers->number; i++) {
- w -= iphp->rrp.peers->peer[i].weight;
+ for (peer = iphp->rrp.peers->peer, i = 0;
+ peer;
+ peer = peer->next, i++)
+ {
+ w -= peer->weight;
if (w < 0) {
break;
}
@@ -204,8 +212,6 @@ ngx_http_upstream_get_ip_hash_peer(ngx_p
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"get ip hash peer, hash: %ui %04XA", p, m);
- peer = &iphp->rrp.peers->peer[p];
-
/* ngx_lock_mutex(iphp->rrp.peers->mutex); */
if (peer->down) {
@@ -236,7 +242,7 @@ ngx_http_upstream_get_ip_hash_peer(ngx_p
}
}
- iphp->rrp.current = p;
+ iphp->rrp.current = peer;
pc->sockaddr = peer->sockaddr;
pc->socklen = peer->socklen;
diff -r 6ff0ebd6fbf4 -r c44459611d91 src/http/modules/ngx_http_upstream_least_conn_module.c
--- a/src/http/modules/ngx_http_upstream_least_conn_module.c Fri Apr 10 13:16:23 2015 +0300
+++ b/src/http/modules/ngx_http_upstream_least_conn_module.c Fri Apr 10 14:48:36 2015 +0300
@@ -130,7 +130,10 @@ ngx_http_upstream_get_least_conn_peer(ng
p = 0;
#endif
- for (i = 0; i < peers->number; i++) {
+ for (peer = peers->peer, i = 0;
+ peer;
+ peer = peer->next, i++)
+ {
n = i / (8 * sizeof(uintptr_t));
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
@@ -139,8 +142,6 @@ ngx_http_upstream_get_least_conn_peer(ng
continue;
}
- peer = &peers->peer[i];
-
if (peer->down) {
continue;
}
@@ -181,8 +182,10 @@ ngx_http_upstream_get_least_conn_peer(ng
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"get least conn peer, many");
- for (i = p; i < peers->number; i++) {
-
+ for (peer = best, i = p;
+ peer;
+ peer = peer->next, i++)
+ {
n = i / (8 * sizeof(uintptr_t));
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
@@ -190,8 +193,6 @@ ngx_http_upstream_get_least_conn_peer(ng
continue;
}
- peer = &peers->peer[i];
-
if (peer->down) {
continue;
}
@@ -233,7 +234,7 @@ ngx_http_upstream_get_least_conn_peer(ng
best->conns++;
- rrp->current = p;
+ rrp->current = best;
n = p / (8 * sizeof(uintptr_t));
m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
@@ -266,8 +267,8 @@ failed:
/* all peers failed, mark them as live for quick recovery */
- for (i = 0; i < peers->number; i++) {
- peers->peer[i].fails = 0;
+ for (peer = peers->peer; peer; peer = peer->next) {
+ peer->fails = 0;
}
pc->name = peers->name;
diff -r 6ff0ebd6fbf4 -r c44459611d91 src/http/ngx_http_upstream_round_robin.c
--- a/src/http/ngx_http_upstream_round_robin.c Fri Apr 10 13:16:23 2015 +0300
+++ b/src/http/ngx_http_upstream_round_robin.c Fri Apr 10 14:48:36 2015 +0300
@@ -34,7 +34,7 @@ ngx_http_upstream_init_round_robin(ngx_c
ngx_url_t u;
ngx_uint_t i, j, n, w;
ngx_http_upstream_server_t *server;
- ngx_http_upstream_rr_peer_t *peer;
+ ngx_http_upstream_rr_peer_t *peer, **peerp;
ngx_http_upstream_rr_peers_t *peers, *backup;
us->peer.init = ngx_http_upstream_init_round_robin_peer;
@@ -61,12 +61,16 @@ ngx_http_upstream_init_round_robin(ngx_c
return NGX_ERROR;
}
- peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)
- + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1));
+ peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t));
if (peers == NULL) {
return NGX_ERROR;
}
+ peer = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peer_t) * n);
+ if (peer == NULL) {
+ return NGX_ERROR;
+ }
+
peers->single = (n == 1);
peers->number = n;
peers->weighted = (w != n);
@@ -74,7 +78,7 @@ ngx_http_upstream_init_round_robin(ngx_c
peers->name = &us->host;
n = 0;
- peer = peers->peer;
+ peerp = &peers->peer;
for (i = 0; i < us->servers->nelts; i++) {
if (server[i].backup) {
@@ -92,6 +96,9 @@ ngx_http_upstream_init_round_robin(ngx_c
peer[n].fail_timeout = server[i].fail_timeout;
More information about the nginx-devel
mailing list