forked from GoDannyLai/binlog_rollback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binlog_file.go
284 lines (247 loc) · 9.92 KB
/
binlog_file.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"bytes"
"dannytools/ehand"
"dannytools/logging"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/juju/errors"
"github.com/siddontang/go-mysql/mysql"
"github.com/siddontang/go-mysql/replication"
"github.com/toolkits/file"
//"github.com/davecgh/go-spew/spew"
)
var (
fileBinEventHandlingIndex uint64 = 0
fileTrxIndex uint64 = 0
)
type BinFileParser struct {
parser *replication.BinlogParser
}
func (this BinFileParser) MyParseAllBinlogFiles(cfg *ConfCmd, evChan chan MyBinEvent, statChan chan BinEventStats, orgSqlChan chan OrgSqlPrint) {
defer close(evChan)
defer close(statChan)
defer close(orgSqlChan)
gLogger.WriteToLogByFieldsNormalOnlyMsg("start to parse binlog from local files", logging.INFO)
binlog, binpos := GetFirstBinlogPosToParse(cfg)
binBaseName, binBaseIndx := GetBinlogBasenameAndIndex(binlog)
for {
if cfg.IfSetStopFilePos {
if cfg.StopFilePos.Compare(mysql.Position{Name: filepath.Base(binlog), Pos: 4}) < 1 {
break
}
}
gLogger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("start to parse %s %d\n", binlog, binpos), logging.INFO)
result, err := this.MyParseOneBinlogFile(cfg, binlog, evChan, statChan, orgSqlChan)
if err != nil {
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "error to parse binlog", logging.ERROR, ehand.ERR_BINLOG_EVENT)
break
}
if result == C_reBreak {
break
} else if result == C_reFileEnd {
if !cfg.IfSetStopParsPoint && !cfg.IfSetStopDateTime {
//just parse one binlog
break
}
binlog = filepath.Join(cfg.BinlogDir, GetNextBinlog(binBaseName, binBaseIndx))
if !file.IsFile(binlog) {
gLogger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("%s not exists nor a file\n", binlog), logging.WARNING)
break
}
binBaseIndx++
binpos = 4
} else {
gLogger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("this should not happen: return value of MyParseOneBinlog is %d\n",
result), logging.WARNING)
break
}
}
gLogger.WriteToLogByFieldsNormalOnlyMsg("finish parsing binlog from local files", logging.INFO)
//g_MaxBin_Event_Idx.SetMaxBinEventIdx(fileBinEventHandlingIndex)
}
func (this BinFileParser) MyParseOneBinlogFile(cfg *ConfCmd, name string, evChan chan MyBinEvent, statChan chan BinEventStats, orgSqlChan chan OrgSqlPrint) (int, error) {
// process: 0, continue: 1, break: 2
f, err := os.Open(name)
if f != nil {
defer f.Close()
}
if err != nil {
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "fail to open "+name, logging.ERROR, ehand.ERR_FILE_OPEN)
return C_reBreak, errors.Trace(err)
}
fileTypeBytes := int64(4)
b := make([]byte, fileTypeBytes)
if _, err = f.Read(b); err != nil {
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "fail to read "+name, logging.ERROR, ehand.ERR_FILE_READ)
return C_reBreak, errors.Trace(err)
} else if !bytes.Equal(b, replication.BinLogFileHeader) {
gLogger.WriteToLogByFieldsNormalOnlyMsg(name+" is not a valid binlog file, head 4 bytes must fe'bin' ", logging.WARNING)
return C_reBreak, errors.Trace(err)
}
// must not seek to other position, otherwise the program may panic because formatevent, table map event is skipped
if _, err = f.Seek(fileTypeBytes, os.SEEK_SET); err != nil {
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, fmt.Sprintf("error seek %s to %d", name, fileTypeBytes),
logging.ERROR, ehand.ERR_FILE_SEEK)
return C_reBreak, errors.Trace(err)
}
var binlog string = filepath.Base(name)
return this.MyParseReader(cfg, f, evChan, &binlog, statChan, orgSqlChan)
}
func (this BinFileParser) MyParseReader(cfg *ConfCmd, r io.Reader, evChan chan MyBinEvent, binlog *string, statChan chan BinEventStats, orgSqlChan chan OrgSqlPrint) (int, error) {
// process: 0, continue: 1, break: 2, EOF: 3
var (
err error
n int64
db string = ""
tb string = ""
sql string = ""
sqlType string = ""
rowCnt uint32 = 0
trxStatus int = 0
sqlLower string = ""
tbMapPos uint32 = 0
orgSqlEvent *replication.RowsQueryEvent
)
for {
headBuf := make([]byte, replication.EventHeaderSize)
if _, err = io.ReadFull(r, headBuf); err == io.EOF {
return C_reFileEnd, nil
} else if err != nil {
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "fail to read binlog event header of "+*binlog,
logging.ERROR, ehand.ERR_FILE_READ)
return C_reBreak, errors.Trace(err)
}
var h *replication.EventHeader
h, err = this.parser.ParseHeader(headBuf)
if err != nil {
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "fail to parse binlog event header of "+*binlog,
logging.ERROR, ehand.ERR_BINEVENT_HEADER)
return C_reBreak, errors.Trace(err)
}
//fmt.Printf("parsing %s %d %s\n", *binlog, h.LogPos, GetDatetimeStr(int64(h.Timestamp), int64(0), DATETIME_FORMAT))
if h.EventSize <= uint32(replication.EventHeaderSize) {
err = errors.Errorf("invalid event header, event size is %d, too small", h.EventSize)
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "", logging.ERROR, ehand.ERR_BINEVENT_HEADER)
return C_reBreak, err
}
var buf bytes.Buffer
if n, err = io.CopyN(&buf, r, int64(h.EventSize)-int64(replication.EventHeaderSize)); err != nil {
err = errors.Errorf("get event body err %v, need %d - %d, but got %d", err, h.EventSize, replication.EventHeaderSize, n)
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "", logging.ERROR, ehand.ERR_BINEVENT_BODY)
return C_reBreak, err
}
//h.Dump(os.Stdout)
data := buf.Bytes()
var rawData []byte
rawData = append(rawData, headBuf...)
rawData = append(rawData, data...)
eventLen := int(h.EventSize) - replication.EventHeaderSize
if len(data) != eventLen {
err = errors.Errorf("invalid data size %d in event %s, less event length %d", len(data), h.EventType, eventLen)
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "", logging.ERROR, ehand.ERR_BINEVENT_BODY)
return C_reBreak, err
}
var e replication.Event
e, err = this.parser.ParseEvent(h, data, rawData)
if err != nil {
gLogger.WriteToLogByFieldsErrorExtramsgExitCode(err, "fail to parse binlog event body of "+*binlog,
logging.ERROR, ehand.ERR_BINEVENT_BODY)
return C_reBreak, errors.Trace(err)
}
if h.EventType == replication.TABLE_MAP_EVENT {
tbMapPos = h.LogPos - h.EventSize // avoid mysqlbing mask the row event as unknown table row event
}
//e.Dump(os.Stdout)
//can not advance this check, because we need to parse table map event or table may not found. Also we must seek ahead the read file position
chRe := CheckBinHeaderCondition(cfg, h, *binlog)
if chRe == C_reBreak {
return C_reBreak, nil
} else if chRe == C_reContinue {
continue
} else if chRe == C_reFileEnd {
return C_reFileEnd, nil
}
if cfg.IfWriteOrgSql && h.EventType == replication.ROWS_QUERY_EVENT {
orgSqlEvent = e.(*replication.RowsQueryEvent)
orgSqlChan <- OrgSqlPrint{Binlog: *binlog, DateTime: h.Timestamp,
StartPos: h.LogPos - h.EventSize, StopPos: h.LogPos, QuerySql: string(orgSqlEvent.Query)}
continue
}
//binEvent := &replication.BinlogEvent{RawData: rawData, Header: h, Event: e}
binEvent := &replication.BinlogEvent{Header: h, Event: e} // we donnot need raw data
oneMyEvent := &MyBinEvent{MyPos: mysql.Position{Name: *binlog, Pos: h.LogPos},
StartPos: tbMapPos}
//StartPos: h.LogPos - h.EventSize}
chRe = oneMyEvent.CheckBinEvent(cfg, binEvent, binlog)
if chRe == C_reBreak {
return C_reBreak, nil
} else if chRe == C_reContinue {
continue
} else if chRe == C_reFileEnd {
return C_reFileEnd, nil
} else if chRe == C_reProcess {
// output analysis result whatever the WorkType is
db, tb, sqlType, sql, rowCnt = GetDbTbAndQueryAndRowCntFromBinevent(binEvent)
if sqlType == "query" {
sqlLower = strings.ToLower(sql)
if sqlLower == "begin" {
trxStatus = C_trxBegin
fileTrxIndex++
} 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" && oneMyEvent.IfRowsEvent {
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
}
if ifSendEvent {
fileBinEventHandlingIndex++
oneMyEvent.EventIdx = fileBinEventHandlingIndex
oneMyEvent.SqlType = sqlType
oneMyEvent.Timestamp = h.Timestamp
oneMyEvent.TrxIndex = fileTrxIndex
oneMyEvent.TrxStatus = trxStatus
evChan <- *oneMyEvent
}
}
if sqlType != "" {
if sqlType == "query" {
if oneMyEvent.QuerySql != nil {
statChan <- BinEventStats{Timestamp: h.Timestamp, Binlog: *binlog, StartPos: h.LogPos - h.EventSize, StopPos: h.LogPos,
Database: oneMyEvent.QuerySql.GetDatabasesAll(","), Table: oneMyEvent.QuerySql.GetFullTablesAll(","), QuerySql: sql,
RowCnt: rowCnt, QueryType: sqlType, ParsedSqlInfo: oneMyEvent.QuerySql.Copy()}
} else {
statChan <- BinEventStats{Timestamp: h.Timestamp, Binlog: *binlog, StartPos: h.LogPos - h.EventSize, StopPos: h.LogPos,
Database: db, Table: tb, QuerySql: sql, RowCnt: rowCnt, QueryType: sqlType}
}
} else {
statChan <- BinEventStats{Timestamp: h.Timestamp, Binlog: *binlog, StartPos: tbMapPos, StopPos: h.LogPos,
Database: db, Table: tb, QuerySql: sql, RowCnt: rowCnt, QueryType: sqlType}
}
}
}
}
return C_reFileEnd, nil
}