-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_previous_gtid.go
78 lines (67 loc) · 1.61 KB
/
get_previous_gtid.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
package mysql_binlog_utils
import (
"encoding/binary"
"io"
"os"
"strconv"
)
func GetPreviousGtids(binlogPath string) (gtidDesc string, err error) {
file, err := os.Open(binlogPath)
if nil != err {
return "", err
}
defer file.Close()
p := int64(4)
headerBs := make([]byte, 19)
payloadBs := make([]byte, 1024)
for {
if _, err := file.Seek(p, 0); nil != err {
if "EOF" == err.Error() {
break
}
return gtidDesc, err
}
if _, err := io.ReadFull(file, headerBs); nil != err {
if "EOF" == err.Error() {
break
}
return gtidDesc, err
}
length := binary.LittleEndian.Uint32(headerBs[9:13])
eventType := int(headerBs[4])
if PREVIOUS_GTIDS_LOG_EVENT != eventType {
p += int64(length)
continue
}
payloadLength := length - 19
if payloadLength > uint32(len(payloadBs)) {
payloadBs = make([]byte, payloadLength)
}
if _, err := io.ReadFull(file, payloadBs[:payloadLength]); nil != err {
if "EOF" == err.Error() {
break
}
return gtidDesc, err
}
ret := ""
sidNumberCount := bytesToUint(payloadBs[0:8])
pos := 8
for i := uint(0); i < sidNumberCount; i++ {
if "" != ret {
ret = ret + ","
}
uuid := bytesToUuid(payloadBs[pos : pos+16])
ret = ret + uuid
internalCount := bytesToUint(payloadBs[pos+16 : pos+16+8])
pos = pos + 16 + 8
for i := uint(0); i < internalCount; i++ {
from := bytesToUint64(payloadBs[pos : pos+8])
to := bytesToUint64(payloadBs[pos+8:pos+16]) - 1
pos = pos + 16
ret = ret + ":" + strconv.FormatUint(from, 10) + "-" + strconv.FormatUint(to, 10)
}
}
return ret, nil
}
return gtidDesc, nil
}