Upstream: improved peer selection concurrency for hash and ip_hash.

This commit is contained in:
Ruslan Ermilov 2018-06-14 07:03:50 +03:00
parent 63e8a1d926
commit 2eab9efbe4
3 changed files with 21 additions and 3 deletions

View File

@ -176,7 +176,7 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"get hash peer, try: %ui", pc->tries);
ngx_http_upstream_rr_peers_wlock(hp->rrp.peers);
ngx_http_upstream_rr_peers_rlock(hp->rrp.peers);
if (hp->tries > 20 || hp->rrp.peers->single) {
ngx_http_upstream_rr_peers_unlock(hp->rrp.peers);
@ -228,10 +228,13 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
goto next;
}
ngx_http_upstream_rr_peer_lock(hp->rrp.peers, peer);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"get hash peer, value:%uD, peer:%ui", hp->hash, p);
if (peer->down) {
ngx_http_upstream_rr_peer_unlock(hp->rrp.peers, peer);
goto next;
}
@ -239,10 +242,12 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
&& peer->fails >= peer->max_fails
&& now - peer->checked <= peer->fail_timeout)
{
ngx_http_upstream_rr_peer_unlock(hp->rrp.peers, peer);
goto next;
}
if (peer->max_conns && peer->conns >= peer->max_conns) {
ngx_http_upstream_rr_peer_unlock(hp->rrp.peers, peer);
goto next;
}
@ -268,6 +273,7 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
peer->checked = now;
}
ngx_http_upstream_rr_peer_unlock(hp->rrp.peers, peer);
ngx_http_upstream_rr_peers_unlock(hp->rrp.peers);
hp->rrp.tried[n] |= m;

View File

@ -161,7 +161,7 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
/* TODO: cached */
ngx_http_upstream_rr_peers_wlock(iphp->rrp.peers);
ngx_http_upstream_rr_peers_rlock(iphp->rrp.peers);
if (iphp->tries > 20 || iphp->rrp.peers->single) {
ngx_http_upstream_rr_peers_unlock(iphp->rrp.peers);
@ -201,7 +201,10 @@ 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 %04XL", p, (uint64_t) m);
ngx_http_upstream_rr_peer_lock(iphp->rrp.peers, peer);
if (peer->down) {
ngx_http_upstream_rr_peer_unlock(iphp->rrp.peers, peer);
goto next;
}
@ -209,10 +212,12 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
&& peer->fails >= peer->max_fails
&& now - peer->checked <= peer->fail_timeout)
{
ngx_http_upstream_rr_peer_unlock(iphp->rrp.peers, peer);
goto next;
}
if (peer->max_conns && peer->conns >= peer->max_conns) {
ngx_http_upstream_rr_peer_unlock(iphp->rrp.peers, peer);
goto next;
}
@ -238,6 +243,7 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
peer->checked = now;
}
ngx_http_upstream_rr_peer_unlock(iphp->rrp.peers, peer);
ngx_http_upstream_rr_peers_unlock(iphp->rrp.peers);
iphp->rrp.tried[n] |= m;

View File

@ -176,7 +176,7 @@ ngx_stream_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
"get hash peer, try: %ui", pc->tries);
ngx_stream_upstream_rr_peers_wlock(hp->rrp.peers);
ngx_stream_upstream_rr_peers_rlock(hp->rrp.peers);
if (hp->tries > 20 || hp->rrp.peers->single) {
ngx_stream_upstream_rr_peers_unlock(hp->rrp.peers);
@ -227,10 +227,13 @@ ngx_stream_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
goto next;
}
ngx_stream_upstream_rr_peer_lock(hp->rrp.peers, peer);
ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pc->log, 0,
"get hash peer, value:%uD, peer:%ui", hp->hash, p);
if (peer->down) {
ngx_stream_upstream_rr_peer_unlock(hp->rrp.peers, peer);
goto next;
}
@ -238,10 +241,12 @@ ngx_stream_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
&& peer->fails >= peer->max_fails
&& now - peer->checked <= peer->fail_timeout)
{
ngx_stream_upstream_rr_peer_unlock(hp->rrp.peers, peer);
goto next;
}
if (peer->max_conns && peer->conns >= peer->max_conns) {
ngx_stream_upstream_rr_peer_unlock(hp->rrp.peers, peer);
goto next;
}
@ -267,6 +272,7 @@ ngx_stream_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
peer->checked = now;
}
ngx_stream_upstream_rr_peer_unlock(hp->rrp.peers, peer);
ngx_stream_upstream_rr_peers_unlock(hp->rrp.peers);
hp->rrp.tried[n] |= m;