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

     1  // Copyright 2022 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/log"
    28  	"github.com/pingcap/tiflow/pkg/errors"
    29  	"github.com/pingcap/tiflow/pkg/logutil"
    30  	"go.uber.org/zap"
    31  )
    32  
    33  // main starts to run the test case logic after MySQL, TiDB and DM have been set up.
    34  // NOTE: run this in the same K8s namespace as DM-master.
    35  func main() {
    36  	code := 0
    37  	defer func() {
    38  		os.Exit(code)
    39  	}()
    40  
    41  	cfg := newConfig()
    42  	err := cfg.parse(os.Args[1:])
    43  	switch errors.Cause(err) {
    44  	case nil:
    45  	case flag.ErrHelp:
    46  		return
    47  	default:
    48  		fmt.Println("parse cmd flags err:", err.Error())
    49  		code = 2
    50  		return
    51  	}
    52  
    53  	err = logutil.InitLogger(&logutil.Config{
    54  		Level: "info",
    55  	})
    56  	if err != nil {
    57  		fmt.Println("init logger error:", err.Error())
    58  		code = 2
    59  		return
    60  	}
    61  
    62  	go func() {
    63  		//nolint:errcheck,gosec
    64  		http.ListenAndServe("0.0.0.0:8899", nil) // for pprof
    65  	}()
    66  
    67  	rand.Seed(time.Now().UnixNano())
    68  
    69  	ctx, cancel := context.WithTimeout(context.Background(), cfg.Duration)
    70  	defer cancel()
    71  
    72  	sc := make(chan os.Signal, 1)
    73  	signal.Notify(sc,
    74  		syscall.SIGHUP,
    75  		syscall.SIGINT,
    76  		syscall.SIGTERM,
    77  		syscall.SIGQUIT)
    78  	go func() {
    79  		sig := <-sc
    80  		log.Info("got signal to exit", zap.Stringer("signal", sig))
    81  		cancel()
    82  	}()
    83  
    84  	// run tests cases
    85  	err = runCases(ctx, cfg)
    86  	if err != nil {
    87  		log.Error("run cases failed", zap.Error(err))
    88  		code = 2
    89  		return
    90  	}
    91  }