psync2-pingoff.tcl 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 external:skip"}} {
  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 external:skip"}} {
  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. set replofs [status $master master_repl_offset]
  103. wait_for_condition 50 100 {
  104. [status $replica3 master_repl_offset] > $replofs &&
  105. [status $replica4 master_repl_offset] > $replofs
  106. } else {
  107. fail "replica didn't sync in time"
  108. }
  109. # make everyone sync from the replica1 that didn't get the last ping from the old master
  110. # replica4 will keep syncing from the old master which now syncs from replica1
  111. # and replica2 will re-connect to the old master (which went back in time)
  112. set new_master_host [srv -3 host]
  113. set new_master_port [srv -3 port]
  114. $replica3 replicaof $new_master_host $new_master_port
  115. $master replicaof $new_master_host $new_master_port
  116. $replica2 replicaof $master_host $master_port
  117. wait_for_condition 50 1000 {
  118. [status $replica2 master_link_status] == "up" &&
  119. [status $replica3 master_link_status] == "up" &&
  120. [status $replica4 master_link_status] == "up" &&
  121. [status $master master_link_status] == "up"
  122. } else {
  123. fail "replicas didn't connect"
  124. }
  125. # make sure replication is still alive and kicking
  126. $replica1 incr x
  127. wait_for_condition 50 1000 {
  128. [$replica2 get x] == 2 &&
  129. [$replica3 get x] == 2 &&
  130. [$replica4 get x] == 2 &&
  131. [$master get x] == 2
  132. } else {
  133. fail "replicas didn't get incr"
  134. }
  135. # make sure we have the right amount of full syncs
  136. assert_equal [status $master sync_full] 6
  137. assert_equal [status $replica1 sync_full] 2
  138. assert_equal [status $replica2 sync_full] 0
  139. assert_equal [status $replica3 sync_full] 0
  140. assert_equal [status $replica4 sync_full] 0
  141. # force psync
  142. $master client kill type master
  143. $replica2 client kill type master
  144. $replica3 client kill type master
  145. $replica4 client kill type master
  146. # make sure replication is still alive and kicking
  147. $replica1 incr x
  148. wait_for_condition 50 1000 {
  149. [$replica2 get x] == 3 &&
  150. [$replica3 get x] == 3 &&
  151. [$replica4 get x] == 3 &&
  152. [$master get x] == 3
  153. } else {
  154. fail "replicas didn't get incr"
  155. }
  156. # make sure we have the right amount of full syncs
  157. assert_equal [status $master sync_full] 6
  158. assert_equal [status $replica1 sync_full] 2
  159. assert_equal [status $replica2 sync_full] 0
  160. assert_equal [status $replica3 sync_full] 0
  161. assert_equal [status $replica4 sync_full] 0
  162. }
  163. }}}}}
  164. start_server {tags {"psync2 external:skip"}} {
  165. start_server {} {
  166. start_server {} {
  167. for {set j 0} {$j < 3} {incr j} {
  168. set R($j) [srv [expr 0-$j] client]
  169. set R_host($j) [srv [expr 0-$j] host]
  170. set R_port($j) [srv [expr 0-$j] port]
  171. $R($j) CONFIG SET repl-ping-replica-period 1
  172. }
  173. test "Chained replicas disconnect when replica re-connect with the same master" {
  174. # Add a second replica as a chained replica of the current replica
  175. $R(1) replicaof $R_host(0) $R_port(0)
  176. $R(2) replicaof $R_host(1) $R_port(1)
  177. wait_for_condition 50 1000 {
  178. [status $R(2) master_link_status] == "up"
  179. } else {
  180. fail "Chained replica not replicating from its master"
  181. }
  182. # Do a write on the master, and wait for the master to
  183. # send some PINGs to its replica
  184. $R(0) INCR counter2
  185. set replofs [status $R(0) master_repl_offset]
  186. wait_for_condition 50 100 {
  187. [status $R(1) master_repl_offset] > $replofs &&
  188. [status $R(2) master_repl_offset] > $replofs
  189. } else {
  190. fail "replica didn't sync in time"
  191. }
  192. set sync_partial_master [status $R(0) sync_partial_ok]
  193. set sync_partial_replica [status $R(1) sync_partial_ok]
  194. $R(0) CONFIG SET repl-ping-replica-period 100
  195. # Disconnect the master's direct replica
  196. $R(0) client kill type replica
  197. wait_for_condition 50 1000 {
  198. [status $R(1) master_link_status] == "up" &&
  199. [status $R(2) master_link_status] == "up" &&
  200. [status $R(0) sync_partial_ok] == $sync_partial_master + 1 &&
  201. [status $R(1) sync_partial_ok] == $sync_partial_replica
  202. } else {
  203. fail "Disconnected replica failed to PSYNC with master"
  204. }
  205. # Verify that the replica and its replica's meaningful and real
  206. # offsets match with the master
  207. assert_equal [status $R(0) master_repl_offset] [status $R(1) master_repl_offset]
  208. assert_equal [status $R(0) master_repl_offset] [status $R(2) master_repl_offset]
  209. # make sure replication is still alive and kicking
  210. $R(0) incr counter2
  211. wait_for_condition 50 1000 {
  212. [$R(1) get counter2] == 2 && [$R(2) get counter2] == 2
  213. } else {
  214. fail "replicas didn't get incr"
  215. }
  216. assert_equal [status $R(0) master_repl_offset] [status $R(1) master_repl_offset]
  217. assert_equal [status $R(0) master_repl_offset] [status $R(2) master_repl_offset]
  218. }
  219. }}}