Skip to content

Commit 012f316

Browse files
spacewanderagentzh
authored andcommitted
optimize: destroy the Lua VM and avoid running any init_worker_by_lua* code inside cache helper processes.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent 7c7b2e0 commit 012f316

File tree

7 files changed

+225
-66
lines changed

7 files changed

+225
-66
lines changed

README.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,8 @@ This hook is often used to create per-worker reoccurring timers (via the [ngx.ti
14871487

14881488
This directive was first introduced in the `v0.9.5` release.
14891489

1490+
This hook no longer runs in the cache manager and cache loader processes since the `v0.10.12` release.
1491+
14901492
[Back to TOC](#directives)
14911493

14921494
init_worker_by_lua_block
@@ -1514,6 +1516,8 @@ For instance,
15141516

15151517
This directive was first introduced in the `v0.9.17` release.
15161518

1519+
This hook no longer runs in the cache manager and cache loader processes since the `v0.10.12` release.
1520+
15171521
[Back to TOC](#directives)
15181522

15191523
init_worker_by_lua_file
@@ -1529,6 +1533,8 @@ Similar to [init_worker_by_lua](#init_worker_by_lua), but accepts the file path
15291533

15301534
This directive was first introduced in the `v0.9.5` release.
15311535

1536+
This hook no longer runs in the cache manager and cache loader processes since the `v0.10.12` release.
1537+
15321538
[Back to TOC](#directives)
15331539

15341540
set_by_lua

doc/HttpLuaModule.wiki

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,8 @@ This hook is often used to create per-worker reoccurring timers (via the [[#ngx.
11961196
11971197
This directive was first introduced in the <code>v0.9.5</code> release.
11981198
1199+
This hook no longer runs in the cache manager and cache loader processes since the <code>v0.10.12</code> release.
1200+
11991201
== init_worker_by_lua_block ==
12001202
12011203
'''syntax:''' ''init_worker_by_lua_block { lua-script }''
@@ -1219,6 +1221,8 @@ For instance,
12191221
12201222
This directive was first introduced in the <code>v0.9.17</code> release.
12211223
1224+
This hook no longer runs in the cache manager and cache loader processes since the <code>v0.10.12</code> release.
1225+
12221226
== init_worker_by_lua_file ==
12231227
12241228
'''syntax:''' ''init_worker_by_lua_file <lua-file-path>''
@@ -1231,6 +1235,8 @@ Similar to [[#init_worker_by_lua|init_worker_by_lua]], but accepts the file path
12311235
12321236
This directive was first introduced in the <code>v0.9.5</code> release.
12331237
1238+
This hook no longer runs in the cache manager and cache loader processes since the <code>v0.10.12</code> release.
1239+
12341240
== set_by_lua ==
12351241
12361242
'''syntax:''' ''set_by_lua $res <lua-script-str> [$arg1 $arg2 ...]''

src/ngx_http_lua_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ typedef struct {
162162

163163
struct ngx_http_lua_main_conf_s {
164164
lua_State *lua;
165+
ngx_pool_cleanup_t *vm_cleanup;
165166

166167
ngx_str_t lua_path;
167168
ngx_str_t lua_cpath;

src/ngx_http_lua_initworkerby.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,30 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
4040

4141
lmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_lua_module);
4242

43-
if (lmcf == NULL
44-
|| lmcf->init_worker_handler == NULL
45-
|| lmcf->lua == NULL)
43+
if (lmcf == NULL || lmcf->lua == NULL) {
44+
return NGX_OK;
45+
}
46+
47+
/* lmcf != NULL && lmcf->lua != NULL */
48+
49+
/* disable init_worker_by_lua* and destroy lua VM in cache processes */
50+
if (ngx_process == NGX_PROCESS_HELPER
51+
#if defined(HAVE_PRIVILEGED_PROCESS_PATCH) && !NGX_WIN32
52+
&& !ngx_is_privileged_agent
53+
#endif
54+
)
4655
{
56+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
57+
"lua close the global Lua VM %p in the "
58+
"cache helper process %P", lmcf->lua, ngx_pid);
59+
60+
lmcf->vm_cleanup->handler(lmcf->vm_cleanup->data);
61+
lmcf->vm_cleanup->handler = NULL;
62+
63+
return NGX_OK;
64+
}
65+
66+
if (lmcf->init_worker_handler == NULL) {
4767
return NGX_OK;
4868
}
4969

src/ngx_http_lua_util.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,6 +3768,12 @@ ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
37683768

37693769
cln->data = state;
37703770

3771+
if (lmcf->vm_cleanup == NULL) {
3772+
/* this assignment will happen only once,
3773+
* and also only for the main Lua VM */
3774+
lmcf->vm_cleanup = cln;
3775+
}
3776+
37713777
if (pcln) {
37723778
*pcln = cln;
37733779
}

t/124-init-worker.t

Lines changed: 182 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
use Test::Nginx::Socket::Lua;
44

55
#worker_connections(1014);
6-
#master_on();
6+
master_on();
77
#workers(2);
88
#log_level('warn');
99

1010
repeat_each(1);
1111

12-
plan tests => repeat_each() * (blocks() * 4);
12+
plan tests => repeat_each() * (blocks() * 4 + 4);
1313

1414
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
1515
$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';
@@ -756,3 +756,183 @@ GET /t
756756
ok
757757
--- no_error_log
758758
[error]
759+
760+
761+
762+
=== TEST 20: destory Lua VM in cache processes (without privileged agent or shdict)
763+
--- http_config
764+
lua_package_path "../lua-resty-core/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;";
765+
766+
proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:1m;
767+
768+
#lua_shared_dict dummy 500k;
769+
770+
init_by_lua_block {
771+
-- require "resty.core.regex"
772+
-- assert(ngx.re.match("hello, world", [[hello, \w+]], "joi"))
773+
}
774+
775+
--- config
776+
location = /t {
777+
return 200;
778+
}
779+
--- request
780+
GET /t
781+
--- grep_error_log eval: qr/lua close the global Lua VM \S+ in the cache helper process \d+|lua close the global Lua VM \S+$/
782+
--- grep_error_log_out eval
783+
qr/\Alua close the global Lua VM ([0-9A-F]+) in the cache helper process \d+
784+
lua close the global Lua VM \1
785+
lua close the global Lua VM \1 in the cache helper process \d+
786+
lua close the global Lua VM \1
787+
(?:lua close the global Lua VM [0-9A-F]+
788+
)*\z/
789+
--- no_error_log
790+
[error]
791+
start privileged agent process
792+
793+
794+
795+
=== TEST 21: destory Lua VM in cache processes (without privileged agent but with shdict)
796+
--- http_config
797+
lua_package_path "../lua-resty-core/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;";
798+
799+
proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:1m;
800+
801+
lua_shared_dict dummy 500k;
802+
803+
init_by_lua_block {
804+
-- require "resty.core.regex"
805+
-- assert(ngx.re.match("hello, world", [[hello, \w+]], "joi"))
806+
}
807+
808+
--- config
809+
location = /t {
810+
return 200;
811+
}
812+
--- request
813+
GET /t
814+
--- grep_error_log eval: qr/lua close the global Lua VM \S+ in the cache helper process \d+|lua close the global Lua VM \S+$/
815+
--- grep_error_log_out eval
816+
qr/\Alua close the global Lua VM ([0-9A-F]+) in the cache helper process \d+
817+
lua close the global Lua VM \1
818+
lua close the global Lua VM \1 in the cache helper process \d+
819+
lua close the global Lua VM \1
820+
(?:lua close the global Lua VM [0-9A-F]+
821+
)*\z/
822+
--- no_error_log
823+
[error]
824+
start privileged agent process
825+
826+
827+
828+
=== TEST 22: destory Lua VM in cache processes (with privileged agent)
829+
--- http_config
830+
lua_package_path "../lua-resty-core/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;";
831+
832+
#lua_shared_dict dogs 1m;
833+
834+
proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:1m;
835+
836+
init_by_lua_block {
837+
assert(require "ngx.process".enable_privileged_agent())
838+
-- require "resty.core.regex"
839+
-- assert(ngx.re.match("hello, world", [[hello, \w+]], "joi"))
840+
}
841+
842+
--- config
843+
location = /t {
844+
return 200;
845+
}
846+
--- request
847+
GET /t
848+
--- grep_error_log eval: qr/lua close the global Lua VM \S+ in the cache helper process \d+|lua close the global Lua VM \S+$/
849+
--- grep_error_log_out eval
850+
qr/\Alua close the global Lua VM ([0-9A-F]+) in the cache helper process \d+
851+
lua close the global Lua VM \1
852+
lua close the global Lua VM \1 in the cache helper process \d+
853+
lua close the global Lua VM \1
854+
(?:lua close the global Lua VM [0-9A-F]+
855+
)*\z/
856+
--- error_log eval
857+
qr/start privileged agent process \d+/
858+
--- no_error_log
859+
[error]
860+
861+
862+
863+
=== TEST 23: destory Lua VM in cache processes (with init worker and privileged agent)
864+
--- http_config
865+
lua_package_path "../lua-resty-core/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;";
866+
867+
#lua_shared_dict dogs 1m;
868+
869+
proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:1m;
870+
871+
init_by_lua_block {
872+
assert(require "ngx.process".enable_privileged_agent())
873+
-- require "resty.core.regex"
874+
-- assert(ngx.re.match("hello, world", [[hello, \w+]], "joi"))
875+
}
876+
877+
init_worker_by_lua_block {
878+
ngx.log(ngx.WARN, "hello from init worker by lua")
879+
}
880+
881+
--- config
882+
location = /t {
883+
return 200;
884+
}
885+
--- request
886+
GET /t
887+
--- grep_error_log eval: qr/hello from init worker by lua/
888+
--- grep_error_log_out
889+
hello from init worker by lua
890+
hello from init worker by lua
891+
892+
--- error_log eval
893+
[
894+
qr/start privileged agent process \d+$/,
895+
qr/lua close the global Lua VM ([0-9A-F]+) in the cache helper process \d+$/,
896+
qr/lua close the global Lua VM ([0-9A-F]+)$/,
897+
]
898+
--- no_error_log
899+
[error]
900+
901+
902+
903+
=== TEST 24: destory Lua VM in cache processes (with init worker but without privileged agent)
904+
--- http_config
905+
lua_package_path "../lua-resty-core/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;";
906+
907+
#lua_shared_dict dogs 1m;
908+
909+
proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:1m;
910+
911+
init_by_lua_block {
912+
-- require "resty.core.regex"
913+
-- assert(ngx.re.match("hello, world", [[hello, \w+]], "joi"))
914+
}
915+
916+
init_worker_by_lua_block {
917+
ngx.log(ngx.WARN, "hello from init worker by lua")
918+
}
919+
920+
--- config
921+
location = /t {
922+
return 200;
923+
}
924+
--- request
925+
GET /t
926+
927+
--- grep_error_log eval: qr/hello from init worker by lua/
928+
--- grep_error_log_out
929+
hello from init worker by lua
930+
931+
--- error_log eval
932+
[
933+
qr/lua close the global Lua VM ([0-9A-F]+) in the cache helper process \d+$/,
934+
qr/lua close the global Lua VM ([0-9A-F]+)$/,
935+
]
936+
--- no_error_log
937+
[error]
938+
start privileged agent process

t/135-worker-id.t

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ workers(2);
99

1010
#repeat_each(2);
1111

12-
plan tests => repeat_each() * (blocks() * 3 + 1);
12+
plan tests => repeat_each() * (blocks() * 3);
1313

1414
#no_diff();
1515
#no_long_string();
@@ -31,63 +31,3 @@ GET /lua
3131
--- no_error_log
3232
[error]
3333
--- skip_nginx: 3: <=1.9.0
34-
35-
36-
37-
=== TEST 2: worker id should be nil for non-worker processes
38-
--- http_config
39-
proxy_cache_path conf/cache levels=1:2 keys_zone=my-cache:8m max_size=10m inactive=60m;
40-
proxy_temp_path conf/temp;
41-
42-
lua_shared_dict counters 1m;
43-
44-
init_by_lua_block {
45-
ngx.shared.counters:set("c", 0)
46-
}
47-
48-
init_worker_by_lua_block {
49-
ngx.shared.counters:incr("c", 1)
50-
ngx.log(ngx.INFO, ngx.worker.pid(), ": worker id ", ngx.worker.id());
51-
}
52-
--- config
53-
location = /t {
54-
content_by_lua_block {
55-
local counters = ngx.shared.counters
56-
local ok, c
57-
for i = 1, 100 do
58-
c = counters:get("c")
59-
if c >= 4 then
60-
ok = true
61-
break
62-
end
63-
local delay = 0.001 * i
64-
if delay > 0.1 then
65-
delay = 0.1
66-
end
67-
ngx.sleep(delay)
68-
end
69-
if ok then
70-
ngx.say("ok")
71-
else
72-
ngx.say("not ok: c=", c)
73-
end
74-
}
75-
}
76-
location /cache {
77-
proxy_pass http://127.0.0.1:$server_port;
78-
proxy_cache my-cache;
79-
}
80-
--- request
81-
GET /t
82-
--- response_body
83-
ok
84-
--- grep_error_log eval: qr/worker id nil/
85-
--- grep_error_log_out
86-
worker id nil
87-
worker id nil
88-
--- no_error_log
89-
[error]
90-
--- wait: 0.1
91-
--- skip_nginx: 3: <=1.9.0
92-
--- log_level: info
93-
--- timeout: 6

0 commit comments

Comments
 (0)