[nginx] Upstream: the "zone" directive.

Ruslan Ermilov ru at nginx.com
Thu Apr 16 12:23:20 UTC 2015


details:   http://hg.nginx.org/nginx/rev/79ddb0bdb273
branches:  
changeset: 6103:79ddb0bdb273
user:      Ruslan Ermilov <ru at nginx.com>
date:      Tue Apr 14 19:01:25 2015 +0300
description:
Upstream: the "zone" directive.

Upstreams with the "zone" directive are kept in shared memory,
with a consistent view of all worker processes.

diffstat:

 auto/modules                                     |    6 +
 auto/options                                     |    4 +
 auto/sources                                     |    5 +
 src/core/ngx_cycle.c                             |    4 +-
 src/core/ngx_cycle.h                             |    1 +
 src/core/ngx_rwlock.c                            |    8 +
 src/http/modules/ngx_http_upstream_zone_module.c |  213 +++++++++++++++++++++++
 src/http/ngx_http_upstream.h                     |    4 +
 src/http/ngx_http_upstream_round_robin.c         |  120 ++++++++++++-
 src/http/ngx_http_upstream_round_robin.h         |   49 +++++-
 10 files changed, 407 insertions(+), 7 deletions(-)

diffs (truncated from 560 to 300 lines):

diff -r 3264b7828f72 -r 79ddb0bdb273 auto/modules
--- a/auto/modules	Tue Apr 14 19:01:23 2015 +0300
+++ b/auto/modules	Tue Apr 14 19:01:25 2015 +0300
@@ -391,6 +391,12 @@ if [ $HTTP_UPSTREAM_KEEPALIVE = YES ]; t
     HTTP_SRCS="$HTTP_SRCS $HTTP_UPSTREAM_KEEPALIVE_SRCS"
 fi
 
+if [ $HTTP_UPSTREAM_ZONE = YES ]; then
+    have=NGX_HTTP_UPSTREAM_ZONE . auto/have
+    HTTP_MODULES="$HTTP_MODULES $HTTP_UPSTREAM_ZONE_MODULE"
+    HTTP_SRCS="$HTTP_SRCS $HTTP_UPSTREAM_ZONE_SRCS"
+fi
+
 if [ $HTTP_STUB_STATUS = YES ]; then
     have=NGX_STAT_STUB . auto/have
     HTTP_MODULES="$HTTP_MODULES ngx_http_stub_status_module"
diff -r 3264b7828f72 -r 79ddb0bdb273 auto/options
--- a/auto/options	Tue Apr 14 19:01:23 2015 +0300
+++ b/auto/options	Tue Apr 14 19:01:25 2015 +0300
@@ -103,6 +103,7 @@ HTTP_UPSTREAM_HASH=YES
 HTTP_UPSTREAM_IP_HASH=YES
 HTTP_UPSTREAM_LEAST_CONN=YES
 HTTP_UPSTREAM_KEEPALIVE=YES
+HTTP_UPSTREAM_ZONE=YES
 
 # STUB
 HTTP_STUB_STATUS=NO
@@ -256,6 +257,7 @@ use the \"--without-http_limit_conn_modu
         --without-http_upstream_least_conn_module)
                                          HTTP_UPSTREAM_LEAST_CONN=NO ;;
         --without-http_upstream_keepalive_module) HTTP_UPSTREAM_KEEPALIVE=NO ;;
+        --without-http_upstream_zone_module) HTTP_UPSTREAM_ZONE=NO  ;;
 
         --with-http_perl_module)         HTTP_PERL=YES              ;;
         --with-perl_modules_path=*)      NGX_PERL_MODULES="$value"  ;;
@@ -406,6 +408,8 @@ cat << END
                                      disable ngx_http_upstream_least_conn_module
   --without-http_upstream_keepalive_module
                                      disable ngx_http_upstream_keepalive_module
+  --without-http_upstream_zone_module
+                                     disable ngx_http_upstream_zone_module
 
   --with-http_perl_module            enable ngx_http_perl_module
   --with-perl_modules_path=PATH      set Perl modules path
diff -r 3264b7828f72 -r 79ddb0bdb273 auto/sources
--- a/auto/sources	Tue Apr 14 19:01:23 2015 +0300
+++ b/auto/sources	Tue Apr 14 19:01:25 2015 +0300
@@ -513,6 +513,11 @@ HTTP_UPSTREAM_KEEPALIVE_SRCS=" \
     src/http/modules/ngx_http_upstream_keepalive_module.c"
 
 
+HTTP_UPSTREAM_ZONE_MODULE=ngx_http_upstream_zone_module
+HTTP_UPSTREAM_ZONE_SRCS=" \
+    src/http/modules/ngx_http_upstream_zone_module.c"
+
+
 MAIL_INCS="src/mail"
 
 MAIL_DEPS="src/mail/ngx_mail.h"
diff -r 3264b7828f72 -r 79ddb0bdb273 src/core/ngx_cycle.c
--- a/src/core/ngx_cycle.c	Tue Apr 14 19:01:23 2015 +0300
+++ b/src/core/ngx_cycle.c	Tue Apr 14 19:01:25 2015 +0300
@@ -441,7 +441,8 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
             }
 
             if (shm_zone[i].tag == oshm_zone[n].tag
-                && shm_zone[i].shm.size == oshm_zone[n].shm.size)
+                && shm_zone[i].shm.size == oshm_zone[n].shm.size
+                && !shm_zone[i].noreuse)
             {
                 shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
 
@@ -1234,6 +1235,7 @@ ngx_shared_memory_add(ngx_conf_t *cf, ng
     shm_zone->shm.exists = 0;
     shm_zone->init = NULL;
     shm_zone->tag = tag;
+    shm_zone->noreuse = 0;
 
     return shm_zone;
 }
diff -r 3264b7828f72 -r 79ddb0bdb273 src/core/ngx_cycle.h
--- a/src/core/ngx_cycle.h	Tue Apr 14 19:01:23 2015 +0300
+++ b/src/core/ngx_cycle.h	Tue Apr 14 19:01:25 2015 +0300
@@ -31,6 +31,7 @@ struct ngx_shm_zone_s {
     ngx_shm_t                 shm;
     ngx_shm_zone_init_pt      init;
     void                     *tag;
+    ngx_uint_t                noreuse;  /* unsigned  noreuse:1; */
 };
 
 
diff -r 3264b7828f72 -r 79ddb0bdb273 src/core/ngx_rwlock.c
--- a/src/core/ngx_rwlock.c	Tue Apr 14 19:01:23 2015 +0300
+++ b/src/core/ngx_rwlock.c	Tue Apr 14 19:01:25 2015 +0300
@@ -109,4 +109,12 @@ ngx_rwlock_unlock(ngx_atomic_t *lock)
 }
 
 
+#else
+
+#if (NGX_HTTP_UPSTREAM_ZONE)
+
+#error ngx_atomic_cmp_set() is not defined!
+
 #endif
+
+#endif
diff -r 3264b7828f72 -r 79ddb0bdb273 src/http/modules/ngx_http_upstream_zone_module.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/http/modules/ngx_http_upstream_zone_module.c	Tue Apr 14 19:01:25 2015 +0300
@@ -0,0 +1,213 @@
+
+/*
+ * Copyright (C) Ruslan Ermilov
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+
+static char *ngx_http_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
+static ngx_int_t ngx_http_upstream_init_zone(ngx_shm_zone_t *shm_zone,
+    void *data);
+
+
+static ngx_command_t  ngx_http_upstream_zone_commands[] = {
+
+    { ngx_string("zone"),
+      NGX_HTTP_UPS_CONF|NGX_CONF_TAKE2,
+      ngx_http_upstream_zone,
+      0,
+      0,
+      NULL },
+
+      ngx_null_command
+};
+
+
+static ngx_http_module_t  ngx_http_upstream_zone_module_ctx = {
+    NULL,                                  /* preconfiguration */
+    NULL,                                  /* postconfiguration */
+
+    NULL,                                  /* create main configuration */
+    NULL,                                  /* init main configuration */
+
+    NULL,                                  /* create server configuration */
+    NULL,                                  /* merge server configuration */
+
+    NULL,                                  /* create location configuration */
+    NULL                                   /* merge location configuration */
+};
+
+
+ngx_module_t  ngx_http_upstream_zone_module = {
+    NGX_MODULE_V1,
+    &ngx_http_upstream_zone_module_ctx,    /* module context */
+    ngx_http_upstream_zone_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 char *
+ngx_http_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+    ngx_http_upstream_srv_conf_t  *uscf;
+    ssize_t                        size;
+    ngx_str_t                     *value;
+
+    uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
+
+    value = cf->args->elts;
+
+    if (!value[1].len) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "invalid zone name \"%V\"", &value[1]);
+        return NGX_CONF_ERROR;
+    }
+
+    size = ngx_parse_size(&value[2]);
+
+    if (size == NGX_ERROR) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "invalid zone size \"%V\"", &value[2]);
+        return NGX_CONF_ERROR;
+    }
+
+    if (size < (ssize_t) (8 * ngx_pagesize)) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "zone \"%V\" is too small", &value[1]);
+        return NGX_CONF_ERROR;
+    }
+
+    uscf->shm_zone = ngx_shared_memory_add(cf, &value[1], size,
+                                           &ngx_http_upstream_module);
+    if (uscf->shm_zone == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    if (uscf->shm_zone->data) {
+        uscf = uscf->shm_zone->data;
+
+        ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
+                           "upstream \"%V\" in %s:%ui "
+                           "is already bound to zone \"%V\"",
+                           &uscf->host, uscf->file_name, uscf->line,
+                           &value[1]);
+        return NGX_CONF_ERROR;
+    }
+
+    uscf->shm_zone->init = ngx_http_upstream_init_zone;
+    uscf->shm_zone->data = uscf;
+
+    uscf->shm_zone->noreuse = 1;
+
+    return NGX_CONF_OK;
+}
+
+
+static ngx_int_t
+ngx_http_upstream_init_zone(ngx_shm_zone_t *shm_zone, void *data)
+{
+    ngx_http_upstream_srv_conf_t  *ouscf = data;
+
+    size_t                          len;
+    ngx_slab_pool_t                *shpool;
+    ngx_http_upstream_rr_peer_t    *peer, **peerp;
+    ngx_http_upstream_rr_peers_t   *peers, *backup;
+    ngx_http_upstream_srv_conf_t   *uscf;
+
+    uscf = shm_zone->data;
+
+    if (ouscf) {
+        ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
+                      "zone \"%V\" cannot be reused", &shm_zone->shm.name);
+        return NGX_ERROR;
+    }
+
+    shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
+
+    if (shm_zone->shm.exists) {
+        return NGX_ERROR;
+    }
+
+
+    /* copy peers to shared memory */
+
+    len = sizeof(" in upstream zone \"\"") + shm_zone->shm.name.len;
+
+    shpool->log_ctx = ngx_slab_alloc(shpool, len);
+    if (shpool->log_ctx == NULL) {
+        return NGX_ERROR;
+    }
+
+    ngx_sprintf(shpool->log_ctx, " in upstream zone \"%V\"%Z",
+                &shm_zone->shm.name);
+
+    peers = ngx_slab_alloc(shpool, sizeof(ngx_http_upstream_rr_peers_t));
+    if (peers == NULL) {
+        return NGX_ERROR;
+    }
+
+    ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_http_upstream_rr_peers_t));
+
+    peers->shpool = shpool;
+
+    for (peerp = &peers->peer; *peerp; peerp = &peer->next) {
+        /* pool is unlocked */
+        peer = ngx_slab_calloc_locked(shpool,
+                                      sizeof(ngx_http_upstream_rr_peer_t));
+        if (peer == NULL) {
+            return NGX_ERROR;
+        }
+
+        ngx_memcpy(peer, *peerp, sizeof(ngx_http_upstream_rr_peer_t));
+
+        *peerp = peer;
+    }
+
+    if (peers->next == NULL) {
+        goto done;
+    }
+
+    backup = ngx_slab_alloc(shpool, sizeof(ngx_http_upstream_rr_peers_t));
+    if (backup == NULL) {
+        return NGX_ERROR;
+    }
+
+    ngx_memcpy(backup, peers->next, sizeof(ngx_http_upstream_rr_peers_t));



More information about the nginx-devel mailing list