Permalink
Browse files

add the upstream busyness module. not work well yet

  • Loading branch information...
yaoweibin committed Apr 8, 2011
1 parent f5cfc7e commit 5bf2315fb426ca71d4e4628defa8babc60b3efcd
View
4 config
@@ -4,7 +4,7 @@ ngx_feature_run=no
ngx_feature_incs=
ngx_feature_path="$ngx_addon_dir/modules $ngx_addon_dir"
ngx_feature_deps="$ngx_addon_dir/ngx_tcp.h $ngx_addon_dir/ngx_tcp_session.h $ngx_addon_dir/ngx_tcp_upstream.h $ngx_addon_dir/ngx_tcp_upstream_check.h $ngx_addon_dir/ngx_tcp_upstream_round_robin.h"
-ngx_tcp_src="$ngx_addon_dir/ngx_tcp.c $ngx_addon_dir/ngx_tcp_core_module.c $ngx_addon_dir/ngx_tcp_session.c $ngx_addon_dir/ngx_tcp_access.c $ngx_addon_dir/ngx_tcp_upstream.c $ngx_addon_dir/ngx_tcp_upstream_round_robin.c $ngx_addon_dir/modules/ngx_tcp_proxy_module.c $ngx_addon_dir/modules/ngx_tcp_upstream_ip_hash_module.c $ngx_addon_dir/ngx_tcp_upstream_check.c $ngx_addon_dir/http_response_parse.c $ngx_addon_dir/smtp_response_parse.c"
+ngx_tcp_src="$ngx_addon_dir/ngx_tcp.c $ngx_addon_dir/ngx_tcp_core_module.c $ngx_addon_dir/ngx_tcp_session.c $ngx_addon_dir/ngx_tcp_access.c $ngx_addon_dir/ngx_tcp_upstream.c $ngx_addon_dir/ngx_tcp_upstream_round_robin.c $ngx_addon_dir/modules/ngx_tcp_proxy_module.c $ngx_addon_dir/modules/ngx_tcp_upstream_ip_hash_module.c $ngx_addon_dir/modules/ngx_tcp_upstream_busyness_module.c $ngx_addon_dir/ngx_tcp_upstream_check.c $ngx_addon_dir/http_response_parse.c $ngx_addon_dir/smtp_response_parse.c"
ngx_tcp_ssl_deps="$ngx_addon_dir/modules/ngx_tcp_ssl_module.h"
ngx_tcp_ssl_src="$ngx_addon_dir/modules/ngx_tcp_ssl_module.c"
ngx_feature_test="int a;"
@@ -15,7 +15,7 @@ if [ $ngx_found = yes ]; then
ngx_addon_name=ngx_tcp_module
TCP_CORE_MODULES="ngx_tcp_module ngx_tcp_core_module ngx_tcp_upstream_module"
- TCP_MODULES="ngx_tcp_proxy_module ngx_tcp_upstream_ip_hash_module"
+ TCP_MODULES="ngx_tcp_proxy_module ngx_tcp_upstream_ip_hash_module ngx_tcp_upstream_busyness_module"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_feature_deps"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_tcp_src"
@@ -225,6 +225,7 @@ ngx_tcp_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
uscf->flags = NGX_TCP_UPSTREAM_CREATE
|NGX_TCP_UPSTREAM_MAX_FAILS
|NGX_TCP_UPSTREAM_FAIL_TIMEOUT
+ |NGX_TCP_UPSTREAM_MAX_BUSY
|NGX_TCP_UPSTREAM_DOWN;
return NGX_CONF_OK;
View
@@ -432,8 +432,7 @@ ngx_tcp_log_error(ngx_log_t *log, u_char *buf, size_t len)
return p;
}
- p = ngx_snprintf(buf, len, ", server: %V",
- s->addr_text);
+ p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
len -= p - buf;
buf = p;
@@ -444,7 +443,9 @@ ngx_tcp_log_error(ngx_log_t *log, u_char *buf, size_t len)
return p;
}
- p = ngx_snprintf(buf, len, ", upstream: %V", pctx->upstream->name);
+ if (pctx->upstream->connection) {
+ p = ngx_snprintf(buf, len, ", upstream: %V", pctx->upstream->name);
+ }
}
return p;
View
@@ -352,7 +352,7 @@ ngx_tcp_upstream_connect(ngx_tcp_session_t *s, ngx_tcp_upstream_t *u)
/* rc == NGX_OK or rc == NGX_AGAIN */
- if (u->peer.check_index != NGX_INVALID_INDEX) {
+ if (u->peer.check_index != NGX_INVALID_CHECK_INDEX) {
ngx_tcp_check_get_peer(u->peer.check_index);
}
@@ -496,6 +496,11 @@ ngx_tcp_upstream_next(ngx_tcp_session_t *s, ngx_tcp_upstream_t *u,
(void) ngx_ssl_shutdown(u->peer.connection);
}
#endif
+
+ if (u->peer.check_index != NGX_INVALID_CHECK_INDEX) {
+ ngx_tcp_check_free_peer(u->peer.check_index);
+ u->peer.check_index = NGX_INVALID_CHECK_INDEX;
+ }
ngx_close_connection(u->peer.connection);
}
@@ -548,9 +553,9 @@ ngx_tcp_upstream_finalize_session(ngx_tcp_session_t *s,
u->peer.free(&u->peer, u->peer.data, 0);
}
- if (u->peer.check_index != NGX_INVALID_INDEX) {
+ if (u->peer.check_index != NGX_INVALID_CHECK_INDEX) {
ngx_tcp_check_free_peer(u->peer.check_index);
- u->peer.check_index = NGX_INVALID_INDEX;
+ u->peer.check_index = NGX_INVALID_CHECK_INDEX;
}
if (u->peer.connection) {
@@ -559,7 +564,7 @@ ngx_tcp_upstream_finalize_session(ngx_tcp_session_t *s,
"close tcp upstream connection: %d",
u->peer.connection->fd);
- ngx_close_connection(u->peer.connection);
+ ngx_close_connection(u->peer.connection);
}
u->peer.connection = NULL;
View
@@ -251,6 +251,9 @@ ngx_tcp_check_add_peer(ngx_conf_t *cf, ngx_tcp_upstream_srv_conf_t *uscf,
peers_conf = umcf->peers_conf;
peer_conf = ngx_array_push(&peers_conf->peers);
+ if (peer_conf == NULL) {
+ return NGX_INVALID_CHECK_INDEX;
+ }
ngx_memzero(peer_conf, sizeof(ngx_tcp_check_peer_conf_t));
@@ -275,7 +278,22 @@ ngx_tcp_check_peer_down(ngx_uint_t index)
peer_conf = check_peers_ctx->peers.elts;
return (peer_conf[index].shm->down |
- (peer_conf[index].shm->business > peer_conf[index].max_busy));
+ (peer_conf[index].shm->busyness > peer_conf[index].max_busy));
+}
+
+
+ngx_uint_t
+ngx_tcp_check_get_peer_busyness(ngx_uint_t index)
+{
+ ngx_tcp_check_peer_conf_t *peer_conf;
+
+ if (check_peers_ctx == NULL || index >= check_peers_ctx->peers.nelts) {
+ return (ngx_uint_t) (-1);
+ }
+
+ peer_conf = check_peers_ctx->peers.elts;
+
+ return peer_conf[index].shm->busyness;
}
@@ -292,7 +310,7 @@ ngx_tcp_check_get_peer(ngx_uint_t index)
ngx_spinlock(&peer_conf[index].shm->lock, ngx_pid, 1024);
- peer_conf[index].shm->business++;
+ peer_conf[index].shm->busyness++;
peer_conf[index].shm->access_count++;
ngx_spinlock_unlock(&peer_conf[index].shm->lock);
@@ -312,8 +330,8 @@ ngx_tcp_check_free_peer(ngx_uint_t index)
ngx_spinlock(&peer_conf[index].shm->lock, ngx_pid, 1024);
- if (peer_conf[index].shm->business > 0) {
- peer_conf[index].shm->business--;
+ if (peer_conf[index].shm->busyness > 0) {
+ peer_conf[index].shm->busyness--;
}
ngx_spinlock_unlock(&peer_conf[index].shm->lock);
@@ -366,7 +384,7 @@ ngx_tcp_upstream_check_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data)
peer_shm->fall_count = 0;
peer_shm->rise_count = 0;
- peer_shm->business = 0;
+ peer_shm->busyness = 0;
peer_shm->down = 1;
}
@@ -1795,7 +1813,7 @@ ngx_tcp_upstream_check_status_handler(ngx_http_request_t *r)
" <th>Index</th>\n"
" <th>Name</th>\n"
" <th>Status</th>\n"
- " <th>Business</th>\n"
+ " <th>Busyness</th>\n"
" <th>Rise counts</th>\n"
" <th>Fall counts</th>\n"
" <th>Access counts</th>\n"
@@ -1819,7 +1837,7 @@ ngx_tcp_upstream_check_status_handler(ngx_http_request_t *r)
i,
&peer_conf[i].peer->name,
peer_shm[i].down ? "down" : "up",
- peer_shm[i].business,
+ peer_shm[i].busyness,
peer_shm[i].rise_count,
peer_shm[i].fall_count,
peer_shm[i].access_count,
View
@@ -61,7 +61,7 @@ typedef struct {
ngx_uint_t rise_count;
ngx_atomic_t lock;
- ngx_atomic_t business;
+ ngx_atomic_t busyness;
ngx_atomic_t down;
ngx_uint_t access_count;
@@ -223,6 +223,8 @@ ngx_uint_t ngx_tcp_check_add_peer(ngx_conf_t *cf, ngx_tcp_upstream_srv_conf_t *u
ngx_uint_t ngx_tcp_check_peer_down(ngx_uint_t index);
+ngx_uint_t ngx_tcp_check_get_peer_busyness(ngx_uint_t index);
+
void ngx_tcp_check_get_peer(ngx_uint_t index);
void ngx_tcp_check_free_peer(ngx_uint_t index);
@@ -62,12 +62,12 @@ ngx_tcp_upstream_init_round_robin(ngx_conf_t *cf,
peers->peer[n].check_index =
ngx_tcp_check_add_peer(cf, us, &server[i].addrs[j], server[i].max_busy);
- if (peers->peer[n].check_index == (ngx_uint_t) NGX_ERROR) {
+ if (peers->peer[n].check_index == (ngx_uint_t) NGX_INVALID_CHECK_INDEX) {
return NGX_ERROR;
}
}
else {
- peers->peer[n].check_index = (ngx_uint_t) NGX_ERROR;
+ peers->peer[n].check_index = (ngx_uint_t) NGX_INVALID_CHECK_INDEX;
}
n++;
@@ -127,12 +127,12 @@ ngx_tcp_upstream_init_round_robin(ngx_conf_t *cf,
backup->peer[n].check_index =
ngx_tcp_check_add_peer(cf, us, &server[i].addrs[j], server[i].max_busy);
- if (backup->peer[n].check_index == (ngx_uint_t) NGX_ERROR) {
+ if (backup->peer[n].check_index == (ngx_uint_t) NGX_INVALID_CHECK_INDEX) {
return NGX_ERROR;
}
}
else {
- backup->peer[n].check_index = (ngx_uint_t) NGX_ERROR;
+ backup->peer[n].check_index = (ngx_uint_t) NGX_INVALID_CHECK_INDEX;
}
n++;
@@ -193,7 +193,7 @@ ngx_tcp_upstream_init_round_robin(ngx_conf_t *cf,
peers->peer[i].current_weight = 1;
peers->peer[i].max_fails = 1;
peers->peer[i].fail_timeout = 10;
- peers->peer[i].check_index = (ngx_uint_t) NGX_ERROR;
+ peers->peer[i].check_index = (ngx_uint_t) NGX_INVALID_CHECK_INDEX;
}
us->peer.data = peers;
@@ -254,6 +254,8 @@ ngx_tcp_upstream_init_round_robin_peer(ngx_tcp_session_t *s,
s->upstream->peer.get = ngx_tcp_upstream_get_round_robin_peer;
s->upstream->peer.free = ngx_tcp_upstream_free_round_robin_peer;
s->upstream->peer.tries = rrp->peers->number;
+ s->upstream->peer.check_index = NGX_INVALID_CHECK_INDEX;
+ s->upstream->peer.name = NULL;
#if (NGX_TCP_SSL)
s->upstream->peer.set_session =
ngx_tcp_upstream_set_round_robin_peer_session;
View
@@ -1,5 +1,5 @@
diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c
-index 770a590..26dd221 100644
+index c0485c6..bfb1f5c 100644
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -66,7 +66,7 @@ static ngx_str_t err_levels[] = {
@@ -12,7 +12,7 @@ index 770a590..26dd221 100644
diff --git a/src/core/ngx_log.h b/src/core/ngx_log.h
-index b736aa2..0b1507b 100644
+index 1054836..ee3f2cc 100644
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -29,6 +29,7 @@
@@ -33,14 +33,14 @@ index b736aa2..0b1507b 100644
#define NGX_LOG_DEBUG_ALL 0x7ffffff0
diff --git a/src/event/ngx_event_connect.h b/src/event/ngx_event_connect.h
-index 6cad042..8371b55 100644
+index d64f1bb..d15aa9a 100644
--- a/src/event/ngx_event_connect.h
+++ b/src/event/ngx_event_connect.h
@@ -32,6 +32,7 @@ typedef void (*ngx_event_save_peer_session_pt)(ngx_peer_connection_t *pc,
void *data);
#endif
-+#define NGX_INVALED_INDEX (-1)
++#define NGX_INVALID_CHECK_INDEX (ngx_uint_t)(-1)
struct ngx_peer_connection_s {
ngx_connection_t *connection;

0 comments on commit 5bf2315

Please sign in to comment.