aof.tcl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. set defaults { appendonly {yes} appendfilename {appendonly.aof} }
  2. set server_path [tmpdir server.aof]
  3. set aof_path "$server_path/appendonly.aof"
  4. proc append_to_aof {str} {
  5. upvar fp fp
  6. puts -nonewline $fp $str
  7. }
  8. proc create_aof {code} {
  9. upvar fp fp aof_path aof_path
  10. set fp [open $aof_path w+]
  11. uplevel 1 $code
  12. close $fp
  13. }
  14. proc start_server_aof {overrides code} {
  15. upvar defaults defaults srv srv server_path server_path
  16. set config [concat $defaults $overrides]
  17. set srv [start_server [list overrides $config]]
  18. uplevel 1 $code
  19. kill_server $srv
  20. }
  21. tags {"aof external:skip"} {
  22. ## Server can start when aof-load-truncated is set to yes and AOF
  23. ## is truncated, with an incomplete MULTI block.
  24. create_aof {
  25. append_to_aof [formatCommand set foo hello]
  26. append_to_aof [formatCommand multi]
  27. append_to_aof [formatCommand set bar world]
  28. }
  29. start_server_aof [list dir $server_path aof-load-truncated yes] {
  30. test "Unfinished MULTI: Server should start if load-truncated is yes" {
  31. assert_equal 1 [is_alive $srv]
  32. }
  33. }
  34. ## Should also start with truncated AOF without incomplete MULTI block.
  35. create_aof {
  36. append_to_aof [formatCommand incr foo]
  37. append_to_aof [formatCommand incr foo]
  38. append_to_aof [formatCommand incr foo]
  39. append_to_aof [formatCommand incr foo]
  40. append_to_aof [formatCommand incr foo]
  41. append_to_aof [string range [formatCommand incr foo] 0 end-1]
  42. }
  43. start_server_aof [list dir $server_path aof-load-truncated yes] {
  44. test "Short read: Server should start if load-truncated is yes" {
  45. assert_equal 1 [is_alive $srv]
  46. }
  47. test "Truncated AOF loaded: we expect foo to be equal to 5" {
  48. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  49. wait_done_loading $client
  50. assert {[$client get foo] eq "5"}
  51. }
  52. test "Append a new command after loading an incomplete AOF" {
  53. $client incr foo
  54. }
  55. }
  56. # Now the AOF file is expected to be correct
  57. start_server_aof [list dir $server_path aof-load-truncated yes] {
  58. test "Short read + command: Server should start" {
  59. assert_equal 1 [is_alive $srv]
  60. }
  61. test "Truncated AOF loaded: we expect foo to be equal to 6 now" {
  62. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  63. wait_done_loading $client
  64. assert {[$client get foo] eq "6"}
  65. }
  66. }
  67. ## Test that the server exits when the AOF contains a format error
  68. create_aof {
  69. append_to_aof [formatCommand set foo hello]
  70. append_to_aof "!!!"
  71. append_to_aof [formatCommand set foo hello]
  72. }
  73. start_server_aof [list dir $server_path aof-load-truncated yes] {
  74. test "Bad format: Server should have logged an error" {
  75. set pattern "*Bad file format reading the append only file*"
  76. set retry 10
  77. while {$retry} {
  78. set result [exec tail -1 < [dict get $srv stdout]]
  79. if {[string match $pattern $result]} {
  80. break
  81. }
  82. incr retry -1
  83. after 1000
  84. }
  85. if {$retry == 0} {
  86. error "assertion:expected error not found on config file"
  87. }
  88. }
  89. }
  90. ## Test the server doesn't start when the AOF contains an unfinished MULTI
  91. create_aof {
  92. append_to_aof [formatCommand set foo hello]
  93. append_to_aof [formatCommand multi]
  94. append_to_aof [formatCommand set bar world]
  95. }
  96. start_server_aof [list dir $server_path aof-load-truncated no] {
  97. test "Unfinished MULTI: Server should have logged an error" {
  98. set pattern "*Unexpected end of file reading the append only file*"
  99. set retry 10
  100. while {$retry} {
  101. set result [exec tail -1 < [dict get $srv stdout]]
  102. if {[string match $pattern $result]} {
  103. break
  104. }
  105. incr retry -1
  106. after 1000
  107. }
  108. if {$retry == 0} {
  109. error "assertion:expected error not found on config file"
  110. }
  111. }
  112. }
  113. ## Test that the server exits when the AOF contains a short read
  114. create_aof {
  115. append_to_aof [formatCommand set foo hello]
  116. append_to_aof [string range [formatCommand set bar world] 0 end-1]
  117. }
  118. start_server_aof [list dir $server_path aof-load-truncated no] {
  119. test "Short read: Server should have logged an error" {
  120. set pattern "*Unexpected end of file reading the append only file*"
  121. set retry 10
  122. while {$retry} {
  123. set result [exec tail -1 < [dict get $srv stdout]]
  124. if {[string match $pattern $result]} {
  125. break
  126. }
  127. incr retry -1
  128. after 1000
  129. }
  130. if {$retry == 0} {
  131. error "assertion:expected error not found on config file"
  132. }
  133. }
  134. }
  135. ## Test that redis-check-aof indeed sees this AOF is not valid
  136. test "Short read: Utility should confirm the AOF is not valid" {
  137. catch {
  138. exec src/redis-check-aof $aof_path
  139. } result
  140. assert_match "*not valid*" $result
  141. }
  142. test "Short read: Utility should show the abnormal line num in AOF" {
  143. create_aof {
  144. append_to_aof [formatCommand set foo hello]
  145. append_to_aof "!!!"
  146. }
  147. catch {
  148. exec src/redis-check-aof $aof_path
  149. } result
  150. assert_match "*ok_up_to_line=8*" $result
  151. }
  152. test "Short read: Utility should be able to fix the AOF" {
  153. set result [exec src/redis-check-aof --fix $aof_path << "y\n"]
  154. assert_match "*Successfully truncated AOF*" $result
  155. }
  156. ## Test that the server can be started using the truncated AOF
  157. start_server_aof [list dir $server_path aof-load-truncated no] {
  158. test "Fixed AOF: Server should have been started" {
  159. assert_equal 1 [is_alive $srv]
  160. }
  161. test "Fixed AOF: Keyspace should contain values that were parseable" {
  162. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  163. wait_done_loading $client
  164. assert_equal "hello" [$client get foo]
  165. assert_equal "" [$client get bar]
  166. }
  167. }
  168. ## Test that SPOP (that modifies the client's argc/argv) is correctly free'd
  169. create_aof {
  170. append_to_aof [formatCommand sadd set foo]
  171. append_to_aof [formatCommand sadd set bar]
  172. append_to_aof [formatCommand spop set]
  173. }
  174. start_server_aof [list dir $server_path aof-load-truncated no] {
  175. test "AOF+SPOP: Server should have been started" {
  176. assert_equal 1 [is_alive $srv]
  177. }
  178. test "AOF+SPOP: Set should have 1 member" {
  179. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  180. wait_done_loading $client
  181. assert_equal 1 [$client scard set]
  182. }
  183. }
  184. ## Uses the alsoPropagate() API.
  185. create_aof {
  186. append_to_aof [formatCommand sadd set foo]
  187. append_to_aof [formatCommand sadd set bar]
  188. append_to_aof [formatCommand sadd set gah]
  189. append_to_aof [formatCommand spop set 2]
  190. }
  191. start_server_aof [list dir $server_path] {
  192. test "AOF+SPOP: Server should have been started" {
  193. assert_equal 1 [is_alive $srv]
  194. }
  195. test "AOF+SPOP: Set should have 1 member" {
  196. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  197. wait_done_loading $client
  198. assert_equal 1 [$client scard set]
  199. }
  200. }
  201. ## Test that PEXPIREAT is loaded correctly
  202. create_aof {
  203. append_to_aof [formatCommand rpush list foo]
  204. append_to_aof [formatCommand pexpireat list 1000]
  205. append_to_aof [formatCommand rpush list bar]
  206. }
  207. start_server_aof [list dir $server_path aof-load-truncated no] {
  208. test "AOF+EXPIRE: Server should have been started" {
  209. assert_equal 1 [is_alive $srv]
  210. }
  211. test "AOF+EXPIRE: List should be empty" {
  212. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  213. wait_done_loading $client
  214. assert_equal 0 [$client llen list]
  215. }
  216. }
  217. start_server {overrides {appendonly {yes} appendfilename {appendonly.aof}}} {
  218. test {Redis should not try to convert DEL into EXPIREAT for EXPIRE -1} {
  219. r set x 10
  220. r expire x -1
  221. }
  222. }
  223. start_server {overrides {appendonly {yes} appendfilename {appendonly.aof} appendfsync always}} {
  224. test {AOF fsync always barrier issue} {
  225. set rd [redis_deferring_client]
  226. # Set a sleep when aof is flushed, so that we have a chance to look
  227. # at the aof size and detect if the response of an incr command
  228. # arrives before the data was written (and hopefully fsynced)
  229. # We create a big reply, which will hopefully not have room in the
  230. # socket buffers, and will install a write handler, then we sleep
  231. # a big and issue the incr command, hoping that the last portion of
  232. # the output buffer write, and the processing of the incr will happen
  233. # in the same event loop cycle.
  234. # Since the socket buffers and timing are unpredictable, we fuzz this
  235. # test with slightly different sizes and sleeps a few times.
  236. for {set i 0} {$i < 10} {incr i} {
  237. r debug aof-flush-sleep 0
  238. r del x
  239. r setrange x [expr {int(rand()*5000000)+10000000}] x
  240. r debug aof-flush-sleep 500000
  241. set aof [file join [lindex [r config get dir] 1] appendonly.aof]
  242. set size1 [file size $aof]
  243. $rd get x
  244. after [expr {int(rand()*30)}]
  245. $rd incr new_value
  246. $rd read
  247. $rd read
  248. set size2 [file size $aof]
  249. assert {$size1 != $size2}
  250. }
  251. }
  252. }
  253. start_server {overrides {appendonly {yes} appendfilename {appendonly.aof}}} {
  254. test {GETEX should not append to AOF} {
  255. set aof [file join [lindex [r config get dir] 1] appendonly.aof]
  256. r set foo bar
  257. set before [file size $aof]
  258. r getex foo
  259. set after [file size $aof]
  260. assert_equal $before $after
  261. }
  262. }
  263. ## Test that the server exits when the AOF contains a unknown command
  264. create_aof {
  265. append_to_aof [formatCommand set foo hello]
  266. append_to_aof [formatCommand bla foo hello]
  267. append_to_aof [formatCommand set foo hello]
  268. }
  269. start_server_aof [list dir $server_path aof-load-truncated yes] {
  270. test "Unknown command: Server should have logged an error" {
  271. set pattern "*Unknown command 'bla' reading the append only file*"
  272. set retry 10
  273. while {$retry} {
  274. set result [exec tail -1 < [dict get $srv stdout]]
  275. if {[string match $pattern $result]} {
  276. break
  277. }
  278. incr retry -1
  279. after 1000
  280. }
  281. if {$retry == 0} {
  282. error "assertion:expected error not found on config file"
  283. }
  284. }
  285. }
  286. # Test that LMPOP/BLMPOP work fine with AOF.
  287. create_aof {
  288. append_to_aof [formatCommand lpush mylist a b c]
  289. append_to_aof [formatCommand rpush mylist2 1 2 3]
  290. append_to_aof [formatCommand lpush mylist3 a b c d e]
  291. }
  292. start_server_aof [list dir $server_path aof-load-truncated no] {
  293. test "AOF+LMPOP/BLMPOP: pop elements from the list" {
  294. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  295. set client2 [redis [dict get $srv host] [dict get $srv port] 1 $::tls]
  296. wait_done_loading $client
  297. # Pop all elements from mylist, should be blmpop delete mylist.
  298. $client lmpop 1 mylist left count 1
  299. $client blmpop 0 1 mylist left count 10
  300. # Pop all elements from mylist2, should be lmpop delete mylist2.
  301. $client blmpop 0 2 mylist mylist2 right count 10
  302. $client lmpop 2 mylist mylist2 right count 2
  303. # Blocking path, be blocked and then released.
  304. $client2 blmpop 0 2 mylist mylist2 left count 2
  305. after 100
  306. $client lpush mylist2 a b c
  307. # Pop up the last element in mylist2
  308. $client blmpop 0 3 mylist mylist2 mylist3 left count 1
  309. # Leave two elements in mylist3.
  310. $client blmpop 0 3 mylist mylist2 mylist3 right count 3
  311. }
  312. }
  313. start_server_aof [list dir $server_path aof-load-truncated no] {
  314. test "AOF+LMPOP/BLMPOP: after pop elements from the list" {
  315. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  316. wait_done_loading $client
  317. # mylist and mylist2 no longer exist.
  318. assert_equal 0 [$client exists mylist mylist2]
  319. # Length of mylist3 is two.
  320. assert_equal 2 [$client llen mylist3]
  321. }
  322. }
  323. # Test that ZMPOP/BZMPOP work fine with AOF.
  324. create_aof {
  325. append_to_aof [formatCommand zadd myzset 1 one 2 two 3 three]
  326. append_to_aof [formatCommand zadd myzset2 4 four 5 five 6 six]
  327. append_to_aof [formatCommand zadd myzset3 1 one 2 two 3 three 4 four 5 five]
  328. }
  329. start_server_aof [list dir $server_path aof-load-truncated no] {
  330. test "AOF+ZMPOP/BZMPOP: pop elements from the zset" {
  331. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  332. set client2 [redis [dict get $srv host] [dict get $srv port] 1 $::tls]
  333. wait_done_loading $client
  334. # Pop all elements from myzset, should be bzmpop delete myzset.
  335. $client zmpop 1 myzset min count 1
  336. $client bzmpop 0 1 myzset min count 10
  337. # Pop all elements from myzset2, should be zmpop delete myzset2.
  338. $client bzmpop 0 2 myzset myzset2 max count 10
  339. $client zmpop 2 myzset myzset2 max count 2
  340. # Blocking path, be blocked and then released.
  341. $client2 bzmpop 0 2 myzset myzset2 min count 2
  342. after 100
  343. $client zadd myzset2 1 one 2 two 3 three
  344. # Pop up the last element in myzset2
  345. $client bzmpop 0 3 myzset myzset2 myzset3 min count 1
  346. # Leave two elements in myzset3.
  347. $client bzmpop 0 3 myzset myzset2 myzset3 max count 3
  348. }
  349. }
  350. start_server_aof [list dir $server_path aof-load-truncated no] {
  351. test "AOF+ZMPOP/BZMPOP: after pop elements from the zset" {
  352. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  353. wait_done_loading $client
  354. # myzset and myzset2 no longer exist.
  355. assert_equal 0 [$client exists myzset myzset2]
  356. # Length of myzset3 is two.
  357. assert_equal 2 [$client zcard myzset3]
  358. }
  359. }
  360. test {Generate timestamp annotations in AOF} {
  361. start_server {overrides {appendonly {yes} appendfilename {appendonly.aof}}} {
  362. r config set aof-timestamp-enabled yes
  363. r config set aof-use-rdb-preamble no
  364. set aof [file join [lindex [r config get dir] 1] appendonly.aof]
  365. r set foo bar
  366. assert_match "#TS:*" [exec head -n 1 $aof]
  367. r bgrewriteaof
  368. waitForBgrewriteaof r
  369. assert_match "#TS:*" [exec head -n 1 $aof]
  370. }
  371. }
  372. # redis could load AOF which has timestamp annotations inside
  373. create_aof {
  374. append_to_aof "#TS:1628217470\r\n"
  375. append_to_aof [formatCommand set foo1 bar1]
  376. append_to_aof "#TS:1628217471\r\n"
  377. append_to_aof [formatCommand set foo2 bar2]
  378. append_to_aof "#TS:1628217472\r\n"
  379. append_to_aof "#TS:1628217473\r\n"
  380. append_to_aof [formatCommand set foo3 bar3]
  381. append_to_aof "#TS:1628217474\r\n"
  382. }
  383. start_server_aof [list dir $server_path] {
  384. test {Successfully load AOF which has timestamp annotations inside} {
  385. set c [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  386. wait_done_loading $c
  387. assert_equal "bar1" [$c get foo1]
  388. assert_equal "bar2" [$c get foo2]
  389. assert_equal "bar3" [$c get foo3]
  390. }
  391. }
  392. test {Truncate AOF to specific timestamp} {
  393. # truncate to timestamp 1628217473
  394. exec src/redis-check-aof --truncate-to-timestamp 1628217473 $aof_path
  395. start_server_aof [list dir $server_path] {
  396. set c [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  397. wait_done_loading $c
  398. assert_equal "bar1" [$c get foo1]
  399. assert_equal "bar2" [$c get foo2]
  400. assert_equal "bar3" [$c get foo3]
  401. }
  402. # truncate to timestamp 1628217471
  403. exec src/redis-check-aof --truncate-to-timestamp 1628217471 $aof_path
  404. start_server_aof [list dir $server_path] {
  405. set c [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  406. wait_done_loading $c
  407. assert_equal "bar1" [$c get foo1]
  408. assert_equal "bar2" [$c get foo2]
  409. assert_equal "" [$c get foo3]
  410. }
  411. # truncate to timestamp 1628217470
  412. exec src/redis-check-aof --truncate-to-timestamp 1628217470 $aof_path
  413. start_server_aof [list dir $server_path] {
  414. set c [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  415. wait_done_loading $c
  416. assert_equal "bar1" [$c get foo1]
  417. assert_equal "" [$c get foo2]
  418. }
  419. # truncate to timestamp 1628217469
  420. catch {exec src/redis-check-aof --truncate-to-timestamp 1628217469 $aof_path} e
  421. assert_match {*aborting*} $e
  422. }
  423. }