rdb.tcl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. set server_path [tmpdir "server.rdb-encoding-test"]
  2. # Copy RDB with different encodings in server path
  3. exec cp tests/assets/encodings.rdb $server_path
  4. start_server [list overrides [list "dir" $server_path "dbfilename" "encodings.rdb"]] {
  5. test "RDB encoding loading test" {
  6. r select 0
  7. csvdump r
  8. } {"0","compressible","string","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9. "0","hash","hash","a","1","aa","10","aaa","100","b","2","bb","20","bbb","200","c","3","cc","30","ccc","300","ddd","400","eee","5000000000",
  10. "0","hash_zipped","hash","a","1","b","2","c","3",
  11. "0","list","list","1","2","3","a","b","c","100000","6000000000","1","2","3","a","b","c","100000","6000000000","1","2","3","a","b","c","100000","6000000000",
  12. "0","list_zipped","list","1","2","3","a","b","c","100000","6000000000",
  13. "0","number","string","10"
  14. "0","set","set","1","100000","2","3","6000000000","a","b","c",
  15. "0","set_zipped_1","set","1","2","3","4",
  16. "0","set_zipped_2","set","100000","200000","300000","400000",
  17. "0","set_zipped_3","set","1000000000","2000000000","3000000000","4000000000","5000000000","6000000000",
  18. "0","string","string","Hello World"
  19. "0","zset","zset","a","1","b","2","c","3","aa","10","bb","20","cc","30","aaa","100","bbb","200","ccc","300","aaaa","1000","cccc","123456789","bbbb","5000000000",
  20. "0","zset_zipped","zset","a","1","b","2","c","3",
  21. }
  22. }
  23. set server_path [tmpdir "server.rdb-startup-test"]
  24. start_server [list overrides [list "dir" $server_path]] {
  25. test {Server started empty with non-existing RDB file} {
  26. r debug digest
  27. } {0000000000000000000000000000000000000000}
  28. # Save an RDB file, needed for the next test.
  29. r save
  30. }
  31. start_server [list overrides [list "dir" $server_path]] {
  32. test {Server started empty with empty RDB file} {
  33. r debug digest
  34. } {0000000000000000000000000000000000000000}
  35. }
  36. start_server [list overrides [list "dir" $server_path]] {
  37. test {Test RDB stream encoding} {
  38. for {set j 0} {$j < 1000} {incr j} {
  39. if {rand() < 0.9} {
  40. r xadd stream * foo $j
  41. } else {
  42. r xadd stream * bar $j
  43. }
  44. }
  45. r xgroup create stream mygroup 0
  46. r xreadgroup GROUP mygroup Alice COUNT 1 STREAMS stream >
  47. set digest [r debug digest]
  48. r debug reload
  49. set newdigest [r debug digest]
  50. assert {$digest eq $newdigest}
  51. r del stream
  52. }
  53. }
  54. # Helper function to start a server and kill it, just to check the error
  55. # logged.
  56. set defaults {}
  57. proc start_server_and_kill_it {overrides code} {
  58. upvar defaults defaults srv srv server_path server_path
  59. set config [concat $defaults $overrides]
  60. set srv [start_server [list overrides $config]]
  61. uplevel 1 $code
  62. kill_server $srv
  63. }
  64. # Make the RDB file unreadable
  65. file attributes [file join $server_path dump.rdb] -permissions 0222
  66. # Detect root account (it is able to read the file even with 002 perm)
  67. set isroot 0
  68. catch {
  69. open [file join $server_path dump.rdb]
  70. set isroot 1
  71. }
  72. # Now make sure the server aborted with an error
  73. if {!$isroot} {
  74. start_server_and_kill_it [list "dir" $server_path] {
  75. test {Server should not start if RDB file can't be open} {
  76. wait_for_condition 50 100 {
  77. [string match {*Fatal error loading*} \
  78. [exec tail -1 < [dict get $srv stdout]]]
  79. } else {
  80. fail "Server started even if RDB was unreadable!"
  81. }
  82. }
  83. }
  84. }
  85. # Fix permissions of the RDB file.
  86. file attributes [file join $server_path dump.rdb] -permissions 0666
  87. # Corrupt its CRC64 checksum.
  88. set filesize [file size [file join $server_path dump.rdb]]
  89. set fd [open [file join $server_path dump.rdb] r+]
  90. fconfigure $fd -translation binary
  91. seek $fd -8 end
  92. puts -nonewline $fd "foobar00"; # Corrupt the checksum
  93. close $fd
  94. # Now make sure the server aborted with an error
  95. start_server_and_kill_it [list "dir" $server_path] {
  96. test {Server should not start if RDB is corrupted} {
  97. wait_for_condition 50 100 {
  98. [string match {*CRC error*} \
  99. [exec tail -10 < [dict get $srv stdout]]]
  100. } else {
  101. fail "Server started even if RDB was corrupted!"
  102. }
  103. }
  104. }
  105. start_server {} {
  106. test {Test FLUSHALL aborts bgsave} {
  107. r config set rdb-key-save-delay 1000
  108. r debug populate 1000
  109. r bgsave
  110. assert_equal [s rdb_bgsave_in_progress] 1
  111. r flushall
  112. after 200
  113. assert_equal [s rdb_bgsave_in_progress] 0
  114. # make sure the server is still writable
  115. r set x xx
  116. }
  117. }
  118. test {client freed during loading} {
  119. start_server [list overrides [list key-load-delay 10 rdbcompression no]] {
  120. # create a big rdb that will take long to load. it is important
  121. # for keys to be big since the server processes events only once in 2mb.
  122. # 100mb of rdb, 100k keys will load in more than 1 second
  123. r debug populate 100000 key 1000
  124. restart_server 0 false
  125. # make sure it's still loading
  126. assert_equal [s loading] 1
  127. # connect and disconnect 10 clients
  128. set clients {}
  129. for {set j 0} {$j < 10} {incr j} {
  130. lappend clients [redis_deferring_client]
  131. }
  132. foreach rd $clients {
  133. $rd debug log bla
  134. }
  135. foreach rd $clients {
  136. $rd read
  137. }
  138. foreach rd $clients {
  139. $rd close
  140. }
  141. # make sure the server freed the clients
  142. wait_for_condition 100 100 {
  143. [s connected_clients] < 3
  144. } else {
  145. fail "clients didn't disconnect"
  146. }
  147. # make sure it's still loading
  148. assert_equal [s loading] 1
  149. # no need to keep waiting for loading to complete
  150. exec kill [srv 0 pid]
  151. }
  152. }