psync2-pingoff.tcl 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # These tests were added together with the meaningful offset implementation
  2. # in redis 6.0.0, which was later abandoned in 6.0.4, they used to test that
  3. # servers are able to PSYNC with replicas even if the replication stream has
  4. # PINGs at the end which present in one sever and missing on another.
  5. # We keep these tests just because they reproduce edge cases in the replication
  6. # logic in hope they'll be able to spot some problem in the future.
  7. start_server {tags {"psync2"}} {
  8. start_server {} {
  9. # Config
  10. set debug_msg 0 ; # Enable additional debug messages
  11. for {set j 0} {$j < 2} {incr j} {
  12. set R($j) [srv [expr 0-$j] client]
  13. set R_host($j) [srv [expr 0-$j] host]
  14. set R_port($j) [srv [expr 0-$j] port]
  15. $R($j) CONFIG SET repl-ping-replica-period 1
  16. if {$debug_msg} {puts "Log file: [srv [expr 0-$j] stdout]"}
  17. }
  18. # Setup replication
  19. test "PSYNC2 pingoff: setup" {
  20. $R(1) replicaof $R_host(0) $R_port(0)
  21. $R(0) set foo bar
  22. wait_for_condition 50 1000 {
  23. [status $R(1) master_link_status] == "up" &&
  24. [$R(0) dbsize] == 1 && [$R(1) dbsize] == 1
  25. } else {
  26. fail "Replicas not replicating from master"
  27. }
  28. }
  29. test "PSYNC2 pingoff: write and wait replication" {
  30. $R(0) INCR counter
  31. $R(0) INCR counter
  32. $R(0) INCR counter
  33. wait_for_condition 50 1000 {
  34. [$R(0) GET counter] eq [$R(1) GET counter]
  35. } else {
  36. fail "Master and replica don't agree about counter"
  37. }
  38. }
  39. # In this test we'll make sure the replica will get stuck, but with
  40. # an active connection: this way the master will continue to send PINGs
  41. # every second (we modified the PING period earlier)
  42. test "PSYNC2 pingoff: pause replica and promote it" {
  43. $R(1) MULTI
  44. $R(1) DEBUG SLEEP 5
  45. $R(1) SLAVEOF NO ONE
  46. $R(1) EXEC
  47. $R(1) ping ; # Wait for it to return back available
  48. }
  49. test "Make the old master a replica of the new one and check conditions" {
  50. assert_equal [status $R(1) sync_full] 0
  51. $R(0) REPLICAOF $R_host(1) $R_port(1)
  52. wait_for_condition 50 1000 {
  53. [status $R(1) sync_full] == 1
  54. } else {
  55. fail "The new master was not able to sync"
  56. }
  57. # make sure replication is still alive and kicking
  58. $R(1) incr x
  59. wait_for_condition 50 1000 {
  60. [status $R(0) loading] == 0 &&
  61. [$R(0) get x] == 1
  62. } else {
  63. fail "replica didn't get incr"
  64. }
  65. assert_equal [status $R(0) master_repl_offset] [status $R(1) master_repl_offset]
  66. }
  67. }}
  68. start_server {tags {"psync2"}} {
  69. start_server {} {
  70. start_server {} {
  71. start_server {} {
  72. start_server {} {
  73. test {test various edge cases of repl topology changes with missing pings at the end} {
  74. set master [srv -4 client]
  75. set master_host [srv -4 host]
  76. set master_port [srv -4 port]
  77. set replica1 [srv -3 client]
  78. set replica2 [srv -2 client]
  79. set replica3 [srv -1 client]
  80. set replica4 [srv -0 client]
  81. $replica1 replicaof $master_host $master_port
  82. $replica2 replicaof $master_host $master_port
  83. $replica3 replicaof $master_host $master_port
  84. $replica4 replicaof $master_host $master_port
  85. wait_for_condition 50 1000 {
  86. [status $master connected_slaves] == 4
  87. } else {
  88. fail "replicas didn't connect"
  89. }
  90. $master incr x
  91. wait_for_condition 50 1000 {
  92. [$replica1 get x] == 1 && [$replica2 get x] == 1 &&
  93. [$replica3 get x] == 1 && [$replica4 get x] == 1
  94. } else {
  95. fail "replicas didn't get incr"
  96. }
  97. # disconnect replica1 and replica2
  98. # and wait for the master to send a ping to replica3 and replica4
  99. $replica1 replicaof no one
  100. $replica2 replicaof 127.0.0.1 1 ;# we can't promote it to master since that will cycle the replication id
  101. $master config set repl-ping-replica-period 1
  102. after 1500
  103. # make everyone sync from the replica1 that didn't get the last ping from the old master
  104. # replica4 will keep syncing from the old master which now syncs from replica1
  105. # and replica2 will re-connect to the old master (which went back in time)
  106. set new_master_host [srv -3 host]
  107. set new_master_port [srv -3 port]
  108. $replica3 replicaof $new_master_host $new_master_port
  109. $master replicaof $new_master_host $new_master_port
  110. $replica2 replicaof $master_host $master_port
  111. wait_for_condition 50 1000 {
  112. [status $replica2 master_link_status] == "up" &&
  113. [status $replica3 master_link_status] == "up" &&
  114. [status $replica4 master_link_status] == "up" &&
  115. [status $master master_link_status] == "up"
  116. } else {
  117. fail "replicas didn't connect"
  118. }
  119. # make sure replication is still alive and kicking
  120. $replica1 incr x
  121. wait_for_condition 50 1000 {
  122. [$replica2 get x] == 2 &&
  123. [$replica3 get x] == 2 &&
  124. [$replica4 get x] == 2 &&
  125. [$master get x] == 2
  126. } else {
  127. fail "replicas didn't get incr"
  128. }
  129. # make sure we have the right amount of full syncs
  130. assert_equal [status $master sync_full] 6
  131. assert_equal [status $replica1 sync_full] 2
  132. assert_equal [status $replica2 sync_full] 0
  133. assert_equal [status $replica3 sync_full] 0
  134. assert_equal [status $replica4 sync_full] 0
  135. # force psync
  136. $master client kill type master
  137. $replica2 client kill type master
  138. $replica3 client kill type master
  139. $replica4 client kill type master
  140. # make sure replication is still alive and kicking
  141. $replica1 incr x
  142. wait_for_condition 50 1000 {
  143. [$replica2 get x] == 3 &&
  144. [$replica3 get x] == 3 &&
  145. [$replica4 get x] == 3 &&
  146. [$master get x] == 3
  147. } else {
  148. fail "replicas didn't get incr"
  149. }
  150. # make sure we have the right amount of full syncs
  151. assert_equal [status $master sync_full] 6
  152. assert_equal [status $replica1 sync_full] 2
  153. assert_equal [status $replica2 sync_full] 0
  154. assert_equal [status $replica3 sync_full] 0
  155. assert_equal [status $replica4 sync_full] 0
  156. }
  157. }}}}}
  158. start_server {tags {"psync2"}} {
  159. start_server {} {
  160. start_server {} {
  161. for {set j 0} {$j < 3} {incr j} {
  162. set R($j) [srv [expr 0-$j] client]
  163. set R_host($j) [srv [expr 0-$j] host]
  164. set R_port($j) [srv [expr 0-$j] port]
  165. $R($j) CONFIG SET repl-ping-replica-period 1
  166. }
  167. test "Chained replicas disconnect when replica re-connect with the same master" {
  168. # Add a second replica as a chained replica of the current replica
  169. $R(1) replicaof $R_host(0) $R_port(0)
  170. $R(2) replicaof $R_host(1) $R_port(1)
  171. wait_for_condition 50 1000 {
  172. [status $R(2) master_link_status] == "up"
  173. } else {
  174. fail "Chained replica not replicating from its master"
  175. }
  176. # Do a write on the master, and wait for 3 seconds for the master to
  177. # send some PINGs to its replica
  178. $R(0) INCR counter2
  179. after 2000
  180. set sync_partial_master [status $R(0) sync_partial_ok]
  181. set sync_partial_replica [status $R(1) sync_partial_ok]
  182. $R(0) CONFIG SET repl-ping-replica-period 100
  183. # Disconnect the master's direct replica
  184. $R(0) client kill type replica
  185. wait_for_condition 50 1000 {
  186. [status $R(1) master_link_status] == "up" &&
  187. [status $R(2) master_link_status] == "up" &&
  188. [status $R(0) sync_partial_ok] == $sync_partial_master + 1 &&
  189. [status $R(1) sync_partial_ok] == $sync_partial_replica
  190. } else {
  191. fail "Disconnected replica failed to PSYNC with master"
  192. }
  193. # Verify that the replica and its replica's meaningful and real
  194. # offsets match with the master
  195. assert_equal [status $R(0) master_repl_offset] [status $R(1) master_repl_offset]
  196. assert_equal [status $R(0) master_repl_offset] [status $R(2) master_repl_offset]
  197. # make sure replication is still alive and kicking
  198. $R(0) incr counter2
  199. wait_for_condition 50 1000 {
  200. [$R(1) get counter2] == 2 && [$R(2) get counter2] == 2
  201. } else {
  202. fail "replicas didn't get incr"
  203. }
  204. assert_equal [status $R(0) master_repl_offset] [status $R(1) master_repl_offset]
  205. assert_equal [status $R(0) master_repl_offset] [status $R(2) master_repl_offset]
  206. }
  207. }}}