dump.tcl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. start_server {tags {"dump"}} {
  2. test {DUMP / RESTORE are able to serialize / unserialize a simple key} {
  3. r set foo bar
  4. set encoded [r dump foo]
  5. r del foo
  6. list [r exists foo] [r restore foo 0 $encoded] [r ttl foo] [r get foo]
  7. } {0 OK -1 bar}
  8. test {RESTORE can set an arbitrary expire to the materialized key} {
  9. r set foo bar
  10. set encoded [r dump foo]
  11. r del foo
  12. r restore foo 5000 $encoded
  13. set ttl [r pttl foo]
  14. assert {$ttl >= 3000 && $ttl <= 5000}
  15. r get foo
  16. } {bar}
  17. test {RESTORE can set an expire that overflows a 32 bit integer} {
  18. r set foo bar
  19. set encoded [r dump foo]
  20. r del foo
  21. r restore foo 2569591501 $encoded
  22. set ttl [r pttl foo]
  23. assert {$ttl >= (2569591501-3000) && $ttl <= 2569591501}
  24. r get foo
  25. } {bar}
  26. test {RESTORE can set an absolute expire} {
  27. r set foo bar
  28. set encoded [r dump foo]
  29. r del foo
  30. set now [clock milliseconds]
  31. r restore foo [expr $now+3000] $encoded absttl
  32. set ttl [r pttl foo]
  33. assert {$ttl >= 2900 && $ttl <= 3100}
  34. r get foo
  35. } {bar}
  36. test {RESTORE with ABSTTL in the past} {
  37. r set foo bar
  38. set encoded [r dump foo]
  39. set now [clock milliseconds]
  40. r debug set-active-expire 0
  41. r restore foo [expr $now-3000] $encoded absttl REPLACE
  42. catch {r debug object foo} e
  43. r debug set-active-expire 1
  44. set e
  45. } {ERR no such key}
  46. test {RESTORE can set LRU} {
  47. r set foo bar
  48. set encoded [r dump foo]
  49. r del foo
  50. r config set maxmemory-policy allkeys-lru
  51. r restore foo 0 $encoded idletime 1000
  52. set idle [r object idletime foo]
  53. assert {$idle >= 1000 && $idle <= 1010}
  54. r get foo
  55. } {bar}
  56. test {RESTORE can set LFU} {
  57. r set foo bar
  58. set encoded [r dump foo]
  59. r del foo
  60. r config set maxmemory-policy allkeys-lfu
  61. r restore foo 0 $encoded freq 100
  62. set freq [r object freq foo]
  63. assert {$freq == 100}
  64. r get foo
  65. } {bar}
  66. test {RESTORE returns an error of the key already exists} {
  67. r set foo bar
  68. set e {}
  69. catch {r restore foo 0 "..."} e
  70. set e
  71. } {*BUSYKEY*}
  72. test {RESTORE can overwrite an existing key with REPLACE} {
  73. r set foo bar1
  74. set encoded1 [r dump foo]
  75. r set foo bar2
  76. set encoded2 [r dump foo]
  77. r del foo
  78. r restore foo 0 $encoded1
  79. r restore foo 0 $encoded2 replace
  80. r get foo
  81. } {bar2}
  82. test {RESTORE can detect a syntax error for unrecongized options} {
  83. catch {r restore foo 0 "..." invalid-option} e
  84. set e
  85. } {*syntax*}
  86. test {DUMP of non existing key returns nil} {
  87. r dump nonexisting_key
  88. } {}
  89. test {MIGRATE is caching connections} {
  90. # Note, we run this as first test so that the connection cache
  91. # is empty.
  92. set first [srv 0 client]
  93. r set key "Some Value"
  94. start_server {tags {"repl"}} {
  95. set second [srv 0 client]
  96. set second_host [srv 0 host]
  97. set second_port [srv 0 port]
  98. assert_match {*migrate_cached_sockets:0*} [r -1 info]
  99. r -1 migrate $second_host $second_port key 9 1000
  100. assert_match {*migrate_cached_sockets:1*} [r -1 info]
  101. }
  102. }
  103. test {MIGRATE cached connections are released after some time} {
  104. after 15000
  105. assert_match {*migrate_cached_sockets:0*} [r info]
  106. }
  107. test {MIGRATE is able to migrate a key between two instances} {
  108. set first [srv 0 client]
  109. r set key "Some Value"
  110. start_server {tags {"repl"}} {
  111. set second [srv 0 client]
  112. set second_host [srv 0 host]
  113. set second_port [srv 0 port]
  114. assert {[$first exists key] == 1}
  115. assert {[$second exists key] == 0}
  116. set ret [r -1 migrate $second_host $second_port key 9 5000]
  117. assert {$ret eq {OK}}
  118. assert {[$first exists key] == 0}
  119. assert {[$second exists key] == 1}
  120. assert {[$second get key] eq {Some Value}}
  121. assert {[$second ttl key] == -1}
  122. }
  123. }
  124. test {MIGRATE is able to copy a key between two instances} {
  125. set first [srv 0 client]
  126. r del list
  127. r lpush list a b c d
  128. start_server {tags {"repl"}} {
  129. set second [srv 0 client]
  130. set second_host [srv 0 host]
  131. set second_port [srv 0 port]
  132. assert {[$first exists list] == 1}
  133. assert {[$second exists list] == 0}
  134. set ret [r -1 migrate $second_host $second_port list 9 5000 copy]
  135. assert {$ret eq {OK}}
  136. assert {[$first exists list] == 1}
  137. assert {[$second exists list] == 1}
  138. assert {[$first lrange list 0 -1] eq [$second lrange list 0 -1]}
  139. }
  140. }
  141. test {MIGRATE will not overwrite existing keys, unless REPLACE is used} {
  142. set first [srv 0 client]
  143. r del list
  144. r lpush list a b c d
  145. start_server {tags {"repl"}} {
  146. set second [srv 0 client]
  147. set second_host [srv 0 host]
  148. set second_port [srv 0 port]
  149. assert {[$first exists list] == 1}
  150. assert {[$second exists list] == 0}
  151. $second set list somevalue
  152. catch {r -1 migrate $second_host $second_port list 9 5000 copy} e
  153. assert_match {ERR*} $e
  154. set res [r -1 migrate $second_host $second_port list 9 5000 copy replace]
  155. assert {$ret eq {OK}}
  156. assert {[$first exists list] == 1}
  157. assert {[$second exists list] == 1}
  158. assert {[$first lrange list 0 -1] eq [$second lrange list 0 -1]}
  159. }
  160. }
  161. test {MIGRATE propagates TTL correctly} {
  162. set first [srv 0 client]
  163. r set key "Some Value"
  164. start_server {tags {"repl"}} {
  165. set second [srv 0 client]
  166. set second_host [srv 0 host]
  167. set second_port [srv 0 port]
  168. assert {[$first exists key] == 1}
  169. assert {[$second exists key] == 0}
  170. $first expire key 10
  171. set ret [r -1 migrate $second_host $second_port key 9 5000]
  172. assert {$ret eq {OK}}
  173. assert {[$first exists key] == 0}
  174. assert {[$second exists key] == 1}
  175. assert {[$second get key] eq {Some Value}}
  176. assert {[$second ttl key] >= 7 && [$second ttl key] <= 10}
  177. }
  178. }
  179. test {MIGRATE can correctly transfer large values} {
  180. set first [srv 0 client]
  181. r del key
  182. for {set j 0} {$j < 40000} {incr j} {
  183. r rpush key 1 2 3 4 5 6 7 8 9 10
  184. r rpush key "item 1" "item 2" "item 3" "item 4" "item 5" \
  185. "item 6" "item 7" "item 8" "item 9" "item 10"
  186. }
  187. assert {[string length [r dump key]] > (1024*64)}
  188. start_server {tags {"repl"}} {
  189. set second [srv 0 client]
  190. set second_host [srv 0 host]
  191. set second_port [srv 0 port]
  192. assert {[$first exists key] == 1}
  193. assert {[$second exists key] == 0}
  194. set ret [r -1 migrate $second_host $second_port key 9 10000]
  195. assert {$ret eq {OK}}
  196. assert {[$first exists key] == 0}
  197. assert {[$second exists key] == 1}
  198. assert {[$second ttl key] == -1}
  199. assert {[$second llen key] == 40000*20}
  200. }
  201. }
  202. test {MIGRATE can correctly transfer hashes} {
  203. set first [srv 0 client]
  204. r del key
  205. r hmset key field1 "item 1" field2 "item 2" field3 "item 3" \
  206. field4 "item 4" field5 "item 5" field6 "item 6"
  207. start_server {tags {"repl"}} {
  208. set second [srv 0 client]
  209. set second_host [srv 0 host]
  210. set second_port [srv 0 port]
  211. assert {[$first exists key] == 1}
  212. assert {[$second exists key] == 0}
  213. set ret [r -1 migrate $second_host $second_port key 9 10000]
  214. assert {$ret eq {OK}}
  215. assert {[$first exists key] == 0}
  216. assert {[$second exists key] == 1}
  217. assert {[$second ttl key] == -1}
  218. }
  219. }
  220. test {MIGRATE timeout actually works} {
  221. set first [srv 0 client]
  222. r set key "Some Value"
  223. start_server {tags {"repl"}} {
  224. set second [srv 0 client]
  225. set second_host [srv 0 host]
  226. set second_port [srv 0 port]
  227. assert {[$first exists key] == 1}
  228. assert {[$second exists key] == 0}
  229. set rd [redis_deferring_client]
  230. $rd debug sleep 1.0 ; # Make second server unable to reply.
  231. set e {}
  232. catch {r -1 migrate $second_host $second_port key 9 500} e
  233. assert_match {IOERR*} $e
  234. }
  235. }
  236. test {MIGRATE can migrate multiple keys at once} {
  237. set first [srv 0 client]
  238. r set key1 "v1"
  239. r set key2 "v2"
  240. r set key3 "v3"
  241. start_server {tags {"repl"}} {
  242. set second [srv 0 client]
  243. set second_host [srv 0 host]
  244. set second_port [srv 0 port]
  245. assert {[$first exists key1] == 1}
  246. assert {[$second exists key1] == 0}
  247. set ret [r -1 migrate $second_host $second_port "" 9 5000 keys key1 key2 key3]
  248. assert {$ret eq {OK}}
  249. assert {[$first exists key1] == 0}
  250. assert {[$first exists key2] == 0}
  251. assert {[$first exists key3] == 0}
  252. assert {[$second get key1] eq {v1}}
  253. assert {[$second get key2] eq {v2}}
  254. assert {[$second get key3] eq {v3}}
  255. }
  256. }
  257. test {MIGRATE with multiple keys must have empty key arg} {
  258. catch {r MIGRATE 127.0.0.1 6379 NotEmpty 9 5000 keys a b c} e
  259. set e
  260. } {*empty string*}
  261. test {MIGRATE with multiple keys migrate just existing ones} {
  262. set first [srv 0 client]
  263. r set key1 "v1"
  264. r set key2 "v2"
  265. r set key3 "v3"
  266. start_server {tags {"repl"}} {
  267. set second [srv 0 client]
  268. set second_host [srv 0 host]
  269. set second_port [srv 0 port]
  270. set ret [r -1 migrate $second_host $second_port "" 9 5000 keys nokey-1 nokey-2 nokey-2]
  271. assert {$ret eq {NOKEY}}
  272. assert {[$first exists key1] == 1}
  273. assert {[$second exists key1] == 0}
  274. set ret [r -1 migrate $second_host $second_port "" 9 5000 keys nokey-1 key1 nokey-2 key2 nokey-3 key3]
  275. assert {$ret eq {OK}}
  276. assert {[$first exists key1] == 0}
  277. assert {[$first exists key2] == 0}
  278. assert {[$first exists key3] == 0}
  279. assert {[$second get key1] eq {v1}}
  280. assert {[$second get key2] eq {v2}}
  281. assert {[$second get key3] eq {v3}}
  282. }
  283. }
  284. test {MIGRATE with multiple keys: stress command rewriting} {
  285. set first [srv 0 client]
  286. r flushdb
  287. r mset a 1 b 2 c 3 d 4 c 5 e 6 f 7 g 8 h 9 i 10 l 11 m 12 n 13 o 14 p 15 q 16
  288. start_server {tags {"repl"}} {
  289. set second [srv 0 client]
  290. set second_host [srv 0 host]
  291. set second_port [srv 0 port]
  292. set ret [r -1 migrate $second_host $second_port "" 9 5000 keys a b c d e f g h i l m n o p q]
  293. assert {[$first dbsize] == 0}
  294. assert {[$second dbsize] == 15}
  295. }
  296. }
  297. test {MIGRATE with multiple keys: delete just ack keys} {
  298. set first [srv 0 client]
  299. r flushdb
  300. r mset a 1 b 2 c 3 d 4 c 5 e 6 f 7 g 8 h 9 i 10 l 11 m 12 n 13 o 14 p 15 q 16
  301. start_server {tags {"repl"}} {
  302. set second [srv 0 client]
  303. set second_host [srv 0 host]
  304. set second_port [srv 0 port]
  305. $second mset c _ d _; # Two busy keys and no REPLACE used
  306. catch {r -1 migrate $second_host $second_port "" 9 5000 keys a b c d e f g h i l m n o p q} e
  307. assert {[$first dbsize] == 2}
  308. assert {[$second dbsize] == 15}
  309. assert {[$first exists c] == 1}
  310. assert {[$first exists d] == 1}
  311. }
  312. }
  313. test {MIGRATE AUTH: correct and wrong password cases} {
  314. set first [srv 0 client]
  315. r del list
  316. r lpush list a b c d
  317. start_server {tags {"repl"}} {
  318. set second [srv 0 client]
  319. set second_host [srv 0 host]
  320. set second_port [srv 0 port]
  321. $second config set requirepass foobar
  322. $second auth foobar
  323. assert {[$first exists list] == 1}
  324. assert {[$second exists list] == 0}
  325. set ret [r -1 migrate $second_host $second_port list 9 5000 AUTH foobar]
  326. assert {$ret eq {OK}}
  327. assert {[$second exists list] == 1}
  328. assert {[$second lrange list 0 -1] eq {d c b a}}
  329. r -1 lpush list a b c d
  330. $second config set requirepass foobar2
  331. catch {r -1 migrate $second_host $second_port list 9 5000 AUTH foobar} err
  332. assert_match {*WRONGPASS*} $err
  333. }
  334. }
  335. }