]> git.saurik.com Git - redis.git/blob - tests/integration/replication.tcl
Sentinel: do not crash against slaves not publishing the runid.
[redis.git] / tests / integration / replication.tcl
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
13 test {BRPOPLPUSH replication, when blocking against empty list} {
14 set rd [redis_deferring_client]
15 $rd brpoplpush a b 5
16 r lpush a foo
17 wait_for_condition 50 100 {
18 [r debug digest] eq [r -1 debug digest]
19 } else {
20 fail "Master and slave have different digest: [r debug digest] VS [r -1 debug digest]"
21 }
22 }
23
24 test {BRPOPLPUSH replication, list exists} {
25 set rd [redis_deferring_client]
26 r lpush c 1
27 r lpush c 2
28 r lpush c 3
29 $rd brpoplpush c d 5
30 after 1000
31 assert_equal [r debug digest] [r -1 debug digest]
32 }
33 }
34 }
35
36 start_server {tags {"repl"}} {
37 r set mykey foo
38
39 start_server {} {
40 test {Second server should have role master at first} {
41 s role
42 } {master}
43
44 test {SLAVEOF should start with link status "down"} {
45 r slaveof [srv -1 host] [srv -1 port]
46 s master_link_status
47 } {down}
48
49 test {The role should immediately be changed to "slave"} {
50 s role
51 } {slave}
52
53 wait_for_sync r
54 test {Sync should have transferred keys from master} {
55 r get mykey
56 } {foo}
57
58 test {The link status should be up} {
59 s master_link_status
60 } {up}
61
62 test {SET on the master should immediately propagate} {
63 r -1 set mykey bar
64 if {$::valgrind} {after 2000}
65 r 0 get mykey
66 } {bar}
67
68 test {FLUSHALL should replicate} {
69 r -1 flushall
70 if {$::valgrind} {after 2000}
71 list [r -1 dbsize] [r 0 dbsize]
72 } {0 0}
73 }
74 }
75
76 proc start_write_load {host port seconds} {
77 exec tclsh8.5 tests/helpers/gen_write_load.tcl $host $port $seconds &
78 }
79
80 proc stop_write_load {handle} {
81 catch {exec /bin/kill -9 $handle}
82 }
83
84 start_server {tags {"repl"}} {
85 set master [srv 0 client]
86 set master_host [srv 0 host]
87 set master_port [srv 0 port]
88 set slaves {}
89 set load_handle0 [start_write_load $master_host $master_port 3]
90 set load_handle1 [start_write_load $master_host $master_port 5]
91 set load_handle2 [start_write_load $master_host $master_port 20]
92 set load_handle3 [start_write_load $master_host $master_port 8]
93 set load_handle4 [start_write_load $master_host $master_port 4]
94 start_server {} {
95 lappend slaves [srv 0 client]
96 start_server {} {
97 lappend slaves [srv 0 client]
98 start_server {} {
99 lappend slaves [srv 0 client]
100 test "Connect multiple slaves at the same time (issue #141)" {
101 # Send SALVEOF commands to slaves
102 [lindex $slaves 0] slaveof $master_host $master_port
103 [lindex $slaves 1] slaveof $master_host $master_port
104 [lindex $slaves 2] slaveof $master_host $master_port
105
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:*,online*slave1:*,online*slave2:*,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
121 # Stop the write load
122 stop_write_load $load_handle0
123 stop_write_load $load_handle1
124 stop_write_load $load_handle2
125 stop_write_load $load_handle3
126 stop_write_load $load_handle4
127
128 # Wait that slaves exit the "loading" state
129 wait_for_condition 500 100 {
130 ![string match {*loading:1*} [[lindex $slaves 0] info]] &&
131 ![string match {*loading:1*} [[lindex $slaves 1] info]] &&
132 ![string match {*loading:1*} [[lindex $slaves 2] info]]
133 } else {
134 fail "Slaves still loading data after too much time"
135 }
136
137 # Make sure that slaves and master have same number of keys
138 wait_for_condition 500 100 {
139 [$master dbsize] == [[lindex $slaves 0] dbsize] &&
140 [$master dbsize] == [[lindex $slaves 1] dbsize] &&
141 [$master dbsize] == [[lindex $slaves 2] dbsize]
142 } else {
143 fail "Different number of keys between masted and slave after too long time."
144 }
145
146 # Check digests
147 set digest [$master debug digest]
148 set digest0 [[lindex $slaves 0] debug digest]
149 set digest1 [[lindex $slaves 1] debug digest]
150 set digest2 [[lindex $slaves 2] debug digest]
151 assert {$digest ne 0000000000000000000000000000000000000000}
152 assert {$digest eq $digest0}
153 assert {$digest eq $digest1}
154 assert {$digest eq $digest2}
155 }
156 }
157 }
158 }
159 }