|
| 1 | +start_server {tags {"psync2"}} { |
| 2 | +start_server {} { |
| 3 | +start_server {} { |
| 4 | +start_server {} { |
| 5 | +start_server {} { |
| 6 | + set master_id 0 ; # Current master |
| 7 | + set start_time [clock seconds] ; # Test start time |
| 8 | + set counter_value 0 ; # Current value of the Redis counter "x" |
| 9 | + |
| 10 | + # Config |
| 11 | + set debug_msg 0 ; # Enable additional debug messages |
| 12 | + |
| 13 | + set no_exit 0; ; # Do not exit at end of the test |
| 14 | + |
| 15 | + set duration 20 ; # Total test seconds |
| 16 | + |
| 17 | + set genload 1 ; # Load master with writes at every cycle |
| 18 | + |
| 19 | + set genload_time 5000 ; # Writes duration time in ms |
| 20 | + |
| 21 | + set disconnect 1 ; # Break replication link between random |
| 22 | + # master and slave instances while the |
| 23 | + # master is loaded with writes. |
| 24 | + |
| 25 | + set disconnect_period 1000 ; # Disconnect repl link every N ms. |
| 26 | + |
| 27 | + for {set j 0} {$j < 5} {incr j} { |
| 28 | + set R($j) [srv [expr 0-$j] client] |
| 29 | + set R_host($j) [srv [expr 0-$j] host] |
| 30 | + set R_port($j) [srv [expr 0-$j] port] |
| 31 | + if {$debug_msg} {puts "Log file: [srv [expr 0-$j] stdout]"} |
| 32 | + } |
| 33 | + |
| 34 | + set cycle 1 |
| 35 | + while {([clock seconds]-$start_time) < $duration} { |
| 36 | + test "PSYNC2: --- CYCLE $cycle ---" { |
| 37 | + incr cycle |
| 38 | + } |
| 39 | + |
| 40 | + # Create a random replication layout. |
| 41 | + # Start with switching master (this simulates a failover). |
| 42 | + |
| 43 | + # 1) Select the new master. |
| 44 | + set master_id [randomInt 5] |
| 45 | + set used [list $master_id] |
| 46 | + test "PSYNC2: \[NEW LAYOUT\] Set #$master_id as master" { |
| 47 | + $R($master_id) slaveof no one |
| 48 | + if {$counter_value == 0} { |
| 49 | + $R($master_id) set x $counter_value |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + # 2) Attach all the slaves to a random instance |
| 54 | + while {[llength $used] != 5} { |
| 55 | + while 1 { |
| 56 | + set slave_id [randomInt 5] |
| 57 | + if {[lsearch -exact $used $slave_id] == -1} break |
| 58 | + } |
| 59 | + set rand [randomInt [llength $used]] |
| 60 | + set mid [lindex $used $rand] |
| 61 | + set master_host $R_host($mid) |
| 62 | + set master_port $R_port($mid) |
| 63 | + |
| 64 | + test "PSYNC2: Set #$slave_id to replicate from #$mid" { |
| 65 | + $R($slave_id) slaveof $master_host $master_port |
| 66 | + } |
| 67 | + lappend used $slave_id |
| 68 | + } |
| 69 | + |
| 70 | + # 3) Increment the counter and wait for all the instances |
| 71 | + # to converge. |
| 72 | + test "PSYNC2: cluster is consistent after failover" { |
| 73 | + $R($master_id) incr x; incr counter_value |
| 74 | + for {set j 0} {$j < 5} {incr j} { |
| 75 | + wait_for_condition 50 1000 { |
| 76 | + [$R($j) get x] == $counter_value |
| 77 | + } else { |
| 78 | + fail "Instance #$j x variable is inconsistent" |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + # 4) Generate load while breaking the connection of random |
| 84 | + # slave-master pairs. |
| 85 | + test "PSYNC2: generate load while killing replication links" { |
| 86 | + set t [clock milliseconds] |
| 87 | + set next_break [expr {$t+$disconnect_period}] |
| 88 | + while {[clock milliseconds]-$t < $genload_time} { |
| 89 | + if {$genload} { |
| 90 | + $R($master_id) incr x; incr counter_value |
| 91 | + } |
| 92 | + if {[clock milliseconds] == $next_break} { |
| 93 | + set next_break \ |
| 94 | + [expr {[clock milliseconds]+$disconnect_period}] |
| 95 | + set slave_id [randomInt 5] |
| 96 | + if {$disconnect} { |
| 97 | + $R($slave_id) client kill type master |
| 98 | + if {$debug_msg} { |
| 99 | + puts "+++ Breaking link for slave #$slave_id" |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + # 5) Increment the counter and wait for all the instances |
| 107 | + set x [$R($master_id) get x] |
| 108 | + test "PSYNC2: cluster is consistent after load (x = $x)" { |
| 109 | + for {set j 0} {$j < 5} {incr j} { |
| 110 | + wait_for_condition 50 1000 { |
| 111 | + [$R($j) get x] == $counter_value |
| 112 | + } else { |
| 113 | + fail "Instance #$j x variable is inconsistent" |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + # Put down the old master so that it cannot generate more |
| 119 | + # replication stream, this way in the next master switch, the time at |
| 120 | + # which we move slaves away is not important, each will have full |
| 121 | + # history (otherwise PINGs will make certain slaves have more history), |
| 122 | + # and sometimes a full resync will be needed. |
| 123 | + $R($master_id) slaveof 127.0.0.1 0 ;# We use port zero to make it fail. |
| 124 | + |
| 125 | + if {$debug_msg} { |
| 126 | + for {set j 0} {$j < 5} {incr j} { |
| 127 | + puts "$j: sync_full: [status $R($j) sync_full]" |
| 128 | + puts "$j: id1 : [status $R($j) master_replid]:[status $R($j) master_repl_offset]" |
| 129 | + puts "$j: id2 : [status $R($j) master_replid2]:[status $R($j) second_repl_offset]" |
| 130 | + puts "$j: backlog : firstbyte=[status $R($j) repl_backlog_first_byte_offset] len=[status $R($j) repl_backlog_histlen]" |
| 131 | + puts "---" |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + test "PSYNC2: total sum of full synchronizations is exactly 4" { |
| 136 | + set sum 0 |
| 137 | + for {set j 0} {$j < 5} {incr j} { |
| 138 | + incr sum [status $R($j) sync_full] |
| 139 | + } |
| 140 | + assert {$sum == 4} |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + test "PSYNC2: Bring the master back again for next test" { |
| 145 | + $R($master_id) slaveof no one |
| 146 | + set master_host $R_host($master_id) |
| 147 | + set master_port $R_port($master_id) |
| 148 | + for {set j 0} {$j < 5} {incr j} { |
| 149 | + if {$j == $master_id} continue |
| 150 | + $R($j) slaveof $master_host $master_port |
| 151 | + } |
| 152 | + |
| 153 | + # Wait for slaves to sync |
| 154 | + wait_for_condition 50 1000 { |
| 155 | + [status $R($master_id) connected_slaves] == 4 |
| 156 | + } else { |
| 157 | + fail "Slave not reconnecting" |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + test "PSYNC2: Partial resync after restart using RDB aux fields" { |
| 162 | + # Pick a random slave |
| 163 | + set slave_id [expr {($master_id+1)%5}] |
| 164 | + set sync_count [status $R($master_id) sync_full] |
| 165 | + catch { |
| 166 | + $R($slave_id) config rewrite |
| 167 | + $R($slave_id) debug restart |
| 168 | + } |
| 169 | + wait_for_condition 50 1000 { |
| 170 | + [status $R($master_id) connected_slaves] == 4 |
| 171 | + } else { |
| 172 | + fail "Slave not reconnecting" |
| 173 | + } |
| 174 | + set new_sync_count [status $R($master_id) sync_full] |
| 175 | + assert {$sync_count == $new_sync_count} |
| 176 | + } |
| 177 | + |
| 178 | + if {$no_exit} { |
| 179 | + while 1 { puts -nonewline .; flush stdout; after 1000} |
| 180 | + } |
| 181 | + |
| 182 | +}}}}} |
0 commit comments