/
event.go
480 lines (398 loc) · 12.5 KB
/
event.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
Copyright 2018 liipx(lipengxiang)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package binlog
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
"time"
)
// BinEventBody describe event body
type BinEventBody interface {
isEventBody()
}
// BinEvent binary log event definition - Kurt Larsen (ChaosHour) added the following:
type BinEvent struct {
Header *BinEventHeader
Body BinEventBody
ChecksumType byte
ChecksumVal []byte
DatabaseName string // Kurt Larsen (ChaosHour) added this
TableName string // Kurt Larsen (ChaosHour) added this
ColumnCount uint64 // Kurt Larsen (ChaosHour) added this
ColumnTypes []byte // Kurt Larsen (ChaosHour) added this
ColumnNames []string // Kurt Larsen (ChaosHour) added this
}
/*
// Validation event validity check - Kurt Larsen (ChaosHour) added this Original code below:
func (event *BinEvent) Validation(bin *BinaryLogInfo, header, body []byte) ([]byte, error) {
if bin == nil {
return body, fmt.Errorf("empty BinaryLogInfo")
}
if event.Header == nil {
return body, fmt.Errorf("empty event header")
}
if l := int64(len(body) + len(header)); l != event.Header.EventSize {
return body, fmt.Errorf("event size got %d need %d", l, event.Header.EventSize)
}
if bin.description != nil && bin.description.hasCheckSum {
index := len(body) - binlogChecksumLength - 1
event.ChecksumType = body[index]
event.ChecksumVal = body[index+1:]
body = body[:index+1]
if !ChecksumValidate(event.ChecksumType, event.ChecksumVal, append(header, body...)) || len(event.ChecksumVal) != 4 {
return body, fmt.Errorf("binlog checksum validation failed")
}
}
return body, nil
}
*/
// Validation event validity check - Kurt Larsen (ChaosHour) added this:
func (event *BinEvent) Validation(bin *BinaryLogInfo, header, body []byte) ([]byte, error) {
// Check if BinaryLogInfo object is nil
if bin == nil {
return body, fmt.Errorf("empty BinaryLogInfo")
}
// Check if event header is nil
if event.Header == nil {
return body, fmt.Errorf("empty event header")
}
// Check if event size matches the sum of the length of the header and body
if l := int64(len(body) + len(header)); l != event.Header.EventSize {
return body, fmt.Errorf("event size got %d need %d", l, event.Header.EventSize)
}
// Check if binary log has a checksum
if bin.description != nil && bin.description.hasCheckSum {
// Calculate the index of the checksum value in the body slice
index := len(body) - binlogChecksumLength - 1
// Set the checksum type and value in the BinEvent object
event.ChecksumType = body[index]
event.ChecksumVal = body[index+1:]
// Truncate the body slice to remove the checksum value
body = body[:index+1]
// Validate the checksum value
if !ChecksumValidate(event.ChecksumType, event.ChecksumVal, append(header, body...)) || len(event.ChecksumVal) != 4 {
return body, fmt.Errorf("binlog checksum validation failed")
}
}
// Return the body slice and nil error if no errors were encountered
return body, nil
}
// String interface implement - Kurt Larsen (ChaosHour) added this:
func (event *BinEvent) String() string {
return fmt.Sprintf("Header:%s, Body:%s", event.Header, event.Body)
}
// BaseEventBody is base off all events
type BaseEventBody struct{}
func (event *BaseEventBody) isEventBody() {}
// BinEventUnParsed save the event data when the event type not supported yet.
type BinEventUnParsed struct {
BaseEventBody
Data []byte
}
func decodeUnSupportEvent(data []byte) (*BinEventUnParsed, error) {
return &BinEventUnParsed{
Data: data,
}, nil
}
// mysql binlog version > 1 (version > mysql 4.0.0), size = 19
var defaultEventHeaderSize int64 = 19
// BinEventHeader binary log header definition
// https://dev.mysql.com/doc/internals/en/binlog-event-header.html
type BinEventHeader struct {
Timestamp int64
EventType uint8
ServerID int64
EventSize int64
LogPos int64
Flag uint16
}
// Type function will translate event type into string
func (header *BinEventHeader) Type() string {
return EventType2Str[header.EventType]
}
// String interface implement
func (header *BinEventHeader) String() string {
return fmt.Sprintf("Type:%s, Time:%s, ServerID:%d, EventSize:%d, EventEndPos:%d, Flag:0x%x",
header.Type(),
time.Unix(header.Timestamp, 0),
header.Timestamp,
header.EventSize,
header.LogPos,
header.Flag,
)
}
func decodeEventHeader(data []byte, size int64) (*BinEventHeader, error) {
if l := len(data); int64(l) < size {
return nil, fmt.Errorf("invalid event header size %d, should be %d", l, size)
}
var pos int
eventHeader := &BinEventHeader{}
// timestamp
eventHeader.Timestamp = int64(binary.LittleEndian.Uint32(data[pos:]))
pos += 4
// event_type
eventHeader.EventType = data[pos]
pos++
// serverId
eventHeader.ServerID = int64(binary.LittleEndian.Uint32(data[pos:]))
pos += 4
// event_size
eventHeader.EventSize = int64(binary.LittleEndian.Uint32(data[pos:]))
pos += 4
// version > 2
if size > 13 {
// log_pos
eventHeader.LogPos = int64(binary.LittleEndian.Uint32(data[pos:]))
pos += 4
// flags
eventHeader.Flag = binary.LittleEndian.Uint16(data[pos:])
}
return eventHeader, nil
}
// BinFmtDescEvent is the definition of FORMAT_DESCRIPTION_EVENT
// https://dev.mysql.com/doc/internals/en/format-description-event.html
type BinFmtDescEvent struct {
BaseEventBody
BinlogVersion int
MySQLVersion string
CreateTime int64
EventHeaderLength int64
EventTypeHeader []byte
// cache the result of hasCheckSum()
hasCheckSum bool
}
func decodeFmtDescEvent(data []byte) (*BinFmtDescEvent, error) {
var pos int
desc := &BinFmtDescEvent{}
// binlog-version
desc.BinlogVersion = int(binary.LittleEndian.Uint16(data))
pos += 2
// mysql-server version
//desc.MySQLVersion = string(bytes.Trim(data[pos:pos+50], string(0x00)))
desc.MySQLVersion = string(bytes.Trim(data[pos:pos+50], "\x00"))
desc.hasCheckSum = hasChecksum(desc.MySQLVersion)
pos += 50
// create timestamp
desc.CreateTime = int64(binary.LittleEndian.Uint32(data[pos:]))
pos += 4
// event header length
desc.EventHeaderLength = int64(data[pos])
pos++
// event type header lengths
desc.EventTypeHeader = data[pos:]
return desc, nil
}
// BinQueryEvent is the definition of QUERY_EVENT
// https://dev.mysql.com/doc/internals/en/query-event.html
type BinQueryEvent struct {
BaseEventBody
SlaveProxyID int64
ExecutionTime int64
ErrorCode uint16
statusVarsLength int
StatusVars []byte
Schema string
Query string
}
func decodeQueryEvent(data []byte, binlogVersion int) (*BinQueryEvent, error) {
var pos int
event := &BinQueryEvent{}
// slave_proxy_id
event.SlaveProxyID = int64(binary.LittleEndian.Uint32(data[pos:]))
pos += 4
// execution time
event.ExecutionTime = int64(binary.LittleEndian.Uint32(data[pos:]))
pos += 4
// schema length
schemaLength := int(uint8(data[pos]))
pos++
// error-code
event.ErrorCode = binary.LittleEndian.Uint16(data[pos:])
pos += 2
if binlogVersion >= 4 {
// status-vars length
event.statusVarsLength = int(binary.LittleEndian.Uint16(data[pos:]))
pos += 2
// status-vars
event.StatusVars = data[pos : pos+event.statusVarsLength]
pos += event.statusVarsLength
}
// schema
event.Schema = string(data[pos : pos+schemaLength])
pos += schemaLength
// ignore 0x00
pos++
// query
event.Query = string(data[pos:])
return event, nil
}
// Statue will format status_vars of QUERY_EVENT
// TODO decode QUERY_EVENT status_var
func (event *BinQueryEvent) Statue() error {
fmt.Println(event.statusVarsLength)
for i := 0; i < event.statusVarsLength; {
// got status_vars key
k := event.StatusVars[i]
i++
// decode values
switch k {
case QFlags2Code:
v := event.StatusVars[i : i+4]
i += 4
// TODO
fmt.Println("QFlags2Code", v)
case QSQLModeCode:
v := event.StatusVars[i : i+8]
i += 8
// TODO
fmt.Println("QSQLModeCode", v)
case QCatalog:
n := int(event.StatusVars[i])
v := string(event.StatusVars[i+1 : i+1+n])
i += 1 + n + 1
// TODO
fmt.Println("QCatalog", v)
case QAutoIncrement:
increment := binary.LittleEndian.Uint32(event.StatusVars[i:])
offset := binary.LittleEndian.Uint32(event.StatusVars[i+2:])
// TODO
fmt.Printf("QAutoIncrement %d, %d\n", increment, offset)
case QCharsetCode:
clientCharSet := event.StatusVars[i : i+2]
i += 2
collationConnection := event.StatusVars[i : i+2]
i += 2
collationServer := event.StatusVars[i : i+2]
i += 2
// TODO
fmt.Println("QCharsetCode", clientCharSet, collationConnection, collationServer)
case QTimeZoneCode:
n := int(event.StatusVars[i])
v := string(event.StatusVars[i+1 : i+1+n])
i += 1 + n
// TODO
fmt.Println("QTimeZoneCode", v)
case QCatalogNZCode:
n := int(event.StatusVars[i])
v := string(event.StatusVars[i+1 : i+1+n])
i += 1 + n
// TODO
fmt.Printf("QCatalogNZCode %s\n", v)
case QLCTimeNamesCode:
// TODO
fmt.Println("QLCTimeNamesCode")
case QCharsetDatabaseCode:
// TODO
fmt.Println("QCharsetDatabaseCode")
case QTableMapForUpdateCode:
// TODO
fmt.Println("QTableMapForUpdateCode")
case QMasterDataWrittenCode:
// TODO
fmt.Println("QMasterDataWrittenCode")
case QInvokers:
// TODO
fmt.Println("QInvokers")
case QUpdatedDBNames:
// TODO
fmt.Println("QUpdatedDBNames")
case QMicroseconds:
microseconds := binary.LittleEndian.Uint32(event.StatusVars[i:])
i += 3
// TODO
fmt.Printf("QMicroseconds %d\n", microseconds)
default:
return fmt.Errorf("unknown status var %x", k)
}
}
return nil
}
// TODO decode QUERY_EVENT status_var
func (event *BinQueryEvent) String() string {
return fmt.Sprintf("%s %s", event.Schema, event.Query)
}
// BinXIDEvent is the definition of XID_EVENT
// https://dev.mysql.com/doc/internals/en/xid-event.html
// Transaction ID for 2PC, written whenever a COMMIT is expected.
type BinXIDEvent struct {
BaseEventBody
XID uint64
}
func decodeXIDEvent(data []byte) (*BinXIDEvent, error) {
return &BinXIDEvent{
XID: binary.LittleEndian.Uint64(data),
}, nil
}
// BinIntvarEvent is the definition of INTVAR_EVENT
// https://dev.mysql.com/doc/internals/en/xid-event.html
// Transaction ID for 2PC, written whenever a COMMIT is expected.
type BinIntvarEvent struct {
BaseEventBody
Type uint8
Value uint64
}
func decodeIntvarEvent(data []byte) (*BinIntvarEvent, error) {
return &BinIntvarEvent{
Type: data[0],
Value: binary.LittleEndian.Uint64(data[1:]),
}, nil
}
// TODO: BinIntvarEvent.Type format
// BinRotateEvent is the definition of ROTATE_EVENT
// https://dev.mysql.com/doc/internals/en/rotate-event.html
// The rotate event is added to the binlog as last event to tell the reader what binlog to request next.
type BinRotateEvent struct {
BaseEventBody
Position uint64
FileName string
}
func decodeRotateEvent(data []byte, binlogVersion int) (*BinRotateEvent, error) {
event := &BinRotateEvent{}
var pos int
if binlogVersion > 1 {
event.Position = binary.LittleEndian.Uint64(data)
pos += 8
}
event.FileName = strings.TrimSpace(string(data[pos:]))
return event, nil
}
// BinPreGTIDsEvent is the definition of PREVIOUS_GTIDS_EVENT
// TODO: PREVIOUS_GTIDS_EVENT
type BinPreGTIDsEvent struct{ BaseEventBody }
// Binlog::Rows_query_log_event - Kurt Larsen
// https://dev.mysql.com/doc/internals/en/rows-query-log-event.html
// The rows_query_log_event is added to the binlog as last event to tell the reader what query was executed.
type BinRowsQueryEvent struct {
BaseEventBody
SlaveProxyID uint32
ExecutionTime uint32
SchemaName string
Query string
}
// Binlog_version_event
// https://dev.mysql.com/doc/internals/en/binlog-version-event.html
// The binlog_version_event is added to the binlog as last event to tell the reader what binlog version was used.
type BinLogVersionEvent struct {
BaseEventBody
Version uint16
}
// Binlog_checksum_event
// https://dev.mysql.com/doc/internals/en/binlog-checksum-event.html
// The binlog_checksum_event is added to the binlog as last event to tell the reader what binlog checksum was used.
type BinLogChecksumEvent struct {
BaseEventBody
Flags uint16
Checksum uint32
}