replication-psync.tcl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #
  15. # If reconnect is > 0, the test actually try to break the connection and
  16. # reconnect with the master, otherwise just the initial synchronization is
  17. # checked for consistency.
  18. proc test_psync {descr duration backlog_size backlog_ttl delay cond diskless reconnect} {
  19. start_server {tags {"repl"}} {
  20. start_server {} {
  21. set master [srv -1 client]
  22. set master_host [srv -1 host]
  23. set master_port [srv -1 port]
  24. set slave [srv 0 client]
  25. $master config set repl-backlog-size $backlog_size
  26. $master config set repl-backlog-ttl $backlog_ttl
  27. $master config set repl-diskless-sync $diskless
  28. $master config set repl-diskless-sync-delay 1
  29. set load_handle0 [start_bg_complex_data $master_host $master_port 9 100000]
  30. set load_handle1 [start_bg_complex_data $master_host $master_port 11 100000]
  31. set load_handle2 [start_bg_complex_data $master_host $master_port 12 100000]
  32. test {Slave should be able to synchronize with the master} {
  33. $slave slaveof $master_host $master_port
  34. wait_for_condition 50 100 {
  35. [lindex [r role] 0] eq {slave} &&
  36. [lindex [r role] 3] eq {connected}
  37. } else {
  38. fail "Replication not started."
  39. }
  40. }
  41. # Check that the background clients are actually writing.
  42. test {Detect write load to master} {
  43. wait_for_condition 50 100 {
  44. [$master dbsize] > 100
  45. } else {
  46. fail "Can't detect write load from background clients."
  47. }
  48. }
  49. test "Test replication partial resync: $descr (diskless: $diskless, reconnect: $reconnect)" {
  50. # Now while the clients are writing data, break the maste-slave
  51. # link multiple times.
  52. if ($reconnect) {
  53. for {set j 0} {$j < $duration*10} {incr j} {
  54. after 100
  55. # catch {puts "MASTER [$master dbsize] keys, SLAVE [$slave dbsize] keys"}
  56. if {($j % 20) == 0} {
  57. catch {
  58. if {$delay} {
  59. $slave multi
  60. $slave client kill $master_host:$master_port
  61. $slave debug sleep $delay
  62. $slave exec
  63. } else {
  64. $slave client kill $master_host:$master_port
  65. }
  66. }
  67. }
  68. }
  69. }
  70. stop_bg_complex_data $load_handle0
  71. stop_bg_complex_data $load_handle1
  72. stop_bg_complex_data $load_handle2
  73. set retry 10
  74. while {$retry && ([$master debug digest] ne [$slave debug digest])}\
  75. {
  76. after 1000
  77. incr retry -1
  78. }
  79. assert {[$master dbsize] > 0}
  80. if {[$master debug digest] ne [$slave debug digest]} {
  81. set csv1 [csvdump r]
  82. set csv2 [csvdump {r -1}]
  83. set fd [open /tmp/repldump1.txt w]
  84. puts -nonewline $fd $csv1
  85. close $fd
  86. set fd [open /tmp/repldump2.txt w]
  87. puts -nonewline $fd $csv2
  88. close $fd
  89. puts "Master - Slave inconsistency"
  90. puts "Run diff -u against /tmp/repldump*.txt for more info"
  91. }
  92. assert_equal [r debug digest] [r -1 debug digest]
  93. eval $cond
  94. }
  95. }
  96. }
  97. }
  98. foreach diskless {no yes} {
  99. test_psync {no reconnection, just sync} 6 1000000 3600 0 {
  100. } $diskless 0
  101. test_psync {ok psync} 6 100000000 3600 0 {
  102. assert {[s -1 sync_partial_ok] > 0}
  103. } $diskless 1
  104. test_psync {no backlog} 6 100 3600 0.5 {
  105. assert {[s -1 sync_partial_err] > 0}
  106. } $diskless 1
  107. test_psync {ok after delay} 3 100000000 3600 3 {
  108. assert {[s -1 sync_partial_ok] > 0}
  109. } $diskless 1
  110. test_psync {backlog expired} 3 100000000 1 3 {
  111. assert {[s -1 sync_partial_err] > 0}
  112. } $diskless 1
  113. }