From 4d53631022f4c01878310298a9508cc7ee073012 Mon Sep 17 00:00:00 2001 From: Ruslan Ermilov Date: Fri, 10 Apr 2015 14:48:36 +0300 Subject: [PATCH] Upstream: store peers as a linked list. This is an API change. --- .../modules/ngx_http_upstream_hash_module.c | 73 ++++++------ .../ngx_http_upstream_ip_hash_module.c | 16 ++- .../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(-) diff --git a/src/http/modules/ngx_http_upstream_hash_module.c b/src/http/modules/ngx_http_upstream_hash_module.c index daea0432c..249733f72 100644 --- a/src/http/modules/ngx_http_upstream_hash_module.c +++ b/src/http/modules/ngx_http_upstream_hash_module.c @@ -211,11 +211,19 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data) 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_connection_t *pc, void *data) 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_connection_t *pc, void *data) } } - 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 *cf, ngx_http_upstream_srv_conf_t *us) 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_peer_connection_t *pc, void *data) 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_peer_connection_t *pc, void *data) 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_peer_connection_t *pc, void *data) continue; } - peer = &hp->rrp.peers->peer[i]; - if (peer->down) { continue; } @@ -541,32 +548,13 @@ ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data) 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_peer_connection_t *pc, void *data) 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 --git a/src/http/modules/ngx_http_upstream_ip_hash_module.c b/src/http/modules/ngx_http_upstream_ip_hash_module.c index 638e0778b..2c6506d6f 100644 --- a/src/http/modules/ngx_http_upstream_ip_hash_module.c +++ b/src/http/modules/ngx_http_upstream_ip_hash_module.c @@ -181,11 +181,19 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data) 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_peer_connection_t *pc, void *data) 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_peer_connection_t *pc, void *data) } } - iphp->rrp.current = p; + iphp->rrp.current = peer; pc->sockaddr = peer->sockaddr; pc->socklen = peer->socklen; diff --git a/src/http/modules/ngx_http_upstream_least_conn_module.c b/src/http/modules/ngx_http_upstream_least_conn_module.c index f2a4ec9eb..61b538631 100644 --- a/src/http/modules/ngx_http_upstream_least_conn_module.c +++ b/src/http/modules/ngx_http_upstream_least_conn_module.c @@ -130,7 +130,10 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data) 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(ngx_peer_connection_t *pc, void *data) continue; } - peer = &peers->peer[i]; - if (peer->down) { continue; } @@ -181,8 +182,10 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data) 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(ngx_peer_connection_t *pc, void *data) continue; } - peer = &peers->peer[i]; - if (peer->down) { continue; } @@ -233,7 +234,7 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data) 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 --git a/src/http/ngx_http_upstream_round_robin.c b/src/http/ngx_http_upstream_round_robin.c index 61c042f70..8d7046ade 100644 --- a/src/http/ngx_http_upstream_round_robin.c +++ b/src/http/ngx_http_upstream_round_robin.c @@ -34,7 +34,7 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf, 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_conf_t *cf, 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_conf_t *cf, 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_conf_t *cf, peer[n].fail_timeout = server[i].fail_timeout; peer[n].down = server[i].down; peer[n].server = server[i].name; + + *peerp = &peer[n]; + peerp = &peer[n].next; n++; } } @@ -116,12 +123,16 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf, return NGX_OK; } - backup = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) - + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1)); + backup = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)); if (backup == 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 = 0; backup->single = 0; backup->number = n; @@ -130,7 +141,7 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf, backup->name = &us->host; n = 0; - peer = backup->peer; + peerp = &backup->peer; for (i = 0; i < us->servers->nelts; i++) { if (!server[i].backup) { @@ -148,6 +159,9 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf, peer[n].fail_timeout = server[i].fail_timeout; peer[n].down = server[i].down; peer[n].server = server[i].name; + + *peerp = &peer[n]; + peerp = &peer[n].next; n++; } } @@ -184,19 +198,23 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf, n = u.naddrs; - 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 = 0; peers->total_weight = n; peers->name = &us->host; - peer = peers->peer; + peerp = &peers->peer; for (i = 0; i < u.naddrs; i++) { peer[i].sockaddr = u.addrs[i].sockaddr; @@ -207,6 +225,8 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf, peer[i].current_weight = 0; peer[i].max_fails = 1; peer[i].fail_timeout = 10; + *peerp = &peer[i]; + peerp = &peer[i].next; } us->peer.data = peers; @@ -236,7 +256,7 @@ ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r, } rrp->peers = us->peer.data; - rrp->current = 0; + rrp->current = NULL; n = rrp->peers->number; @@ -280,7 +300,7 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r, socklen_t socklen; ngx_uint_t i, n; struct sockaddr *sockaddr; - ngx_http_upstream_rr_peer_t *peer; + ngx_http_upstream_rr_peer_t *peer, **peerp; ngx_http_upstream_rr_peers_t *peers; ngx_http_upstream_rr_peer_data_t *rrp; @@ -295,18 +315,21 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r, r->upstream->peer.data = rrp; } - peers = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peers_t) - + sizeof(ngx_http_upstream_rr_peer_t) * (ur->naddrs - 1)); + peers = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peers_t)); if (peers == NULL) { return NGX_ERROR; } + peer = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peer_t) + * ur->naddrs); + if (peer == NULL) { + return NGX_ERROR; + } + peers->single = (ur->naddrs == 1); peers->number = ur->naddrs; peers->name = &ur->host; - peer = peers->peer; - if (ur->sockaddr) { peer[0].sockaddr = ur->sockaddr; peer[0].socklen = ur->socklen; @@ -316,8 +339,10 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r, peer[0].current_weight = 0; peer[0].max_fails = 1; peer[0].fail_timeout = 10; + peers->peer = peer; } else { + peerp = &peers->peer; for (i = 0; i < ur->naddrs; i++) { @@ -356,11 +381,13 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r, peer[i].current_weight = 0; peer[i].max_fails = 1; peer[i].fail_timeout = 10; + *peerp = &peer[i]; + peerp = &peer[i].next; } } rrp->peers = peers; - rrp->current = 0; + rrp->current = NULL; if (rrp->peers->number <= 8 * sizeof(uintptr_t)) { rrp->tried = &rrp->data; @@ -409,12 +436,14 @@ ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data) /* ngx_lock_mutex(peers->mutex); */ if (peers->single) { - peer = &peers->peer[0]; + peer = peers->peer; if (peer->down) { goto failed; } + rrp->current = peer; + } else { /* there are several peers */ @@ -426,8 +455,8 @@ ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data) } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, - "get rr peer, current: %ui %i", - rrp->current, peer->current_weight); + "get rr peer, current: %p %i", + peer, peer->current_weight); } pc->sockaddr = peer->sockaddr; @@ -468,8 +497,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; } /* ngx_unlock_mutex(peers->mutex); */ @@ -486,7 +515,7 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp) time_t now; uintptr_t m; ngx_int_t total; - ngx_uint_t i, n; + ngx_uint_t i, n, p; ngx_http_upstream_rr_peer_t *peer, *best; now = ngx_time(); @@ -494,7 +523,14 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp) best = NULL; total = 0; - for (i = 0; i < rrp->peers->number; i++) { +#if (NGX_SUPPRESS_WARN) + p = 0; +#endif + + for (peer = 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)); @@ -503,8 +539,6 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp) continue; } - peer = &rrp->peers->peer[i]; - if (peer->down) { continue; } @@ -525,6 +559,7 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp) if (best == NULL || peer->current_weight > best->current_weight) { best = peer; + p = i; } } @@ -532,12 +567,10 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp) return NULL; } - i = best - &rrp->peers->peer[0]; + rrp->current = best; - rrp->current = i; - - n = i / (8 * sizeof(uintptr_t)); - m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); + n = p / (8 * sizeof(uintptr_t)); + m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t)); rrp->tried[n] |= m; @@ -565,7 +598,7 @@ ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data, /* TODO: NGX_PEER_KEEPALIVE */ - peer = &rrp->peers->peer[rrp->current]; + peer = rrp->current; if (rrp->peers->single) { @@ -589,8 +622,8 @@ ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data, } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, - "free rr peer failed: %ui %i", - rrp->current, peer->effective_weight); + "free rr peer failed: %p %i", + peer, peer->effective_weight); if (peer->effective_weight < 0) { peer->effective_weight = 0; @@ -629,7 +662,7 @@ ngx_http_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc, ngx_ssl_session_t *ssl_session; ngx_http_upstream_rr_peer_t *peer; - peer = &rrp->peers->peer[rrp->current]; + peer = rrp->current; /* TODO: threads only mutex */ /* ngx_lock_mutex(rrp->peers->mutex); */ @@ -665,7 +698,7 @@ ngx_http_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc, ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, "save session: %p", ssl_session); - peer = &rrp->peers->peer[rrp->current]; + peer = rrp->current; /* TODO: threads only mutex */ /* ngx_lock_mutex(rrp->peers->mutex); */ diff --git a/src/http/ngx_http_upstream_round_robin.h b/src/http/ngx_http_upstream_round_robin.h index 8927d98c6..53beac8e3 100644 --- a/src/http/ngx_http_upstream_round_robin.h +++ b/src/http/ngx_http_upstream_round_robin.h @@ -14,7 +14,9 @@ #include -typedef struct { +typedef struct ngx_http_upstream_rr_peer_s ngx_http_upstream_rr_peer_t; + +struct ngx_http_upstream_rr_peer_s { struct sockaddr *sockaddr; socklen_t socklen; ngx_str_t name; @@ -38,7 +40,9 @@ typedef struct { #if (NGX_HTTP_SSL) ngx_ssl_session_t *ssl_session; /* local to a process */ #endif -} ngx_http_upstream_rr_peer_t; + + ngx_http_upstream_rr_peer_t *next; +}; typedef struct ngx_http_upstream_rr_peers_s ngx_http_upstream_rr_peers_t; @@ -55,13 +59,13 @@ struct ngx_http_upstream_rr_peers_s { ngx_http_upstream_rr_peers_t *next; - ngx_http_upstream_rr_peer_t peer[1]; + ngx_http_upstream_rr_peer_t *peer; }; typedef struct { ngx_http_upstream_rr_peers_t *peers; - ngx_uint_t current; + ngx_http_upstream_rr_peer_t *current; uintptr_t *tried; uintptr_t data; } ngx_http_upstream_rr_peer_data_t;