github.com/matrixorigin/matrixone@v1.2.0/pkg/common/chaos/restart.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  	"context"
    19  	"math/rand"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/stopper"
    23  )
    24  
    25  type restartTester struct {
    26  	cfg     Config
    27  	stopper *stopper.Stopper
    28  }
    29  
    30  func newRestartTester(cfg Config) tester {
    31  	t := &restartTester{
    32  		cfg: cfg,
    33  	}
    34  	return t
    35  }
    36  
    37  func (t *restartTester) start() error {
    38  	t.initStopper()
    39  	return t.stopper.RunTask(t.do)
    40  }
    41  
    42  func (t *restartTester) stop() error {
    43  	t.stopper.Stop()
    44  	t.stopper = nil
    45  	return nil
    46  }
    47  
    48  func (t *restartTester) name() string {
    49  	return "restart-tester"
    50  }
    51  
    52  func (t *restartTester) initStopper() {
    53  	t.stopper = stopper.NewStopper("chaos-restart-tester")
    54  }
    55  
    56  func (t *restartTester) do(ctx context.Context) {
    57  	if t.cfg.Restart.KillInterval.Duration == 0 {
    58  		return
    59  	}
    60  
    61  	timer := time.NewTimer(t.cfg.Restart.KillInterval.Duration)
    62  	action := 0
    63  	target := -1
    64  	actionFuncs := []func(){
    65  		func() {
    66  			target = t.cfg.Restart.Targets[int(rand.Int31n(int32(len(t.cfg.Restart.Targets))))]
    67  			if err := t.cfg.Restart.KillFunc(target); err != nil {
    68  				panic(err)
    69  			}
    70  			timer.Reset(t.cfg.Restart.RestartInterval.Duration)
    71  		},
    72  		func() {
    73  			if err := t.cfg.Restart.RestartFunc(target); err != nil {
    74  				panic(err)
    75  			}
    76  			timer.Reset(t.cfg.Restart.KillInterval.Duration)
    77  		},
    78  	}
    79  
    80  	for {
    81  		select {
    82  		case <-ctx.Done():
    83  			return
    84  		case <-timer.C:
    85  			actionFuncs[action%len(actionFuncs)]()
    86  			action++
    87  		}
    88  	}
    89  }