github.com/matrixorigin/matrixone@v1.2.0/pkg/common/chaos/chaos.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package chaos 16 17 import ( 18 "database/sql" 19 "time" 20 ) 21 22 // ChaosTester chaos tester 23 type ChaosTester struct { 24 testers []tester 25 } 26 27 // NewChaosTester create chaos tester 28 func NewChaosTester(cfg Config) *ChaosTester { 29 t := &ChaosTester{} 30 t.testers = append(t.testers, newRestartTester(cfg)) 31 return t 32 } 33 34 func (t *ChaosTester) waitSystemBootStrapCompleted() { 35 for { 36 ok, err := t.doCheckBootStrapCompleted() 37 if err != nil || !ok { 38 time.Sleep(time.Second * 5) 39 continue 40 } 41 return 42 } 43 } 44 45 func (t *ChaosTester) doCheckBootStrapCompleted() (bool, error) { 46 db, err := sql.Open("mysql", "dump:111@tcp(127.0.0.1:6001)/mo_catalog") 47 defer func() { 48 _ = db.Close() 49 }() 50 if err != nil { 51 return false, err 52 } 53 54 res, err := db.Query("show tables") 55 if err != nil { 56 return false, err 57 } 58 defer func() { 59 _ = res.Close() 60 }() 61 if res.Err() != nil { 62 return false, res.Err() 63 } 64 65 var name string 66 for res.Next() { 67 if err := res.Scan(&name); err != nil { 68 return false, err 69 } 70 if name == "mo_account" { 71 return true, nil 72 } 73 } 74 return false, nil 75 } 76 77 func (t *ChaosTester) Start() error { 78 go func() { 79 t.waitSystemBootStrapCompleted() 80 for _, tester := range t.testers { 81 if err := tester.start(); err != nil { 82 panic(err) 83 } 84 } 85 }() 86 return nil 87 } 88 89 func (t *ChaosTester) Stop() error { 90 for _, tester := range t.testers { 91 if err := tester.stop(); err != nil { 92 return err 93 } 94 } 95 return nil 96 }