-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbinlog_repl.go
174 lines (143 loc) · 4.81 KB
/
binlog_repl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"context"
"fmt"
"strings"
"github.com/siddontang/go-mysql/mysql"
"github.com/siddontang/go-mysql/replication"
)
/*
type ReplBinlogStreamer struct {
cfg ConfCmd
binSyncCfg replication.BinlogSyncerConfig
replBinSyncer *replication.BinlogSyncer
replBinStreamer *replication.BinlogStreamer
}
*/
func ParserAllBinEventsFromRepl(cfg ConfCmd, eventChan chan MyBinEvent, statChan chan BinEventStats) {
defer close(eventChan)
defer close(statChan)
replStreamer := NewReplBinlogStreamer(cfg)
SendBinlogEventRepl(cfg, replStreamer, eventChan, statChan)
}
func NewReplBinlogStreamer(cfg ConfCmd) *replication.BinlogStreamer {
replCfg := replication.BinlogSyncerConfig{
ServerID: uint32(cfg.ServerId),
Flavor: cfg.MysqlType,
Host: cfg.Host,
Port: uint16(cfg.Port),
User: cfg.User,
Password: cfg.Passwd,
Charset: "utf8",
SemiSyncEnabled: false,
ParseTime: true,
}
replSyncer := replication.NewBinlogSyncer(replCfg)
syncPosition := mysql.Position{Name: cfg.StartFile, Pos: uint32(cfg.StartPos)}
replStreamer, err := replSyncer.StartSync(syncPosition)
if err != nil {
errMsg := fmt.Sprintf("error replication from master %s:%d ", cfg.Host, cfg.Port)
CheckErr(err, errMsg, ERR_MYSQL_REPL, true)
}
return replStreamer
}
func SendBinlogEventRepl(cfg ConfCmd, streamer *replication.BinlogStreamer, eventChan chan MyBinEvent, statChan chan BinEventStats) {
//defer close(statChan)
//defer close(eventChan)
var (
chkRe int
currentBinlog *string = &cfg.StartFile
binEventIdx uint64 = 0
trxIndex uint64 = 0
trxStatus int = 0
sqlLower string = ""
db string = ""
tb string = ""
sql string = ""
sqlType string = ""
rowCnt uint32 = 0
tbMapPos uint32 = 0
justStart bool = true
)
//defer g_MaxBin_Event_Idx.SetMaxBinEventIdx()
for {
ev, err := streamer.GetEvent(context.Background())
if err != nil {
fmt.Println("error to get binlog event: %s\n", err)
break
}
if !cfg.IfSetStopParsPoint && !justStart {
//just parse one binlog. the first event is rotate event
if ev.Header.EventType == replication.ROTATE_EVENT {
break
}
}
justStart = false
if ev.Header.EventType == replication.TABLE_MAP_EVENT {
tbMapPos = ev.Header.LogPos - ev.Header.EventSize // avoid mysqlbing mask the row event as unknown table row event
}
ev.RawData = []byte{} // we donnot need raw data
chkRe = CheckBinHeaderCondition(cfg, ev.Header, currentBinlog)
if chkRe == RE_BREAK {
break
} else if chkRe == RE_CONTINUE {
continue
} else if chkRe == RE_FILE_END {
continue
}
oneMyEvent := &MyBinEvent{MyPos: mysql.Position{Name: *currentBinlog, Pos: ev.Header.LogPos},
StartPos: tbMapPos}
//StartPos: ev.Header.LogPos - ev.Header.EventSize}
chkRe = oneMyEvent.CheckBinEvent(cfg, ev, currentBinlog)
if chkRe == RE_CONTINUE {
continue
} else if chkRe == RE_BREAK {
break
} else if chkRe == RE_PROCESS {
db, tb, sqlType, sql, rowCnt = GetDbTbAndQueryAndRowCntFromBinevent(ev)
if sqlType == "query" {
sqlLower = strings.ToLower(sql)
if sqlLower == "begin" {
trxStatus = TRX_STATUS_BEGIN
trxIndex++
} else if sqlLower == "commit" {
trxStatus = TRX_STATUS_COMMIT
} else if sqlLower == "rollback" {
trxStatus = TRX_STATUS_ROLLBACK
}
} else {
trxStatus = TRX_STATUS_PROGRESS
}
if cfg.WorkType != "stats" && oneMyEvent.IfRowsEvent {
tbKey := GetAbsTableName(string(oneMyEvent.BinEvent.Table.Schema),
string(oneMyEvent.BinEvent.Table.Table))
if _, ok := G_TablesColumnsInfo.tableInfos[tbKey]; ok {
binEventIdx++
oneMyEvent.EventIdx = binEventIdx
oneMyEvent.SqlType = sqlType
oneMyEvent.Timestamp = ev.Header.Timestamp
oneMyEvent.TrxIndex = trxIndex
oneMyEvent.TrxStatus = trxStatus
eventChan <- *oneMyEvent
} /* else {
fmt.Printf("no table struct found for %s, it maybe dropped, skip it. RowsEvent position:%s", tbKey, oneMyEvent.MyPos.String())
}*/
}
// output analysis result whatever the WorkType is
if sqlType != "" {
if sqlType == "query" {
statChan <- BinEventStats{Timestamp: ev.Header.Timestamp, Binlog: *currentBinlog, StartPos: ev.Header.LogPos - ev.Header.EventSize, StopPos: ev.Header.LogPos,
Database: db, Table: tb, QuerySql: sql, RowCnt: rowCnt, QueryType: sqlType}
} else {
statChan <- BinEventStats{Timestamp: ev.Header.Timestamp, Binlog: *currentBinlog, StartPos: tbMapPos, StopPos: ev.Header.LogPos,
Database: db, Table: tb, QuerySql: sql, RowCnt: rowCnt, QueryType: sqlType}
}
}
} else if chkRe == RE_FILE_END {
continue
} else {
fmt.Printf("this should not happen: return value of CheckBinEvent() is %d\n", chkRe)
}
}
//g_MaxBin_Event_Idx.SetMaxBinEventIdx(binEventIdx)
}