thibaultcha / lua-cassandra

Pure Lua driver for Apache Cassandra
https://thibaultcha.github.io/lua-cassandra
Other
98 stars 35 forks source link

cluster mode has connection pool ? #82

Closed zhenzhenyang closed 7 years ago

zhenzhenyang commented 7 years ago

lua cassandra socket.lua not use https://github.com/openresty/lua-nginx-module#tcpsockconnect,why?

2017/01/16 20:58:37 [alert] 3128#0: worker process 15100 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15111 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15113 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15115 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15117 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15119 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15121 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15123 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15125 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15127 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15129 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15131 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15133 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15135 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15137 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [alert] 3128#0: worker process 15139 exited on signal 11 (core dumped) 2017/01/16 20:58:37 [warn] 15141#0: 14775 [lua] socket.lua:152: tcp(): no support for cosockets in this context, falling back to LuaSocket, context: init_worker_by_lua 2017/01/16 20:58:37 [warn] 15143#0: 14776 [lua] socket.lua:152: tcp(): no support for cosockets in this context, falling back to LuaSocket, context: init_worker_by_lua

================================================ when I reload the nginx, http module error log reports upon errors

thibaultcha commented 7 years ago

lua cassandra socket.lua not use https://github.com/openresty/lua-nginx-module#tcpsockconnect,why?

It does if the phase you are using this library in supports cosockets.

when I reload the nginx, http module error log reports upon errors

Please provide a minimally reproducible example of the problem you think you are encountering, and how you think it should behave. Also, please note the reported log:

2017/01/16 20:58:37 [warn] 15143#0: 14776 [lua] socket.lua:152: tcp(): no support for cosockets in this context, falling back to LuaSocket, context: init_worker_by_lua

Which I believe is explicit enough.

craveica commented 7 years ago

https://github.com/thibaultcha/lua-cassandra/blob/master/docs/examples/intro.lua#L87-L94 will make the workers crash. It might work in init_by_lua_block instead.

thibaultcha commented 7 years ago

This example does not crash workers on my side. However, it would be better to call it in the init phase indeed, or to rely on the first request in a compatible context (such as content_by_lua) to refresh the cluster.

zhenzhenyang commented 7 years ago

thanks very much,as you said , lua-cassandra supports cosocket,I tried successfully. thanks again. when I reload the nginx,nginx http phase produce errors: 2017/01/17 16:14:10 [alert] 23101#0: worker process 26556 exited on signal 11 (core dumped)

then I modify the code on the picture below,there is no errors.

qq 20170119135049

but when I use "ab" to press the nginx,it produce errors below and the "ab" results is very bad, has 443 fails

2017/01/19 13:41:44 [error] 30527#0: 35470 [lua] content_by_lua(nginx.conf:62):16: could not retrieve users: could not refresh cluster: failed to acquire refresh lock: timeout, client: 10.1.15.105, server: localhost, request: "GET /cass HTTP/1.0", host: "10.1.15.105:8088" 2017/01/19 13:41:44 [error] 30527#0: 35471 [lua] content_by_lua(nginx.conf:62):16: could not retrieve users: could not refresh cluster: failed to acquire refresh lock: timeout, client: 10.1.15.105, server: localhost, request: "GET /cass HTTP/1.0", host: "10.1.15.105:8088" 2017/01/19 13:41:44 [error] 30527#0: *35472 [lua] content_by_lua(nginx.conf:62):16: could not retrieve users: could not refresh cluster: failed to acquire refresh lock: timeout, client: 10.1.15.105, server: localhost, request: "GET /cass HTTP/1.0", host: "10.1.15.105:8088" ab results:

[root@10.1.15.105 cassandra]# ab -n 1000 -c 500 'http://10.1.15.105:8088/cass' This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 10.1.15.105 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests

Server Software: openresty/1.11.2.1 Server Hostname: 10.1.15.105 Server Port: 8088

Document Path: /cass Document Length: 8 bytes

Concurrency Level: 500 Time taken for tests: 7.004 seconds Complete requests: 1000 Failed requests: 443 (Connect: 0, Receive: 0, Length: 443, Exceptions: 0) Write errors: 0 Non-2xx responses: 443 Total transferred: 418523 bytes HTML transferred: 244119 bytes Requests per second: 142.77 [#/sec] (mean) Time per request: 3502.225 [ms] (mean) Time per request: 7.004 [ms] (mean, across all concurrent requests) Transfer rate: 58.35 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 0 286 440.2 22 1001 Processing: 111 2687 2102.0 953 5203 Waiting: 111 2687 2102.0 953 5203 Total: 111 2972 2418.8 968 6010

Percentage of the requests served within a certain time (ms) 50% 968 66% 5111 75% 6003 80% 6004 90% 6006 95% 6008 98% 6008 99% 6009 100% 6010 (longest request)

my code below: server { listen 8088; server_name localhost; access_log logs/host.access.log main; error_log logs/host.error.log debug; location /cass { access_log logs/cass.log main; error_log logs/ecass.log debug; content_by_lua_block { local Cluster = require 'resty.cassandra.cluster' local cluster, err = Cluster.new { shm = 'cassandra', -- defined by the lua_shared_dict directive contact_points = {"10.10.121.152", "10.10.121.151","10.10.121.149","10.10.121.148","10.10.121.139","10.10.121.138","10.10.121.120","10.10.121.121","10.10.121.122","10.10.121.119"}, keyspace = 'play_record', connect_timeout=3000 } if not cluster then ngx.log(ngx.ERR, 'could not create cluster: ', err) return ngx.exit(500) end local rows, err = cluster:execute("select * from audience.livod_vt_partner_chn where partner='ZJYD' and chn_code='G_CCTV-8' and vt='3' limit 1") if not rows then ngx.log(ngx.ERR, 'could not retrieve users: ', err) return ngx.exit(500) end for i, row in ipairs(rows) do ngx.say(i, ": ", rows[i].numerator) end } }

thibaultcha commented 7 years ago

You should avoid creating the cluster in each request and use the technique described in the examples where the cluster is stored as an upvalue in the main chunk of a module. This is a common technique in ngx_lua to have variables with a lifecycle that lasts beyond a single request.

zhenzhenyang commented 7 years ago

but the cluster.lua has coordinator:setkeepalive(), cosocket setkeepalive function is put the connection in the pools .I modified the setkeepalive : coordinator:setkeepalive(300000,100)

thibaultcha commented 7 years ago

btw, I tried this example on my side and I am able to reload nginx without any worker crashing.

thibaultcha commented 7 years ago

but the cluster.lua has coordinator:setkeepalive(), cosocket setkeepalive function is put the connection in the pools .I modified the setkeepalive : coordinator:setkeepalive(300000,100)

What is your point? You wish to configure the connection pool from the cluster API? I have trouble understanding what you are expecting from this conversation, please clarify.

zhenzhenyang commented 7 years ago

I expect I use the lua-cassandra cluster mode as redis.lua in OR. I no need create the cluster instance at init phase , I create the cluster in each request ,and the lua-cassandra driver put the request into the connection pools.

thibaultcha commented 7 years ago

Well, that's not how this module works, sorry. Each library has different needs and their usage varies. The lua-resty-redis module does not have to handle the same level of complexity as this library does by being statefull and dealing with a cluster of nodes, their health, prepared statements, load balancing policies, etc...

thibaultcha commented 7 years ago

This library can be used like the lua-resty-redis one if you remove the cluster abstraction (that lua-resty-redis does not have in the first place) and simply use the cassandra module.

zhenzhenyang commented 7 years ago

ok,thank you very very very much ,I will try put in init phase .

thibaultcha commented 7 years ago

Considering this resolved then!