[nginx] Upstream: generic hash module.
Roman Arutyunyan
arut at nginx.com
Mon Jun 2 12:21:56 UTC 2014
details: http://hg.nginx.org/nginx/rev/efc84a5723b3
branches:
changeset: 5717:efc84a5723b3
user: Roman Arutyunyan <arut at nginx.com>
date: Mon Jun 02 16:16:22 2014 +0400
description:
Upstream: generic hash module.
diffstat:
auto/modules | 5 +
auto/options | 4 +
auto/sources | 4 +
src/http/modules/ngx_http_upstream_hash_module.c | 631 +++++++++++++++++++++++
src/http/ngx_http_upstream.c | 1 +
src/http/ngx_http_upstream.h | 1 +
src/http/ngx_http_upstream_round_robin.c | 2 +
src/http/ngx_http_upstream_round_robin.h | 1 +
8 files changed, 649 insertions(+), 0 deletions(-)
diffs (truncated from 744 to 300 lines):
diff -r 34d460c5d186 -r efc84a5723b3 auto/modules
--- a/auto/modules Thu May 29 21:15:19 2014 +0400
+++ b/auto/modules Mon Jun 02 16:16:22 2014 +0400
@@ -371,6 +371,11 @@ if [ $HTTP_MP4 = YES ]; then
HTTP_SRCS="$HTTP_SRCS $HTTP_MP4_SRCS"
fi
+if [ $HTTP_UPSTREAM_HASH = YES ]; then
+ HTTP_MODULES="$HTTP_MODULES $HTTP_UPSTREAM_HASH_MODULE"
+ HTTP_SRCS="$HTTP_SRCS $HTTP_UPSTREAM_HASH_SRCS"
+fi
+
if [ $HTTP_UPSTREAM_IP_HASH = YES ]; then
HTTP_MODULES="$HTTP_MODULES $HTTP_UPSTREAM_IP_HASH_MODULE"
HTTP_SRCS="$HTTP_SRCS $HTTP_UPSTREAM_IP_HASH_SRCS"
diff -r 34d460c5d186 -r efc84a5723b3 auto/options
--- a/auto/options Thu May 29 21:15:19 2014 +0400
+++ b/auto/options Mon Jun 02 16:16:22 2014 +0400
@@ -99,6 +99,7 @@ HTTP_FLV=NO
HTTP_MP4=NO
HTTP_GUNZIP=NO
HTTP_GZIP_STATIC=NO
+HTTP_UPSTREAM_HASH=YES
HTTP_UPSTREAM_IP_HASH=YES
HTTP_UPSTREAM_LEAST_CONN=YES
HTTP_UPSTREAM_KEEPALIVE=YES
@@ -251,6 +252,7 @@ use the \"--without-http_limit_conn_modu
--without-http_limit_req_module) HTTP_LIMIT_REQ=NO ;;
--without-http_empty_gif_module) HTTP_EMPTY_GIF=NO ;;
--without-http_browser_module) HTTP_BROWSER=NO ;;
+ --without-http_upstream_hash_module) HTTP_UPSTREAM_HASH=NO ;;
--without-http_upstream_ip_hash_module) HTTP_UPSTREAM_IP_HASH=NO ;;
--without-http_upstream_least_conn_module)
HTTP_UPSTREAM_LEAST_CONN=NO ;;
@@ -395,6 +397,8 @@ cat << END
--without-http_limit_req_module disable ngx_http_limit_req_module
--without-http_empty_gif_module disable ngx_http_empty_gif_module
--without-http_browser_module disable ngx_http_browser_module
+ --without-http_upstream_hash_module
+ disable ngx_http_upstream_hash_module
--without-http_upstream_ip_hash_module
disable ngx_http_upstream_ip_hash_module
--without-http_upstream_least_conn_module
diff -r 34d460c5d186 -r efc84a5723b3 auto/sources
--- a/auto/sources Thu May 29 21:15:19 2014 +0400
+++ b/auto/sources Mon Jun 02 16:16:22 2014 +0400
@@ -497,6 +497,10 @@ HTTP_GZIP_STATIC_MODULE=ngx_http_gzip_st
HTTP_GZIP_STATIC_SRCS=src/http/modules/ngx_http_gzip_static_module.c
+HTTP_UPSTREAM_HASH_MODULE=ngx_http_upstream_hash_module
+HTTP_UPSTREAM_HASH_SRCS=src/http/modules/ngx_http_upstream_hash_module.c
+
+
HTTP_UPSTREAM_IP_HASH_MODULE=ngx_http_upstream_ip_hash_module
HTTP_UPSTREAM_IP_HASH_SRCS=src/http/modules/ngx_http_upstream_ip_hash_module.c
diff -r 34d460c5d186 -r efc84a5723b3 src/http/modules/ngx_http_upstream_hash_module.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/http/modules/ngx_http_upstream_hash_module.c Mon Jun 02 16:16:22 2014 +0400
@@ -0,0 +1,631 @@
+
+/*
+ * Copyright (C) Roman Arutyunyan
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+
+typedef struct {
+ uint32_t hash;
+ ngx_str_t *server;
+} ngx_http_upstream_chash_point_t;
+
+
+typedef struct {
+ ngx_uint_t number;
+ ngx_http_upstream_chash_point_t point[1];
+} ngx_http_upstream_chash_points_t;
+
+
+typedef struct {
+ ngx_http_complex_value_t key;
+ ngx_http_upstream_chash_points_t *points;
+} ngx_http_upstream_hash_srv_conf_t;
+
+
+typedef struct {
+ /* the round robin data must be first */
+ ngx_http_upstream_rr_peer_data_t rrp;
+ ngx_http_upstream_hash_srv_conf_t *conf;
+ ngx_str_t key;
+ ngx_uint_t tries;
+ ngx_uint_t rehash;
+ uint32_t hash;
+ ngx_event_get_peer_pt get_rr_peer;
+} ngx_http_upstream_hash_peer_data_t;
+
+
+static ngx_int_t ngx_http_upstream_init_hash(ngx_conf_t *cf,
+ ngx_http_upstream_srv_conf_t *us);
+static ngx_int_t ngx_http_upstream_init_hash_peer(ngx_http_request_t *r,
+ ngx_http_upstream_srv_conf_t *us);
+static ngx_int_t ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc,
+ void *data);
+
+static ngx_int_t ngx_http_upstream_init_chash(ngx_conf_t *cf,
+ ngx_http_upstream_srv_conf_t *us);
+static void ngx_http_upstream_add_chash_point(
+ ngx_http_upstream_chash_points_t *points, uint32_t hash, ngx_str_t *server);
+static ngx_uint_t ngx_http_upstream_find_chash_point(
+ ngx_http_upstream_chash_points_t *points, uint32_t hash);
+static ngx_int_t ngx_http_upstream_init_chash_peer(ngx_http_request_t *r,
+ ngx_http_upstream_srv_conf_t *us);
+static ngx_int_t ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc,
+ void *data);
+
+static void *ngx_http_upstream_hash_create_conf(ngx_conf_t *cf);
+static char *ngx_http_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf);
+
+
+static ngx_command_t ngx_http_upstream_hash_commands[] = {
+
+ { ngx_string("hash"),
+ NGX_HTTP_UPS_CONF|NGX_CONF_TAKE12,
+ ngx_http_upstream_hash,
+ NGX_HTTP_SRV_CONF_OFFSET,
+ 0,
+ NULL },
+
+ ngx_null_command
+};
+
+
+static ngx_http_module_t ngx_http_upstream_hash_module_ctx = {
+ NULL, /* preconfiguration */
+ NULL, /* postconfiguration */
+
+ NULL, /* create main configuration */
+ NULL, /* init main configuration */
+
+ ngx_http_upstream_hash_create_conf, /* create server configuration */
+ NULL, /* merge server configuration */
+
+ NULL, /* create location configuration */
+ NULL /* merge location configuration */
+};
+
+
+ngx_module_t ngx_http_upstream_hash_module = {
+ NGX_MODULE_V1,
+ &ngx_http_upstream_hash_module_ctx, /* module context */
+ ngx_http_upstream_hash_commands, /* module directives */
+ NGX_HTTP_MODULE, /* module type */
+ NULL, /* init master */
+ NULL, /* init module */
+ NULL, /* init process */
+ NULL, /* init thread */
+ NULL, /* exit thread */
+ NULL, /* exit process */
+ NULL, /* exit master */
+ NGX_MODULE_V1_PADDING
+};
+
+
+static ngx_int_t
+ngx_http_upstream_init_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
+{
+ if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ us->peer.init = ngx_http_upstream_init_hash_peer;
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_upstream_init_hash_peer(ngx_http_request_t *r,
+ ngx_http_upstream_srv_conf_t *us)
+{
+ ngx_http_upstream_hash_srv_conf_t *hcf;
+ ngx_http_upstream_hash_peer_data_t *hp;
+
+ hp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_hash_peer_data_t));
+ if (hp == NULL) {
+ return NGX_ERROR;
+ }
+
+ r->upstream->peer.data = &hp->rrp;
+
+ if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ r->upstream->peer.get = ngx_http_upstream_get_hash_peer;
+
+ hcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_hash_module);
+
+ if (ngx_http_complex_value(r, &hcf->key, &hp->key) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "upstream hash key:\"%V\"", &hp->key);
+
+ hp->conf = hcf;
+ hp->tries = 0;
+ hp->rehash = 0;
+ hp->hash = 0;
+ hp->get_rr_peer = ngx_http_upstream_get_round_robin_peer;
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
+{
+ ngx_http_upstream_hash_peer_data_t *hp = data;
+
+ time_t now;
+ u_char buf[NGX_INT_T_LEN];
+ size_t size;
+ uint32_t hash;
+ ngx_int_t w;
+ uintptr_t m;
+ ngx_uint_t i, n, p;
+ ngx_http_upstream_rr_peer_t *peer;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+ "get hash peer, try: %ui", pc->tries);
+
+ if (hp->tries > 20 || hp->rrp.peers->single) {
+ return hp->get_rr_peer(pc, &hp->rrp);
+ }
+
+ now = ngx_time();
+
+ pc->cached = 0;
+ pc->connection = NULL;
+
+ for ( ;; ) {
+
+ /*
+ * Hash expression is compatible with Cache::Memcached:
+ * ((crc32([REHASH] KEY) >> 16) & 0x7fff) + PREV_HASH
+ * with REHASH omitted at the first iteration.
+ */
+
+ ngx_crc32_init(hash);
+
+ if (hp->rehash > 0) {
+ size = ngx_sprintf(buf, "%ui", hp->rehash) - buf;
+ ngx_crc32_update(&hash, buf, size);
+ }
+
+ ngx_crc32_update(&hash, hp->key.data, hp->key.len);
+ ngx_crc32_final(hash);
+
+ hash = (hash >> 16) & 0x7fff;
+
+ hp->hash += hash;
+ hp->rehash++;
+
+ if (!hp->rrp.peers->weighted) {
+ p = hp->hash % hp->rrp.peers->number;
+
+ } 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;
+ if (w < 0) {
+ break;
+ }
+ }
+
+ p = i;
+ }
+
+ n = p / (8 * sizeof(uintptr_t));
+ m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
+
+ if (hp->rrp.tried[n] & m) {
+ goto next;
+ }
+
+ 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) {
More information about the nginx-devel
mailing list