7
7
"fmt"
8
8
"strconv"
9
9
"strings"
10
+ "time"
10
11
11
12
mocov1beta2 "github.com/cybozu-go/moco/api/v1beta2"
12
13
. "github.com/onsi/ginkgo"
@@ -116,6 +117,7 @@ var _ = Context("replication", func() {
116
117
117
118
It ("should switch the primary if requested" , func () {
118
119
kubectlSafe (nil , "moco" , "-n" , "repl" , "switchover" , "test" )
120
+ time .Sleep (10 * time .Second )
119
121
Eventually (func () int {
120
122
cluster , err := getCluster ("repl" , "test" )
121
123
if err != nil {
@@ -142,6 +144,61 @@ var _ = Context("replication", func() {
142
144
}).Should (Succeed ())
143
145
})
144
146
147
+ It ("should switch the primary even with replication delays and new primary has initialized" , func () {
148
+ kubectlSafe (nil , "moco" , "-n" , "repl" , "mysql" , "-u" , "moco-admin" , "test" , "--index" , "2" , "--" ,
149
+ "-e" , "STOP REPLICA SQL_THREAD" )
150
+ kubectlSafe (nil , "moco" , "-n" , "donor" , "mysql" , "-u" , "moco-admin" , "single" , "--" ,
151
+ "-D" , "test" , "--init_command=SET autocommit=1" , "-e" , "INSERT INTO t (data) VALUES ('eee')" )
152
+ kubectlSafe (nil , "delete" , "pvc" , "-n" , "repl" , "--wait=false" , "mysql-data-moco-test-0" )
153
+ kubectlSafe (nil , "delete" , "pod" , "-n" , "repl" , "--grace-period=1" , "moco-test-0" )
154
+ time .Sleep (60 * time .Second )
155
+ Eventually (func () error {
156
+ cluster , err := getCluster ("repl" , "test" )
157
+ if err != nil {
158
+ return err
159
+ }
160
+ for _ , cond := range cluster .Status .Conditions {
161
+ if cond .Type != mocov1beta2 .ConditionAvailable {
162
+ continue
163
+ }
164
+ if cond .Status == metav1 .ConditionTrue {
165
+ return nil
166
+ }
167
+ return fmt .Errorf ("cluster is not available: %s" , cond .Status )
168
+ }
169
+ return errors .New ("no available condition" )
170
+ }).Should (Succeed ())
171
+
172
+ kubectlSafe (nil , "moco" , "-n" , "repl" , "switchover" , "test" )
173
+ time .Sleep (10 * time .Second )
174
+ Eventually (func () int {
175
+ cluster , err := getCluster ("repl" , "test" )
176
+ if err != nil {
177
+ return 0
178
+ }
179
+ return cluster .Status .CurrentPrimaryIndex
180
+ }).ShouldNot (Equal (1 ))
181
+ time .Sleep (10 * time .Second )
182
+ kubectlSafe (nil , "moco" , "-n" , "repl" , "mysql" , "-u" , "moco-admin" , "test" , "--index" , "2" , "--" ,
183
+ "-e" , "START REPLICA SQL_THREAD" )
184
+ Eventually (func () error {
185
+ cluster , err := getCluster ("repl" , "test" )
186
+ if err != nil {
187
+ return err
188
+ }
189
+ for _ , cond := range cluster .Status .Conditions {
190
+ if cond .Type != mocov1beta2 .ConditionHealthy {
191
+ continue
192
+ }
193
+ if cond .Status == metav1 .ConditionTrue {
194
+ return nil
195
+ }
196
+ return fmt .Errorf ("cluster is not healthy: %s" , cond .Status )
197
+ }
198
+ return errors .New ("no health condition" )
199
+ }).Should (Succeed ())
200
+ })
201
+
145
202
It ("should be able to scale out the cluster" , func () {
146
203
Eventually (func () error {
147
204
cluster , err := getCluster ("repl" , "test" )
@@ -177,12 +234,12 @@ var _ = Context("replication", func() {
177
234
178
235
It ("should detect errant transactions" , func () {
179
236
Eventually (func () error {
180
- _ , err := kubectl (nil , "moco" , "-n" , "repl" , "mysql" , "-u" , "moco-admin" , "--index" , "0 " , "test" , "--" ,
237
+ _ , err := kubectl (nil , "moco" , "-n" , "repl" , "mysql" , "-u" , "moco-admin" , "--index" , "2 " , "test" , "--" ,
181
238
"-e" , "SET GLOBAL read_only=0" )
182
239
if err != nil {
183
240
return err
184
241
}
185
- _ , err = kubectl (nil , "moco" , "-n" , "repl" , "mysql" , "-u" , "moco-admin" , "--index" , "0 " , "test" , "--" ,
242
+ _ , err = kubectl (nil , "moco" , "-n" , "repl" , "mysql" , "-u" , "moco-admin" , "--index" , "2 " , "test" , "--" ,
186
243
"-e" , "CREATE DATABASE errant" )
187
244
return err
188
245
}).Should (Succeed ())
@@ -258,11 +315,11 @@ var _ = Context("replication", func() {
258
315
cluster , err := getCluster ("repl" , "test" )
259
316
Expect (err ).NotTo (HaveOccurred ())
260
317
261
- kubectlSafe (nil , "delete" , "-n" , "repl" , "--wait=false" , "pvc" , "mysql-data-moco-test-0 " )
262
- kubectlSafe (nil , "delete" , "-n" , "repl" , "--grace-period=1" , "pod" , cluster .PodName (0 ))
318
+ kubectlSafe (nil , "delete" , "-n" , "repl" , "--wait=false" , "pvc" , "mysql-data-moco-test-2 " )
319
+ kubectlSafe (nil , "delete" , "-n" , "repl" , "--grace-period=1" , "pod" , cluster .PodName (2 ))
263
320
264
321
Eventually (func () error {
265
- out , err := kubectl (nil , "-n" , "repl" , "get" , "pod" , cluster .PodName (0 ), "-o" , "json" )
322
+ out , err := kubectl (nil , "-n" , "repl" , "get" , "pod" , cluster .PodName (2 ), "-o" , "json" )
266
323
if err != nil {
267
324
return err
268
325
}
@@ -277,7 +334,7 @@ var _ = Context("replication", func() {
277
334
}
278
335
if cond .Reason == "Unschedulable" {
279
336
fmt .Println ("re-deleting pending pod..." )
280
- _ , err := kubectl (nil , "delete" , "-n" , "repl" , "--grace-period=1" , "pod" , cluster .PodName (0 ))
337
+ _ , err := kubectl (nil , "delete" , "-n" , "repl" , "--grace-period=1" , "pod" , cluster .PodName (2 ))
281
338
if err != nil {
282
339
return fmt .Errorf ("failed to delete pod: %w" , err )
283
340
}
@@ -322,7 +379,7 @@ var _ = Context("replication", func() {
322
379
323
380
Eventually (func () error {
324
381
_ , err := kubectl (nil , "moco" , "-n" , "repl" , "mysql" , "-u" , "moco-writable" , "test" , "--" ,
325
- "-D" , "test" , "--init_command=SET autocommit=1" , "-e" , "INSERT INTO t (data) VALUES ('eee ')" )
382
+ "-D" , "test" , "--init_command=SET autocommit=1" , "-e" , "INSERT INTO t (data) VALUES ('fff ')" )
326
383
return err
327
384
}).Should (Succeed ())
328
385
})
@@ -341,7 +398,7 @@ var _ = Context("replication", func() {
341
398
}
342
399
count , _ := strconv .Atoi (strings .TrimSpace (string (out )))
343
400
return count
344
- }).Should (Equal (5 ))
401
+ }).Should (Equal (6 ))
345
402
})
346
403
347
404
It ("should delete clusters" , func () {
0 commit comments