1
0

aof.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. }