From 35e6cd74eca1247762ffc4babe0d1e119b8e7c09 Mon Sep 17 00:00:00 2001 From: splitice Date: Thu, 4 Feb 2016 14:40:24 +1100 Subject: [PATCH 01/31] Initial Commit of balancer_by_lua_* port --- config | 2 + src/ngx_stream_lua_balancer.c | 595 ++++++++++++++++++++++++++++++++ src/ngx_stream_lua_balancer.h | 27 ++ src/ngx_stream_lua_common.h | 24 ++ src/ngx_stream_lua_module.c | 15 + src/ngx_stream_lua_output.c | 4 + src/ngx_stream_lua_sleep.c | 4 + src/ngx_stream_lua_socket_tcp.c | 4 + src/ngx_stream_lua_socket_udp.c | 4 + 9 files changed, 679 insertions(+) create mode 100644 src/ngx_stream_lua_balancer.c create mode 100644 src/ngx_stream_lua_balancer.h diff --git a/config b/config index 6c309226..67485c34 100644 --- a/config +++ b/config @@ -335,6 +335,7 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \ $ngx_addon_dir/src/ngx_stream_lua_misc.c \ $ngx_addon_dir/src/ngx_stream_lua_initby.c \ $ngx_addon_dir/src/ngx_stream_lua_initworkerby.c \ + $ngx_addon_dir/src/ngx_stream_lua_balancer.c \ " NGX_ADDON_DEPS="$NGX_ADDON_DEPS \ @@ -371,6 +372,7 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \ $ngx_addon_dir/src/ngx_stream_lua_misc.h \ $ngx_addon_dir/src/ngx_stream_lua_initby.h \ $ngx_addon_dir/src/ngx_stream_lua_initworkerby.h \ + $ngx_addon_dir/src/ngx_stream_lua_balancer.h \ " ngx_feature="export symbols by default (-E)" diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c new file mode 100644 index 00000000..a0c09b28 --- /dev/null +++ b/src/ngx_stream_lua_balancer.c @@ -0,0 +1,595 @@ + +/* + * Copyright (C) Yichun Zhang (agentzh) + */ + + +#ifndef DDEBUG +#define DDEBUG 0 +#endif +#include "ddebug.h" + + +#include "ngx_stream_lua_cache.h" +#include "ngx_stream_lua_balancer.h" +#include "ngx_stream_lua_util.h" +#include "ngx_stream_lua_directive.h" + + +struct ngx_stream_lua_balancer_peer_data_s { + /* the round robin data must be first */ + ngx_stream_upstream_rr_peer_data_t rrp; + + ngx_stream_lua_srv_conf_t *conf; + ngx_stream_session_t *session; + + ngx_event_get_peer_pt get_rr_peer; + + ngx_uint_t more_tries; + ngx_uint_t total_tries; + + struct sockaddr *sockaddr; + socklen_t socklen; + + ngx_str_t host; + in_port_t port; + + int last_peer_state; +}; + + +static ngx_int_t ngx_stream_lua_balancer_init(ngx_conf_t *cf, + ngx_stream_upstream_srv_conf_t *us); +static ngx_int_t ngx_stream_lua_balancer_init_peer(ngx_stream_session_t *r, + ngx_stream_upstream_srv_conf_t *us); +static ngx_int_t ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, + void *data); +static ngx_int_t ngx_stream_lua_balancer_by_chunk(lua_State *L, + ngx_stream_session_t *r); +void ngx_stream_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data, + ngx_uint_t state); + + +ngx_int_t +ngx_stream_lua_balancer_handler_file(ngx_stream_session_t *r, + ngx_stream_lua_srv_conf_t *lscf, lua_State *L) +{ + ngx_int_t rc; + + rc = ngx_stream_lua_cache_loadfile(r->connection->log, L, + lscf->balancer.src.data, + lscf->balancer.src_key); + if (rc != NGX_OK) { + return rc; + } + + /* make sure we have a valid code chunk */ + ngx_stream_lua_assert(lua_isfunction(L, -1)); + + return ngx_stream_lua_balancer_by_chunk(L, r); +} + + +ngx_int_t +ngx_stream_lua_balancer_handler_inline(ngx_stream_session_t *r, + ngx_stream_lua_srv_conf_t *lscf, lua_State *L) +{ + ngx_int_t rc; + + rc = ngx_stream_lua_cache_loadbuffer(r->connection->log, L, + lscf->balancer.src.data, + lscf->balancer.src.len, + lscf->balancer.src_key, + "=balancer_by_lua"); + if (rc != NGX_OK) { + return rc; + } + + /* make sure we have a valid code chunk */ + ngx_stream_lua_assert(lua_isfunction(L, -1)); + + return ngx_stream_lua_balancer_by_chunk(L, r); +} + + +char * +ngx_stream_lua_balancer_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf) +{ + char *rv; + ngx_conf_t save; + + save = *cf; + cf->handler = ngx_stream_lua_balancer_by_lua; + cf->handler_conf = conf; + + rv = ngx_stream_lua_conf_lua_block_parse(cf, cmd); + + *cf = save; + + return rv; +} + + +char * +ngx_stream_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf) +{ + u_char *p; + u_char *name; + ngx_str_t *value; + ngx_stream_lua_srv_conf_t *lscf = conf; + + ngx_stream_upstream_srv_conf_t *uscf; + + dd("enter"); + + /* must specifiy a content handler */ + if (cmd->post == NULL) { + return NGX_CONF_ERROR; + } + + if (lscf->balancer.handler) { + return "is duplicate"; + } + + value = cf->args->elts; + + lscf->balancer.handler = (ngx_stream_lua_srv_conf_handler_pt) cmd->post; + + if (cmd->post == ngx_stream_lua_balancer_handler_file) { + /* Lua code in an external file */ + + name = ngx_stream_lua_rebase_path(cf->pool, value[1].data, + value[1].len); + if (name == NULL) { + return NGX_CONF_ERROR; + } + + lscf->balancer.src.data = name; + lscf->balancer.src.len = ngx_strlen(name); + + p = ngx_palloc(cf->pool, NGX_STREAM_LUA_FILE_KEY_LEN + 1); + if (p == NULL) { + return NGX_CONF_ERROR; + } + + lscf->balancer.src_key = p; + + p = ngx_copy(p, NGX_STREAM_LUA_FILE_TAG, NGX_STREAM_LUA_FILE_TAG_LEN); + p = ngx_stream_lua_digest_hex(p, value[1].data, value[1].len); + *p = '\0'; + + } else { + /* inlined Lua code */ + + lscf->balancer.src = value[1]; + + p = ngx_palloc(cf->pool, NGX_STREAM_LUA_INLINE_KEY_LEN + 1); + if (p == NULL) { + return NGX_CONF_ERROR; + } + + lscf->balancer.src_key = p; + + p = ngx_copy(p, NGX_STREAM_LUA_INLINE_TAG, NGX_STREAM_LUA_INLINE_TAG_LEN); + p = ngx_stream_lua_digest_hex(p, value[1].data, value[1].len); + *p = '\0'; + } + + uscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_upstream_module); + + if (uscf->peer.init_upstream) { + ngx_conf_log_error(NGX_LOG_WARN, cf, 0, + "load balancing method redefined"); + } + + uscf->peer.init_upstream = ngx_stream_lua_balancer_init; + + uscf->flags = NGX_STREAM_UPSTREAM_CREATE + |NGX_STREAM_UPSTREAM_WEIGHT + |NGX_STREAM_UPSTREAM_MAX_FAILS + |NGX_STREAM_UPSTREAM_FAIL_TIMEOUT + |NGX_STREAM_UPSTREAM_DOWN; + + return NGX_CONF_OK; +} + + +static ngx_int_t +ngx_stream_lua_balancer_init(ngx_conf_t *cf, + ngx_stream_upstream_srv_conf_t *us) +{ + if (ngx_stream_upstream_init_round_robin(cf, us) != NGX_OK) { + return NGX_ERROR; + } + + /* this callback is called upon individual sessions */ + us->peer.init = ngx_stream_lua_balancer_init_peer; + + return NGX_OK; +} + + +static ngx_int_t +ngx_stream_lua_balancer_init_peer(ngx_stream_session_t *r, + ngx_stream_upstream_srv_conf_t *us) +{ + ngx_stream_lua_srv_conf_t *bcf; + ngx_stream_lua_balancer_peer_data_t *bp; + + bp = ngx_pcalloc(r->connection->pool, sizeof(ngx_stream_lua_balancer_peer_data_t)); + if (bp == NULL) { + return NGX_ERROR; + } + + r->upstream->peer.data = &bp->rrp; + + if (ngx_stream_upstream_init_round_robin_peer(r, us) != NGX_OK) { + return NGX_ERROR; + } + + r->upstream->peer.get = ngx_stream_lua_balancer_get_peer; + r->upstream->peer.free = ngx_stream_lua_balancer_free_peer; + + bcf = ngx_stream_conf_upstream_srv_conf(us, ngx_stream_lua_module); + + bp->conf = bcf; + bp->get_rr_peer = ngx_stream_upstream_get_round_robin_peer; + bp->session = r; + + return NGX_OK; +} + + +static ngx_int_t +ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) +{ + lua_State *L; + ngx_int_t rc; + ngx_stream_session_t *r; + ngx_stream_lua_ctx_t *ctx; + ngx_stream_lua_srv_conf_t *lscf; + ngx_stream_lua_main_conf_t *lmcf; + ngx_stream_lua_balancer_peer_data_t *bp = data; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, + "lua balancer peer, try: %ui", pc->tries); + + lscf = bp->conf; + + r = bp->session; + + ngx_stream_lua_assert(lscf->balancer.handler && r); + + ctx = ngx_stream_get_module_ctx(r, ngx_stream_lua_module); + + if (ctx == NULL) { + ctx = ngx_stream_lua_create_ctx(r); + if (ctx == NULL) { + return NGX_ERROR; + } + + L = ngx_stream_lua_get_lua_vm(r, ctx); + + } else { + L = ngx_stream_lua_get_lua_vm(r, ctx); + + dd("reset ctx"); + ngx_stream_lua_reset_ctx(r, L, ctx); + } + + ctx->context = NGX_STREAM_LUA_CONTEXT_BALANCER; + + bp->sockaddr = NULL; + bp->socklen = 0; + bp->more_tries = 0; + bp->total_tries++; + + lmcf = ngx_stream_get_module_main_conf(r, ngx_stream_lua_module); + + /* balancer_by_lua does not support yielding and + * there cannot be any conflicts among concurrent sessions, + * thus it is safe to store the peer data in the main conf. + */ + lmcf->balancer_peer_data = bp; + + rc = lscf->balancer.handler(r->connection->log, lmcf, L); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (ctx->exited && ctx->exit_code != NGX_OK) { + rc = ctx->exit_code; + if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { + return rc; + } + + if (rc > NGX_OK) { + return NGX_ERROR; + } + } + + if (bp->sockaddr && bp->socklen) { + pc->sockaddr = bp->sockaddr; + pc->socklen = bp->socklen; + pc->name = &bp->host; + bp->rrp.peers->single = 0; + + if (bp->more_tries) { + r->upstream->peer.tries += bp->more_tries; + } + + dd("tries: %d", (int) r->upstream->peer.tries); + + return NGX_OK; + } + + return bp->get_rr_peer(pc, &bp->rrp); +} + + +static ngx_int_t +ngx_stream_lua_balancer_by_chunk(lua_State *L, ngx_stream_session_t *r) +{ + u_char *err_msg; + size_t len; + ngx_int_t rc; + + /* init nginx context in Lua VM */ + //ngx_stream_lua_set_req(L, r); + ngx_stream_lua_create_new_globals_table(L, 0 /* narr */, 1 /* nrec */); + + /* {{{ make new env inheriting main thread's globals table */ + lua_createtable(L, 0, 1 /* nrec */); /* the metatable for the new env */ + ngx_stream_lua_get_globals_table(L); + lua_setfield(L, -2, "__index"); + lua_setmetatable(L, -2); /* setmetatable({}, {__index = _G}) */ + /* }}} */ + + lua_setfenv(L, -2); /* set new running env for the code closure */ + + lua_pushcfunction(L, ngx_stream_lua_traceback); + lua_insert(L, 1); /* put it under chunk and args */ + + /* protected call user code */ + rc = lua_pcall(L, 0, 1, 1); + + lua_remove(L, 1); /* remove traceback function */ + + dd("rc == %d", (int) rc); + + if (rc != 0) { + /* error occured when running loaded code */ + err_msg = (u_char *) lua_tolstring(L, -1, &len); + + if (err_msg == NULL) { + err_msg = (u_char *) "unknown reason"; + len = sizeof("unknown reason") - 1; + } + + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "failed to run balancer_by_lua*: %*s", len, err_msg); + + lua_settop(L, 0); /* clear remaining elems on stack */ + + return NGX_ERROR; + } + + lua_settop(L, 0); /* clear remaining elems on stack */ + return rc; +} + + +void +ngx_stream_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data, + ngx_uint_t state) +{ + ngx_stream_lua_balancer_peer_data_t *bp = data; + + if (bp->sockaddr && bp->socklen) { + bp->last_peer_state = (int) state; + + if (pc->tries) { + pc->tries--; + } + + return; + } + + /* fallback */ + + ngx_stream_upstream_free_round_robin_peer(pc, data, state); +} + + +#ifndef NGX_LUA_NO_FFI_API + +int +ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *r, + const u_char *addr, size_t addr_len, int port, char **err) +{ + ngx_url_t url; + ngx_stream_lua_ctx_t *ctx; + ngx_stream_upstream_t *u; + + ngx_stream_lua_main_conf_t *lmcf; + ngx_stream_lua_balancer_peer_data_t *bp; + + if (r == NULL) { + *err = "no session found"; + return NGX_ERROR; + } + + u = r->upstream; + + if (u == NULL) { + *err = "no upstream found"; + return NGX_ERROR; + } + + ctx = ngx_stream_get_module_ctx(r, ngx_stream_lua_module); + if (ctx == NULL) { + *err = "no ctx found"; + return NGX_ERROR; + } + + if ((ctx->context & NGX_STREAM_LUA_CONTEXT_BALANCER) == 0) { + *err = "API disabled in the current context"; + return NGX_ERROR; + } + + lmcf = ngx_stream_get_module_main_conf(r, ngx_stream_lua_module); + + /* we cannot read r->upstream->peer.data here directly because + * it could be overridden by other modules like + * ngx_stream_upstream_keepalive_module. + */ + bp = lmcf->balancer_peer_data; + if (bp == NULL) { + *err = "no upstream peer data found"; + return NGX_ERROR; + } + + ngx_memzero(&url, sizeof(ngx_url_t)); + + url.url.data = ngx_palloc(r->connection->pool, addr_len); + if (url.url.data == NULL) { + *err = "no memory"; + return NGX_ERROR; + } + + ngx_memcpy(url.url.data, addr, addr_len); + + url.url.len = addr_len; + url.default_port = (in_port_t) port; + url.uri_part = 0; + url.no_resolve = 1; + + if (ngx_parse_url(r->connection->pool, &url) != NGX_OK) { + if (url.err) { + *err = url.err; + } + + return NGX_ERROR; + } + + if (url.addrs && url.addrs[0].sockaddr) { + bp->sockaddr = url.addrs[0].sockaddr; + bp->socklen = url.addrs[0].socklen; + bp->host = url.addrs[0].name; + + } else { + *err = "no host allowed"; + return NGX_ERROR; + } + + return NGX_OK; +} + + +int +ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *r, + int count, char **err) +{ + ngx_stream_lua_ctx_t *ctx; + ngx_stream_upstream_t *u; + + ngx_stream_lua_main_conf_t *lmcf; + ngx_stream_lua_balancer_peer_data_t *bp; + + if (r == NULL) { + *err = "no session found"; + return NGX_ERROR; + } + + u = r->upstream; + + if (u == NULL) { + *err = "no upstream found"; + return NGX_ERROR; + } + + ctx = ngx_stream_get_module_ctx(r, ngx_stream_lua_module); + if (ctx == NULL) { + *err = "no ctx found"; + return NGX_ERROR; + } + + if ((ctx->context & NGX_STREAM_LUA_CONTEXT_BALANCER) == 0) { + *err = "API disabled in the current context"; + return NGX_ERROR; + } + + lmcf = ngx_stream_get_module_main_conf(r, ngx_stream_lua_module); + + bp = lmcf->balancer_peer_data; + if (bp == NULL) { + *err = "no upstream peer data found"; + return NGX_ERROR; + } + + + *err = NULL; + + bp->more_tries = count; + return NGX_OK; +} + + +int +ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *r, + int *status, char **err) +{ + /* NOT PORTED: IS IT POSSIBLE? */ + ngx_stream_lua_ctx_t *ctx; + ngx_stream_upstream_t *u; + //ngx_stream_upstream_state_t *state; + + ngx_stream_lua_balancer_peer_data_t *bp; + ngx_stream_lua_main_conf_t *lmcf; + + if (r == NULL) { + *err = "no session found"; + return NGX_ERROR; + } + + u = r->upstream; + + if (u == NULL) { + *err = "no upstream found"; + return NGX_ERROR; + } + + ctx = ngx_stream_get_module_ctx(r, ngx_stream_lua_module); + if (ctx == NULL) { + *err = "no ctx found"; + return NGX_ERROR; + } + + if ((ctx->context & NGX_STREAM_LUA_CONTEXT_BALANCER) == 0) { + *err = "API disabled in the current context"; + return NGX_ERROR; + } + + lmcf = ngx_stream_get_module_main_conf(r, ngx_stream_lua_module); + + bp = lmcf->balancer_peer_data; + if (bp == NULL) { + *err = "no upstream peer data found"; + return NGX_ERROR; + } + + /*if (r->upstream_states && r->upstream_states->nelts > 1) { + state = r->upstream_states->elts; + *status = (int) state[r->upstream_states->nelts - 2].status; + + } else { + *status = 0; + }*/ + + return bp->last_peer_state; +} + +#endif /* NGX_LUA_NO_FFI_API */ diff --git a/src/ngx_stream_lua_balancer.h b/src/ngx_stream_lua_balancer.h new file mode 100644 index 00000000..d7296a0a --- /dev/null +++ b/src/ngx_stream_lua_balancer.h @@ -0,0 +1,27 @@ + +/* + * Copyright (C) Yichun Zhang (agentzh) + */ + + +#ifndef _NGX_STREAM_LUA_BALANCER_H_INCLUDED_ +#define _NGX_STREAM_LUA_BALANCER_H_INCLUDED_ + + +#include "ngx_stream_lua_common.h" + + +ngx_int_t ngx_stream_lua_balancer_handler_inline(ngx_stream_session_t *r, + ngx_stream_lua_srv_conf_t *lscf, lua_State *L); + +ngx_int_t ngx_stream_lua_balancer_handler_file(ngx_stream_session_t *r, + ngx_stream_lua_srv_conf_t *lscf, lua_State *L); + +char *ngx_stream_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf); + +char *ngx_stream_lua_balancer_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf); + + +#endif /* _NGX_HTTP_LUA_BALANCER_H_INCLUDED_ */ diff --git a/src/ngx_stream_lua_common.h b/src/ngx_stream_lua_common.h index 1662df0c..303d281a 100644 --- a/src/ngx_stream_lua_common.h +++ b/src/ngx_stream_lua_common.h @@ -49,6 +49,7 @@ #define NGX_STREAM_LUA_CONTEXT_LOG 0x002 #define NGX_STREAM_LUA_CONTEXT_TIMER 0x004 #define NGX_STREAM_LUA_CONTEXT_INIT_WORKER 0x008 +#define NGX_STREAM_LUA_CONTEXT_BALANCER 0x010 /* Nginx Stream Lua Inline tag prefix */ @@ -78,6 +79,10 @@ typedef void (*ngx_stream_lua_cleanup_pt)(void *data); typedef struct ngx_stream_lua_cleanup_s ngx_stream_lua_cleanup_t; + +typedef struct ngx_http_lua_balancer_peer_data_s + ngx_http_lua_balancer_peer_data_t; + struct ngx_stream_lua_cleanup_s { ngx_stream_lua_cleanup_pt handler; void *data; @@ -103,9 +108,14 @@ typedef struct { typedef struct ngx_stream_lua_semaphore_mm_s ngx_stream_lua_semaphore_mm_t; typedef struct ngx_stream_lua_main_conf_s ngx_stream_lua_main_conf_t; +typedef struct ngx_stream_lua_balancer_peer_data_s + ngx_stream_lua_balancer_peer_data_t; typedef ngx_int_t (*ngx_stream_lua_main_conf_handler_pt)(ngx_log_t *log, ngx_stream_lua_main_conf_t *lmcf, lua_State *L); + +typedef ngx_int_t (*ngx_stream_lua_srv_conf_handler_pt)(ngx_log_t *log, + ngx_stream_lua_main_conf_t *lmcf, lua_State *L); typedef struct { @@ -151,6 +161,12 @@ struct ngx_stream_lua_main_conf_s { ngx_uint_t shm_zones_inited; ngx_stream_lua_semaphore_mm_t *semaphore_mm; + + ngx_stream_lua_balancer_peer_data_t *balancer_peer_data; + /* balancer_by_lua does not support yielding and + * there cannot be any conflicts among concurrent requests, + * thus it is safe to store the peer data in the main conf. + */ unsigned requires_access:1; unsigned requires_shm:1; @@ -212,6 +228,14 @@ typedef struct { ngx_uint_t lingering_close; ngx_msec_t lingering_time; ngx_msec_t lingering_timeout; + + struct { + ngx_str_t src; + u_char *src_key; + + ngx_stream_lua_srv_conf_handler_pt handler; + } balancer; + } ngx_stream_lua_srv_conf_t; diff --git a/src/ngx_stream_lua_module.c b/src/ngx_stream_lua_module.c index 76a7c089..2ad5f30a 100644 --- a/src/ngx_stream_lua_module.c +++ b/src/ngx_stream_lua_module.c @@ -14,6 +14,7 @@ #include "ngx_stream_lua_common.h" #include "ngx_stream_lua_directive.h" #include "ngx_stream_lua_contentby.h" +#include "ngx_stream_lua_balancer.h" #include "ngx_stream_lua_semaphore.h" #include "ngx_stream_lua_initby.h" #include "ngx_stream_lua_initworkerby.h" @@ -103,6 +104,20 @@ static ngx_command_t ngx_stream_lua_commands[] = { NGX_STREAM_SRV_CONF_OFFSET, 0, (void *) ngx_stream_lua_content_handler_file }, + + { ngx_string("balancer_by_lua_block"), + NGX_STREAM_UPS_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, + ngx_stream_lua_balancer_by_lua_block, + NGX_STREAM_SRV_CONF_OFFSET, + 0, + (void *) ngx_stream_lua_balancer_handler_inline }, + + { ngx_string("balancer_by_lua_file"), + NGX_STREAM_UPS_CONF|NGX_CONF_TAKE1, + ngx_stream_lua_balancer_by_lua, + NGX_STREAM_SRV_CONF_OFFSET, + 0, + (void *) ngx_stream_lua_balancer_handler_file }, { ngx_string("lua_max_running_timers"), NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1, diff --git a/src/ngx_stream_lua_output.c b/src/ngx_stream_lua_output.c index e8dbd7f1..1459e905 100644 --- a/src/ngx_stream_lua_output.c +++ b/src/ngx_stream_lua_output.c @@ -555,7 +555,9 @@ ngx_stream_lua_ngx_flush(lua_State *L) static int ngx_stream_lua_ngx_eof(lua_State *L) { +#if (NGX_DEBUG) ngx_connection_t *c; +#endif ngx_stream_session_t *s; ngx_stream_lua_ctx_t *ctx; @@ -591,10 +593,12 @@ ngx_stream_lua_ngx_eof(lua_State *L) ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT); +#if (NGX_DEBUG) c = s->connection; ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, "stream lua send eof"); +#endif lua_pushinteger(L, 1); return 1; diff --git a/src/ngx_stream_lua_sleep.c b/src/ngx_stream_lua_sleep.c index ddb67bfb..33323563 100644 --- a/src/ngx_stream_lua_sleep.c +++ b/src/ngx_stream_lua_sleep.c @@ -83,7 +83,9 @@ ngx_stream_lua_ngx_sleep(lua_State *L) void ngx_stream_lua_sleep_handler(ngx_event_t *ev) { +#if (NGX_DEBUG) ngx_connection_t *c; +#endif ngx_stream_session_t *s; ngx_stream_lua_ctx_t *ctx; ngx_stream_lua_co_ctx_t *coctx; @@ -91,7 +93,9 @@ ngx_stream_lua_sleep_handler(ngx_event_t *ev) coctx = ev->data; s = coctx->data; +#if (NGX_DEBUG) c = s->connection; +#endif ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); diff --git a/src/ngx_stream_lua_socket_tcp.c b/src/ngx_stream_lua_socket_tcp.c index 06afea1d..3cb9dcd3 100644 --- a/src/ngx_stream_lua_socket_tcp.c +++ b/src/ngx_stream_lua_socket_tcp.c @@ -759,7 +759,9 @@ static void ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx) { ngx_stream_session_t *s; +#if (NGX_DEBUG) ngx_connection_t *c; +#endif ngx_stream_lua_resolved_t *ur; ngx_stream_lua_ctx_t *lctx; lua_State *L; @@ -777,7 +779,9 @@ ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx) u = ctx->data; s = u->request; +#if (NGX_DEBUG) c = s->connection; +#endif ur = u->resolved; ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, diff --git a/src/ngx_stream_lua_socket_udp.c b/src/ngx_stream_lua_socket_udp.c index 5d6c37f4..aa8254cd 100644 --- a/src/ngx_stream_lua_socket_udp.c +++ b/src/ngx_stream_lua_socket_udp.c @@ -417,7 +417,9 @@ static void ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx) { ngx_stream_session_t *s; +#if (NGX_DEBUG) ngx_connection_t *c; +#endif ngx_stream_lua_resolved_t *ur; ngx_stream_lua_ctx_t *lctx; lua_State *L; @@ -436,7 +438,9 @@ ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx) u = ctx->data; s = u->session; +#if (NGX_DEBUG) c = s->connection; +#endif ur = u->resolved; ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, From 559a9b41ba7d4cdf9e600a3ff907585e064d6f5b Mon Sep 17 00:00:00 2001 From: splitice Date: Thu, 4 Feb 2016 15:35:54 +1100 Subject: [PATCH 02/31] add balancer to FFI context and fix get_phase --- src/ngx_stream_lua_control.c | 6 ++++-- src/ngx_stream_lua_phase.c | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ngx_stream_lua_control.c b/src/ngx_stream_lua_control.c index bf1690c5..93c6f14f 100644 --- a/src/ngx_stream_lua_control.c +++ b/src/ngx_stream_lua_control.c @@ -57,7 +57,8 @@ ngx_stream_lua_ngx_exit(lua_State *L) } ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT - | NGX_STREAM_LUA_CONTEXT_TIMER); + | NGX_STREAM_LUA_CONTEXT_TIMER + | NGX_STREAM_LUA_CONTEXT_BALANCER); rc = (ngx_int_t) luaL_checkinteger(L, 1); @@ -145,7 +146,8 @@ ngx_stream_lua_ffi_exit(ngx_stream_session_t *s, int status, u_char *err, } if (ngx_stream_lua_ffi_check_context(ctx, NGX_STREAM_LUA_CONTEXT_CONTENT - | NGX_STREAM_LUA_CONTEXT_TIMER, + | NGX_STREAM_LUA_CONTEXT_TIMER + | NGX_STREAM_LUA_CONTEXT_BALANCER, err, errlen) != NGX_OK) { diff --git a/src/ngx_stream_lua_phase.c b/src/ngx_stream_lua_phase.c index 436305cf..03ff8330 100644 --- a/src/ngx_stream_lua_phase.c +++ b/src/ngx_stream_lua_phase.c @@ -57,6 +57,11 @@ ngx_stream_lua_ngx_get_phase(lua_State *L) case NGX_STREAM_LUA_CONTEXT_TIMER: lua_pushliteral(L, "timer"); break; + + + case NGX_STREAM_LUA_CONTEXT_BALANCER: + lua_pushliteral(L, "balancer"); + break; default: return luaL_error(L, "unknown phase: %d", (int) ctx->context); From 0729016ecb9d0ab19fdf6ad87fd501a36a299fd2 Mon Sep 17 00:00:00 2001 From: splitice Date: Thu, 4 Feb 2016 15:56:25 +1100 Subject: [PATCH 03/31] fix segfault due to miss-matched types --- src/ngx_stream_lua_balancer.c | 3 +-- src/ngx_stream_lua_common.h | 7 ++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index a0c09b28..271cfdfd 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -162,7 +162,6 @@ ngx_stream_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, } else { /* inlined Lua code */ - lscf->balancer.src = value[1]; p = ngx_palloc(cf->pool, NGX_STREAM_LUA_INLINE_KEY_LEN + 1); @@ -294,7 +293,7 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) */ lmcf->balancer_peer_data = bp; - rc = lscf->balancer.handler(r->connection->log, lmcf, L); + rc = lscf->balancer.handler(r->connection->log, lscf, L); if (rc == NGX_ERROR) { return NGX_ERROR; diff --git a/src/ngx_stream_lua_common.h b/src/ngx_stream_lua_common.h index 303d281a..37d57597 100644 --- a/src/ngx_stream_lua_common.h +++ b/src/ngx_stream_lua_common.h @@ -107,6 +107,7 @@ typedef struct { typedef struct ngx_stream_lua_semaphore_mm_s ngx_stream_lua_semaphore_mm_t; typedef struct ngx_stream_lua_main_conf_s ngx_stream_lua_main_conf_t; +typedef struct ngx_stream_lua_srv_conf_s ngx_stream_lua_srv_conf_t; typedef struct ngx_stream_lua_balancer_peer_data_s ngx_stream_lua_balancer_peer_data_t; @@ -115,7 +116,7 @@ typedef ngx_int_t (*ngx_stream_lua_main_conf_handler_pt)(ngx_log_t *log, ngx_stream_lua_main_conf_t *lmcf, lua_State *L); typedef ngx_int_t (*ngx_stream_lua_srv_conf_handler_pt)(ngx_log_t *log, - ngx_stream_lua_main_conf_t *lmcf, lua_State *L); + ngx_stream_lua_srv_conf_t *lscf, lua_State *L); typedef struct { @@ -182,7 +183,7 @@ typedef void (*ngx_stream_lua_event_handler_pt)(ngx_stream_session_t *s, ngx_stream_lua_ctx_t *ctx); -typedef struct { +struct ngx_stream_lua_srv_conf_s { #if (NGX_STREAM_SSL) ngx_ssl_t *ssl; /* shared by SSL cosockets */ @@ -237,7 +238,7 @@ typedef struct { } balancer; -} ngx_stream_lua_srv_conf_t; +}; enum { From 14a545c0678147eec50298076a2c02ddd346747b Mon Sep 17 00:00:00 2001 From: splitice Date: Thu, 4 Feb 2016 16:30:10 +1100 Subject: [PATCH 04/31] first part of porting tests, the configuration --- t/138-balancer.t | 240 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 t/138-balancer.t diff --git a/t/138-balancer.t b/t/138-balancer.t new file mode 100644 index 00000000..0e6580cb --- /dev/null +++ b/t/138-balancer.t @@ -0,0 +1,240 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: + +use Test::Nginx::Socket::Lua; + +#worker_connections(1014); +#master_on(); +#workers(2); +#log_level('warn'); + +repeat_each(2); + +plan tests => repeat_each() * (blocks() * 4 + 8); + +#no_diff(); +no_long_string(); +run_tests(); + +__DATA__ + +=== TEST 1: simple logging +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + print("hello from balancer by lua!") + } + } +--- config + proxy_pass backend; +--- stream_response +--- error_log eval +[ +'[lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream,', +qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"}, +] +--- no_error_log +[warn] + + + +=== TEST 2: exit 403 +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + print("hello from balancer by lua!") + ngx.exit(403) + } + } +--- config + proxy_pass backend; +--- stream_response +--- error_log +[lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream, +--- no_error_log eval +[ +'[warn]', +qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"}, +] + + + +=== TEST 3: exit OK +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + print("hello from balancer by lua!") + ngx.exit(ngx.OK) + } + } +--- config + proxy_pass backend; +--- stream_response +--- error_log eval +[ +'[lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream,', +qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"}, +] +--- no_error_log +[warn] + + + +=== TEST 4: ngx.var works +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + print("1: variable foo = ", ngx.var.foo) + ngx.var.foo = tonumber(ngx.var.foo) + 1 + print("2: variable foo = ", ngx.var.foo) + } + } +--- config + set $foo 32; + proxy_pass backend; +--- stream_response +--- error_log eval +[ +"1: variable foo = 32", +"2: variable foo = 33", +qr/\[crit\] .* connect\(\) .*? failed/, +] +--- no_error_log +[warn] + + + +=== TEST 5: ngx.req.get_headers works +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + print("header foo: ", ngx.req.get_headers()["foo"]) + } + } +--- config + proxy_pass backend; +--- stream_response +--- error_log eval +[ +"header foo: bar", +qr/\[crit\] .* connect\(\) .*? failed/, +] +--- no_error_log +[warn] + + + +=== TEST 6: ngx.req.get_uri_args() works +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + print("arg foo: ", ngx.req.get_uri_args()["foo"]) + } + } +--- config + proxy_pass backend; +--- stream_response +--- error_log eval +["arg foo: bar", +qr/\[crit\] .* connect\(\) .*? failed/, +] +--- no_error_log +[warn] + + + +=== TEST 7: ngx.req.get_method() works +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + print("method: ", ngx.req.get_method()) + } + } +--- config + proxy_pass backend; +--- stream_response +--- error_log eval +[ +"method: GET", +qr/\[crit\] .* connect\(\) .*? failed/, +] +--- no_error_log +[warn] + + + +=== TEST 8: simple logging (by_lua_file) +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_file html/a.lua; + } +--- config + proxy_pass backend; +--- stream_response +--- error_log eval +[ +'[lua] a.lua:1: hello from balancer by lua! while connecting to upstream,', +qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"}, +] +--- no_error_log +[warn] + + + +=== TEST 9: cosockets are disabled +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + local sock, err = ngx.socket.tcp() + } + } +--- config + proxy_pass backend; +--- stream_response +--- error_log eval +qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ + + + +=== TEST 10: ngx.sleep is disabled +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + ngx.sleep(0.1) + } + } +--- config + proxy_pass backend; +--- stream_response +--- error_log eval +qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ + + + +=== TEST 11: get_phase +--- stream_server_config + upstream backend { + server 0.0.0.1; + balancer_by_lua_block { + print("I am in phase ", ngx.get_phase()) + } + } +--- config + proxy_pass backend; +--- stream_response +--- grep_error_log eval: qr/I am in phase \w+/ +--- grep_error_log_out +I am in phase balancer +--- error_log eval +qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"} +--- no_error_log +[error] \ No newline at end of file From 463a8ebfc2b418d404f53373a8032c5d56a4f0c2 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 5 Feb 2016 09:39:32 +1100 Subject: [PATCH 05/31] fix log passing --- src/ngx_stream_lua_balancer.c | 20 ++++++++++---------- src/ngx_stream_lua_balancer.h | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 271cfdfd..b79cc32b 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -45,18 +45,18 @@ static ngx_int_t ngx_stream_lua_balancer_init_peer(ngx_stream_session_t *r, static ngx_int_t ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data); static ngx_int_t ngx_stream_lua_balancer_by_chunk(lua_State *L, - ngx_stream_session_t *r); + ngx_log_t *log); void ngx_stream_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state); ngx_int_t -ngx_stream_lua_balancer_handler_file(ngx_stream_session_t *r, +ngx_stream_lua_balancer_handler_file(ngx_log_t *log, ngx_stream_lua_srv_conf_t *lscf, lua_State *L) { ngx_int_t rc; - rc = ngx_stream_lua_cache_loadfile(r->connection->log, L, + rc = ngx_stream_lua_cache_loadfile(log, L, lscf->balancer.src.data, lscf->balancer.src_key); if (rc != NGX_OK) { @@ -66,17 +66,17 @@ ngx_stream_lua_balancer_handler_file(ngx_stream_session_t *r, /* make sure we have a valid code chunk */ ngx_stream_lua_assert(lua_isfunction(L, -1)); - return ngx_stream_lua_balancer_by_chunk(L, r); + return ngx_stream_lua_balancer_by_chunk(L, log); } ngx_int_t -ngx_stream_lua_balancer_handler_inline(ngx_stream_session_t *r, +ngx_stream_lua_balancer_handler_inline(ngx_log_t *log, ngx_stream_lua_srv_conf_t *lscf, lua_State *L) { ngx_int_t rc; - rc = ngx_stream_lua_cache_loadbuffer(r->connection->log, L, + rc = ngx_stream_lua_cache_loadbuffer(log, L, lscf->balancer.src.data, lscf->balancer.src.len, lscf->balancer.src_key, @@ -88,7 +88,7 @@ ngx_stream_lua_balancer_handler_inline(ngx_stream_session_t *r, /* make sure we have a valid code chunk */ ngx_stream_lua_assert(lua_isfunction(L, -1)); - return ngx_stream_lua_balancer_by_chunk(L, r); + return ngx_stream_lua_balancer_by_chunk(L, log); } @@ -293,7 +293,7 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) */ lmcf->balancer_peer_data = bp; - rc = lscf->balancer.handler(r->connection->log, lscf, L); + rc = lscf->balancer.handler(pc->log, lscf, L); if (rc == NGX_ERROR) { return NGX_ERROR; @@ -330,7 +330,7 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) static ngx_int_t -ngx_stream_lua_balancer_by_chunk(lua_State *L, ngx_stream_session_t *r) +ngx_stream_lua_balancer_by_chunk(lua_State *L, ngx_log_t *log) { u_char *err_msg; size_t len; @@ -368,7 +368,7 @@ ngx_stream_lua_balancer_by_chunk(lua_State *L, ngx_stream_session_t *r) len = sizeof("unknown reason") - 1; } - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + ngx_log_error(NGX_LOG_ERR, log, 0, "failed to run balancer_by_lua*: %*s", len, err_msg); lua_settop(L, 0); /* clear remaining elems on stack */ diff --git a/src/ngx_stream_lua_balancer.h b/src/ngx_stream_lua_balancer.h index d7296a0a..217f13a2 100644 --- a/src/ngx_stream_lua_balancer.h +++ b/src/ngx_stream_lua_balancer.h @@ -11,10 +11,10 @@ #include "ngx_stream_lua_common.h" -ngx_int_t ngx_stream_lua_balancer_handler_inline(ngx_stream_session_t *r, +ngx_int_t ngx_stream_lua_balancer_handler_inline(ngx_log_t *r, ngx_stream_lua_srv_conf_t *lscf, lua_State *L); -ngx_int_t ngx_stream_lua_balancer_handler_file(ngx_stream_session_t *r, +ngx_int_t ngx_stream_lua_balancer_handler_file(ngx_log_t *r, ngx_stream_lua_srv_conf_t *lscf, lua_State *L); char *ngx_stream_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, From 485ec806241f5552cb52b940eea846407f46a267 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 5 Feb 2016 10:19:03 +1100 Subject: [PATCH 06/31] pass the stream session correctly --- src/ngx_stream_lua_balancer.c | 16 ++++++++-------- src/ngx_stream_lua_balancer.h | 4 ++-- src/ngx_stream_lua_common.h | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index b79cc32b..d977519c 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -45,13 +45,13 @@ static ngx_int_t ngx_stream_lua_balancer_init_peer(ngx_stream_session_t *r, static ngx_int_t ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data); static ngx_int_t ngx_stream_lua_balancer_by_chunk(lua_State *L, - ngx_log_t *log); + ngx_log_t *log, ngx_stream_session_t *s); void ngx_stream_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state); ngx_int_t -ngx_stream_lua_balancer_handler_file(ngx_log_t *log, +ngx_stream_lua_balancer_handler_file(ngx_stream_session_t *s, ngx_log_t *log, ngx_stream_lua_srv_conf_t *lscf, lua_State *L) { ngx_int_t rc; @@ -66,12 +66,12 @@ ngx_stream_lua_balancer_handler_file(ngx_log_t *log, /* make sure we have a valid code chunk */ ngx_stream_lua_assert(lua_isfunction(L, -1)); - return ngx_stream_lua_balancer_by_chunk(L, log); + return ngx_stream_lua_balancer_by_chunk(L, log, s); } ngx_int_t -ngx_stream_lua_balancer_handler_inline(ngx_log_t *log, +ngx_stream_lua_balancer_handler_inline(ngx_stream_session_t *s, ngx_log_t *log, ngx_stream_lua_srv_conf_t *lscf, lua_State *L) { ngx_int_t rc; @@ -88,7 +88,7 @@ ngx_stream_lua_balancer_handler_inline(ngx_log_t *log, /* make sure we have a valid code chunk */ ngx_stream_lua_assert(lua_isfunction(L, -1)); - return ngx_stream_lua_balancer_by_chunk(L, log); + return ngx_stream_lua_balancer_by_chunk(L, log, s); } @@ -293,7 +293,7 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) */ lmcf->balancer_peer_data = bp; - rc = lscf->balancer.handler(pc->log, lscf, L); + rc = lscf->balancer.handler(r, pc->log, lscf, L); if (rc == NGX_ERROR) { return NGX_ERROR; @@ -330,14 +330,14 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) static ngx_int_t -ngx_stream_lua_balancer_by_chunk(lua_State *L, ngx_log_t *log) +ngx_stream_lua_balancer_by_chunk(lua_State *L, ngx_log_t *log, ngx_stream_session_t *s) { u_char *err_msg; size_t len; ngx_int_t rc; /* init nginx context in Lua VM */ - //ngx_stream_lua_set_req(L, r); + ngx_stream_lua_set_session(L, s); ngx_stream_lua_create_new_globals_table(L, 0 /* narr */, 1 /* nrec */); /* {{{ make new env inheriting main thread's globals table */ diff --git a/src/ngx_stream_lua_balancer.h b/src/ngx_stream_lua_balancer.h index 217f13a2..df98289a 100644 --- a/src/ngx_stream_lua_balancer.h +++ b/src/ngx_stream_lua_balancer.h @@ -11,10 +11,10 @@ #include "ngx_stream_lua_common.h" -ngx_int_t ngx_stream_lua_balancer_handler_inline(ngx_log_t *r, +ngx_int_t ngx_stream_lua_balancer_handler_inline(ngx_stream_session_t *s, ngx_log_t *r, ngx_stream_lua_srv_conf_t *lscf, lua_State *L); -ngx_int_t ngx_stream_lua_balancer_handler_file(ngx_log_t *r, +ngx_int_t ngx_stream_lua_balancer_handler_file(ngx_stream_session_t *s, ngx_log_t *r, ngx_stream_lua_srv_conf_t *lscf, lua_State *L); char *ngx_stream_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, diff --git a/src/ngx_stream_lua_common.h b/src/ngx_stream_lua_common.h index 37d57597..237b4d59 100644 --- a/src/ngx_stream_lua_common.h +++ b/src/ngx_stream_lua_common.h @@ -115,7 +115,7 @@ typedef struct ngx_stream_lua_balancer_peer_data_s typedef ngx_int_t (*ngx_stream_lua_main_conf_handler_pt)(ngx_log_t *log, ngx_stream_lua_main_conf_t *lmcf, lua_State *L); -typedef ngx_int_t (*ngx_stream_lua_srv_conf_handler_pt)(ngx_log_t *log, +typedef ngx_int_t (*ngx_stream_lua_srv_conf_handler_pt)(ngx_stream_session_t *s, ngx_log_t *log, ngx_stream_lua_srv_conf_t *lscf, lua_State *L); From a969b82a7903528965904b2c962e1c66d2ca5e2a Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 5 Feb 2016 11:51:53 +1100 Subject: [PATCH 07/31] request/session r -> s --- src/ngx_stream_lua_balancer.c | 84 +++++++++++++++++------------------ 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index d977519c..6a76c8db 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -40,7 +40,7 @@ struct ngx_stream_lua_balancer_peer_data_s { static ngx_int_t ngx_stream_lua_balancer_init(ngx_conf_t *cf, ngx_stream_upstream_srv_conf_t *us); -static ngx_int_t ngx_stream_lua_balancer_init_peer(ngx_stream_session_t *r, +static ngx_int_t ngx_stream_lua_balancer_init_peer(ngx_stream_session_t *s, ngx_stream_upstream_srv_conf_t *us); static ngx_int_t ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data); @@ -211,31 +211,31 @@ ngx_stream_lua_balancer_init(ngx_conf_t *cf, static ngx_int_t -ngx_stream_lua_balancer_init_peer(ngx_stream_session_t *r, +ngx_stream_lua_balancer_init_peer(ngx_stream_session_t *s, ngx_stream_upstream_srv_conf_t *us) { ngx_stream_lua_srv_conf_t *bcf; ngx_stream_lua_balancer_peer_data_t *bp; - bp = ngx_pcalloc(r->connection->pool, sizeof(ngx_stream_lua_balancer_peer_data_t)); + bp = ngx_pcalloc(s->connection->pool, sizeof(ngx_stream_lua_balancer_peer_data_t)); if (bp == NULL) { return NGX_ERROR; } - r->upstream->peer.data = &bp->rrp; + s->upstream->peer.data = &bp->rrp; - if (ngx_stream_upstream_init_round_robin_peer(r, us) != NGX_OK) { + if (ngx_stream_upstream_init_round_robin_peer(s, us) != NGX_OK) { return NGX_ERROR; } - r->upstream->peer.get = ngx_stream_lua_balancer_get_peer; - r->upstream->peer.free = ngx_stream_lua_balancer_free_peer; + s->upstream->peer.get = ngx_stream_lua_balancer_get_peer; + s->upstream->peer.free = ngx_stream_lua_balancer_free_peer; bcf = ngx_stream_conf_upstream_srv_conf(us, ngx_stream_lua_module); bp->conf = bcf; bp->get_rr_peer = ngx_stream_upstream_get_round_robin_peer; - bp->session = r; + bp->session = s; return NGX_OK; } @@ -246,36 +246,36 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) { lua_State *L; ngx_int_t rc; - ngx_stream_session_t *r; + ngx_stream_session_t *s; ngx_stream_lua_ctx_t *ctx; ngx_stream_lua_srv_conf_t *lscf; ngx_stream_lua_main_conf_t *lmcf; ngx_stream_lua_balancer_peer_data_t *bp = data; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, - "lua balancer peer, try: %ui", pc->tries); + "lua balancer pees, try: %ui", pc->tries); lscf = bp->conf; - r = bp->session; + s = bp->session; ngx_stream_lua_assert(lscf->balancer.handler && r); - ctx = ngx_stream_get_module_ctx(r, ngx_stream_lua_module); + ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); if (ctx == NULL) { - ctx = ngx_stream_lua_create_ctx(r); + ctx = ngx_stream_lua_create_ctx(s); if (ctx == NULL) { return NGX_ERROR; } - L = ngx_stream_lua_get_lua_vm(r, ctx); + L = ngx_stream_lua_get_lua_vm(s, ctx); } else { - L = ngx_stream_lua_get_lua_vm(r, ctx); + L = ngx_stream_lua_get_lua_vm(s, ctx); dd("reset ctx"); - ngx_stream_lua_reset_ctx(r, L, ctx); + ngx_stream_lua_reset_ctx(s, L, ctx); } ctx->context = NGX_STREAM_LUA_CONTEXT_BALANCER; @@ -285,7 +285,7 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) bp->more_tries = 0; bp->total_tries++; - lmcf = ngx_stream_get_module_main_conf(r, ngx_stream_lua_module); + lmcf = ngx_stream_get_module_main_conf(s, ngx_stream_lua_module); /* balancer_by_lua does not support yielding and * there cannot be any conflicts among concurrent sessions, @@ -293,7 +293,7 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) */ lmcf->balancer_peer_data = bp; - rc = lscf->balancer.handler(r, pc->log, lscf, L); + rc = lscf->balancer.handler(s, pc->log, lscf, L); if (rc == NGX_ERROR) { return NGX_ERROR; @@ -317,10 +317,10 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) bp->rrp.peers->single = 0; if (bp->more_tries) { - r->upstream->peer.tries += bp->more_tries; + s->upstream->peer.tries += bp->more_tries; } - dd("tries: %d", (int) r->upstream->peer.tries); + dd("tries: %d", (int) s->upstream->peer.tries); return NGX_OK; } @@ -406,8 +406,8 @@ ngx_stream_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data, #ifndef NGX_LUA_NO_FFI_API int -ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *r, - const u_char *addr, size_t addr_len, int port, char **err) +ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *s, + const u_char *adds, size_t addr_len, int port, char **err) { ngx_url_t url; ngx_stream_lua_ctx_t *ctx; @@ -416,19 +416,19 @@ ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *r, ngx_stream_lua_main_conf_t *lmcf; ngx_stream_lua_balancer_peer_data_t *bp; - if (r == NULL) { + if (s == NULL) { *err = "no session found"; return NGX_ERROR; } - u = r->upstream; + u = s->upstream; if (u == NULL) { *err = "no upstream found"; return NGX_ERROR; } - ctx = ngx_stream_get_module_ctx(r, ngx_stream_lua_module); + ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); if (ctx == NULL) { *err = "no ctx found"; return NGX_ERROR; @@ -439,9 +439,9 @@ ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *r, return NGX_ERROR; } - lmcf = ngx_stream_get_module_main_conf(r, ngx_stream_lua_module); + lmcf = ngx_stream_get_module_main_conf(s, ngx_stream_lua_module); - /* we cannot read r->upstream->peer.data here directly because + /* we cannot read s->upstream->peer.data here directly because * it could be overridden by other modules like * ngx_stream_upstream_keepalive_module. */ @@ -453,20 +453,20 @@ ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *r, ngx_memzero(&url, sizeof(ngx_url_t)); - url.url.data = ngx_palloc(r->connection->pool, addr_len); + url.url.data = ngx_palloc(s->connection->pool, addr_len); if (url.url.data == NULL) { *err = "no memory"; return NGX_ERROR; } - ngx_memcpy(url.url.data, addr, addr_len); + ngx_memcpy(url.url.data, adds, addr_len); url.url.len = addr_len; url.default_port = (in_port_t) port; url.uri_part = 0; url.no_resolve = 1; - if (ngx_parse_url(r->connection->pool, &url) != NGX_OK) { + if (ngx_parse_url(s->connection->pool, &url) != NGX_OK) { if (url.err) { *err = url.err; } @@ -489,7 +489,7 @@ ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *r, int -ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *r, +ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *s, int count, char **err) { ngx_stream_lua_ctx_t *ctx; @@ -498,19 +498,19 @@ ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *r, ngx_stream_lua_main_conf_t *lmcf; ngx_stream_lua_balancer_peer_data_t *bp; - if (r == NULL) { + if (s == NULL) { *err = "no session found"; return NGX_ERROR; } - u = r->upstream; + u = s->upstream; if (u == NULL) { *err = "no upstream found"; return NGX_ERROR; } - ctx = ngx_stream_get_module_ctx(r, ngx_stream_lua_module); + ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); if (ctx == NULL) { *err = "no ctx found"; return NGX_ERROR; @@ -521,7 +521,7 @@ ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *r, return NGX_ERROR; } - lmcf = ngx_stream_get_module_main_conf(r, ngx_stream_lua_module); + lmcf = ngx_stream_get_module_main_conf(s, ngx_stream_lua_module); bp = lmcf->balancer_peer_data; if (bp == NULL) { @@ -538,7 +538,7 @@ ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *r, int -ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *r, +ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s, int *status, char **err) { /* NOT PORTED: IS IT POSSIBLE? */ @@ -549,19 +549,19 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *r, ngx_stream_lua_balancer_peer_data_t *bp; ngx_stream_lua_main_conf_t *lmcf; - if (r == NULL) { + if (s == NULL) { *err = "no session found"; return NGX_ERROR; } - u = r->upstream; + u = s->upstream; if (u == NULL) { *err = "no upstream found"; return NGX_ERROR; } - ctx = ngx_stream_get_module_ctx(r, ngx_stream_lua_module); + ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); if (ctx == NULL) { *err = "no ctx found"; return NGX_ERROR; @@ -572,7 +572,7 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *r, return NGX_ERROR; } - lmcf = ngx_stream_get_module_main_conf(r, ngx_stream_lua_module); + lmcf = ngx_stream_get_module_main_conf(s, ngx_stream_lua_module); bp = lmcf->balancer_peer_data; if (bp == NULL) { @@ -580,8 +580,8 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *r, return NGX_ERROR; } - /*if (r->upstream_states && r->upstream_states->nelts > 1) { - state = r->upstream_states->elts; + /*if (r->upstream_states && s->upstream_states->nelts > 1) { + state = s->upstream_states->elts; *status = (int) state[r->upstream_states->nelts - 2].status; } else { From 70942f998b8d681aafcd4b0550641092d8930987 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 5 Feb 2016 11:59:46 +1100 Subject: [PATCH 08/31] FFI: get_last_failure comment the reason for non-implementation. Return status 0 always. --- src/ngx_stream_lua_balancer.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 6a76c8db..f9481218 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -541,7 +541,11 @@ int ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s, int *status, char **err) { - /* NOT PORTED: IS IT POSSIBLE? */ + + // This is not yet implemented. + // The stream module does not appear to have this available + // Or at-least it does not yet + ngx_stream_lua_ctx_t *ctx; ngx_stream_upstream_t *u; //ngx_stream_upstream_state_t *state; @@ -587,6 +591,8 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s, } else { *status = 0; }*/ + + *status = 0 return bp->last_peer_state; } From 1527ee1c383e54f16550706ad75e472d979e2599 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 5 Feb 2016 12:00:54 +1100 Subject: [PATCH 09/31] Style: remove extra whitespace / newline --- src/ngx_stream_lua_phase.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ngx_stream_lua_phase.c b/src/ngx_stream_lua_phase.c index 03ff8330..d4bc4235 100644 --- a/src/ngx_stream_lua_phase.c +++ b/src/ngx_stream_lua_phase.c @@ -57,8 +57,7 @@ ngx_stream_lua_ngx_get_phase(lua_State *L) case NGX_STREAM_LUA_CONTEXT_TIMER: lua_pushliteral(L, "timer"); break; - - + case NGX_STREAM_LUA_CONTEXT_BALANCER: lua_pushliteral(L, "balancer"); break; From 9d3efbf8fa95de8a823b9cd13a970a82745180f2 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 5 Feb 2016 12:13:57 +1100 Subject: [PATCH 10/31] style fixes --- src/ngx_stream_lua_balancer.c | 20 ++++++++++---------- src/ngx_stream_lua_common.h | 1 - 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index f9481218..81a41d54 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -57,8 +57,8 @@ ngx_stream_lua_balancer_handler_file(ngx_stream_session_t *s, ngx_log_t *log, ngx_int_t rc; rc = ngx_stream_lua_cache_loadfile(log, L, - lscf->balancer.src.data, - lscf->balancer.src_key); + lscf->balancer.src.data, + lscf->balancer.src_key); if (rc != NGX_OK) { return rc; } @@ -77,10 +77,10 @@ ngx_stream_lua_balancer_handler_inline(ngx_stream_session_t *s, ngx_log_t *log, ngx_int_t rc; rc = ngx_stream_lua_cache_loadbuffer(log, L, - lscf->balancer.src.data, - lscf->balancer.src.len, - lscf->balancer.src_key, - "=balancer_by_lua"); + lscf->balancer.src.data, + lscf->balancer.src.len, + lscf->balancer.src_key, + "=balancer_by_lua"); if (rc != NGX_OK) { return rc; } @@ -118,7 +118,7 @@ ngx_stream_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, u_char *p; u_char *name; ngx_str_t *value; - ngx_stream_lua_srv_conf_t *lscf = conf; + ngx_stream_lua_srv_conf_t *lscf = conf; ngx_stream_upstream_srv_conf_t *uscf; @@ -141,7 +141,7 @@ ngx_stream_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, /* Lua code in an external file */ name = ngx_stream_lua_rebase_path(cf->pool, value[1].data, - value[1].len); + value[1].len); if (name == NULL) { return NGX_CONF_ERROR; } @@ -244,8 +244,8 @@ ngx_stream_lua_balancer_init_peer(ngx_stream_session_t *s, static ngx_int_t ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) { - lua_State *L; - ngx_int_t rc; + lua_State *L; + ngx_int_t rc; ngx_stream_session_t *s; ngx_stream_lua_ctx_t *ctx; ngx_stream_lua_srv_conf_t *lscf; diff --git a/src/ngx_stream_lua_common.h b/src/ngx_stream_lua_common.h index 237b4d59..b2db4c0b 100644 --- a/src/ngx_stream_lua_common.h +++ b/src/ngx_stream_lua_common.h @@ -114,7 +114,6 @@ typedef struct ngx_stream_lua_balancer_peer_data_s typedef ngx_int_t (*ngx_stream_lua_main_conf_handler_pt)(ngx_log_t *log, ngx_stream_lua_main_conf_t *lmcf, lua_State *L); - typedef ngx_int_t (*ngx_stream_lua_srv_conf_handler_pt)(ngx_stream_session_t *s, ngx_log_t *log, ngx_stream_lua_srv_conf_t *lscf, lua_State *L); From 1477f6dc6db2cf10608601b62363afec233674e2 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 5 Feb 2016 20:14:33 +1100 Subject: [PATCH 11/31] missed semicolon --- src/ngx_stream_lua_balancer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 81a41d54..011ecda8 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -592,7 +592,7 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s, *status = 0; }*/ - *status = 0 + *status = 0; return bp->last_peer_state; } From 0a87fc2014c9e069e749e18c864eb7dbd753ca67 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 9 Aug 2016 13:35:02 +0000 Subject: [PATCH 12/31] style fixes based on ngx-style.pl --- src/ngx_stream_lua_balancer.c | 24 ++++++++++++------------ src/ngx_stream_lua_common.h | 6 +++--- src/ngx_stream_lua_module.c | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 011ecda8..d64537d8 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -409,9 +409,9 @@ int ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *s, const u_char *adds, size_t addr_len, int port, char **err) { - ngx_url_t url; - ngx_stream_lua_ctx_t *ctx; - ngx_stream_upstream_t *u; + ngx_url_t url; + ngx_stream_lua_ctx_t *ctx; + ngx_stream_upstream_t *u; ngx_stream_lua_main_conf_t *lmcf; ngx_stream_lua_balancer_peer_data_t *bp; @@ -529,8 +529,7 @@ ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *s, return NGX_ERROR; } - - *err = NULL; + *err = NULL; bp->more_tries = count; return NGX_OK; @@ -541,14 +540,15 @@ int ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s, int *status, char **err) { - - // This is not yet implemented. - // The stream module does not appear to have this available - // Or at-least it does not yet + + /* This is not yet implemented. + * The stream module does not appear to have this available + * Or at-least it does not yet + */ ngx_stream_lua_ctx_t *ctx; ngx_stream_upstream_t *u; - //ngx_stream_upstream_state_t *state; + /* ngx_stream_upstream_state_t *state; */ ngx_stream_lua_balancer_peer_data_t *bp; ngx_stream_lua_main_conf_t *lmcf; @@ -591,8 +591,8 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s, } else { *status = 0; }*/ - - *status = 0; + + *status = 0; return bp->last_peer_state; } diff --git a/src/ngx_stream_lua_common.h b/src/ngx_stream_lua_common.h index a25de142..93ab21c5 100644 --- a/src/ngx_stream_lua_common.h +++ b/src/ngx_stream_lua_common.h @@ -170,7 +170,7 @@ struct ngx_stream_lua_main_conf_s { ngx_uint_t shm_zones_inited; ngx_stream_lua_semaphore_mm_t *semaphore_mm; - + ngx_stream_lua_balancer_peer_data_t *balancer_peer_data; /* balancer_by_lua does not support yielding and * there cannot be any conflicts among concurrent requests, @@ -237,8 +237,8 @@ struct ngx_stream_lua_srv_conf_s { ngx_uint_t lingering_close; ngx_msec_t lingering_time; ngx_msec_t lingering_timeout; - - struct { + + struct { ngx_str_t src; u_char *src_key; diff --git a/src/ngx_stream_lua_module.c b/src/ngx_stream_lua_module.c index 2ad5f30a..ae1e4dd6 100644 --- a/src/ngx_stream_lua_module.c +++ b/src/ngx_stream_lua_module.c @@ -104,7 +104,7 @@ static ngx_command_t ngx_stream_lua_commands[] = { NGX_STREAM_SRV_CONF_OFFSET, 0, (void *) ngx_stream_lua_content_handler_file }, - + { ngx_string("balancer_by_lua_block"), NGX_STREAM_UPS_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, ngx_stream_lua_balancer_by_lua_block, From 291d72f428da5937949f5c0ec728d7aaa3c66325 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 9 Aug 2016 15:46:46 +0000 Subject: [PATCH 13/31] add comment about *status=0 --- src/ngx_stream_lua_balancer.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index d64537d8..7a44c206 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -592,6 +592,11 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s, *status = 0; }*/ + /* + * Given that the stream upstream module is very basic in comparison + * with the http upstream module, we do not have any status output + * available at this moment. + */ *status = 0; return bp->last_peer_state; From 78efab039c9070c9854eca31b5a6ef0396adc237 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 9 Aug 2016 13:28:05 -0400 Subject: [PATCH 14/31] travis tests --- .travis.yml | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..c217c54d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,81 @@ +sudo: required +dist: trusty + +os: linux + +language: c + +compiler: + - gcc + - clang + +cache: + directories: + - download-cache + +env: + global: + - LUAJIT_PREFIX=/opt/luajit21 + - LUAJIT_LIB=$LUAJIT_PREFIX/lib + - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH + - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1 + - LUA_INCLUDE_DIR=$LUAJIT_INC + - LUA_CMODULE_DIR=/lib + - PCRE_VER=8.33 + - PCRE_PREFIX=/opt/pcre + - PCRE_LIB=$PCRE_PREFIX/lib + - PCRE_INC=$PCRE_PREFIX/include + - JOBS=3 + - NGX_BUILD_JOBS=$JOBS + - TEST_NGINX_SLEEP=0.006 + matrix: + - NGINX_VERSION=1.9.15 + +services: + - memcache + - redis-server + +before_install: + - sudo apt-get install -qq -y axel cpanminus libgd-dev libtest-base-perl libtext-diff-perl liburi-perl libwww-perl libtest-longstring-perl liblist-moreutils-perl > build.log 2>&1 || (cat build.log && exit 1) + +install: + - if [ ! -d download-cache ]; then mkdir download-cache; fi + - if [ ! -f download-cache/pcre-$PCRE_VER.tar.gz ]; then wget -O download-cache/pcre-$PCRE_VER.tar.gz http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-$PCRE_VER.tar.gz; fi + - git clone https://github.com/openresty/nginx-devel-utils.git + - git clone https://github.com/openresty/lua-cjson.git + - git clone https://github.com/openresty/openresty.git ../openresty + - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx + - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module + - git clone https://github.com/openresty/mockeagain.git + - git clone https://github.com/openresty/test-nginx.git + - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git + - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module + - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module + - git clone https://github.com/openresty/memc-nginx-module.git ../memc-nginx-module + - git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module + - git clone https://github.com/openresty/stream-echo-nginx-module.git ../stream-echo-nginx-module + +script: + - tar zxf download-cache/pcre-$PCRE_VER.tar.gz + - cd pcre-$PCRE_VER/ + - ./configure --prefix=$PCRE_PREFIX --enable-jit --enable-utf --enable-unicode-properties > build.log 2>&1 || (cat build.log && exit 1) + - make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1) + - sudo PATH=$PATH make install > build.log 2>&1 || (cat build.log && exit 1) + - cd .. + - cd luajit2 + - make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1) + - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1) + - cd ../test-nginx && sudo cpanm . && cd .. + - cd lua-cjson/ && make -j$JOBS && sudo make install && cd .. + - cd mockeagain/ && make CC=$CC -j$JOBS && cd .. + - export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH + - export NGX_BUILD_CC=$CC + - sh util/build.sh $NGINX_VERSION > build.log 2>&1 || (cat build.log && exit 1) + - nginx -V + - ldd `which nginx`|grep -E 'luajit|ssl|pcre' + - export LD_PRELOAD=$PWD/mockeagain/mockeagain.so + - export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH + - export TEST_NGINX_RESOLVER=8.8.4.4 + - dig +short @$TEST_NGINX_RESOLVER openresty.org || exit 0 + - dig +short @$TEST_NGINX_RESOLVER agentzh.org || exit 0 + - prove -r t From 69785dce067db22c736f9953b099b863b65185d7 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 9 Aug 2016 14:08:56 -0400 Subject: [PATCH 15/31] remove extraneous resolve test --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c217c54d..034db78b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -77,5 +77,4 @@ script: - export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH - export TEST_NGINX_RESOLVER=8.8.4.4 - dig +short @$TEST_NGINX_RESOLVER openresty.org || exit 0 - - dig +short @$TEST_NGINX_RESOLVER agentzh.org || exit 0 - prove -r t From 4552ef2c9ddc9e60133641faed3358ae744c1e21 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 9 Aug 2016 15:30:14 -0400 Subject: [PATCH 16/31] fix balancer tests --- .travis.yml | 1 + t/138-balancer.t | 58 ++++++++++++++++++++++++------------------------ 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/.travis.yml b/.travis.yml index 034db78b..c217c54d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -77,4 +77,5 @@ script: - export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH - export TEST_NGINX_RESOLVER=8.8.4.4 - dig +short @$TEST_NGINX_RESOLVER openresty.org || exit 0 + - dig +short @$TEST_NGINX_RESOLVER agentzh.org || exit 0 - prove -r t diff --git a/t/138-balancer.t b/t/138-balancer.t index 0e6580cb..e1245552 100644 --- a/t/138-balancer.t +++ b/t/138-balancer.t @@ -18,15 +18,15 @@ run_tests(); __DATA__ === TEST 1: simple logging ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { print("hello from balancer by lua!") } } ---- config - proxy_pass backend; +--- stream_server_config + proxy_pass backend; --- stream_response --- error_log eval [ @@ -39,7 +39,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ === TEST 2: exit 403 ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { @@ -47,7 +47,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ ngx.exit(403) } } ---- config +--- stream_server_config proxy_pass backend; --- stream_response --- error_log @@ -61,7 +61,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ === TEST 3: exit OK ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { @@ -69,7 +69,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ ngx.exit(ngx.OK) } } ---- config +--- stream_server_config proxy_pass backend; --- stream_response --- error_log eval @@ -83,7 +83,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ === TEST 4: ngx.var works ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { @@ -92,9 +92,9 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ print("2: variable foo = ", ngx.var.foo) } } ---- config - set $foo 32; - proxy_pass backend; +--- stream_server_config + set $foo 32; + proxy_pass backend; --- stream_response --- error_log eval [ @@ -108,14 +108,14 @@ qr/\[crit\] .* connect\(\) .*? failed/, === TEST 5: ngx.req.get_headers works ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { print("header foo: ", ngx.req.get_headers()["foo"]) } } ---- config +--- stream_server_config proxy_pass backend; --- stream_response --- error_log eval @@ -129,14 +129,14 @@ qr/\[crit\] .* connect\(\) .*? failed/, === TEST 6: ngx.req.get_uri_args() works ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { print("arg foo: ", ngx.req.get_uri_args()["foo"]) } } ---- config +--- stream_server_config proxy_pass backend; --- stream_response --- error_log eval @@ -149,14 +149,14 @@ qr/\[crit\] .* connect\(\) .*? failed/, === TEST 7: ngx.req.get_method() works ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { print("method: ", ngx.req.get_method()) } } ---- config +--- stream_server_config proxy_pass backend; --- stream_response --- error_log eval @@ -170,12 +170,12 @@ qr/\[crit\] .* connect\(\) .*? failed/, === TEST 8: simple logging (by_lua_file) ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_file html/a.lua; } ---- config +--- stream_server_config proxy_pass backend; --- stream_response --- error_log eval @@ -189,15 +189,15 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ === TEST 9: cosockets are disabled ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { local sock, err = ngx.socket.tcp() } } ---- config - proxy_pass backend; +--- stream_server_config + proxy_pass backend; --- stream_response --- error_log eval qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ @@ -205,15 +205,15 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disable === TEST 10: ngx.sleep is disabled ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { ngx.sleep(0.1) } } ---- config - proxy_pass backend; +--- stream_server_config + proxy_pass backend; --- stream_response --- error_log eval qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ @@ -221,15 +221,15 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disable === TEST 11: get_phase ---- stream_server_config +--- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { print("I am in phase ", ngx.get_phase()) } } ---- config - proxy_pass backend; +--- stream_server_config + proxy_pass backend; --- stream_response --- grep_error_log eval: qr/I am in phase \w+/ --- grep_error_log_out @@ -237,4 +237,4 @@ I am in phase balancer --- error_log eval qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"} --- no_error_log -[error] \ No newline at end of file +[error] From 9c65d893372b9da1cc8616c1ca61f87717be8b41 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 9 Aug 2016 16:41:59 -0400 Subject: [PATCH 17/31] add empty --config sections in balancer test --- t/138-balancer.t | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/t/138-balancer.t b/t/138-balancer.t index e1245552..21402451 100644 --- a/t/138-balancer.t +++ b/t/138-balancer.t @@ -27,6 +27,7 @@ __DATA__ } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- error_log eval [ @@ -49,6 +50,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- error_log [lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream, @@ -71,6 +73,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- error_log eval [ @@ -95,6 +98,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ --- stream_server_config set $foo 32; proxy_pass backend; +--- config --- stream_response --- error_log eval [ @@ -117,6 +121,7 @@ qr/\[crit\] .* connect\(\) .*? failed/, } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- error_log eval [ @@ -138,6 +143,7 @@ qr/\[crit\] .* connect\(\) .*? failed/, } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- error_log eval ["arg foo: bar", @@ -158,6 +164,7 @@ qr/\[crit\] .* connect\(\) .*? failed/, } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- error_log eval [ @@ -177,6 +184,7 @@ qr/\[crit\] .* connect\(\) .*? failed/, } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- error_log eval [ @@ -198,6 +206,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- error_log eval qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ @@ -214,6 +223,7 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disable } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- error_log eval qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ @@ -230,6 +240,7 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disable } --- stream_server_config proxy_pass backend; +--- config --- stream_response --- grep_error_log eval: qr/I am in phase \w+/ --- grep_error_log_out From bb12e577bb811f7b6a46d22a62de92f03602c832 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 9 Aug 2016 17:52:53 -0400 Subject: [PATCH 18/31] add stream request to tests --- t/138-balancer.t | 96 +++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 71 deletions(-) diff --git a/t/138-balancer.t b/t/138-balancer.t index 21402451..46c62dea 100644 --- a/t/138-balancer.t +++ b/t/138-balancer.t @@ -28,6 +28,8 @@ __DATA__ --- stream_server_config proxy_pass backend; --- config +--- stream_request chomp +hello world --- stream_response --- error_log eval [ @@ -39,18 +41,20 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ -=== TEST 2: exit 403 +=== TEST 2: exit DECLINED --- stream_config upstream backend { server 0.0.0.1; balancer_by_lua_block { print("hello from balancer by lua!") - ngx.exit(403) + ngx.exit(ngx.DECLINED) } } --- stream_server_config proxy_pass backend; --- config +--- stream_request chomp +hello world --- stream_response --- error_log [lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream, @@ -74,6 +78,8 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ --- stream_server_config proxy_pass backend; --- config +--- stream_request chomp +hello world --- stream_response --- error_log eval [ @@ -99,6 +105,8 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ set $foo 32; proxy_pass backend; --- config +--- stream_request chomp +hello world --- stream_response --- error_log eval [ @@ -111,72 +119,7 @@ qr/\[crit\] .* connect\(\) .*? failed/, -=== TEST 5: ngx.req.get_headers works ---- stream_config - upstream backend { - server 0.0.0.1; - balancer_by_lua_block { - print("header foo: ", ngx.req.get_headers()["foo"]) - } - } ---- stream_server_config - proxy_pass backend; ---- config ---- stream_response ---- error_log eval -[ -"header foo: bar", -qr/\[crit\] .* connect\(\) .*? failed/, -] ---- no_error_log -[warn] - - - -=== TEST 6: ngx.req.get_uri_args() works ---- stream_config - upstream backend { - server 0.0.0.1; - balancer_by_lua_block { - print("arg foo: ", ngx.req.get_uri_args()["foo"]) - } - } ---- stream_server_config - proxy_pass backend; ---- config ---- stream_response ---- error_log eval -["arg foo: bar", -qr/\[crit\] .* connect\(\) .*? failed/, -] ---- no_error_log -[warn] - - - -=== TEST 7: ngx.req.get_method() works ---- stream_config - upstream backend { - server 0.0.0.1; - balancer_by_lua_block { - print("method: ", ngx.req.get_method()) - } - } ---- stream_server_config - proxy_pass backend; ---- config ---- stream_response ---- error_log eval -[ -"method: GET", -qr/\[crit\] .* connect\(\) .*? failed/, -] ---- no_error_log -[warn] - - - -=== TEST 8: simple logging (by_lua_file) +=== TEST 5: simple logging (by_lua_file) --- stream_config upstream backend { server 0.0.0.1; @@ -185,6 +128,11 @@ qr/\[crit\] .* connect\(\) .*? failed/, --- stream_server_config proxy_pass backend; --- config +--- stream_request chomp +hello world +--- user_files +>>> a.lua +print("hello from balancer by lua!") --- stream_response --- error_log eval [ @@ -196,7 +144,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ -=== TEST 9: cosockets are disabled +=== TEST 6: cosockets are disabled --- stream_config upstream backend { server 0.0.0.1; @@ -207,13 +155,15 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\ --- stream_server_config proxy_pass backend; --- config +--- stream_request chomp +hello world --- stream_response --- error_log eval qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ -=== TEST 10: ngx.sleep is disabled +=== TEST 7: ngx.sleep is disabled --- stream_config upstream backend { server 0.0.0.1; @@ -224,13 +174,15 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disable --- stream_server_config proxy_pass backend; --- config +--- stream_request chomp +hello world --- stream_response --- error_log eval qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ -=== TEST 11: get_phase +=== TEST 8: get_phase --- stream_config upstream backend { server 0.0.0.1; @@ -241,6 +193,8 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disable --- stream_server_config proxy_pass backend; --- config +--- stream_request chomp +hello world --- stream_response --- grep_error_log eval: qr/I am in phase \w+/ --- grep_error_log_out From da0cb969d0bc463e9e7aaf347c23a34b50c326a0 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 9 Aug 2016 20:30:22 -0400 Subject: [PATCH 19/31] remove http specific tests from suite 138-balancer.t --- t/138-balancer.t | 141 +++++++++-------------------------------------- 1 file changed, 26 insertions(+), 115 deletions(-) diff --git a/t/138-balancer.t b/t/138-balancer.t index 46c62dea..1b60708c 100644 --- a/t/138-balancer.t +++ b/t/138-balancer.t @@ -1,6 +1,6 @@ # vim:set ft= ts=4 sw=4 et fdm=marker: -use Test::Nginx::Socket::Lua; +use Test::Nginx::Socket::Lua::Stream; #worker_connections(1014); #master_on(); @@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua; repeat_each(2); -plan tests => repeat_each() * (blocks() * 4 + 8); +plan tests => repeat_each() * (blocks() * 2 + 3); #no_diff(); no_long_string(); @@ -20,186 +20,97 @@ __DATA__ === TEST 1: simple logging --- stream_config upstream backend { - server 0.0.0.1; + server 0.0.0.1:80; balancer_by_lua_block { print("hello from balancer by lua!") } } --- stream_server_config proxy_pass backend; ---- config ---- stream_request chomp -hello world ---- stream_response --- error_log eval [ '[lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream,', -qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"}, +qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "0\.0\.0\.1:80"}, ] ---- no_error_log -[warn] -=== TEST 2: exit DECLINED +=== TEST 2: simple logging (by_lua_file) --- stream_config upstream backend { - server 0.0.0.1; - balancer_by_lua_block { - print("hello from balancer by lua!") - ngx.exit(ngx.DECLINED) - } - } ---- stream_server_config - proxy_pass backend; ---- config ---- stream_request chomp -hello world ---- stream_response ---- error_log -[lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream, ---- no_error_log eval -[ -'[warn]', -qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"}, -] - - - -=== TEST 3: exit OK ---- stream_config - upstream backend { - server 0.0.0.1; - balancer_by_lua_block { - print("hello from balancer by lua!") - ngx.exit(ngx.OK) - } + server 0.0.0.1:80; + balancer_by_lua_file html/a.lua; } --- stream_server_config proxy_pass backend; ---- config ---- stream_request chomp -hello world ---- stream_response +--- user_files +>>> a.lua +print("hello from balancer by lua!") --- error_log eval [ -'[lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream,', -qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"}, +'[lua] a.lua:1: hello from balancer by lua! while connecting to upstream,', +qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "0\.0\.0\.1:80"}, ] ---- no_error_log -[warn] -=== TEST 4: ngx.var works +=== TEST 3: cosockets are disabled --- stream_config upstream backend { - server 0.0.0.1; + server 0.0.0.1:80; balancer_by_lua_block { - print("1: variable foo = ", ngx.var.foo) - ngx.var.foo = tonumber(ngx.var.foo) + 1 - print("2: variable foo = ", ngx.var.foo) + local sock, err = ngx.socket.tcp() } } ---- stream_server_config - set $foo 32; - proxy_pass backend; ---- config ---- stream_request chomp -hello world ---- stream_response ---- error_log eval -[ -"1: variable foo = 32", -"2: variable foo = 33", -qr/\[crit\] .* connect\(\) .*? failed/, -] ---- no_error_log -[warn] - - - -=== TEST 5: simple logging (by_lua_file) ---- stream_config - upstream backend { - server 0.0.0.1; - balancer_by_lua_file html/a.lua; - } --- stream_server_config proxy_pass backend; ---- config ---- stream_request chomp -hello world ---- user_files ->>> a.lua -print("hello from balancer by lua!") ---- stream_response --- error_log eval -[ -'[lua] a.lua:1: hello from balancer by lua! while connecting to upstream,', -qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"}, -] ---- no_error_log -[warn] +qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of \(unknown\)/ -=== TEST 6: cosockets are disabled +=== TEST 4: ngx.exit fails --- stream_config upstream backend { - server 0.0.0.1; + server 0.0.0.1:80; balancer_by_lua_block { - local sock, err = ngx.socket.tcp() + ngx.exit(ngx.DECLINED) } } --- stream_server_config proxy_pass backend; ---- config ---- stream_request chomp -hello world ---- stream_response --- error_log eval -qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ +qr/\[error\] .*? failed to run balancer_by_lua\*: attempt to yield across C-call boundary/ -=== TEST 7: ngx.sleep is disabled +=== TEST 5: ngx.sleep is disabled --- stream_config upstream backend { - server 0.0.0.1; + server 0.0.0.1:80; balancer_by_lua_block { ngx.sleep(0.1) } } --- stream_server_config proxy_pass backend; ---- config ---- stream_request chomp -hello world ---- stream_response --- error_log eval -qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ +qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of \(unknown\)/ -=== TEST 8: get_phase +=== TEST 6: get_phase --- stream_config upstream backend { - server 0.0.0.1; + server 0.0.0.1:80; balancer_by_lua_block { print("I am in phase ", ngx.get_phase()) } } --- stream_server_config proxy_pass backend; ---- config ---- stream_request chomp -hello world ---- stream_response --- grep_error_log eval: qr/I am in phase \w+/ --- grep_error_log_out I am in phase balancer --- error_log eval -qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\.0\.1:80/t"} ---- no_error_log -[error] +qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "0\.0\.0\.1:80"} From 52cc121243447381c5d0a382e48aa0ce45f886a9 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Wed, 10 Aug 2016 18:25:24 -0400 Subject: [PATCH 20/31] remove blank lines --- src/ngx_stream_lua_common.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ngx_stream_lua_common.h b/src/ngx_stream_lua_common.h index 93ab21c5..2b652bec 100644 --- a/src/ngx_stream_lua_common.h +++ b/src/ngx_stream_lua_common.h @@ -241,11 +241,9 @@ struct ngx_stream_lua_srv_conf_s { struct { ngx_str_t src; u_char *src_key; - ngx_stream_lua_srv_conf_handler_pt handler; } balancer; - }; From 3a430b88f117679b3679f6ef3003a53924e24514 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Wed, 10 Aug 2016 18:27:28 -0400 Subject: [PATCH 21/31] remove travis from personal branch --- .travis.yml | 81 ----------------------------------------------------- 1 file changed, 81 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c217c54d..00000000 --- a/.travis.yml +++ /dev/null @@ -1,81 +0,0 @@ -sudo: required -dist: trusty - -os: linux - -language: c - -compiler: - - gcc - - clang - -cache: - directories: - - download-cache - -env: - global: - - LUAJIT_PREFIX=/opt/luajit21 - - LUAJIT_LIB=$LUAJIT_PREFIX/lib - - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH - - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1 - - LUA_INCLUDE_DIR=$LUAJIT_INC - - LUA_CMODULE_DIR=/lib - - PCRE_VER=8.33 - - PCRE_PREFIX=/opt/pcre - - PCRE_LIB=$PCRE_PREFIX/lib - - PCRE_INC=$PCRE_PREFIX/include - - JOBS=3 - - NGX_BUILD_JOBS=$JOBS - - TEST_NGINX_SLEEP=0.006 - matrix: - - NGINX_VERSION=1.9.15 - -services: - - memcache - - redis-server - -before_install: - - sudo apt-get install -qq -y axel cpanminus libgd-dev libtest-base-perl libtext-diff-perl liburi-perl libwww-perl libtest-longstring-perl liblist-moreutils-perl > build.log 2>&1 || (cat build.log && exit 1) - -install: - - if [ ! -d download-cache ]; then mkdir download-cache; fi - - if [ ! -f download-cache/pcre-$PCRE_VER.tar.gz ]; then wget -O download-cache/pcre-$PCRE_VER.tar.gz http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-$PCRE_VER.tar.gz; fi - - git clone https://github.com/openresty/nginx-devel-utils.git - - git clone https://github.com/openresty/lua-cjson.git - - git clone https://github.com/openresty/openresty.git ../openresty - - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx - - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module - - git clone https://github.com/openresty/mockeagain.git - - git clone https://github.com/openresty/test-nginx.git - - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git - - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module - - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module - - git clone https://github.com/openresty/memc-nginx-module.git ../memc-nginx-module - - git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module - - git clone https://github.com/openresty/stream-echo-nginx-module.git ../stream-echo-nginx-module - -script: - - tar zxf download-cache/pcre-$PCRE_VER.tar.gz - - cd pcre-$PCRE_VER/ - - ./configure --prefix=$PCRE_PREFIX --enable-jit --enable-utf --enable-unicode-properties > build.log 2>&1 || (cat build.log && exit 1) - - make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1) - - sudo PATH=$PATH make install > build.log 2>&1 || (cat build.log && exit 1) - - cd .. - - cd luajit2 - - make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1) - - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1) - - cd ../test-nginx && sudo cpanm . && cd .. - - cd lua-cjson/ && make -j$JOBS && sudo make install && cd .. - - cd mockeagain/ && make CC=$CC -j$JOBS && cd .. - - export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH - - export NGX_BUILD_CC=$CC - - sh util/build.sh $NGINX_VERSION > build.log 2>&1 || (cat build.log && exit 1) - - nginx -V - - ldd `which nginx`|grep -E 'luajit|ssl|pcre' - - export LD_PRELOAD=$PWD/mockeagain/mockeagain.so - - export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH - - export TEST_NGINX_RESOLVER=8.8.4.4 - - dig +short @$TEST_NGINX_RESOLVER openresty.org || exit 0 - - dig +short @$TEST_NGINX_RESOLVER agentzh.org || exit 0 - - prove -r t From c083780dc3985d04fda0b2e34b940d036a88208b Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Wed, 10 Aug 2016 19:18:33 -0400 Subject: [PATCH 22/31] set name of context for balancer_by_lua --- src/ngx_stream_lua_balancer.c | 4 ++-- src/ngx_stream_lua_util.h | 1 + t/138-balancer.t | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 7a44c206..2e0efc07 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -252,8 +252,8 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) ngx_stream_lua_main_conf_t *lmcf; ngx_stream_lua_balancer_peer_data_t *bp = data; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, - "lua balancer pees, try: %ui", pc->tries); + ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, + "lua balancer peer, try: %ui", pc->tries); lscf = bp->conf; diff --git a/src/ngx_stream_lua_util.h b/src/ngx_stream_lua_util.h index 8b646648..4ed5aa06 100644 --- a/src/ngx_stream_lua_util.h +++ b/src/ngx_stream_lua_util.h @@ -88,6 +88,7 @@ void ngx_stream_lua_set_multi_value_table(lua_State *L, int index); : (c) == NGX_STREAM_LUA_CONTEXT_LOG ? "log_by_lua*" \ : (c) == NGX_STREAM_LUA_CONTEXT_TIMER ? "ngx.timer" \ : (c) == NGX_STREAM_LUA_CONTEXT_INIT_WORKER ? "init_worker_by_lua*" \ + : (c) == NGX_STREAM_LUA_CONTEXT_BALANCER ? "balancer_by_lua*" \ : "(unknown)") diff --git a/t/138-balancer.t b/t/138-balancer.t index 1b60708c..2849e0d8 100644 --- a/t/138-balancer.t +++ b/t/138-balancer.t @@ -65,7 +65,7 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "0\.0\.0\.1:8 --- stream_server_config proxy_pass backend; --- error_log eval -qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of \(unknown\)/ +qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ @@ -95,7 +95,7 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: attempt to yield across C-call --- stream_server_config proxy_pass backend; --- error_log eval -qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of \(unknown\)/ +qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disabled in the context of balancer_by_lua\*/ From 07b8540ab58ef605674d5f42b04494fb029a41fa Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Sun, 14 Aug 2016 08:40:17 -0400 Subject: [PATCH 23/31] add support for set_timeouts in balancer_by_lua --- src/ngx_stream_lua_balancer.c | 95 +++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 2e0efc07..ee254255 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -488,6 +488,101 @@ ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *s, } +int +ngx_stream_lua_ffi_balancer_set_timeouts(ngx_stream_session_t *s, + long connect_timeout, long send_timeout, long read_timeout, + char **err) +{ + ngx_stream_lua_ctx_t *ctx; + ngx_stream_upstream_t *u; + +#if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) + ngx_stream_upstream_conf_t *ucf; +#endif + ngx_stream_lua_main_conf_t *lmcf; + ngx_stream_lua_balancer_peer_data_t *bp; + + if (s == NULL) { + *err = "no session found"; + return NGX_ERROR; + } + + u = s->upstream; + + if (u == NULL) { + *err = "no upstream found"; + return NGX_ERROR; + } + + ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); + if (ctx == NULL) { + *err = "no ctx found"; + return NGX_ERROR; + } + + if ((ctx->context & NGX_STREAM_LUA_CONTEXT_BALANCER) == 0) { + *err = "API disabled in the current context"; + return NGX_ERROR; + } + + lmcf = ngx_stream_get_module_main_conf(s, ngx_stream_lua_module); + + bp = lmcf->balancer_peer_data; + if (bp == NULL) { + *err = "no upstream peer data found"; + return NGX_ERROR; + } + +#if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) + if (!bp->cloned_upstream_conf) { + /* we clone the upstream conf for the current session so that + * we do not affect other sessions at all. */ + + ucf = ngx_palloc(s->pool, sizeof(ngx_stream_upstream_conf_t)); + + if (ucf == NULL) { + *err = "no memory"; + return NGX_ERROR; + } + + ngx_memcpy(ucf, u->conf, sizeof(ngx_stream_upstream_conf_t)); + + u->conf = ucf; + bp->cloned_upstream_conf = 1; + + } else { + ucf = u->conf; + } +#endif + + if (connect_timeout > 0) { +#if (HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) + u->connect_timeout = (ngx_msec_t) connect_timeout; +#else + ucf->connect_timeout = (ngx_msec_t) connect_timeout; +#endif + } + + if (send_timeout > 0) { +#if (HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) + u->send_timeout = (ngx_msec_t) send_timeout; +#else + ucf->send_timeout = (ngx_msec_t) send_timeout; +#endif + } + + if (read_timeout > 0) { +#if (HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) + u->read_timeout = (ngx_msec_t) read_timeout; +#else + ucf->read_timeout = (ngx_msec_t) read_timeout; +#endif + } + + return NGX_OK; +} + + int ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *s, int count, char **err) From 9f0c59e86851d05feaea0d8b902080a5a0b77662 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Mon, 15 Aug 2016 22:10:10 -0400 Subject: [PATCH 24/31] set nginx version to 1.11.2 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b07bf673..636955fa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ env: - NGX_BUILD_JOBS=$JOBS - TEST_NGINX_SLEEP=0.006 matrix: - - NGINX_VERSION=1.9.15 + - NGINX_VERSION=1.11.2 services: - memcache From 9136d2596a5d74d83c693c6824adfad3fcdcec71 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 16 Aug 2016 18:03:27 -0400 Subject: [PATCH 25/31] revert travis version to 1.9.15 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 636955fa..b07bf673 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ env: - NGX_BUILD_JOBS=$JOBS - TEST_NGINX_SLEEP=0.006 matrix: - - NGINX_VERSION=1.11.2 + - NGINX_VERSION=1.9.15 services: - memcache From 3d84692591e804ecc3ab52a0a2a27e8280fa4623 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 16 Aug 2016 18:04:11 -0400 Subject: [PATCH 26/31] remove set_timeout support --- src/ngx_stream_lua_balancer.c | 95 ----------------------------------- 1 file changed, 95 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index ee254255..2e0efc07 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -488,101 +488,6 @@ ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *s, } -int -ngx_stream_lua_ffi_balancer_set_timeouts(ngx_stream_session_t *s, - long connect_timeout, long send_timeout, long read_timeout, - char **err) -{ - ngx_stream_lua_ctx_t *ctx; - ngx_stream_upstream_t *u; - -#if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) - ngx_stream_upstream_conf_t *ucf; -#endif - ngx_stream_lua_main_conf_t *lmcf; - ngx_stream_lua_balancer_peer_data_t *bp; - - if (s == NULL) { - *err = "no session found"; - return NGX_ERROR; - } - - u = s->upstream; - - if (u == NULL) { - *err = "no upstream found"; - return NGX_ERROR; - } - - ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); - if (ctx == NULL) { - *err = "no ctx found"; - return NGX_ERROR; - } - - if ((ctx->context & NGX_STREAM_LUA_CONTEXT_BALANCER) == 0) { - *err = "API disabled in the current context"; - return NGX_ERROR; - } - - lmcf = ngx_stream_get_module_main_conf(s, ngx_stream_lua_module); - - bp = lmcf->balancer_peer_data; - if (bp == NULL) { - *err = "no upstream peer data found"; - return NGX_ERROR; - } - -#if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) - if (!bp->cloned_upstream_conf) { - /* we clone the upstream conf for the current session so that - * we do not affect other sessions at all. */ - - ucf = ngx_palloc(s->pool, sizeof(ngx_stream_upstream_conf_t)); - - if (ucf == NULL) { - *err = "no memory"; - return NGX_ERROR; - } - - ngx_memcpy(ucf, u->conf, sizeof(ngx_stream_upstream_conf_t)); - - u->conf = ucf; - bp->cloned_upstream_conf = 1; - - } else { - ucf = u->conf; - } -#endif - - if (connect_timeout > 0) { -#if (HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) - u->connect_timeout = (ngx_msec_t) connect_timeout; -#else - ucf->connect_timeout = (ngx_msec_t) connect_timeout; -#endif - } - - if (send_timeout > 0) { -#if (HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) - u->send_timeout = (ngx_msec_t) send_timeout; -#else - ucf->send_timeout = (ngx_msec_t) send_timeout; -#endif - } - - if (read_timeout > 0) { -#if (HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS) - u->read_timeout = (ngx_msec_t) read_timeout; -#else - ucf->read_timeout = (ngx_msec_t) read_timeout; -#endif - } - - return NGX_OK; -} - - int ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *s, int count, char **err) From dc5a572f0e9cb2587d74c165fc528ed1f24ac1a1 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Tue, 16 Aug 2016 18:24:16 -0400 Subject: [PATCH 27/31] ngx exit support - wip --- src/ngx_stream_lua_control.c | 10 ++++++++++ t/138-balancer.t | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ngx_stream_lua_control.c b/src/ngx_stream_lua_control.c index 93c6f14f..93f559df 100644 --- a/src/ngx_stream_lua_control.c +++ b/src/ngx_stream_lua_control.c @@ -70,6 +70,11 @@ ngx_stream_lua_ngx_exit(lua_State *L) ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, "stream lua exit with code %i", ctx->exit_code); + if (ctx->context & NGX_STREAM_LUA_CONTEXT_BALANCER) + { + return 0; + } + dd("calling yield"); return lua_yield(L, 0); } @@ -160,6 +165,11 @@ ngx_stream_lua_ffi_exit(ngx_stream_session_t *s, int status, u_char *err, ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, "stream lua exit with code %i", ctx->exit_code); + if (ctx->context & NGX_STREAM_LUA_CONTEXT_BALANCER) + { + return 0; + } + return NGX_OK; } #endif /* NGX_LUA_NO_FFI_API */ diff --git a/t/138-balancer.t b/t/138-balancer.t index 2849e0d8..407ff9c4 100644 --- a/t/138-balancer.t +++ b/t/138-balancer.t @@ -69,12 +69,12 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disable -=== TEST 4: ngx.exit fails +=== TEST 4: ngx.exit works --- stream_config upstream backend { server 0.0.0.1:80; balancer_by_lua_block { - ngx.exit(ngx.DECLINED) + ngx.exit(ngx.ERROR) } } --- stream_server_config From a73175badf0baaedb05ebc2eeb9520cab55402a2 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Wed, 17 Aug 2016 09:34:13 -0400 Subject: [PATCH 28/31] fixing balancer tests. check for ngx error and ngx ok --- t/138-balancer.t | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/t/138-balancer.t b/t/138-balancer.t index 407ff9c4..b675a71a 100644 --- a/t/138-balancer.t +++ b/t/138-balancer.t @@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua::Stream; repeat_each(2); -plan tests => repeat_each() * (blocks() * 2 + 3); +plan tests => repeat_each() * (blocks() * 2 + 4); #no_diff(); no_long_string(); @@ -69,22 +69,42 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disable -=== TEST 4: ngx.exit works +=== TEST 4: ngx.exit ERROR works --- stream_config upstream backend { server 0.0.0.1:80; balancer_by_lua_block { + print("hello from balancer by lua!") ngx.exit(ngx.ERROR) } } --- stream_server_config proxy_pass backend; +--- error_log +[lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream + + + +=== TEST 5: ngx.exit OK works +--- stream_config + upstream backend { + server 0.0.0.1:80; + balancer_by_lua_block { + print("hello from balancer by lua!") + ngx.exit(ngx.OK) + } + } +--- stream_server_config + proxy_pass backend; --- error_log eval -qr/\[error\] .*? failed to run balancer_by_lua\*: attempt to yield across C-call boundary/ +[ +'[lua] balancer_by_lua:2: hello from balancer by lua! while connecting to upstream,', +qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "0\.0\.0\.1:80"}, +] -=== TEST 5: ngx.sleep is disabled +=== TEST 6: ngx.sleep is disabled --- stream_config upstream backend { server 0.0.0.1:80; @@ -99,7 +119,7 @@ qr/\[error\] .*? failed to run balancer_by_lua\*: balancer_by_lua:2: API disable -=== TEST 6: get_phase +=== TEST 7: get_phase --- stream_config upstream backend { server 0.0.0.1:80; From 34ae6be2d90169af60af57d0b13464e075dcb262 Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Sun, 21 Aug 2016 19:50:34 -0400 Subject: [PATCH 29/31] check for context mismatch before anything else in balancer functions --- src/ngx_stream_lua_balancer.c | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 2e0efc07..13d36461 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -421,13 +421,6 @@ ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *s, return NGX_ERROR; } - u = s->upstream; - - if (u == NULL) { - *err = "no upstream found"; - return NGX_ERROR; - } - ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); if (ctx == NULL) { *err = "no ctx found"; @@ -439,6 +432,13 @@ ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *s, return NGX_ERROR; } + u = s->upstream; + + if (u == NULL) { + *err = "no upstream found"; + return NGX_ERROR; + } + lmcf = ngx_stream_get_module_main_conf(s, ngx_stream_lua_module); /* we cannot read s->upstream->peer.data here directly because @@ -503,13 +503,6 @@ ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *s, return NGX_ERROR; } - u = s->upstream; - - if (u == NULL) { - *err = "no upstream found"; - return NGX_ERROR; - } - ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); if (ctx == NULL) { *err = "no ctx found"; @@ -521,6 +514,13 @@ ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *s, return NGX_ERROR; } + u = s->upstream; + + if (u == NULL) { + *err = "no upstream found"; + return NGX_ERROR; + } + lmcf = ngx_stream_get_module_main_conf(s, ngx_stream_lua_module); bp = lmcf->balancer_peer_data; @@ -558,13 +558,6 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s, return NGX_ERROR; } - u = s->upstream; - - if (u == NULL) { - *err = "no upstream found"; - return NGX_ERROR; - } - ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module); if (ctx == NULL) { *err = "no ctx found"; @@ -576,6 +569,13 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s, return NGX_ERROR; } + u = s->upstream; + + if (u == NULL) { + *err = "no upstream found"; + return NGX_ERROR; + } + lmcf = ngx_stream_get_module_main_conf(s, ngx_stream_lua_module); bp = lmcf->balancer_peer_data; From 8aa6082d2a421278f87d91e5e09af3d6c319f66f Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Sun, 21 Aug 2016 19:50:52 -0400 Subject: [PATCH 30/31] remove stray http balancer reference --- src/ngx_stream_lua_common.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ngx_stream_lua_common.h b/src/ngx_stream_lua_common.h index 9a982a48..588f9797 100644 --- a/src/ngx_stream_lua_common.h +++ b/src/ngx_stream_lua_common.h @@ -92,9 +92,6 @@ typedef void (*ngx_stream_lua_cleanup_pt)(void *data); typedef struct ngx_stream_lua_cleanup_s ngx_stream_lua_cleanup_t; -typedef struct ngx_http_lua_balancer_peer_data_s - ngx_http_lua_balancer_peer_data_t; - struct ngx_stream_lua_cleanup_s { ngx_stream_lua_cleanup_pt handler; void *data; From f2046132ce2c6579d69d63d5cd3e7d9c3ea85d9b Mon Sep 17 00:00:00 2001 From: Shriram Rajagopalan Date: Mon, 17 Oct 2016 10:30:58 -0400 Subject: [PATCH 31/31] fix compilation error --- src/ngx_stream_lua_balancer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 13d36461..98124f2f 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -259,7 +259,7 @@ ngx_stream_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data) s = bp->session; - ngx_stream_lua_assert(lscf->balancer.handler && r); + ngx_stream_lua_assert(lscf->balancer.handler && s); ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);