github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/debug-tools/binlog-event-blackhole/main.go (about)

     1  // Copyright 2019 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  	"os"
    21  	"os/signal"
    22  	"strings"
    23  	"syscall"
    24  	"time"
    25  
    26  	"github.com/pingcap/errors"
    27  	"github.com/pingcap/tiflow/dm/pkg/log"
    28  	"go.uber.org/zap"
    29  )
    30  
    31  func main() {
    32  	cfg := newConfig()
    33  	err := cfg.parse(os.Args[1:])
    34  	switch errors.Cause(err) {
    35  	case nil:
    36  	case flag.ErrHelp:
    37  		os.Exit(0)
    38  	default:
    39  		fmt.Printf("parse cmd flags err %s \n", err)
    40  		os.Exit(2)
    41  	}
    42  
    43  	err = log.InitLogger(&log.Config{
    44  		File:   cfg.logFile,
    45  		Level:  strings.ToLower(cfg.logLevel),
    46  		Format: cfg.logFormat,
    47  	})
    48  	if err != nil {
    49  		fmt.Printf("init logger error %v", errors.ErrorStack(err))
    50  		os.Exit(2)
    51  	}
    52  
    53  	conn, err := registerSlave(cfg.addr, cfg.username, cfg.password, uint32(cfg.serverID))
    54  	if err != nil {
    55  		log.L().Error("register slave", zap.Error(err))
    56  		os.Exit(2)
    57  	}
    58  	log.L().Info("registered slave", zap.Uint32("connection ID", conn.GetConnectionID()))
    59  
    60  	ctx, cancel := context.WithCancel(context.Background())
    61  
    62  	sc := make(chan os.Signal, 1)
    63  	signal.Notify(sc,
    64  		syscall.SIGHUP,
    65  		syscall.SIGINT,
    66  		syscall.SIGTERM,
    67  		syscall.SIGQUIT)
    68  	go func() {
    69  		sig := <-sc
    70  		cancel()
    71  		log.L().Info("got signal to exit", zap.Stringer("signal", sig))
    72  		err2 := closeConn(conn)
    73  		if err2 != nil {
    74  			log.L().Error("close connection", zap.Error(err2))
    75  		}
    76  	}()
    77  
    78  	err = startSync(conn, uint32(cfg.serverID), cfg.binlogName, uint32(cfg.binlogPos))
    79  	if err != nil {
    80  		log.L().Error("start sync", zap.Error(err))
    81  		os.Exit(2)
    82  	}
    83  	log.L().Info("start sync",
    84  		zap.Int("server-id", cfg.serverID), zap.String("binlog-name", cfg.binlogName),
    85  		zap.Int("binlog-pos", cfg.binlogPos))
    86  
    87  	var (
    88  		eventCount uint64
    89  		byteCount  uint64
    90  		duration   time.Duration
    91  	)
    92  	switch cfg.mode {
    93  	case 1:
    94  		eventCount, byteCount, duration, err = readEventsWithGoMySQL(ctx, conn)
    95  	case 2:
    96  		eventCount, byteCount, duration, err = readEventsWithoutGoMySQL(ctx, conn)
    97  	case 3:
    98  		byteCount, duration, err = readDataOnly(ctx, conn)
    99  	default:
   100  		log.L().Error("invalid mode specified`", zap.Int("mode", cfg.mode))
   101  	}
   102  	if err != nil {
   103  		log.L().Error("read events", zap.Error(err))
   104  	}
   105  
   106  	tps := float64(eventCount) / duration.Seconds()
   107  	speed := float64(byteCount) / duration.Seconds()
   108  	log.L().Info("binlog-event-blackhole exit",
   109  		zap.Uint64("event-count", eventCount), zap.Uint64("byte-count", byteCount),
   110  		zap.Duration("duration", duration), zap.Float64("tps", tps), zap.Float64("throughput (byte/s)", speed))
   111  }