github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/_dmctl_tools/check_exit_safe_binlog.go (about)

     1  // Copyright 2021 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  	"fmt"
    19  	"os"
    20  
    21  	"github.com/go-mysql-org/go-mysql/mysql"
    22  	_ "github.com/go-sql-driver/mysql"
    23  	"github.com/pingcap/errors"
    24  	"github.com/pingcap/tiflow/dm/pkg/binlog"
    25  	"github.com/pingcap/tiflow/dm/pkg/gtid"
    26  	"github.com/pingcap/tiflow/dm/pkg/log"
    27  	"github.com/pingcap/tiflow/dm/tests/utils"
    28  )
    29  
    30  var compareMap = map[string]map[int]struct{}{
    31  	">":  {1: {}},
    32  	">=": {1: {}, 0: {}},
    33  	"=":  {0: {}},
    34  	"<=": {-1: {}, 0: {}},
    35  	"<":  {-1: {}},
    36  }
    37  
    38  func parseBinlogLocation(binlogName string, binlogPos uint32, binlogGTID string) (binlog.Location, error) {
    39  	gset, err := gtid.ParserGTID("mysql", binlogGTID) // default to "".
    40  	if err != nil {
    41  		return binlog.Location{}, err
    42  	}
    43  	return binlog.NewLocation(
    44  		mysql.Position{
    45  			Name: binlogName,
    46  			Pos:  binlogPos,
    47  		},
    48  		gset,
    49  	), nil
    50  }
    51  
    52  // use show-ddl-locks request to test DM-master is online
    53  func main() {
    54  	password := os.Args[1]
    55  	sourceID := os.Args[2]
    56  	compareGTIDStr := os.Args[3]
    57  	compare := os.Args[4]
    58  
    59  	const (
    60  		host = "127.0.0.1"
    61  		user = "test"
    62  		port = 4000
    63  	)
    64  
    65  	err := log.InitLogger(&log.Config{Level: "info"})
    66  	if err != nil {
    67  		utils.ExitWithError(err)
    68  	}
    69  
    70  	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4&interpolateParams=true&maxAllowedPacket=0",
    71  		user, password, host, port)
    72  	db, err := sql.Open("mysql", dsn)
    73  	if err != nil {
    74  		utils.ExitWithError(err)
    75  	}
    76  
    77  	var (
    78  		binlogName, exitSafeBinlogName string
    79  		binlogPos, exitSafeBinlogPos   uint32
    80  		binlogGTID, exitSafeBinlogGTID sql.NullString
    81  	)
    82  	err = db.QueryRow("SELECT binlog_name,binlog_pos,binlog_gtid,exit_safe_binlog_name,exit_safe_binlog_pos,exit_safe_binlog_gtid FROM dm_meta.test_syncer_checkpoint WHERE is_global=1 AND id=?", sourceID).
    83  		Scan(&binlogName, &binlogPos, &binlogGTID, &exitSafeBinlogName, &exitSafeBinlogPos, &exitSafeBinlogGTID)
    84  	if err != nil {
    85  		utils.ExitWithError(err)
    86  	}
    87  
    88  	globalLoc, err := parseBinlogLocation(binlogName, binlogPos, binlogGTID.String)
    89  	if err != nil {
    90  		utils.ExitWithError(err)
    91  	}
    92  	exitSafeModeLoc, err := parseBinlogLocation(exitSafeBinlogName, exitSafeBinlogPos, exitSafeBinlogGTID.String)
    93  	if err != nil {
    94  		utils.ExitWithError(err)
    95  	}
    96  	compareGTID := false
    97  	if compareGTIDStr == "true" {
    98  		compareGTID = true
    99  	}
   100  	t := binlog.CompareLocation(globalLoc, exitSafeModeLoc, compareGTID)
   101  	if mp, ok := compareMap[compare]; ok {
   102  		if _, ok = mp[t]; ok {
   103  			fmt.Println("success")
   104  		} else {
   105  			utils.ExitWithError(errors.Errorf("unmatched compare %s and compare result %d", compare, t))
   106  		}
   107  	} else {
   108  		utils.ExitWithError(errors.Errorf("unknown compare %s", compare))
   109  	}
   110  }