2
0

dump.tcl 13 KB

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