replication.tcl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. test {ROLE in master reports master with a slave} {
  67. set res [r -1 role]
  68. lassign $res role offset slaves
  69. assert {$role eq {master}}
  70. assert {$offset > 0}
  71. assert {[llength $slaves] == 1}
  72. lassign [lindex $slaves 0] master_host master_port slave_offset
  73. assert {$slave_offset <= $offset}
  74. }
  75. test {ROLE in slave reports slave in connected state} {
  76. set res [r role]
  77. lassign $res role master_host master_port slave_state slave_offset
  78. assert {$role eq {slave}}
  79. assert {$slave_state eq {connected}}
  80. }
  81. }
  82. }
  83. foreach dl {no yes} {
  84. start_server {tags {"repl"}} {
  85. set master [srv 0 client]
  86. $master config set repl-diskless-sync $dl
  87. set master_host [srv 0 host]
  88. set master_port [srv 0 port]
  89. set slaves {}
  90. set load_handle0 [start_write_load $master_host $master_port 3]
  91. set load_handle1 [start_write_load $master_host $master_port 5]
  92. set load_handle2 [start_write_load $master_host $master_port 20]
  93. set load_handle3 [start_write_load $master_host $master_port 8]
  94. set load_handle4 [start_write_load $master_host $master_port 4]
  95. start_server {} {
  96. lappend slaves [srv 0 client]
  97. start_server {} {
  98. lappend slaves [srv 0 client]
  99. start_server {} {
  100. lappend slaves [srv 0 client]
  101. test "Connect multiple slaves at the same time (issue #141), diskless=$dl" {
  102. # Send SALVEOF commands to slaves
  103. [lindex $slaves 0] slaveof $master_host $master_port
  104. [lindex $slaves 1] slaveof $master_host $master_port
  105. [lindex $slaves 2] slaveof $master_host $master_port
  106. # Wait for all the three slaves to reach the "online" state
  107. set retry 500
  108. while {$retry} {
  109. set info [r -3 info]
  110. if {[string match {*slave0:*state=online*slave1:*state=online*slave2:*state=online*} $info]} {
  111. break
  112. } else {
  113. incr retry -1
  114. after 100
  115. }
  116. }
  117. if {$retry == 0} {
  118. error "assertion:Slaves not correctly synchronized"
  119. }
  120. # Stop the write load
  121. stop_write_load $load_handle0
  122. stop_write_load $load_handle1
  123. stop_write_load $load_handle2
  124. stop_write_load $load_handle3
  125. stop_write_load $load_handle4
  126. # Wait that slaves exit the "loading" state
  127. wait_for_condition 500 100 {
  128. ![string match {*loading:1*} [[lindex $slaves 0] info]] &&
  129. ![string match {*loading:1*} [[lindex $slaves 1] info]] &&
  130. ![string match {*loading:1*} [[lindex $slaves 2] info]]
  131. } else {
  132. fail "Slaves still loading data after too much time"
  133. }
  134. # Make sure that slaves and master have same number of keys
  135. wait_for_condition 500 100 {
  136. [$master dbsize] == [[lindex $slaves 0] dbsize] &&
  137. [$master dbsize] == [[lindex $slaves 1] dbsize] &&
  138. [$master dbsize] == [[lindex $slaves 2] dbsize]
  139. } else {
  140. fail "Different number of keys between masted and slave after too long time."
  141. }
  142. # Check digests
  143. set digest [$master debug digest]
  144. set digest0 [[lindex $slaves 0] debug digest]
  145. set digest1 [[lindex $slaves 1] debug digest]
  146. set digest2 [[lindex $slaves 2] debug digest]
  147. assert {$digest ne 0000000000000000000000000000000000000000}
  148. assert {$digest eq $digest0}
  149. assert {$digest eq $digest1}
  150. assert {$digest eq $digest2}
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }