thibaultcha / lua-cassandra

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

Error in cluster module #92

Closed kushalkh closed 7 years ago

kushalkh commented 7 years ago

Hi,

I'm trying to use the cluster module as mentioned in the usage. Here is my code:

    local Cluster = require 'resty.cassandra.cluster'
local cluster, err = Cluster.new {
    shm = 'cassandra', -- defined by the lua_shared_dict directive
    contact_points = {'127.0.0.1'},
    keyspace = 'cookie'
}
if not cluster then
    ngx.say(ngx.ERR, 'could not create cluster: ', err)
    ngx.exit(200)
else
    local rows = cluster.execute("SELECT uid FROM tableName where sid = ? limit 1", {sid})
end

I'm seeing an error on execute command. Error:

2017/04/17 17:00:18 [error] 28879#28879: *68 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/cassandra/cluster.lua:837: attempt to call method 'refresh' (a nil value) stack traceback: coroutine 0: /usr/local/openresty/lualib/resty/cassandra/cluster.lua: in function </usr/local/openresty/lualib/resty/cassandra/cluster.lua:835>

What could i be missing? I installed it like it was mentioned.

avadhutp commented 7 years ago

@kushalkh Instead of this

if not cluster then
    ngx.say(ngx.ERR, 'could not create cluster: ', err)
    ngx.exit(200)
else
    local rows = cluster.execute("SELECT uid FROM tableName where sid = ? limit 1", {sid})
end

could you do this

if err ~= nil then
    ngx.say(ngx.ERR, 'could not create cluster: ', err)
    ngx.exit(200)
else
    local rows = cluster.execute("SELECT uid FROM tableName where sid = ? limit 1", {sid})
end

And then send the output?

kushalkh commented 7 years ago

Hi @avadhutp

The error actually pops up when "execute" function is called. There is no error in creating the cluster connection so it always goes into the else statement and tries executing the query.

Thanks for taking a look at this. Looking forward to further advise.

kushalkh commented 7 years ago

On a side note, i'm able to get single server connection and execute query perfectly. I'm seeing this error only on cluster module.

thibaultcha commented 7 years ago

@kushalkh You need to call functions like so: cluster:execute() and not cluster.execute(). I suggest you spend some time reading a bit about the Lua syntax :)