testrdb.tcl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. set testmodule [file normalize tests/modules/testrdb.so]
  2. tags "modules" {
  3. test {modules are able to persist types} {
  4. start_server [list overrides [list loadmodule "$testmodule"]] {
  5. r testrdb.set.key key1 value1
  6. assert_equal "value1" [r testrdb.get.key key1]
  7. r debug reload
  8. assert_equal "value1" [r testrdb.get.key key1]
  9. }
  10. }
  11. test {modules global are lost without aux} {
  12. set server_path [tmpdir "server.module-testrdb"]
  13. start_server [list overrides [list loadmodule "$testmodule" "dir" $server_path] keep_persistence true] {
  14. r testrdb.set.before global1
  15. assert_equal "global1" [r testrdb.get.before]
  16. }
  17. start_server [list overrides [list loadmodule "$testmodule" "dir" $server_path]] {
  18. assert_equal "" [r testrdb.get.before]
  19. }
  20. }
  21. test {modules are able to persist globals before and after} {
  22. set server_path [tmpdir "server.module-testrdb"]
  23. start_server [list overrides [list loadmodule "$testmodule 2" "dir" $server_path] keep_persistence true] {
  24. r testrdb.set.before global1
  25. r testrdb.set.after global2
  26. assert_equal "global1" [r testrdb.get.before]
  27. assert_equal "global2" [r testrdb.get.after]
  28. }
  29. start_server [list overrides [list loadmodule "$testmodule 2" "dir" $server_path]] {
  30. assert_equal "global1" [r testrdb.get.before]
  31. assert_equal "global2" [r testrdb.get.after]
  32. }
  33. }
  34. test {modules are able to persist globals just after} {
  35. set server_path [tmpdir "server.module-testrdb"]
  36. start_server [list overrides [list loadmodule "$testmodule 1" "dir" $server_path] keep_persistence true] {
  37. r testrdb.set.after global2
  38. assert_equal "global2" [r testrdb.get.after]
  39. }
  40. start_server [list overrides [list loadmodule "$testmodule 1" "dir" $server_path]] {
  41. assert_equal "global2" [r testrdb.get.after]
  42. }
  43. }
  44. tags {repl} {
  45. test {diskless loading short read with module} {
  46. start_server [list overrides [list loadmodule "$testmodule"]] {
  47. set replica [srv 0 client]
  48. set replica_host [srv 0 host]
  49. set replica_port [srv 0 port]
  50. start_server [list overrides [list loadmodule "$testmodule"]] {
  51. set master [srv 0 client]
  52. set master_host [srv 0 host]
  53. set master_port [srv 0 port]
  54. # Set master and replica to use diskless replication
  55. $master config set repl-diskless-sync yes
  56. $master config set rdbcompression no
  57. $replica config set repl-diskless-load swapdb
  58. $master config set hz 500
  59. $replica config set hz 500
  60. $master config set dynamic-hz no
  61. $replica config set dynamic-hz no
  62. set start [clock clicks -milliseconds]
  63. for {set k 0} {$k < 30} {incr k} {
  64. r testrdb.set.key key$k [string repeat A [expr {int(rand()*1000000)}]]
  65. }
  66. if {$::verbose} {
  67. set end [clock clicks -milliseconds]
  68. set duration [expr $end - $start]
  69. puts "filling took $duration ms (TODO: use pipeline)"
  70. set start [clock clicks -milliseconds]
  71. }
  72. # Start the replication process...
  73. set loglines [count_log_lines -1]
  74. $master config set repl-diskless-sync-delay 0
  75. $replica replicaof $master_host $master_port
  76. # kill the replication at various points
  77. set attempts 100
  78. if {$::accurate} { set attempts 500 }
  79. for {set i 0} {$i < $attempts} {incr i} {
  80. # wait for the replica to start reading the rdb
  81. # using the log file since the replica only responds to INFO once in 2mb
  82. set res [wait_for_log_messages -1 {"*Loading DB in memory*"} $loglines 2000 1]
  83. set loglines [lindex $res 1]
  84. # add some additional random sleep so that we kill the master on a different place each time
  85. after [expr {int(rand()*50)}]
  86. # kill the replica connection on the master
  87. set killed [$master client kill type replica]
  88. set res [wait_for_log_messages -1 {"*Internal error in RDB*" "*Finished with success*" "*Successful partial resynchronization*"} $loglines 1000 1]
  89. if {$::verbose} { puts $res }
  90. set log_text [lindex $res 0]
  91. set loglines [lindex $res 1]
  92. if {![string match "*Internal error in RDB*" $log_text]} {
  93. # force the replica to try another full sync
  94. $master multi
  95. $master client kill type replica
  96. $master set asdf asdf
  97. # fill replication backlog with new content
  98. $master config set repl-backlog-size 16384
  99. for {set keyid 0} {$keyid < 10} {incr keyid} {
  100. $master set "$keyid string_$keyid" [string repeat A 16384]
  101. }
  102. $master exec
  103. }
  104. # wait for loading to stop (fail)
  105. wait_for_condition 1000 1 {
  106. [s -1 loading] eq 0
  107. } else {
  108. fail "Replica didn't disconnect"
  109. }
  110. }
  111. if {$::verbose} {
  112. set end [clock clicks -milliseconds]
  113. set duration [expr $end - $start]
  114. puts "test took $duration ms"
  115. }
  116. # enable fast shutdown
  117. $master config set rdb-key-save-delay 0
  118. }
  119. }
  120. }
  121. }
  122. }