2
0

scripting.tcl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. start_server {tags {"scripting"}} {
  2. test {EVAL - Does Lua interpreter replies to our requests?} {
  3. r eval {return 'hello'} 0
  4. } {hello}
  5. test {EVAL - Lua integer -> Redis protocol type conversion} {
  6. r eval {return 100.5} 0
  7. } {100}
  8. test {EVAL - Lua string -> Redis protocol type conversion} {
  9. r eval {return 'hello world'} 0
  10. } {hello world}
  11. test {EVAL - Lua true boolean -> Redis protocol type conversion} {
  12. r eval {return true} 0
  13. } {1}
  14. test {EVAL - Lua false boolean -> Redis protocol type conversion} {
  15. r eval {return false} 0
  16. } {}
  17. test {EVAL - Lua status code reply -> Redis protocol type conversion} {
  18. r eval {return {ok='fine'}} 0
  19. } {fine}
  20. test {EVAL - Lua error reply -> Redis protocol type conversion} {
  21. catch {
  22. r eval {return {err='this is an error'}} 0
  23. } e
  24. set _ $e
  25. } {this is an error}
  26. test {EVAL - Lua table -> Redis protocol type conversion} {
  27. r eval {return {1,2,3,'ciao',{1,2}}} 0
  28. } {1 2 3 ciao {1 2}}
  29. test {EVAL - Are the KEYS and ARGV arrays populated correctly?} {
  30. r eval {return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}} 2 a b c d
  31. } {a b c d}
  32. test {EVAL - is Lua able to call Redis API?} {
  33. r set mykey myval
  34. r eval {return redis.call('get',KEYS[1])} 1 mykey
  35. } {myval}
  36. test {EVALSHA - Can we call a SHA1 if already defined?} {
  37. r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey
  38. } {myval}
  39. test {EVALSHA - Can we call a SHA1 in uppercase?} {
  40. r evalsha FD758D1589D044DD850A6F05D52F2EEFD27F033F 1 mykey
  41. } {myval}
  42. test {EVALSHA - Do we get an error on invalid SHA1?} {
  43. catch {r evalsha NotValidShaSUM 0} e
  44. set _ $e
  45. } {NOSCRIPT*}
  46. test {EVALSHA - Do we get an error on non defined SHA1?} {
  47. catch {r evalsha ffd632c7d33e571e9f24556ebed26c3479a87130 0} e
  48. set _ $e
  49. } {NOSCRIPT*}
  50. test {EVAL - Redis integer -> Lua type conversion} {
  51. r eval {
  52. local foo = redis.pcall('incr','x')
  53. return {type(foo),foo}
  54. } 0
  55. } {number 1}
  56. test {EVAL - Redis bulk -> Lua type conversion} {
  57. r set mykey myval
  58. r eval {
  59. local foo = redis.pcall('get','mykey')
  60. return {type(foo),foo}
  61. } 0
  62. } {string myval}
  63. test {EVAL - Redis multi bulk -> Lua type conversion} {
  64. r del mylist
  65. r rpush mylist a
  66. r rpush mylist b
  67. r rpush mylist c
  68. r eval {
  69. local foo = redis.pcall('lrange','mylist',0,-1)
  70. return {type(foo),foo[1],foo[2],foo[3],# foo}
  71. } 0
  72. } {table a b c 3}
  73. test {EVAL - Redis status reply -> Lua type conversion} {
  74. r eval {
  75. local foo = redis.pcall('set','mykey','myval')
  76. return {type(foo),foo['ok']}
  77. } 0
  78. } {table OK}
  79. test {EVAL - Redis error reply -> Lua type conversion} {
  80. r set mykey myval
  81. r eval {
  82. local foo = redis.pcall('incr','mykey')
  83. return {type(foo),foo['err']}
  84. } 0
  85. } {table {ERR value is not an integer or out of range}}
  86. test {EVAL - Redis nil bulk reply -> Lua type conversion} {
  87. r del mykey
  88. r eval {
  89. local foo = redis.pcall('get','mykey')
  90. return {type(foo),foo == false}
  91. } 0
  92. } {boolean 1}
  93. test {EVAL - Is the Lua client using the currently selected DB?} {
  94. r set mykey "this is DB 9"
  95. r select 10
  96. r set mykey "this is DB 10"
  97. r eval {return redis.pcall('get','mykey')} 0
  98. } {this is DB 10}
  99. test {EVAL - SELECT inside Lua should not affect the caller} {
  100. # here we DB 10 is selected
  101. r set mykey "original value"
  102. r eval {return redis.pcall('select','9')} 0
  103. set res [r get mykey]
  104. r select 9
  105. set res
  106. } {original value}
  107. if 0 {
  108. test {EVAL - Script can't run more than configured time limit} {
  109. r config set lua-time-limit 1
  110. catch {
  111. r eval {
  112. local i = 0
  113. while true do i=i+1 end
  114. } 0
  115. } e
  116. set _ $e
  117. } {*execution time*}
  118. }
  119. test {EVAL - Scripts can't run certain commands} {
  120. set e {}
  121. catch {r eval {return redis.pcall('spop','x')} 0} e
  122. set e
  123. } {*not allowed*}
  124. test {EVAL - Scripts can't run certain commands} {
  125. set e {}
  126. catch {
  127. r eval "redis.pcall('randomkey'); return redis.pcall('set','x','ciao')" 0
  128. } e
  129. set e
  130. } {*not allowed after*}
  131. test {EVAL - No arguments to redis.call/pcall is considered an error} {
  132. set e {}
  133. catch {r eval {return redis.call()} 0} e
  134. set e
  135. } {*one argument*}
  136. test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
  137. set e {}
  138. catch {
  139. r eval "redis.call('nosuchcommand')" 0
  140. } e
  141. set e
  142. } {*Unknown Redis*}
  143. test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
  144. set e {}
  145. catch {
  146. r eval "redis.call('get','a','b','c')" 0
  147. } e
  148. set e
  149. } {*number of args*}
  150. test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
  151. set e {}
  152. r set foo bar
  153. catch {
  154. r eval {redis.call('lpush',KEYS[1],'val')} 1 foo
  155. } e
  156. set e
  157. } {*against a key*}
  158. test {SCRIPTING FLUSH - is able to clear the scripts cache?} {
  159. r set mykey myval
  160. set v [r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey]
  161. assert_equal $v myval
  162. set e ""
  163. r script flush
  164. catch {r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey} e
  165. set e
  166. } {NOSCRIPT*}
  167. test {SCRIPT EXISTS - can detect already defined scripts?} {
  168. r eval "return 1+1" 0
  169. r script exists a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bd9 a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bda
  170. } {1 0}
  171. test {SCRIPT LOAD - is able to register scripts in the scripting cache} {
  172. list \
  173. [r script load "return 'loaded'"] \
  174. [r evalsha b534286061d4b9e4026607613b95c06c06015ae8 0]
  175. } {b534286061d4b9e4026607613b95c06c06015ae8 loaded}
  176. test "In the context of Lua the output of random commands gets ordered" {
  177. r del myset
  178. r sadd myset a b c d e f g h i l m n o p q r s t u v z aa aaa azz
  179. r eval {return redis.call('smembers',KEYS[1])} 1 myset
  180. } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z}
  181. test "SORT is normally not alpha re-ordered for the scripting engine" {
  182. r del myset
  183. r sadd myset 1 2 3 4 10
  184. r eval {return redis.call('sort',KEYS[1],'desc')} 1 myset
  185. } {10 4 3 2 1}
  186. test "SORT BY <constant> output gets ordered for scripting" {
  187. r del myset
  188. r sadd myset a b c d e f g h i l m n o p q r s t u v z aa aaa azz
  189. r eval {return redis.call('sort',KEYS[1],'by','_')} 1 myset
  190. } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z}
  191. test "SORT BY <constant> with GET gets ordered for scripting" {
  192. r del myset
  193. r sadd myset a b c
  194. r eval {return redis.call('sort',KEYS[1],'by','_','get','#','get','_:*')} 1 myset
  195. } {a {} b {} c {}}
  196. test "redis.sha1hex() implementation" {
  197. list [r eval {return redis.sha1hex('')} 0] \
  198. [r eval {return redis.sha1hex('Pizza & Mandolino')} 0]
  199. } {da39a3ee5e6b4b0d3255bfef95601890afd80709 74822d82031af7493c20eefa13bd07ec4fada82f}
  200. test {Globals protection reading an undeclared global variable} {
  201. catch {r eval {return a} 0} e
  202. set e
  203. } {*ERR*attempted to access unexisting global*}
  204. test {Globals protection setting an undeclared global*} {
  205. catch {r eval {a=10} 0} e
  206. set e
  207. } {*ERR*attempted to create global*}
  208. test {Test an example script DECR_IF_GT} {
  209. set decr_if_gt {
  210. local current
  211. current = redis.call('get',KEYS[1])
  212. if not current then return nil end
  213. if current > ARGV[1] then
  214. return redis.call('decr',KEYS[1])
  215. else
  216. return redis.call('get',KEYS[1])
  217. end
  218. }
  219. r set foo 5
  220. set res {}
  221. lappend res [r eval $decr_if_gt 1 foo 2]
  222. lappend res [r eval $decr_if_gt 1 foo 2]
  223. lappend res [r eval $decr_if_gt 1 foo 2]
  224. lappend res [r eval $decr_if_gt 1 foo 2]
  225. lappend res [r eval $decr_if_gt 1 foo 2]
  226. set res
  227. } {4 3 2 2 2}
  228. test {Scripting engine resets PRNG at every script execution} {
  229. set rand1 [r eval {return tostring(math.random())} 0]
  230. set rand2 [r eval {return tostring(math.random())} 0]
  231. assert_equal $rand1 $rand2
  232. }
  233. test {Scripting engine PRNG can be seeded correctly} {
  234. set rand1 [r eval {
  235. math.randomseed(ARGV[1]); return tostring(math.random())
  236. } 0 10]
  237. set rand2 [r eval {
  238. math.randomseed(ARGV[1]); return tostring(math.random())
  239. } 0 10]
  240. set rand3 [r eval {
  241. math.randomseed(ARGV[1]); return tostring(math.random())
  242. } 0 20]
  243. assert_equal $rand1 $rand2
  244. assert {$rand2 ne $rand3}
  245. }
  246. test {EVAL does not leak in the Lua stack} {
  247. r set x 0
  248. # Use a non blocking client to speedup the loop.
  249. set rd [redis_deferring_client]
  250. for {set j 0} {$j < 10000} {incr j} {
  251. $rd eval {return redis.call("incr",KEYS[1])} 1 x
  252. }
  253. for {set j 0} {$j < 10000} {incr j} {
  254. $rd read
  255. }
  256. assert {[s used_memory_lua] < 1024*100}
  257. $rd close
  258. r get x
  259. } {10000}
  260. test {EVAL processes writes from AOF in read-only slaves} {
  261. r flushall
  262. r config set appendonly yes
  263. r eval {redis.call("set",KEYS[1],"100")} 1 foo
  264. r eval {redis.call("incr",KEYS[1])} 1 foo
  265. r eval {redis.call("incr",KEYS[1])} 1 foo
  266. wait_for_condition 50 100 {
  267. [s aof_rewrite_in_progress] == 0
  268. } else {
  269. fail "AOF rewrite can't complete after CONFIG SET appendonly yes."
  270. }
  271. r config set slave-read-only yes
  272. r slaveof 127.0.0.1 0
  273. r debug loadaof
  274. set res [r get foo]
  275. r slaveof no one
  276. set res
  277. } {102}
  278. test {We can call scripts rewriting client->argv from Lua} {
  279. r del myset
  280. r sadd myset a b c
  281. r mset a 1 b 2 c 3 d 4
  282. assert {[r spop myset] ne {}}
  283. assert {[r spop myset] ne {}}
  284. assert {[r spop myset] ne {}}
  285. assert {[r mget a b c d] eq {1 2 3 4}}
  286. assert {[r spop myset] eq {}}
  287. }
  288. test {Call Redis command with many args from Lua (issue #1764)} {
  289. r eval {
  290. local i
  291. local x={}
  292. redis.call('del','mylist')
  293. for i=1,100 do
  294. table.insert(x,i)
  295. end
  296. redis.call('rpush','mylist',unpack(x))
  297. return redis.call('lrange','mylist',0,-1)
  298. } 0
  299. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100}
  300. test {Number conversion precision test (issue #1118)} {
  301. r eval {
  302. local value = 9007199254740991
  303. redis.call("set","foo",value)
  304. return redis.call("get","foo")
  305. } 0
  306. } {9007199254740991}
  307. test {String containing number precision test (regression of issue #1118)} {
  308. r eval {
  309. redis.call("set", "key", "12039611435714932082")
  310. return redis.call("get", "key")
  311. } 0
  312. } {12039611435714932082}
  313. }
  314. # Start a new server since the last test in this stanza will kill the
  315. # instance at all.
  316. start_server {tags {"scripting"}} {
  317. test {Timedout read-only scripts can be killed by SCRIPT KILL} {
  318. set rd [redis_deferring_client]
  319. r config set lua-time-limit 10
  320. $rd eval {while true do end} 0
  321. after 200
  322. catch {r ping} e
  323. assert_match {BUSY*} $e
  324. r script kill
  325. after 200 ; # Give some time to Lua to call the hook again...
  326. assert_equal [r ping] "PONG"
  327. }
  328. test {Timedout script link is still usable after Lua returns} {
  329. r config set lua-time-limit 10
  330. r eval {for i=1,100000 do redis.call('ping') end return 'ok'} 0
  331. r ping
  332. } {PONG}
  333. test {Timedout scripts that modified data can't be killed by SCRIPT KILL} {
  334. set rd [redis_deferring_client]
  335. r config set lua-time-limit 10
  336. $rd eval {redis.call('set',KEYS[1],'y'); while true do end} 1 x
  337. after 200
  338. catch {r ping} e
  339. assert_match {BUSY*} $e
  340. catch {r script kill} e
  341. assert_match {UNKILLABLE*} $e
  342. catch {r ping} e
  343. assert_match {BUSY*} $e
  344. }
  345. # Note: keep this test at the end of this server stanza because it
  346. # kills the server.
  347. test {SHUTDOWN NOSAVE can kill a timedout script anyway} {
  348. # The server sould be still unresponding to normal commands.
  349. catch {r ping} e
  350. assert_match {BUSY*} $e
  351. catch {r shutdown nosave}
  352. # Make sure the server was killed
  353. catch {set rd [redis_deferring_client]} e
  354. assert_match {*connection refused*} $e
  355. }
  356. }
  357. start_server {tags {"scripting repl"}} {
  358. start_server {} {
  359. test {Before the slave connects we issue two EVAL commands} {
  360. # One with an error, but still executing a command.
  361. # SHA is: 67164fc43fa971f76fd1aaeeaf60c1c178d25876
  362. catch {
  363. r eval {redis.call('incr',KEYS[1]); redis.call('nonexisting')} 1 x
  364. }
  365. # One command is correct:
  366. # SHA is: 6f5ade10a69975e903c6d07b10ea44c6382381a5
  367. r eval {return redis.call('incr',KEYS[1])} 1 x
  368. } {2}
  369. test {Connect a slave to the main instance} {
  370. r -1 slaveof [srv 0 host] [srv 0 port]
  371. wait_for_condition 50 100 {
  372. [s -1 role] eq {slave} &&
  373. [string match {*master_link_status:up*} [r -1 info replication]]
  374. } else {
  375. fail "Can't turn the instance into a slave"
  376. }
  377. }
  378. test {Now use EVALSHA against the master, with both SHAs} {
  379. # The server should replicate successful and unsuccessful
  380. # commands as EVAL instead of EVALSHA.
  381. catch {
  382. r evalsha 67164fc43fa971f76fd1aaeeaf60c1c178d25876 1 x
  383. }
  384. r evalsha 6f5ade10a69975e903c6d07b10ea44c6382381a5 1 x
  385. } {4}
  386. test {If EVALSHA was replicated as EVAL, 'x' should be '4'} {
  387. wait_for_condition 50 100 {
  388. [r -1 get x] eq {4}
  389. } else {
  390. fail "Expected 4 in x, but value is '[r -1 get x]'"
  391. }
  392. }
  393. test {Replication of script multiple pushes to list with BLPOP} {
  394. set rd [redis_deferring_client]
  395. $rd brpop a 0
  396. r eval {
  397. redis.call("lpush",KEYS[1],"1");
  398. redis.call("lpush",KEYS[1],"2");
  399. } 1 a
  400. set res [$rd read]
  401. $rd close
  402. wait_for_condition 50 100 {
  403. [r -1 lrange a 0 -1] eq [r lrange a 0 -1]
  404. } else {
  405. fail "Expected list 'a' in slave and master to be the same, but they are respectively '[r -1 lrange a 0 -1]' and '[r lrange a 0 -1]'"
  406. }
  407. set res
  408. } {a 1}
  409. test {EVALSHA replication when first call is readonly} {
  410. r del x
  411. r eval {if tonumber(ARGV[1]) > 0 then redis.call('incr', KEYS[1]) end} 1 x 0
  412. r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 0
  413. r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 1
  414. wait_for_condition 50 100 {
  415. [r -1 get x] eq {1}
  416. } else {
  417. fail "Expected 1 in x, but value is '[r -1 get x]'"
  418. }
  419. }
  420. test {Lua scripts using SELECT are replicated correctly} {
  421. r eval {
  422. redis.call("set","foo1","bar1")
  423. redis.call("select","10")
  424. redis.call("incr","x")
  425. redis.call("select","11")
  426. redis.call("incr","z")
  427. } 0
  428. r eval {
  429. redis.call("set","foo1","bar1")
  430. redis.call("select","10")
  431. redis.call("incr","x")
  432. redis.call("select","11")
  433. redis.call("incr","z")
  434. } 0
  435. wait_for_condition 50 100 {
  436. [r -1 debug digest] eq [r debug digest]
  437. } else {
  438. fail "Master-Slave desync after Lua script using SELECT."
  439. }
  440. }
  441. }
  442. }