# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua;use Cwd qw(cwd);
repeat_each(1);
plan tests => repeat_each() * (3 * blocks());
my $pwd = cwd();
our $HttpConfig = qq{ resolver \$TEST_NGINX_RESOLVER; lua_package_path "$pwd/lib/?.lua;$pwd/t/lib/?.lua;;"; lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;";};
$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';$ENV{TEST_NGINX_ZOOKEEPER_HOST} = '127.0.0.1';$ENV{TEST_NGINX_ZOOKEEPER_POST} = 2181; log_level 'debug';no_shuffle();#no_long_string();
run_tests();
__DATA__
=== TEST 1: connect Connect and disconnect form zookeeper--- http_config eval: $::HttpConfig--- configlocation = /t { content_by_lua ' local zookeeper = require "resty.zookeeper"
local zoo = zookeeper:new() local ok, err = zoo:connect("$TEST_NGINX_ZOOKEEPER_HOST", $TEST_NGINX_ZOOKEEPER_POST) if not ok then ngx.say("connect failed ", err) return end ngx.say("connected") zoo:close()
';}--- requestGET /t--- response_bodyconnected
:~/Development/lua-resty-zookeeper$ ./got/query.t .. 1/3# Failed test 'ERROR: client socket timed out - TEST 1: connect# '# at /Library/Perl/5.18/Test/Nginx/Socket.pm line 1710.
# Failed test 'TEST 1: connect - status code ok'# at /Library/Perl/5.18/Test/Nginx/Socket.pm line 891.# got: ''# expected: '200'
# Failed test 'TEST 1: connect - response_body - response is expected (req 0)'# at /Library/Perl/5.18/Test/Nginx/Socket.pm line 1282.# got: ""# length: 0# expected: "connected\x{0a}"# length: 10# strings begin to differ at char 1 (line 1 column 1)t/query.t .. Failed 3/3 subtests
Test Summary Report-------------------t/query.t (Wstat: 0 Tests: 3 Failed: 3) Failed tests: 1-3Files=1, Tests=3, 3 wallclock secs ( 0.02 usr 0.00 sys + 0.13 cusr 0.03 csys = 0.18 CPU)Result: FAIL
thanks for the quick reply,
--[[ send pings as long as the state is connected to keep the tcp connection from timing out ]]local function ping(self) local req = _str_byte4(-2) .. _str_byte4(11)
while self.state == STATE.CONNECTED do local timeout = floor(self.props.timeout / 3000) ngx.sleep(timeout) ngx.log(ngx.DEBUG, "ping: send, timout: ", timeout)
-- send ping, do not wait for any reponse send(self, req) endend
2016/06/29 20:03:44 [debug] 95200#0: *6 event timer del: 587210664: 14672558290952016/06/29 20:03:44 [debug] 95200#0: *6 lua run thread, top:0 c:12016/06/29 20:03:44 [debug] 95200#0: *6 [lua] zookeeper.lua:148: send(): sama continue:2016/06/29 20:03:44 [debug] 95200#0: *6 [lua] zookeeper.lua:452: close(): ping thread killed2016/06/29 20:03:44 [debug] 95200#0: *6 lua deleting light thread2016/06/29 20:03:44 [debug] 95200#0: *6 [lua] zookeeper.lua:459: close(): receive thread killed2016/06/29 20:03:44 [debug] 95200#0: *6 lua finalize socket2016/06/29 20:03:44 [debug] 95200#0: *6 lua http cleanup free: 00007FDE230016482016/06/29 20:03:44 [debug] 95200#0: *6 lua close socket connection2016/06/29 20:03:44 [debug] 95200#0: *6 free: 00007FDE20700200, unused: 482016/06/29 20:03:44 [debug] 95200#0: *6 reusable connection: 02016/06/29 20:03:44 [debug] 95200#0: *6 lua resume returned 02016/06/29 20:03:44 [debug] 95200#0: *6 lua light thread ended normally2016/06/29 20:03:44 [debug] 95200#0: *6 lua deleting light thread2016/06/29 20:03:44 [debug] 95200#0: *6 lua run thread returned -22016/06/29 20:03:44 [debug] 95200#0: worker cycle2016/06/29 20:03:44 [debug] 95200#0: kevent timer: 9999, changes: 02016/06/29 20:03:54 [debug] 95200#0: kevent events: 02016/06/29 20:03:54 [debug] 95200#0: timer delta: 99992016/06/29 20:03:54 [debug] 95200#0: *6 event timer del: 0: 14672558340952016/06/29 20:03:54 [debug] 95200#0: *6 lua sleep timer expired: "/zookeeper?"2016/06/29 20:03:54 [debug] 95200#0: *6 lua run thread, top:0 c:12016/06/29 20:03:54 [debug] 95200#0: *6 [lua] zookeeper.lua:179: ping: send, timout: 102016/06/29 20:03:54 [debug] 95200#0: *6 [lua] zookeeper.lua:136: send(): Packet Size: 82016/06/29 20:03:54 [error] 95200#0: *6 attempt to send data on a closed socket: u:000000000C5D79E8, c:0000000000000000, ft:0 eof:0, client: 127.0.0.1, server: , request: "GET /zookeeper HTTP/1.1", host: "localhost:1080"2016/06/29 20:03:54 [debug] 95200#0: *6 lua resume returned 02016/06/29 20:03:54 [debug] 95200#0: *6 lua light thread ended normally2016/06/29 20:03:54 [debug] 95200#0: *6 lua deleting light thread2016/06/29 20:03:54 [debug] 95200#0: *6 http set discard body2016/06/29 20:03:54 [debug] 95200#0: *6 lua sending last buf of the response body2016/06/29 20:03:54 [debug] 95200#0: *6 http output filter "/zookeeper?"2016/06/29 20:03:54 [debug] 95200#0: *6 http copy filter: "/zookeeper?"2016/06/29 20:03:54 [debug] 95200#0: *6 lua capture body filter, uri "/zookeeper"2016/06/29 20:03:54 [debug] 95200#0: *6 http postpone filter "/zookeeper?" 00007FFF5388ED602016/06/29 20:03:54 [debug] 95200#0: *6 http chunk: 0
Hello!
On Wed, Jun 29, 2016 at 8:15 PM, Ingo Sauerzapf wrote:
> Yes every connection to zookeeper needs to
> periodically send a ping to one of the servers in a zookeeper cluster in
> order for the server to keep it alive.
Seems like it's much easier, more transparent, and more efficient to
let your kernel's TCP stack do "TCP keepalive" for this.
TCP keepalive is not enough. Zookeeper expects each client to
periodically send a Zookeeper ping which is part of the protocol.
I got that covered, not a problem.
> Of course one can just ignore that
> and just do a quick connect(), dosomething(), close(), but that would
> basically violate the idea of zookeeper where clients are expected to be
> permanently connected in order to receive notifications on changes happening
> in zookeeper.
Then you shouldn't create the zookeeper connection in your request
handler in the first place since requests do quit fast.
I fully agree. I am just writing tests right now and in them I want the connection to be short lived.
A real use of the library would however not be in the request.
>
> I am now seeing the sleep timer expiring 10 seconds after the ping thread
> was killed
Then you should kill the light thread doing sleeping as well.
I am killing the thread doing the sleeping and the log shows that
the thread is killed but it is not working and nginx still waits. I will try some more
I must be doing a stupid mistake that I am not able to see right now.
Regards,
-agentzh