antirez/redis — Issue #233: [Feature Request] Lua script debugging tool
Author: barneyii
Created: 2011-12-04 18:06:36+00:00
State: open

I think it would be very useful to be able to write lua scripts in an environment which makes debugging possible.

The best approach I can think of is to allow lua scripts to be run by developers, on their own machine, in an environment which imitates running the script on the redis server. This could be done by providing a lua redisTest module that exports a runScript function expecting the arguments (script, numkeys, key [, key ...], arg [, arg ...]) - that is, exactly the same arguments as expected by the EVAL function. The runScript function would create the KEYS and ARGV tables and run script() with a "redis" module in scope. The redis module would be a lua implementation of the redis client that exactly mirrors the behavior of calls to redis.call() or pcall() in server-side lua scripts.

Example:
-------------------------------------------------------------------
require "redisTest"

redisTest.host = 'localhost'
redisTest.port = 6379

function simpleRedisScript()
return redis.call('set',KEYS[1],'bar')
end

redisTest.runScript( simpleRedisScript, 1, 'foo' )
-------------------------------------------------------------------

redis.call() would simply dispatch a standard request to the redis server and return the results to the lua script with the same semantics as lua scripts run on the server. The runScript function might wrap the script's commands in a transaction to better approach the behavior of scripts in a real context, but it is not really that important for lua scripts run in this way to behave exactly the same as server-side lua scrips from the perspective of the server because the purpose of running scripts locally is to test the script itself. Once the script is known to work in isolation it can be sent to redis normally to test its behavior in proper context. There is therefore no need to prevent developers adding print statements and calls to system time in their locally run scripts - indeed these would be very useful for debugging.

I think an officially supported and documented redisTest lua module will really stimulate creative uses of redis scripting.

Looking forward to feedback about this idea.