github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/tests/processor_panic/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  	"database/sql"
    18  	"flag"
    19  	"fmt"
    20  	"math/rand"
    21  	"os"
    22  
    23  	"github.com/pingcap/errors"
    24  	"github.com/pingcap/log"
    25  	"github.com/pingcap/ticdc/pkg/quotes"
    26  	"github.com/pingcap/ticdc/tests/util"
    27  	"go.uber.org/zap"
    28  	"golang.org/x/sync/errgroup"
    29  )
    30  
    31  const (
    32  	numTables          = 10
    33  	numQueriesPerTable = 200
    34  )
    35  
    36  func main() {
    37  	cfg := util.NewConfig()
    38  	err := cfg.Parse(os.Args[1:])
    39  	switch errors.Cause(err) {
    40  	case nil:
    41  	case flag.ErrHelp:
    42  		os.Exit(0)
    43  	default:
    44  		log.S().Errorf("parse cmd flags err %s\n", err)
    45  		os.Exit(2)
    46  	}
    47  
    48  	sourceDB, err := util.CreateDB(cfg.SourceDBCfg[0])
    49  	if err != nil {
    50  		log.S().Fatal(err)
    51  	}
    52  	defer func() {
    53  		if err := util.CloseDB(sourceDB); err != nil {
    54  			log.S().Errorf("Failed to close source database: %s\n", err)
    55  		}
    56  	}()
    57  
    58  	errg := new(errgroup.Group)
    59  	for i := 0; i < numTables; i++ {
    60  		tableName := fmt.Sprintf("test_table_%d", i)
    61  		errg.Go(func() error {
    62  			err := createTable(sourceDB, tableName)
    63  			if err != nil {
    64  				return errors.Trace(err)
    65  			}
    66  			err = doDMLs(sourceDB, tableName, numQueriesPerTable)
    67  			return err
    68  		})
    69  	}
    70  
    71  	err = errg.Wait()
    72  	if err != nil {
    73  		log.Fatal("process_panic data insertion failed", zap.Error(err))
    74  	}
    75  
    76  	err = createTable(sourceDB, "end_mark_table")
    77  	if err != nil {
    78  		log.Fatal("process_panic could not create end_mark_table")
    79  	}
    80  }
    81  
    82  func createTable(db *sql.DB, tableName string) error {
    83  	_, err := db.Exec("CREATE table " + quotes.QuoteName(tableName) + " (id int primary key, v1 int, v2 int)")
    84  	if err != nil {
    85  		return errors.Trace(err)
    86  	}
    87  	return nil
    88  }
    89  
    90  func doDMLs(db *sql.DB, tableName string, numQueries int) error {
    91  	log.Info("Start inserting data", zap.String("tableName", tableName), zap.Int("numQueries", numQueries))
    92  	stmt, err := db.Prepare("insert into " + quotes.QuoteName(tableName) + " (id, v1, v2) values (?, ?, ?)")
    93  	if err != nil {
    94  		return errors.Trace(err)
    95  	}
    96  
    97  	for i := 0; i <= numQueries; i++ {
    98  		_, err := stmt.Exec(i, rand.Int()%65535, rand.Int()%65536)
    99  		if err != nil {
   100  			return errors.Trace(err)
   101  		}
   102  	}
   103  
   104  	err = stmt.Close()
   105  	if err != nil {
   106  		return errors.Trace(err)
   107  	}
   108  
   109  	log.Info("Finished inserting data", zap.String("tableName", tableName))
   110  	return nil
   111  }