forked from GoDannyLai/binlog_rollback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binlog_repl.go
216 lines (178 loc) · 7.11 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"context"
"dannytools/ehand"
"dannytools/logging"
"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, orgSqlChan chan OrgSqlPrint) {
defer close(eventChan)
defer close(statChan)
defer close(orgSqlChan)
replStreamer := NewReplBinlogStreamer(cfg)
gLogger.WriteToLogByFieldsNormalOnlyMsg("start to get binlog from mysql", logging.INFO)
SendBinlogEventRepl(cfg, replStreamer, eventChan, statChan, orgSqlChan)
gLogger.WriteToLogByFieldsNormalOnlyMsg("finish getting binlog from mysql", logging.INFO)
}
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,
TimestampStringLocation: gBinlogTimeLocation,
ParseTime: false, //donot parse mysql datetime/time column into go time structure, take it as string
UseDecimal: false, // sqlbuilder not support decimal type
}
replSyncer := replication.NewBinlogSyncer(replCfg)
syncPosition := mysql.Position{Name: cfg.StartFile, Pos: uint32(cfg.StartPos)}
replStreamer, err := replSyncer.StartSync(syncPosition)
if err != nil {
gLogger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("error replication from master %s:%d ",
cfg.Host, cfg.Port), logging.ERROR, ehand.ERR_MYSQL_CONNECTION)
}
return replStreamer
}
func SendBinlogEventRepl(cfg *ConfCmd, streamer *replication.BinlogStreamer, eventChan chan MyBinEvent, statChan chan BinEventStats, orgSqlChan chan OrgSqlPrint) {
//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
orgSqlEvent *replication.RowsQueryEvent
)
//defer g_MaxBin_Event_Idx.SetMaxBinEventIdx()
for {
ev, err := streamer.GetEvent(context.Background())
if err != nil {
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "error to get binlog event", logging.ERROR, ehand.ERR_MYSQL_REPL)
break
}
if !cfg.IfSetStopParsPoint && !cfg.IfSetStopDateTime && !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 == C_reBreak {
break
} else if chkRe == C_reContinue {
continue
} else if chkRe == C_reFileEnd {
continue
}
if cfg.IfWriteOrgSql && ev.Header.EventType == replication.ROWS_QUERY_EVENT {
orgSqlEvent = ev.Event.(*replication.RowsQueryEvent)
orgSqlChan <- OrgSqlPrint{Binlog: currentBinlog, DateTime: ev.Header.Timestamp,
StartPos: ev.Header.LogPos - ev.Header.EventSize, StopPos: ev.Header.LogPos, QuerySql: string(orgSqlEvent.Query)}
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, ¤tBinlog)
//gLogger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("check binlog event result %d", chkRe), logging.INFO)
if chkRe == C_reContinue {
continue
} else if chkRe == C_reBreak {
break
} else if chkRe == C_reProcess {
db, tb, sqlType, sql, rowCnt = GetDbTbAndQueryAndRowCntFromBinevent(ev)
if sqlType == "query" {
sqlLower = strings.ToLower(sql)
if sqlLower == "begin" {
trxStatus = C_trxBegin
trxIndex++
} else if sqlLower == "commit" {
trxStatus = C_trxCommit
} else if sqlLower == "rollback" {
trxStatus = C_trxRollback
} else if oneMyEvent.QuerySql != nil && oneMyEvent.QuerySql.IsDml() {
trxStatus = C_trxProcess
rowCnt = 1
}
} else {
trxStatus = C_trxProcess
}
if cfg.WorkType != "stats" {
ifSendEvent := false
if oneMyEvent.IfRowsEvent {
tbKey := GetAbsTableName(string(oneMyEvent.BinEvent.Table.Schema),
string(oneMyEvent.BinEvent.Table.Table))
if _, ok := G_TablesColumnsInfo.tableInfos[tbKey]; ok {
ifSendEvent = true
} else {
gLogger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("no table struct found for %s, it maybe dropped, skip it. RowsEvent position:%s",
tbKey, oneMyEvent.MyPos.String()), logging.ERROR)
}
} else if cfg.WorkType == "2sql" && cfg.ParseStatementSql && oneMyEvent.QuerySql != nil && oneMyEvent.QuerySql.IsDml() {
ifSendEvent = true
//gLogger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("send dml event: %s", spew.Sdump(oneMyEvent.QuerySql)), logging.INFO)
}
if ifSendEvent {
binEventIdx++
oneMyEvent.EventIdx = binEventIdx
oneMyEvent.SqlType = sqlType
oneMyEvent.Timestamp = ev.Header.Timestamp
oneMyEvent.TrxIndex = trxIndex
oneMyEvent.TrxStatus = trxStatus
eventChan <- *oneMyEvent
}
}
// output analysis result whatever the WorkType is
if sqlType != "" {
if sqlType == "query" {
if oneMyEvent.QuerySql != nil {
//gLogger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("send dml/ddl event for statchan: %s", spew.Sdump(oneMyEvent.QuerySql)), logging.INFO)
statChan <- BinEventStats{Timestamp: ev.Header.Timestamp, Binlog: currentBinlog, StartPos: ev.Header.LogPos - ev.Header.EventSize, StopPos: ev.Header.LogPos,
Database: oneMyEvent.QuerySql.GetDatabasesAll(","), Table: oneMyEvent.QuerySql.GetFullTablesAll(","), QuerySql: sql,
RowCnt: rowCnt, QueryType: sqlType, ParsedSqlInfo: oneMyEvent.QuerySql.Copy()}
} else {
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 == C_reFileEnd {
continue
} else {
gLogger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("this should not happen: return value of CheckBinEvent() is %d\n", chkRe),
logging.WARNING)
}
}
}