update code

master
兔子 1 year ago
parent 402ee25ef1
commit ef1d0c37bd

@ -1,9 +1,9 @@
module playground/binlog module binlog
go 1.20 go 1.20
require ( 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/mysql/gtid v0.0.0-20230425105031-298e51a68044
b612.me/starlog v1.3.2 b612.me/starlog v1.3.2
b612.me/staros v1.1.6 b612.me/staros v1.1.6

@ -1,5 +1,5 @@
b612.me/mysql/binlog v0.0.0-20230429091624-0f74dc3dc897 h1:dm3U9OXzbVMMfyPDF6REc6SylBjQ/u3x3k2xlE8+KR0= b612.me/mysql/binlog v0.0.0-20230524072531-39ca67fcfe81 h1:nUTBhXxtHAZd4p2ppbtj6wg5Ji5bbCAsWu6LAo5XvVs=
b612.me/mysql/binlog v0.0.0-20230429091624-0f74dc3dc897/go.mod h1:j9oDZUBx7+GK9X1b1bqO9SHddHvDRSGfwbIISxONqfA= 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 h1:sJrYUl9Sb1tij6ROahFE3r/36Oag3kI92OXDjOKsdwA=
b612.me/mysql/gtid v0.0.0-20230425105031-298e51a68044/go.mod h1:3EHq1jvlm3a92UxagMjfqSSVYb3KW2H3aT5nd4SiD94= b612.me/mysql/gtid v0.0.0-20230425105031-298e51a68044/go.mod h1:3EHq1jvlm3a92UxagMjfqSSVYb3KW2H3aT5nd4SiD94=
b612.me/notify v1.2.4 h1:cjP80V9FeM+ib1DztZdykusakcbjNI4dAB1pXE8U6bo= b612.me/notify v1.2.4 h1:cjP80V9FeM+ib1DztZdykusakcbjNI4dAB1pXE8U6bo=

@ -12,9 +12,12 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"time"
) )
var ( var (
bigThan int
smallThan int
startPos int startPos int
endPos int endPos int
startTime int64 startTime int64
@ -25,9 +28,12 @@ var (
outPath string outPath string
vbo bool vbo bool
skipquery bool skipquery bool
pos int64
prefix string
) )
func init() { 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(&startPos, "start-pos", "S", 0, "startPos of binlog")
cmd.Flags().IntVarP(&endPos, "end-pos", "E", 0, "endPos of binlog") cmd.Flags().IntVarP(&endPos, "end-pos", "E", 0, "endPos of binlog")
cmd.Flags().StringVarP(&includeGtid, "include-gtid", "i", "", "include gtid") 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().Int64Var(&endTime, "endtime", 0, "end unix timestamp")
cmd.Flags().BoolVarP(&vbo, "verbose", "v", false, "show the detail verbose") 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().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{ var cmd = &cobra.Command{
@ -60,21 +68,6 @@ func main() {
func ParseBinlog() { func ParseBinlog() {
var err error 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 foundCount := 0
var totalGtid *gtid.Gtid var totalGtid *gtid.Gtid
var owrt *xlsx.File var owrt *xlsx.File
@ -111,32 +104,32 @@ func ParseBinlog() {
res.SetColWidth(15, 15, 40) res.SetColWidth(15, 15, 40)
owrt.Save(outPath) owrt.Save(outPath)
} }
getParser := func(fpath string) { getParser := func(fpath string) string {
err = binlog.ParseBinlogFile(fpath, func(tx binlog.Transaction) { var sTime, eTime time.Time
if startTime != 0 && tx.Timestamp < startTime { if startTime != 0 {
return sTime = time.Unix(startTime, 0)
} }
if endTime != 0 && tx.Timestamp > endTime { if endTime != 0 {
return eTime = time.Unix(endTime, 0)
} }
if tx.StartPos < startPos { var filter = binlog.BinlogFilter{
return IncludeGtid: includeGtid,
} ExcludeGtid: excludeGtid,
if endPos > 0 && tx.EndPos > endPos { StartPos: startPos,
return EndPos: endPos,
} StartDate: sTime,
if igtid != nil { EndDate: eTime,
if c, _ := igtid.Contain(tx.GTID); !c { BigThan: bigThan,
return SmallThan: smallThan,
} }
} var cGtid *gtid.Gtid
if egtid != nil { err = binlog.ParseBinlogWithFilter(fpath, pos, filter, func(tx binlog.Transaction) {
if c, _ := egtid.Contain(tx.GTID); c {
return
}
}
foundCount++ foundCount++
if cGtid == nil {
cGtid, _ = gtid.Parse(tx.GTID)
} else {
cGtid.Add(tx.GTID)
}
if totalGtid == nil { if totalGtid == nil {
totalGtid, _ = gtid.Parse(tx.GTID) totalGtid, _ = gtid.Parse(tx.GTID)
} else { } else {
@ -174,18 +167,24 @@ func ParseBinlog() {
} }
} }
}) })
var cGtidStr string
if cGtid != nil {
cGtidStr = cGtid.String()
}
if outPath != "" { if outPath != "" {
err = owrt.Save(outPath) err = owrt.Save(outPath)
if err != nil { if err != nil {
starlog.Errorln(err) starlog.Errorln(err)
return return cGtidStr
} }
} }
if err != nil { if err != nil {
starlog.Errorln(err) starlog.Errorln(err)
return return cGtidStr
} }
return cGtidStr
} }
var gtidRes [][]string
for _, fp := range filePath { for _, fp := range filePath {
if staros.IsFolder(fp) { if staros.IsFolder(fp) {
files, err := os.ReadDir(fp) files, err := os.ReadDir(fp)
@ -193,21 +192,26 @@ func ParseBinlog() {
starlog.Errorln("read folder failed:", err) starlog.Errorln("read folder failed:", err)
return return
} }
rgp := regexp.MustCompile(`^mysql-bin\.\d+$`) rgp := regexp.MustCompile(`^` + prefix + `\.\d+$`)
for _, v := range files { for _, v := range files {
if !v.IsDir() && rgp.MatchString(v.Name()) { 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 { } else {
getParser(fp) getParser(fp)
} }
} }
if outPath != "" { if outPath != "" {
owrt.Save(outPath) owrt.Save(outPath)
} }
fmt.Println("") fmt.Println("")
if len(gtidRes) != 0 {
for _, v := range gtidRes {
fmt.Printf("%s:%s\n", v[0], v[1])
}
}
fmt.Println("")
allGtid := "" allGtid := ""
if totalGtid != nil { if totalGtid != nil {
allGtid = totalGtid.String() allGtid = totalGtid.String()

Loading…
Cancel
Save