replication-psync.tcl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. proc start_bg_complex_data {host port db ops} {
  2. set tclsh [info nameofexecutable]
  3. exec $tclsh tests/helpers/bg_complex_data.tcl $host $port $db $ops &
  4. }
  5. proc stop_bg_complex_data {handle} {
  6. catch {exec /bin/kill -9 $handle}
  7. }
  8. # Creates a master-slave pair and breaks the link continuously to force
  9. # partial resyncs attempts, all this while flooding the master with
  10. # write queries.
  11. #
  12. # You can specifiy backlog size, ttl, delay before reconnection, test duration
  13. # in seconds, and an additional condition to verify at the end.
  14. proc test_psync {descr duration backlog_size backlog_ttl delay cond} {
  15. start_server {tags {"repl"}} {
  16. start_server {} {
  17. set master [srv -1 client]
  18. set master_host [srv -1 host]
  19. set master_port [srv -1 port]
  20. set slave [srv 0 client]
  21. $master config set repl-backlog-size $backlog_size
  22. $master config set repl-backlog-ttl $backlog_ttl
  23. set load_handle0 [start_bg_complex_data $master_host $master_port 9 100000]
  24. set load_handle1 [start_bg_complex_data $master_host $master_port 11 100000]
  25. set load_handle2 [start_bg_complex_data $master_host $master_port 12 100000]
  26. test {Slave should be able to synchronize with the master} {
  27. $slave slaveof $master_host $master_port
  28. wait_for_condition 50 100 {
  29. [lindex [r role] 0] eq {slave} &&
  30. [lindex [r role] 3] eq {connected}
  31. } else {
  32. fail "Replication not started."
  33. }
  34. }
  35. # Check that the background clients are actually writing.
  36. test {Detect write load to master} {
  37. wait_for_condition 50 100 {
  38. [$master dbsize] > 100
  39. } else {
  40. fail "Can't detect write load from background clients."
  41. }
  42. }
  43. test "Test replication partial resync: $descr" {
  44. # Now while the clients are writing data, break the maste-slave
  45. # link multiple times.
  46. for {set j 0} {$j < $duration*10} {incr j} {
  47. after 100
  48. # catch {puts "MASTER [$master dbsize] keys, SLAVE [$slave dbsize] keys"}
  49. if {($j % 20) == 0} {
  50. catch {
  51. if {$delay} {
  52. $slave multi
  53. $slave client kill $master_host:$master_port
  54. $slave debug sleep $delay
  55. $slave exec
  56. } else {
  57. $slave client kill $master_host:$master_port
  58. }
  59. }
  60. }
  61. }
  62. stop_bg_complex_data $load_handle0
  63. stop_bg_complex_data $load_handle1
  64. stop_bg_complex_data $load_handle2
  65. set retry 10
  66. while {$retry && ([$master debug digest] ne [$slave debug digest])}\
  67. {
  68. after 1000
  69. incr retry -1
  70. }
  71. assert {[$master dbsize] > 0}
  72. if {[$master debug digest] ne [$slave debug digest]} {
  73. set csv1 [csvdump r]
  74. set csv2 [csvdump {r -1}]
  75. set fd [open /tmp/repldump1.txt w]
  76. puts -nonewline $fd $csv1
  77. close $fd
  78. set fd [open /tmp/repldump2.txt w]
  79. puts -nonewline $fd $csv2
  80. close $fd
  81. puts "Master - Slave inconsistency"
  82. puts "Run diff -u against /tmp/repldump*.txt for more info"
  83. }
  84. assert_equal [r debug digest] [r -1 debug digest]
  85. eval $cond
  86. }
  87. }
  88. }
  89. }
  90. test_psync {ok psync} 6 1000000 3600 0 {
  91. assert {[s -1 sync_partial_ok] > 0}
  92. }
  93. test_psync {no backlog} 6 100 3600 0.5 {
  94. assert {[s -1 sync_partial_err] > 0}
  95. }
  96. test_psync {ok after delay} 3 100000000 3600 3 {
  97. assert {[s -1 sync_partial_ok] > 0}
  98. }
  99. test_psync {backlog expired} 3 100000000 1 3 {
  100. assert {[s -1 sync_partial_err] > 0}
  101. }