-
Notifications
You must be signed in to change notification settings - Fork 35
/
binlog_com.go
203 lines (171 loc) · 4.64 KB
/
binlog_com.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
package main
import (
"path/filepath"
//"fmt"
"sync"
"github.com/siddontang/go-mysql/mysql"
"github.com/siddontang/go-mysql/replication"
sliceKits "github.com/toolkits/slice"
)
type BinEventHandlingIndx struct {
EventIdx uint64
lock sync.RWMutex
finished bool
}
var G_HandlingBinEventIndex *BinEventHandlingIndx
type MaxBinEventIdx struct {
MaxEventIdx uint64
lock sync.RWMutex
}
func (this *MaxBinEventIdx) SetMaxBinEventIdx(idx uint64) {
this.lock.Lock()
defer this.lock.Unlock()
this.MaxEventIdx = idx
}
var g_MaxBin_Event_Idx *MaxBinEventIdx
type BinEventDbTableQuery struct {
database string
table string
sql string
}
type MyBinEvent struct {
MyPos mysql.Position //this is the end position
EventIdx uint64
BinEvent *replication.RowsEvent
StartPos uint32 // this is the start position
IfRowsEvent bool
SqlType string // insert, update, delete
Timestamp uint32
TrxIndex uint64
TrxStatus int // 0:begin, 1: commit, 2: rollback, -1: in_progress
}
func CheckBinHeaderCondition(cfg ConfCmd, header *replication.EventHeader, currentBinlog *string) int {
// process: 0, continue: 1, break: 2
myPos := mysql.Position{Name: *currentBinlog, Pos: header.LogPos}
//fmt.Println(cfg.StartFilePos, cfg.IfSetStopFilePos, myPos)
if cfg.IfSetStartFilePos {
cmpRe := myPos.Compare(cfg.StartFilePos)
if cmpRe == -1 {
return RE_CONTINUE
}
}
if cfg.IfSetStopFilePos {
cmpRe := myPos.Compare(cfg.StopFilePos)
if cmpRe == 1 {
return RE_BREAK
}
}
//fmt.Println(cfg.StartDatetime, cfg.StopDatetime, header.Timestamp)
if cfg.IfSetStartDateTime {
if header.Timestamp < cfg.StartDatetime {
return RE_CONTINUE
}
}
if cfg.IfSetStopDateTime {
if header.Timestamp > cfg.StopDatetime {
return RE_BREAK
}
}
if cfg.FilterSqlLen == 0 {
return RE_PROCESS
}
if header.EventType == replication.WRITE_ROWS_EVENTv1 || header.EventType == replication.WRITE_ROWS_EVENTv2 {
if sliceKits.ContainsString(cfg.FilterSql, "insert") {
return RE_PROCESS
} else {
return RE_CONTINUE
}
}
if header.EventType == replication.UPDATE_ROWS_EVENTv1 || header.EventType == replication.UPDATE_ROWS_EVENTv2 {
if sliceKits.ContainsString(cfg.FilterSql, "update") {
return RE_PROCESS
} else {
return RE_CONTINUE
}
}
if header.EventType == replication.DELETE_ROWS_EVENTv1 || header.EventType == replication.DELETE_ROWS_EVENTv2 {
if sliceKits.ContainsString(cfg.FilterSql, "delete") {
return RE_PROCESS
} else {
return RE_CONTINUE
}
}
return RE_PROCESS
}
func (this *MyBinEvent) CheckBinEvent(cfg ConfCmd, ev *replication.BinlogEvent, currentBinlog *string) int {
myPos := mysql.Position{Name: *currentBinlog, Pos: ev.Header.LogPos}
switch ev.Header.EventType {
case replication.ROTATE_EVENT:
rotatEvent := ev.Event.(*replication.RotateEvent)
*currentBinlog = string(rotatEvent.NextLogName)
if cfg.ToLastLog && cfg.Mode == "repl" && cfg.WorkType == "stats" {
return RE_CONTINUE
}
myPos.Name = string(rotatEvent.NextLogName)
myPos.Pos = uint32(rotatEvent.Position)
if cfg.IfSetStartFilePos {
cmpRe := myPos.Compare(cfg.StartFilePos)
if cmpRe == -1 {
return RE_CONTINUE
}
}
if cfg.IfSetStopFilePos {
cmpRe := myPos.Compare(cfg.StopFilePos)
if cmpRe == 1 {
return RE_BREAK
}
}
this.IfRowsEvent = false
case replication.WRITE_ROWS_EVENTv1,
replication.UPDATE_ROWS_EVENTv1,
replication.DELETE_ROWS_EVENTv1,
replication.WRITE_ROWS_EVENTv2,
replication.UPDATE_ROWS_EVENTv2,
replication.DELETE_ROWS_EVENTv2:
//replication.XID_EVENT,
//replication.TABLE_MAP_EVENT:
wrEvent := ev.Event.(*replication.RowsEvent)
db := string(wrEvent.Table.Schema)
tb := string(wrEvent.Table.Table)
if len(cfg.Databases) > 0 {
if !sliceKits.ContainsString(cfg.Databases, db) {
return RE_CONTINUE
}
}
if len(cfg.Tables) > 0 {
if !sliceKits.ContainsString(cfg.Tables, tb) {
return RE_CONTINUE
}
}
this.BinEvent = wrEvent
this.IfRowsEvent = true
case replication.QUERY_EVENT:
this.IfRowsEvent = false
//queryEvent := ev.Event.(*replication.QueryEvent)
//db = string(queryEvent.Schema)
//sql = string(queryEvent.Query)
case replication.XID_EVENT:
this.IfRowsEvent = false
case replication.MARIADB_GTID_EVENT:
this.IfRowsEvent = false
default:
this.IfRowsEvent = false
return RE_CONTINUE
}
return RE_PROCESS
}
func GetFirstBinlogPosToParse(cfg ConfCmd) (string, int64) {
var binlog string
var pos int64
if cfg.StartFile != "" {
binlog = filepath.Join(cfg.BinlogDir, cfg.StartFile)
} else {
binlog = cfg.GivenBinlogFile
}
if cfg.StartPos != 0 {
pos = int64(cfg.StartPos)
} else {
pos = 4
}
return binlog, pos
}