github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/chaos/cases/main.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package main
    15  
    16  import (
    17  	"context"
    18  	"flag"
    19  	"fmt"
    20  	"math/rand"
    21  	"net/http"
    22  	"os"
    23  	"os/signal"
    24  	"syscall"
    25  	"time"
    26  
    27  	"github.com/pingcap/errors"
    28  	"github.com/pingcap/tiflow/dm/pb"
    29  	"github.com/pingcap/tiflow/dm/pkg/log"
    30  	"go.uber.org/zap"
    31  	"google.golang.org/grpc"
    32  )
    33  
    34  // main starts to run the test case logic after MySQL, TiDB and DM have been set up.
    35  // NOTE: run this in the same K8s namespace as DM-master.
    36  func main() {
    37  	code := 0
    38  	defer func() {
    39  		os.Exit(code)
    40  	}()
    41  
    42  	cfg := newConfig()
    43  	err := cfg.parse(os.Args[1:])
    44  	switch errors.Cause(err) {
    45  	case nil:
    46  	case flag.ErrHelp:
    47  		return
    48  	default:
    49  		fmt.Println("parse cmd flags err:", err.Error())
    50  		code = 2
    51  		return
    52  	}
    53  
    54  	err = log.InitLogger(&log.Config{Level: "info"})
    55  	if err != nil {
    56  		fmt.Println("init logger error:", err.Error())
    57  		code = 2
    58  		return
    59  	}
    60  
    61  	go func() {
    62  		//nolint:errcheck
    63  		http.ListenAndServe("0.0.0.0:8899", nil) // for pprof
    64  	}()
    65  
    66  	rand.Seed(time.Now().UnixNano())
    67  
    68  	ctx, cancel := context.WithCancel(context.Background())
    69  	defer cancel()
    70  
    71  	sc := make(chan os.Signal, 1)
    72  	signal.Notify(sc,
    73  		syscall.SIGHUP,
    74  		syscall.SIGINT,
    75  		syscall.SIGTERM,
    76  		syscall.SIGQUIT)
    77  	go func() {
    78  		sig := <-sc
    79  		log.L().Info("got signal to exit", zap.Stringer("signal", sig))
    80  		cancel()
    81  	}()
    82  
    83  	ctx2, cancel2 := context.WithTimeout(ctx, 60*time.Second)
    84  	defer cancel2()
    85  	masterConn, err := grpc.DialContext(ctx2, cfg.MasterAddr, grpc.WithBlock(), grpc.WithInsecure()) // no TLS support in chaos cases now.
    86  	if err != nil {
    87  		log.L().Error("fail to dail DM-master", zap.String("address", cfg.MasterAddr), zap.Error(err))
    88  		code = 2
    89  		return
    90  	}
    91  	masterCli := pb.NewMasterClient(masterConn)
    92  
    93  	// check whether all members are ready.
    94  	err = checkMembersReadyLoop(ctx2, masterCli, cfg.MasterCount, cfg.WorkerCount) // ctx2, should be done in 60s.
    95  	if err != nil {
    96  		log.L().Error("fail to check members ready", zap.Error(err)) // only log a error, still try to do other things.
    97  	}
    98  
    99  	// create sources.
   100  	err = createSources(ctx, masterCli, cfg)
   101  	if err != nil {
   102  		log.L().Error("fail to create source", zap.Error(err))
   103  		code = 2
   104  		return
   105  	}
   106  
   107  	// set upstream and downstream instances state.
   108  	err = setInstancesState(ctx, &cfg.Target, &cfg.Source1, &cfg.Source2, &cfg.Source3)
   109  	if err != nil {
   110  		log.L().Error("fail to set instances state", zap.Error(err))
   111  		code = 2
   112  		return
   113  	}
   114  
   115  	// context for the duration of running.
   116  	ctx3, cancel3 := context.WithTimeout(ctx, cfg.Duration)
   117  	defer cancel3()
   118  
   119  	// run tests cases
   120  	err = runCases(ctx3, masterCli, cfg.ConfigDir, cfg.Target, cfg.Source1, cfg.Source2, cfg.Source3)
   121  	if err != nil {
   122  		log.L().Error("run cases failed", zap.Error(err))
   123  		code = 2
   124  		return
   125  	}
   126  }