2
0

replication.tcl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. start_server {tags {"repl"}} {
  2. start_server {} {
  3. test {First server should have role slave after SLAVEOF} {
  4. r -1 slaveof [srv 0 host] [srv 0 port]
  5. wait_for_condition 50 100 {
  6. [s -1 role] eq {slave} &&
  7. [string match {*master_link_status:up*} [r -1 info replication]]
  8. } else {
  9. fail "Can't turn the instance into a slave"
  10. }
  11. }
  12. test {BRPOPLPUSH replication, when blocking against empty list} {
  13. set rd [redis_deferring_client]
  14. $rd brpoplpush a b 5
  15. r lpush a foo
  16. wait_for_condition 50 100 {
  17. [r debug digest] eq [r -1 debug digest]
  18. } else {
  19. fail "Master and slave have different digest: [r debug digest] VS [r -1 debug digest]"
  20. }
  21. }
  22. test {BRPOPLPUSH replication, list exists} {
  23. set rd [redis_deferring_client]
  24. r lpush c 1
  25. r lpush c 2
  26. r lpush c 3
  27. $rd brpoplpush c d 5
  28. after 1000
  29. assert_equal [r debug digest] [r -1 debug digest]
  30. }
  31. }
  32. }
  33. start_server {tags {"repl"}} {
  34. r set mykey foo
  35. start_server {} {
  36. test {Second server should have role master at first} {
  37. s role
  38. } {master}
  39. test {SLAVEOF should start with link status "down"} {
  40. r slaveof [srv -1 host] [srv -1 port]
  41. s master_link_status
  42. } {down}
  43. test {The role should immediately be changed to "slave"} {
  44. s role
  45. } {slave}
  46. wait_for_sync r
  47. test {Sync should have transferred keys from master} {
  48. r get mykey
  49. } {foo}
  50. test {The link status should be up} {
  51. s master_link_status
  52. } {up}
  53. test {SET on the master should immediately propagate} {
  54. r -1 set mykey bar
  55. wait_for_condition 500 100 {
  56. [r 0 get mykey] eq {bar}
  57. } else {
  58. fail "SET on master did not propagated on slave"
  59. }
  60. }
  61. test {FLUSHALL should replicate} {
  62. r -1 flushall
  63. if {$::valgrind} {after 2000}
  64. list [r -1 dbsize] [r 0 dbsize]
  65. } {0 0}
  66. }
  67. }
  68. proc start_write_load {host port seconds} {
  69. set tclsh [info nameofexecutable]
  70. exec $tclsh tests/helpers/gen_write_load.tcl $host $port $seconds &
  71. }
  72. proc stop_write_load {handle} {
  73. catch {exec /bin/kill -9 $handle}
  74. }
  75. start_server {tags {"repl"}} {
  76. set master [srv 0 client]
  77. set master_host [srv 0 host]
  78. set master_port [srv 0 port]
  79. set slaves {}
  80. set load_handle0 [start_write_load $master_host $master_port 3]
  81. set load_handle1 [start_write_load $master_host $master_port 5]
  82. set load_handle2 [start_write_load $master_host $master_port 20]
  83. set load_handle3 [start_write_load $master_host $master_port 8]
  84. set load_handle4 [start_write_load $master_host $master_port 4]
  85. start_server {} {
  86. lappend slaves [srv 0 client]
  87. start_server {} {
  88. lappend slaves [srv 0 client]
  89. start_server {} {
  90. lappend slaves [srv 0 client]
  91. test "Connect multiple slaves at the same time (issue #141)" {
  92. # Send SALVEOF commands to slaves
  93. [lindex $slaves 0] slaveof $master_host $master_port
  94. [lindex $slaves 1] slaveof $master_host $master_port
  95. [lindex $slaves 2] slaveof $master_host $master_port
  96. # Wait for all the three slaves to reach the "online" state
  97. set retry 500
  98. while {$retry} {
  99. set info [r -3 info]
  100. if {[string match {*slave0:*state=online*slave1:*state=online*slave2:*state=online*} $info]} {
  101. break
  102. } else {
  103. incr retry -1
  104. after 100
  105. }
  106. }
  107. if {$retry == 0} {
  108. error "assertion:Slaves not correctly synchronized"
  109. }
  110. # Stop the write load
  111. stop_write_load $load_handle0
  112. stop_write_load $load_handle1
  113. stop_write_load $load_handle2
  114. stop_write_load $load_handle3
  115. stop_write_load $load_handle4
  116. # Wait that slaves exit the "loading" state
  117. wait_for_condition 500 100 {
  118. ![string match {*loading:1*} [[lindex $slaves 0] info]] &&
  119. ![string match {*loading:1*} [[lindex $slaves 1] info]] &&
  120. ![string match {*loading:1*} [[lindex $slaves 2] info]]
  121. } else {
  122. fail "Slaves still loading data after too much time"
  123. }
  124. # Make sure that slaves and master have same number of keys
  125. wait_for_condition 500 100 {
  126. [$master dbsize] == [[lindex $slaves 0] dbsize] &&
  127. [$master dbsize] == [[lindex $slaves 1] dbsize] &&
  128. [$master dbsize] == [[lindex $slaves 2] dbsize]
  129. } else {
  130. fail "Different number of keys between masted and slave after too long time."
  131. }
  132. # Check digests
  133. set digest [$master debug digest]
  134. set digest0 [[lindex $slaves 0] debug digest]
  135. set digest1 [[lindex $slaves 1] debug digest]
  136. set digest2 [[lindex $slaves 2] debug digest]
  137. assert {$digest ne 0000000000000000000000000000000000000000}
  138. assert {$digest eq $digest0}
  139. assert {$digest eq $digest1}
  140. assert {$digest eq $digest2}
  141. }
  142. }
  143. }
  144. }
  145. }