aof.tcl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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"} {
  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. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  48. test "Truncated AOF loaded: we expect foo to be equal to 5" {
  49. assert {[$client get foo] eq "5"}
  50. }
  51. test "Append a new command after loading an incomplete AOF" {
  52. $client incr foo
  53. }
  54. }
  55. # Now the AOF file is expected to be correct
  56. start_server_aof [list dir $server_path aof-load-truncated yes] {
  57. test "Short read + command: Server should start" {
  58. assert_equal 1 [is_alive $srv]
  59. }
  60. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  61. test "Truncated AOF loaded: we expect foo to be equal to 6 now" {
  62. assert {[$client get foo] eq "6"}
  63. }
  64. }
  65. ## Test that the server exits when the AOF contains a format error
  66. create_aof {
  67. append_to_aof [formatCommand set foo hello]
  68. append_to_aof "!!!"
  69. append_to_aof [formatCommand set foo hello]
  70. }
  71. start_server_aof [list dir $server_path aof-load-truncated yes] {
  72. test "Bad format: Server should have logged an error" {
  73. set pattern "*Bad file format reading the append only file*"
  74. set retry 10
  75. while {$retry} {
  76. set result [exec tail -1 < [dict get $srv stdout]]
  77. if {[string match $pattern $result]} {
  78. break
  79. }
  80. incr retry -1
  81. after 1000
  82. }
  83. if {$retry == 0} {
  84. error "assertion:expected error not found on config file"
  85. }
  86. }
  87. }
  88. ## Test the server doesn't start when the AOF contains an unfinished MULTI
  89. create_aof {
  90. append_to_aof [formatCommand set foo hello]
  91. append_to_aof [formatCommand multi]
  92. append_to_aof [formatCommand set bar world]
  93. }
  94. start_server_aof [list dir $server_path aof-load-truncated no] {
  95. test "Unfinished MULTI: Server should have logged an error" {
  96. set pattern "*Unexpected end of file reading the append only file*"
  97. set retry 10
  98. while {$retry} {
  99. set result [exec tail -1 < [dict get $srv stdout]]
  100. if {[string match $pattern $result]} {
  101. break
  102. }
  103. incr retry -1
  104. after 1000
  105. }
  106. if {$retry == 0} {
  107. error "assertion:expected error not found on config file"
  108. }
  109. }
  110. }
  111. ## Test that the server exits when the AOF contains a short read
  112. create_aof {
  113. append_to_aof [formatCommand set foo hello]
  114. append_to_aof [string range [formatCommand set bar world] 0 end-1]
  115. }
  116. start_server_aof [list dir $server_path aof-load-truncated no] {
  117. test "Short read: Server should have logged an error" {
  118. set pattern "*Unexpected end of file reading the append only file*"
  119. set retry 10
  120. while {$retry} {
  121. set result [exec tail -1 < [dict get $srv stdout]]
  122. if {[string match $pattern $result]} {
  123. break
  124. }
  125. incr retry -1
  126. after 1000
  127. }
  128. if {$retry == 0} {
  129. error "assertion:expected error not found on config file"
  130. }
  131. }
  132. }
  133. ## Test that redis-check-aof indeed sees this AOF is not valid
  134. test "Short read: Utility should confirm the AOF is not valid" {
  135. catch {
  136. exec src/redis-check-aof $aof_path
  137. } result
  138. assert_match "*not valid*" $result
  139. }
  140. test "Short read: Utility should be able to fix the AOF" {
  141. set result [exec src/redis-check-aof --fix $aof_path << "y\n"]
  142. assert_match "*Successfully truncated AOF*" $result
  143. }
  144. ## Test that the server can be started using the truncated AOF
  145. start_server_aof [list dir $server_path aof-load-truncated no] {
  146. test "Fixed AOF: Server should have been started" {
  147. assert_equal 1 [is_alive $srv]
  148. }
  149. test "Fixed AOF: Keyspace should contain values that were parseable" {
  150. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  151. wait_for_condition 50 100 {
  152. [catch {$client ping} e] == 0
  153. } else {
  154. fail "Loading DB is taking too much time."
  155. }
  156. assert_equal "hello" [$client get foo]
  157. assert_equal "" [$client get bar]
  158. }
  159. }
  160. ## Test that SPOP (that modifies the client's argc/argv) is correctly free'd
  161. create_aof {
  162. append_to_aof [formatCommand sadd set foo]
  163. append_to_aof [formatCommand sadd set bar]
  164. append_to_aof [formatCommand spop set]
  165. }
  166. start_server_aof [list dir $server_path aof-load-truncated no] {
  167. test "AOF+SPOP: Server should have been started" {
  168. assert_equal 1 [is_alive $srv]
  169. }
  170. test "AOF+SPOP: Set should have 1 member" {
  171. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  172. wait_for_condition 50 100 {
  173. [catch {$client ping} e] == 0
  174. } else {
  175. fail "Loading DB is taking too much time."
  176. }
  177. assert_equal 1 [$client scard set]
  178. }
  179. }
  180. ## Uses the alsoPropagate() API.
  181. create_aof {
  182. append_to_aof [formatCommand sadd set foo]
  183. append_to_aof [formatCommand sadd set bar]
  184. append_to_aof [formatCommand sadd set gah]
  185. append_to_aof [formatCommand spop set 2]
  186. }
  187. start_server_aof [list dir $server_path] {
  188. test "AOF+SPOP: Server should have been started" {
  189. assert_equal 1 [is_alive $srv]
  190. }
  191. test "AOF+SPOP: Set should have 1 member" {
  192. set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]
  193. wait_for_condition 50 100 {
  194. [catch {$client ping} e] == 0
  195. } else {
  196. fail "Loading DB is taking too much time."
  197. }
  198. assert_equal 1 [$client scard set]
  199. }
  200. }
  201. ## Test that EXPIREAT is loaded correctly
  202. create_aof {
  203. append_to_aof [formatCommand rpush list foo]
  204. append_to_aof [formatCommand expireat 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_for_condition 50 100 {
  214. [catch {$client ping} e] == 0
  215. } else {
  216. fail "Loading DB is taking too much time."
  217. }
  218. assert_equal 0 [$client llen list]
  219. }
  220. }
  221. start_server {overrides {appendonly {yes} appendfilename {appendonly.aof}}} {
  222. test {Redis should not try to convert DEL into EXPIREAT for EXPIRE -1} {
  223. r set x 10
  224. r expire x -1
  225. }
  226. }
  227. start_server {overrides {appendonly {yes} appendfilename {appendonly.aof} appendfsync always}} {
  228. test {AOF fsync always barrier issue} {
  229. set rd [redis_deferring_client]
  230. # Set a sleep when aof is flushed, so that we have a chance to look
  231. # at the aof size and detect if the response of an incr command
  232. # arrives before the data was written (and hopefully fsynced)
  233. # We create a big reply, which will hopefully not have room in the
  234. # socket buffers, and will install a write handler, then we sleep
  235. # a big and issue the incr command, hoping that the last portion of
  236. # the output buffer write, and the processing of the incr will happen
  237. # in the same event loop cycle.
  238. # Since the socket buffers and timing are unpredictable, we fuzz this
  239. # test with slightly different sizes and sleeps a few times.
  240. for {set i 0} {$i < 10} {incr i} {
  241. r debug aof-flush-sleep 0
  242. r del x
  243. r setrange x [expr {int(rand()*5000000)+10000000}] x
  244. r debug aof-flush-sleep 500000
  245. set aof [file join [lindex [r config get dir] 1] appendonly.aof]
  246. set size1 [file size $aof]
  247. $rd get x
  248. after [expr {int(rand()*30)}]
  249. $rd incr new_value
  250. $rd read
  251. $rd read
  252. set size2 [file size $aof]
  253. assert {$size1 != $size2}
  254. }
  255. }
  256. }
  257. }