lua 脚本修改后,如何能安全重load

222 views
Skip to first unread message

ganggewudi

unread,
Oct 23, 2012, 11:11:07 PM10/23/12
to open...@googlegroups.com
lua脚本修改后,如果用nginx -s reload,能做到安全加载吗?我服务器开启着数据库insert的服务,我发现有时候我修改lua之后,reload会导致偶尔出现插入失败的情况,请agentzh大神告诉我一下是什么原因啊,这是不是潜藏的隐患?服务24小时开启的,导致丢失数据了,虽然不是每次都出现,但是偶尔reload就会导致在那个reload的时间点,有一条或者两条数据插入失败了

agentzh

unread,
Oct 23, 2012, 11:17:45 PM10/23/12
to open...@googlegroups.com
Hello!

2012/10/23 ganggewudi:


> lua脚本修改后,如果用nginx -s
> reload,能做到安全加载吗?

如果你的 nginx 可执行文件没有发生变化,根据设计应当是可以的。

不过在 *NIX 系统中最好还是通过显式发送 HUP 信号给 nginx 的 master 进程来重新加载配置。

> 我服务器开启着数据库insert的服务,我发现有时候我修改lua之后,reload会导致偶尔出现插入失败的情况,请agentzh大神告诉我一下是什么原因啊,这是不是潜藏的隐患?

你的 nginx 错误日志的原始信息贴出来看看?

> 服务24小时开启的,导致丢失数据了,虽然不是每次都出现,但是偶尔reload就会导致在那个reload的时间点,有一条或者两条数据插入失败了
>

我需要更多的调试信息 :) 你能在完全可控的测试或者开发环境中复现这个问题吗?

Best regards,
-agentzh

ganggewudi

unread,
Oct 23, 2012, 11:29:28 PM10/23/12
to open...@googlegroups.com
关键就是偶尔出现啊,比如我reload一下,然后看一下error,偶尔就出现一条insert失败,日志就是一条插入失败的记录,记录了一下时间而已,与我reload的时候是同时的,时间上来说。这个问题重现我没办法展示出来啊。

在 2012年10月24日星期三UTC+8上午11时17分46秒,agentzh写道:

agentzh

unread,
Oct 23, 2012, 11:32:09 PM10/23/12
to open...@googlegroups.com
Hello!

2012/10/23 ganggewudi:


> 关键就是偶尔出现啊,比如我reload一下,然后看一下error,偶尔就出现一条insert失败,日志就是一条插入失败的记录,记录了一下时间而已,与我reload的时候是同时的,时间上来说。这个问题重现我没办法展示出来啊。
>

请提供原始的错误日志信息(越多的不同典型越好)。你的自然语言描述会损失很多关键信息 :) 多谢!

Best regards,
-agentzh

Message has been deleted

ganggewudi

unread,
Oct 23, 2012, 11:40:50 PM10/23/12
to open...@googlegroups.com
基本上就这两个,我都是reload第一时间打开error,log的,不是每次都出现,但是也出现多次了。

2012/10/23 16:11:02 [error] 14276#0: *54929 lua tcp socket read timed out, client: 222.73.27.71, server: localhost, request: "GET http://xx.xx.xx.xx:xxxx/insert?table=online_num(ip,serType,ver,num,operTime)&val=values('222.73.27.71',0,1610107,4,'2012-10-23%2016:11:00') HTTP/1.1", host: "222.73.27.71"

2012/10/23 16:11:02 [error] 14276#0: *54929 [lua] db_insert.lua:19: dodbquery(): failed to connect mysql, client: 222.73.27.71, server: localhost, request: "GET http://xx.xx.xx.xx:xxxx/insert?table=online_num(ip,serType,ver,num,operTime)&val=values('222.73.27.71',0,1610107,4,'2012-10-23%2016:11:00') HTTP/1.1", host: "222.73.27.71"

在 2012年10月24日星期三UTC+8上午11时32分09秒,agentzh写道:

agentzh

unread,
Oct 23, 2012, 11:47:29 PM10/23/12
to open...@googlegroups.com
Hello!

2012/10/23 ganggewudi:


> 基本上就这两个,我都是reload第一时间打开error,log的,不是每次都出现,但是也出现多次了。
>
> 2012/10/23 16:11:02 [error] 14276#0: *54929 lua tcp socket read timed out,
> client: 222.73.27.71, server: localhost, request: "GET
> http://xx.xx.xx.xx:xxxx/insert?table=online_num(ip,serType,ver,num,operTime)&val=values('222.73.27.71',0,1610107,4,'2012-10-23%2016:11:00')
> HTTP/1.1", host: "222.73.27.71"
>
> 2012/10/23 16:11:02 [error] 14276#0: *54929 [lua] db_insert.lua:19:
> dodbquery(): failed to connect mysql, client: 222.73.27.71, server:
> localhost, request: "GET
> http://xx.xx.xx.xx:xxxx/insert?table=online_num(ip,serType,ver,num,operTime)&val=values('222.73.27.71',0,1610107,4,'2012-10-23%2016:11:00')
> HTTP/1.1", host: "222.73.27.71"
>

你能使用 notice 错误日志级别(通过配置 error_log
指令:http://wiki.nginx.org/CoreModule#error_log),并把这两条消息前后的大约 20 ~ 50
行消息都完整地贴出来么?我需要知道这两条消息出现的具体的上下文。

另外,你使用的 nginx 和 ngx_lua 的版本?你所在的操作系统的信息?

Best regards,
-agentzh

Message has been deleted

agentzh

unread,
Oct 24, 2012, 6:07:18 PM10/24/12
to open...@googlegroups.com
Hello!

On Tue, Oct 23, 2012 at 9:09 PM, ganggewudi wrote:
> openresty 1.2.1.14
> OS是Linux version 2.6.32-279.5.2.el6.x86_64
> (mock...@c6b10.bsys.dev.centos.org) (gcc version 4.4.6 20120305 (Red Hat
> 4.4.6-4) (GCC) ) #1 SMP Fri Aug 24 01:07:11 UTC 2012

你可以考虑升级到最新的 openresty 稳定版 1.2.3.8 或者开发版 1.2.4.3 :)

http://openresty.org/#Download

然后使用 Nginx 热替换功能进行动态替换:

http://wiki.nginx.org/CommandLine#Upgrading_To_a_New_Binary_On_The_Fly

注意,当你升级 Nginx 本身时,-sreload 和 HUP 信号重新加载配置是无效的。

> 日志:
> 2012/10/23 11:57:00 [error] 13828#0: *53098 [lua] db_func.lua:24:
> dodbquery(): Access denied for user 'xxxx'@'xx.xx.xx.xx' to database
> 'p2preport', client: 222.73.27.71, server: localhost, request: "GET
> http://114.80.122.98:8080/insert?table=online_num(ip,serType,ver,num,operTime)&val=values('222.73.27.71',0,1610001,600,'2012-10-23%2011:57:00')
> HTTP/1.1", host: "222.73.27.71"
> 2012/10/23 11:57:00 [error] 13828#0: *53098 [lua] db_func.lua:24:
> dodbquery(): Access denied for user 'xxxx'@' xx.xx.xx.xx ' to database
> 'p2preport', client: 222.73.27.71, server: localhost, request: "GET http://
> xx.xx.xx.xx
> :xxxx/insert?table=online_num(ip,serType,ver,num,operTime)&val=values('222.73.27.71',0,1610118,5222,'2012-10-23%2011:57:00')
> HTTP/1.1", host: "222.73.27.71"
> 2012/10/23 13:04:08 [error] 13828#0: *53383 access forbidden by rule,
> client: 180.168.168.82, server: localhost, request: "HEAD / HTTP/1.0"
> 2012/10/23 13:18:48 [error] 13828#0: *53444 access forbidden by rule,
> client: 60.173.11.149, server: localhost, request: "GET http://im.qq.com/
> HTTP/1.1", host: "im.qq.com"
> 2012/10/23 16:11:01 [notice] 14275#0: signal process started

很奇怪的是,你提供的日志中只有这一行 notice 消息。在 HUP 重启时,应该会看到下面这些消息(按出现顺序):

2012/10/24 14:45:20 [notice] 3574#0: signal process started
2012/10/24 14:45:20 [notice] 3428#0: signal 1 (SIGHUP) received, reconfiguring
2012/10/24 14:45:20 [notice] 3428#0: reconfiguring
2012/10/24 14:45:20 [notice] 3428#0: using the "epoll" event method
2012/10/24 14:45:20 [notice] 3428#0: start worker processes
2012/10/24 14:45:20 [notice] 3428#0: start worker process 3587
2012/10/24 14:45:20 [notice] 3429#0: gracefully shutting down
2012/10/24 14:45:20 [notice] 3429#0: exiting
2012/10/24 14:45:20 [notice] 3429#0: exit
2012/10/24 14:45:20 [notice] 3428#0: signal 17 (SIGCHLD) received
2012/10/24 14:45:20 [notice] 3428#0: worker process 3429 exited with code 0
2012/10/24 14:45:20 [notice] 3428#0: signal 29 (SIGIO) received

为何你提供的日志中只出现了第一行消息?是因为你提供的日志还不够完整么?

> 2012/10/23 16:11:02 [error] 14276#0: *54929 lua tcp socket read timed out,
> client: 222.73.27.71, server: localhost, request: "GET http:// xx.xx.xx.xx
> :xxxx
> /insert?table=online_num(ip,serType,ver,num,operTime)&val=values('222.73.27.71',0,1610107,4,'2012-10-23%2016:11:00')
> HTTP/1.1", host: "222.73.27.71"
> 2012/10/23 16:11:02 [error] 14276#0: *54929 [lua] db_insert.lua:19:
> dodbquery(): failed to connect mysql, client: 222.73.27.71, server:
> localhost, request: "GET http:// xx.xx.xx.xx :xxxx
> /insert?table=online_num(ip,serType,ver,num,operTime)&val=values('222.73.27.71',0,1610107,4,'2012-10-23%2016:11:00')
> HTTP/1.1", host: "222.73.27.71"

你能在 Lua 代码中把方法调用返回的 err 出错消息也打印出来么?同时你这条消息是在哪一步操作失败时打印的?是 connect 还是 query 还是其他?

另外,你是否启用了 mysql 连接池?

我使用简单的 lua-resty-mysql 接口查询 select sleep(0.1) 然后施加 ab 10 并发压力再反复使用 HUP
重启,无法复现你的问题(我得不到任何错误)。

你可以考虑尝试使用前面提到的热替换技术替代 -sreload,看看问题是否依旧。

另外也可以尝试把 lua-resty-mysql 对象的超时设置适当调大(我从日志上估计你应当设置的是 1 秒),看看是否问题会消失。

Best regards,
-agentzh

Reply all
Reply to author
Forward
0 new messages