From ef1d0c37bdd51a5fa6a476e33e06a1e2fca6e6f4 Mon Sep 17 00:00:00 2001 From: starainrt Date: Wed, 24 May 2023 16:43:49 +0800 Subject: [PATCH] update code --- go.mod | 4 +-- go.sum | 4 +-- main.go | 96 ++++++++++++++++++++++++++++++--------------------------- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/go.mod b/go.mod index 9b06089..43cd554 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,9 @@ -module playground/binlog +module binlog go 1.20 require ( - b612.me/mysql/binlog v0.0.0-20230429091624-0f74dc3dc897 + b612.me/mysql/binlog v0.0.0-20230524072531-39ca67fcfe81 b612.me/mysql/gtid v0.0.0-20230425105031-298e51a68044 b612.me/starlog v1.3.2 b612.me/staros v1.1.6 diff --git a/go.sum b/go.sum index 59a8a9c..24de8e2 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -b612.me/mysql/binlog v0.0.0-20230429091624-0f74dc3dc897 h1:dm3U9OXzbVMMfyPDF6REc6SylBjQ/u3x3k2xlE8+KR0= -b612.me/mysql/binlog v0.0.0-20230429091624-0f74dc3dc897/go.mod h1:j9oDZUBx7+GK9X1b1bqO9SHddHvDRSGfwbIISxONqfA= +b612.me/mysql/binlog v0.0.0-20230524072531-39ca67fcfe81 h1:nUTBhXxtHAZd4p2ppbtj6wg5Ji5bbCAsWu6LAo5XvVs= +b612.me/mysql/binlog v0.0.0-20230524072531-39ca67fcfe81/go.mod h1:j9oDZUBx7+GK9X1b1bqO9SHddHvDRSGfwbIISxONqfA= b612.me/mysql/gtid v0.0.0-20230425105031-298e51a68044 h1:sJrYUl9Sb1tij6ROahFE3r/36Oag3kI92OXDjOKsdwA= b612.me/mysql/gtid v0.0.0-20230425105031-298e51a68044/go.mod h1:3EHq1jvlm3a92UxagMjfqSSVYb3KW2H3aT5nd4SiD94= b612.me/notify v1.2.4 h1:cjP80V9FeM+ib1DztZdykusakcbjNI4dAB1pXE8U6bo= diff --git a/main.go b/main.go index f838ad0..1588a06 100644 --- a/main.go +++ b/main.go @@ -12,9 +12,12 @@ import ( "path/filepath" "regexp" "strings" + "time" ) var ( + bigThan int + smallThan int startPos int endPos int startTime int64 @@ -25,9 +28,12 @@ var ( outPath string vbo bool skipquery bool + pos int64 + prefix string ) func init() { + cmd.Flags().IntVarP(&endPos, "pos", "P", 0, "skipPos of binlog") cmd.Flags().IntVarP(&startPos, "start-pos", "S", 0, "startPos of binlog") cmd.Flags().IntVarP(&endPos, "end-pos", "E", 0, "endPos of binlog") cmd.Flags().StringVarP(&includeGtid, "include-gtid", "i", "", "include gtid") @@ -38,7 +44,9 @@ func init() { cmd.Flags().Int64Var(&endTime, "endtime", 0, "end unix timestamp") cmd.Flags().BoolVarP(&vbo, "verbose", "v", false, "show the detail verbose") cmd.Flags().BoolVarP(&skipquery, "skip-query", "s", true, "skip query write to xlsx like BEGIN COMMIT") - + cmd.Flags().IntVar(&bigThan, "big", 0, "show tx big than x bytes") + cmd.Flags().IntVar(&smallThan, "small", 0, "show tx small than x bytes") + cmd.Flags().StringVar(&prefix, "prefix", "mysql-bin", "mysql binlog prefix") } var cmd = &cobra.Command{ @@ -60,21 +68,6 @@ func main() { func ParseBinlog() { var err error - var igtid, egtid *gtid.Gtid - if includeGtid != "" { - igtid, err = gtid.Parse(includeGtid) - if err != nil { - starlog.Errorln(err) - return - } - } - if excludeGtid != "" { - egtid, err = gtid.Parse(excludeGtid) - if err != nil { - starlog.Errorln(err) - return - } - } foundCount := 0 var totalGtid *gtid.Gtid var owrt *xlsx.File @@ -111,32 +104,32 @@ func ParseBinlog() { res.SetColWidth(15, 15, 40) owrt.Save(outPath) } - getParser := func(fpath string) { - err = binlog.ParseBinlogFile(fpath, func(tx binlog.Transaction) { - if startTime != 0 && tx.Timestamp < startTime { - return - } - if endTime != 0 && tx.Timestamp > endTime { - return - } - if tx.StartPos < startPos { - return - } - if endPos > 0 && tx.EndPos > endPos { - return - } - if igtid != nil { - if c, _ := igtid.Contain(tx.GTID); !c { - return - } - } - if egtid != nil { - if c, _ := egtid.Contain(tx.GTID); c { - return - } - } - + getParser := func(fpath string) string { + var sTime, eTime time.Time + if startTime != 0 { + sTime = time.Unix(startTime, 0) + } + if endTime != 0 { + eTime = time.Unix(endTime, 0) + } + var filter = binlog.BinlogFilter{ + IncludeGtid: includeGtid, + ExcludeGtid: excludeGtid, + StartPos: startPos, + EndPos: endPos, + StartDate: sTime, + EndDate: eTime, + BigThan: bigThan, + SmallThan: smallThan, + } + var cGtid *gtid.Gtid + err = binlog.ParseBinlogWithFilter(fpath, pos, filter, func(tx binlog.Transaction) { foundCount++ + if cGtid == nil { + cGtid, _ = gtid.Parse(tx.GTID) + } else { + cGtid.Add(tx.GTID) + } if totalGtid == nil { totalGtid, _ = gtid.Parse(tx.GTID) } else { @@ -174,18 +167,24 @@ func ParseBinlog() { } } }) + var cGtidStr string + if cGtid != nil { + cGtidStr = cGtid.String() + } if outPath != "" { err = owrt.Save(outPath) if err != nil { starlog.Errorln(err) - return + return cGtidStr } } if err != nil { starlog.Errorln(err) - return + return cGtidStr } + return cGtidStr } + var gtidRes [][]string for _, fp := range filePath { if staros.IsFolder(fp) { files, err := os.ReadDir(fp) @@ -193,21 +192,26 @@ func ParseBinlog() { starlog.Errorln("read folder failed:", err) return } - rgp := regexp.MustCompile(`^mysql-bin\.\d+$`) + rgp := regexp.MustCompile(`^` + prefix + `\.\d+$`) for _, v := range files { if !v.IsDir() && rgp.MatchString(v.Name()) { - getParser(filepath.Join(fp, v.Name())) + gtidRes = append(gtidRes, []string{v.Name(), getParser(filepath.Join(fp, v.Name()))}) } } } else { getParser(fp) } } - if outPath != "" { owrt.Save(outPath) } fmt.Println("") + if len(gtidRes) != 0 { + for _, v := range gtidRes { + fmt.Printf("%s:%s\n", v[0], v[1]) + } + } + fmt.Println("") allGtid := "" if totalGtid != nil { allGtid = totalGtid.String()