Redis系列六:高并发场景中如何Debug Lua
Posted IT实战联盟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis系列六:高并发场景中如何Debug Lua相关的知识,希望对你有一定的参考价值。
调试redis+lua
基础
-- script.lua
local ping = redis.call('ping')
return ping
# 执行脚本 --ldb --eval
➜ 7Debug git:(main) ✗ redis-cli --ldb --eval /Users/mw/Desktop/Github/Integration/Redis/7Debug/script.lua
Lua debugging session started, please use:
quit -- End the session.
restart -- Restart the script in debug mode again.
help -- Show Lua script debugging commands.
* Stopped at 1, stop reason = step over
# 调试位置
-> 1 local ping = redis.call('ping')
# next
lua debugger> n
<redis> ping
<reply> "+PONG"
* Stopped at 2, stop reason = step over
# 调试位置
-> 2 return ping
# 打印lua变量
lua debugger> print ping
<value> {["ok"]="PONG"}
# next
lua debugger> n
# lua返回的结果
PONG
# 调试结束
(Lua debugging session ended -- dataset changes rolled back)
# 退出redis-cli
127.0.0.1:6379> exit
➜ 7Debug git:(main) ✗
-- script1.lua
-- 测试接受参数
local src = KEYS[1]
local dst = KEYS[2]
local count = tonumber(ARGV[1])
参数
KEYS & ARGV
传递参数时的注意事项
# 传参数: list_a list_b 10
➜ 7Debug git:(main) ✗ redis-cli --ldb --eval /Users/mw/Desktop/Github/Integration/Redis/7Debug/script1.lua list_a list_b 10
Lua debugging session started, please use:
quit -- End the session.
restart -- Restart the script in debug mode again.
help -- Show Lua script debugging commands.
* Stopped at 1, stop reason = step over
-> 1 local src = KEYS[1]
#
lua debugger> print
No local variables in the current context.
#
lua debugger> n
* Stopped at 2, stop reason = step over
-> 2 local dst = KEYS[2]
#
lua debugger> n
* Stopped at 3, stop reason = step over
-> 3 local count = tonumber(ARGV[1])
# 打印 KEYS 全部都是keys
lua debugger> print KEYS
<value> {"list_a"; "list_b"; "10"}
# 打印 ARGV
lua debugger> print ARGV
<value> {}
# 结束
lua debugger> n
(integer) 1
(Lua debugging session ended -- dataset changes rolled back)
127.0.0.1:6379>
**参数 中 KEYS & ARGV 区分: 逗号隔开** 1. 参数`list_b,`, 逗号与key之间没有空格,则认为依旧是KEYS;
# 注意
➜ 7Debug git:(main) ✗ redis-cli --ldb --eval /Users/mw/Desktop/Github/Integration/Redis/7Debug/script1.lua list_a list_b , 10
Lua debugging session started, please use:
quit -- End the session.
restart -- Restart the script in debug mode again.
help -- Show Lua script debugging commands.
* Stopped at 1, stop reason = step over
-> 1 local src = KEYS[1]
# KEYS
lua debugger> print KEYS
<value> {"list_a"; "list_b"}
# ARGV
lua debugger> print ARGV
<value> {"10"}
lua debugger>
lua debugger> n
* Stopped at 2, stop reason = step over
-> 2 local dst = KEYS[2]
lua debugger> n
* Stopped at 3, stop reason = step over
-> 3 local count = tonumber(ARGV[1])
lua debugger> n
* Stopped at 4, stop reason = step over
-> 4 return true
#
lua debugger> print
<value> src = "list_a"
<value> dst = "list_b"
<value> count = 10
lua debugger> n
(integer) 1
(Lua debugging session ended -- dataset changes rolled back)
127.0.0.1:6379>
队列操作
将list_a中的一个元素push到list_b
➜ 7Debug git:(main) ✗ redis-cli --ldb --eval /Users/mw/Desktop/Github/Integration/Redis/7Debug/script1.lua list_a list_b , 10
Lua debugging session started, please use:
quit -- End the session.
restart -- Restart the script in debug mode again.
help -- Show Lua script debugging commands.
* Stopped at 1, stop reason = step over
-> 1 local src = KEYS[1]
# 打印keys
lua debugger> print KEYS
<value> {"list_a"; "list_b"}
# 打印参数
lua debugger> print ARGV
<value> {"10"}
# 初始化队列 list_a、list_b
lua debugger> r lpush list_a 1 2 3 4
<redis> lpush list_a 1 2 3 4
<reply> 4
lua debugger> r lpush list_b a b c d
<redis> lpush list_b a b c d
<reply> 4
lua debugger> n
* Stopped at 2, stop reason = step over
-> 2 local dst = KEYS[2]
lua debugger> n
* Stopped at 3, stop reason = step over
-> 3 local count = tonumber(ARGV[1])
lua debugger> n
* Stopped at 5, stop reason = step over
-> 5 local item = redis.call('rpop', src)
# next 执行pop
lua debugger> n
<redis> rpop list_a
<reply> "1"
* Stopped at 6, stop reason = step over
-> 6 redis.call('lpush', dst, item)
# next 执行push
lua debugger> n
<redis> lpush list_b 1
<reply> 5
* Stopped at 7, stop reason = step over
-> 7 return true
# r: redis; 获取list元素
lua debugger> r lrange list_a 0 -1
<redis> lrange list_a 0 -1
<reply> ["4","3","2"]
# r: redis; 获取list元素
lua debugger> r lrange list_b 0 -1
<redis> lrange list_b 0 -1
# 执行成功
<reply> ["1","d","c","b","a"]
# 重启,将所有执行回滚
lua debugger> restart
Lua debugging session started, please use:
quit -- End the session.
restart -- Restart the script in debug mode again.
help -- Show Lua script debugging commands.
* Stopped at 1, stop reason = step over
-> 1 local src = KEYS[1]
# 回滚至无数据
lua debugger> r lrange list_b 0 -1
<redis> lrange list_b 0 -1
<reply> []
lua debugger>
断点调试
# 写入redis 准备数据
➜ 7Debug git:(main) ✗ redis-cli
127.0.0.1:6379> lpush list_a 1 2 3 4
(integer) 4
127.0.0.1:6379> lpush list_b a b c d
(integer) 4
127.0.0.1:6379> llen list_a
(integer) 4
127.0.0.1:6379> llen list_b
(integer) 4
127.0.0.1:6379>
# 获取全部文件
lua debugger> w
1 local src = KEYS[1]
2 local dst = KEYS[2]
3 local count = tonumber(ARGV[1])
4
-> 5 while count < 10 do
6 local item = redis.call('rpop', src)
7 if item == false then break end
8 redis.call('lpush', dst, item)
9 count = count -1
10 end
11
12 return redis.call('llen', dst);
# 显示第7行 【l:list】
lua debugger> l 7
2 local dst = KEYS[2]
3 local count = tonumber(ARGV[1])
4
-> 5 while count < 10 do
6 local item = redis.call('rpop', src)
7 if item == false then break end
8 redis.call('lpush', dst, item)
9 count = count -1
10 end
11
12 return redis.call('llen', dst);
# 显示第7行前后一行 【l:list】
lua debugger> l 7 1
6 local item = redis.call('rpop', src)
7 if item == false then break end
8 redis.call('lpush', dst, item)
lua debugger> b 6
-> 5 while count < 10 do
#6 local item = redis.call('rpop', src)
7 if item == false then break end
# continue 继续,直到断点执行
lua debugger> c
(integer) 4
(Lua debugging session ended -- dataset changes rolled back)
127.0.0.1:6379>
逐步断点
lua debugger> w
-> 1 local src = KEYS[1]
2 local dst = KEYS[2]
3 local count = tonumber(ARGV[1])
4
5 while count > 0 do
6 local item = redis.call('rpop', src)
7 if item == false then break end
8 redis.call('lpush', dst, item)
9 count = count -1
10 end
11
12 return redis.call('llen', dst);
lua debugger> b 8
7 if item == false then break end
#8 redis.call('lpush', dst, item)
9 count = count -1
# 验证断点位置
lua debugger> w
-> 1 local src = KEYS[1]
2 local dst = KEYS[2]
3 local count = tonumber(ARGV[1])
4
5 while count > 0 do
6 local item = redis.call('rpop', src)
7 if item == false then break end
#8 redis.call('lpush', dst, item)
9 count = count -1
10 end
11
12 return redis.call('llen', dst);
# 等同与next
lua debugger> c
* Stopped at 8, stop reason = break point
->#8 redis.call('lpush', dst, item)
lua debugger> print
<value> src = "list_a"
<value> dst = "list_b"
<value> count = 10
<value> item = "1"
lua debugger> c
* Stopped at 8, stop reason = break point
->#8 redis.call('lpush', dst, item)
lua debugger> c
* Stopped at 8, stop reason = break point
->#8 redis.call('lpush', dst, item)
lua debugger> c
* Stopped at 8, stop reason = break point
->#8 redis.call('lpush', dst, item)
# 打印日志
lua debugger> print
<value> src = "list_a"
<value> dst = "list_b"
<value> count = 7
<value> item = "4"
redis.debug 一步到位
此处有加新代码:redis.debug('dubug value:', item)
lua debugger> w
-> 1 local src = KEYS[1]
2 local dst = KEYS[2]
3 local count = tonumber(ARGV[1])
4
5 while count > 0 do
6 local item = redis.call('rpop', src)
# 采用redis.debug
7 redis.debug('dubug value:', item)
8 if item == false then break end
9 redis.call('lpush', dst, item)
10 count = count -1
11 end
12
13 return redis.call('llen', dst);
# 继续,打印redis.debug处的日志
lua debugger> c
<debug> line 7: "dubug value:", "1"
<debug> line 7: "dubug value:", "2"
<debug> line 7: "dubug value:", "3"
<debug> line 7: "dubug value:", "4"
<debug> line 7: "dubug value:", false
(integer) 8
(Lua debugging session ended -- dataset changes rolled back)
127.0.0.1:6379>
以上是关于Redis系列六:高并发场景中如何Debug Lua的主要内容,如果未能解决你的问题,请参考以下文章