-
Notifications
You must be signed in to change notification settings - Fork 194
/
filter.go
303 lines (252 loc) · 7.08 KB
/
filter.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
// Copyright 2018 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package filter
import (
"regexp"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/tidb-tools/pkg/table-rule-selector"
)
// ActionType indicates how to handle matched items
type ActionType string
// show that how to handle rules
const (
Ignore ActionType = "Ignore"
Do ActionType = "Do"
)
// EventType is DML/DDL Event type
type EventType string
// show DML/DDL Events
const (
ddl EventType = "ddl"
dml EventType = "dml"
// it indicates all dml/ddl events in rule
AllEvent EventType = "all"
AllDDL EventType = "all ddl"
AllDML EventType = "all dml"
// it indicates no any dml/ddl events in rule,
// and equals empty rule.DDLEvent/DMLEvent
NoneEvent EventType = "none"
NoneDDL EventType = "none ddl"
NoneDML EventType = "none dml"
InsertEvent EventType = "insert"
UpdateEvent EventType = "update"
DeleteEvent EventType = "delete"
CreateDatabase EventType = "create database"
DropDatabase EventType = "drop database"
CreateTable EventType = "create table"
DropTable EventType = "drop table"
TruncateTable EventType = "truncate table"
RenameTable EventType = "rename table"
CreateIndex EventType = "create index"
DropIndex EventType = "drop index"
AlertTable EventType = "alter table"
// if need, add more AlertTableOption = "alert table option"
NullEvent EventType = ""
)
// ClassifyEvent classify event into dml/ddl
func ClassifyEvent(event EventType) (EventType, error) {
switch event {
case InsertEvent, UpdateEvent, DeleteEvent:
return dml, nil
case CreateDatabase, DropDatabase, CreateTable, DropTable, TruncateTable, RenameTable, CreateIndex, DropIndex, AlertTable:
return ddl, nil
case NullEvent:
return NullEvent, nil
default:
return NoneEvent, errors.NotValidf("event type %s", event)
}
}
// BinlogEventRule is a rule to filter binlog events
type BinlogEventRule struct {
SchemaPattern string `json:"schema-pattern" toml:"schema-pattern" yaml:"schema-pattern"`
TablePattern string `json:"table-pattern" toml:"table-pattern" yaml:"table-pattern"`
Events []EventType `json:"events" toml:"events" yaml:"events"`
SQLPattern []string `json:"sql-pattern" toml:"sql-pattern" yaml:"sql-pattern"` // regular expression
sqlRegularExp *regexp.Regexp
Action ActionType `json:"action" toml:"action" yaml:"action"`
}
// ToLower covert schema/table pattern to lower case
func (b *BinlogEventRule) ToLower() {
b.SchemaPattern = strings.ToLower(b.SchemaPattern)
b.TablePattern = strings.ToLower(b.TablePattern)
}
// Valid checks validity of rule.
// TODO: check validity of dml/ddl event.
func (b *BinlogEventRule) Valid() error {
if len(b.SQLPattern) > 0 {
reg, err := regexp.Compile("(?i)" + strings.Join(b.SQLPattern, "|"))
if err != nil {
return errors.Annotatef(err, "compile regular expression %+v", b.SQLPattern)
}
b.sqlRegularExp = reg
}
if b.Action != Do && b.Action != Ignore {
return errors.Errorf("action of binlog event rule %+v should not be empty", b)
}
return nil
}
// BinlogEvent filters binlog events by given rules
type BinlogEvent struct {
selector.Selector
caseSensitive bool
}
// NewBinlogEvent returns a binlog event filter
func NewBinlogEvent(caseSensitive bool, rules []*BinlogEventRule) (*BinlogEvent, error) {
b := &BinlogEvent{
Selector: selector.NewTrieSelector(),
caseSensitive: caseSensitive,
}
for _, rule := range rules {
if err := b.AddRule(rule); err != nil {
return nil, errors.Annotatef(err, "initial rule %+v in binlog event filter", rule)
}
}
return b, nil
}
// AddRule adds a rule into binlog event filter
func (b *BinlogEvent) AddRule(rule *BinlogEventRule) error {
if b == nil || rule == nil {
return nil
}
err := rule.Valid()
if err != nil {
return errors.Trace(err)
}
if !b.caseSensitive {
rule.ToLower()
}
err = b.Insert(rule.SchemaPattern, rule.TablePattern, rule, false)
if err != nil {
return errors.Annotatef(err, "add rule %+v into binlog event filter", rule)
}
return nil
}
// UpdateRule updates binlog event filter rule
func (b *BinlogEvent) UpdateRule(rule *BinlogEventRule) error {
if b == nil || rule == nil {
return nil
}
err := rule.Valid()
if err != nil {
return errors.Trace(err)
}
if !b.caseSensitive {
rule.ToLower()
}
err = b.Insert(rule.SchemaPattern, rule.TablePattern, rule, true)
if err != nil {
return errors.Annotatef(err, "update rule %+v into binlog event filter", rule)
}
return nil
}
// RemoveRule removes a rule from binlog event filter
func (b *BinlogEvent) RemoveRule(rule *BinlogEventRule) error {
if b == nil || rule == nil {
return nil
}
if !b.caseSensitive {
rule.ToLower()
}
err := b.Remove(rule.SchemaPattern, rule.TablePattern)
if err != nil {
return errors.Annotatef(err, "remove rule %+v", rule)
}
return nil
}
// Filter filters events or queries by given rules
// returns action and error
func (b *BinlogEvent) Filter(schema, table string, event EventType, rawQuery string) (ActionType, error) {
if b == nil {
return Do, nil
}
tp, err := ClassifyEvent(event)
if err != nil {
return Ignore, errors.Trace(err)
}
schemaL, tableL := schema, table
if !b.caseSensitive {
schemaL, tableL = strings.ToLower(schema), strings.ToLower(table)
}
rules := b.Match(schemaL, tableL)
if len(rules) == 0 {
return Do, nil
}
for _, rule := range rules {
binlogEventRule, ok := rule.(*BinlogEventRule)
if !ok {
return "", errors.NotValidf("rule %+v", rule)
}
if tp != NullEvent {
matched := b.matchEvent(tp, event, binlogEventRule.Events)
// ignore has highest priority
if matched {
if binlogEventRule.Action == Ignore {
return Ignore, nil
}
} else {
if binlogEventRule.Action == Do {
return Ignore, nil
}
}
}
if len(rawQuery) > 0 {
if len(binlogEventRule.SQLPattern) == 0 {
// sql pattern is disabled , just continue
continue
}
matched := binlogEventRule.sqlRegularExp.FindStringIndex(rawQuery) != nil
// ignore has highest priority
if matched {
if binlogEventRule.Action == Ignore {
return Ignore, nil
}
} else {
if binlogEventRule.Action == Do {
return Ignore, nil
}
}
}
}
return Do, nil
}
func (b *BinlogEvent) matchEvent(tp, event EventType, rules []EventType) bool {
for _, rule := range rules {
if rule == AllEvent {
return true
}
if rule == NoneEvent {
return false
}
if tp == ddl {
if rule == AllDDL {
return true
}
if rule == NoneDDL {
return false
}
}
if tp == dml {
if rule == AllDML {
return true
}
if rule == NoneDML {
return false
}
}
if rule == event {
return true
}
}
return false
}