Skip to content

Commit 014dcd7

Browse files
gaojijunJijun Gaolance6716
authored
Fix no table is replicated when excludeTableRegex is set while includeTableRegex is nil (#874)
* Fix bug when excludeTableRegex is set while includeTableRegex is nil * add TestIncludeExcludeTableRegex * fix typo * Fix TestIncludeExcludeTableRegex * improve IncludeTableRegex and ExcludeTableRegex comment --------- Co-authored-by: Jijun Gao <jjgao@clustertech.com> Co-authored-by: lance6716 <lance6716@gmail.com>
1 parent 79f480a commit 014dcd7

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

canal/canal.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,20 @@ func NewCanal(cfg *Config) (*Canal, error) {
9999
return nil, errors.Trace(err)
100100
}
101101

102-
// init table filter
102+
if err := c.initTableFilter(); err != nil {
103+
return nil, errors.Trace(err)
104+
}
105+
106+
return c, nil
107+
}
108+
109+
func (c *Canal) initTableFilter() error {
103110
if n := len(c.cfg.IncludeTableRegex); n > 0 {
104111
c.includeTableRegex = make([]*regexp.Regexp, n)
105112
for i, val := range c.cfg.IncludeTableRegex {
106113
reg, err := regexp.Compile(val)
107114
if err != nil {
108-
return nil, errors.Trace(err)
115+
return errors.Trace(err)
109116
}
110117
c.includeTableRegex[i] = reg
111118
}
@@ -116,7 +123,7 @@ func NewCanal(cfg *Config) (*Canal, error) {
116123
for i, val := range c.cfg.ExcludeTableRegex {
117124
reg, err := regexp.Compile(val)
118125
if err != nil {
119-
return nil, errors.Trace(err)
126+
return errors.Trace(err)
120127
}
121128
c.excludeTableRegex[i] = reg
122129
}
@@ -125,8 +132,7 @@ func NewCanal(cfg *Config) (*Canal, error) {
125132
if c.includeTableRegex != nil || c.excludeTableRegex != nil {
126133
c.tableMatchCache = make(map[string]bool)
127134
}
128-
129-
return c, nil
135+
return nil
130136
}
131137

132138
func (c *Canal) prepareDumper() error {
@@ -143,7 +149,7 @@ func (c *Canal) prepareDumper() error {
143149
}
144150

145151
if c.dumper == nil {
146-
//no mysqldump, use binlog only
152+
// no mysqldump, use binlog only
147153
return nil
148154
}
149155

@@ -293,7 +299,10 @@ func (c *Canal) checkTableMatch(key string) bool {
293299
break
294300
}
295301
}
302+
} else {
303+
matchFlag = true
296304
}
305+
297306
// check exclude
298307
if matchFlag && c.excludeTableRegex != nil {
299308
for _, reg := range c.excludeTableRegex {

canal/canal_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,29 @@ func TestDropIndexExp(t *testing.T) {
405405
}
406406
}
407407
}
408+
409+
func TestIncludeExcludeTableRegex(t *testing.T) {
410+
cfg := NewDefaultConfig()
411+
412+
// include & exclude config
413+
cfg.IncludeTableRegex = make([]string, 1)
414+
cfg.IncludeTableRegex[0] = ".*\\.canal_test"
415+
cfg.ExcludeTableRegex = make([]string, 2)
416+
cfg.ExcludeTableRegex[0] = "mysql\\..*"
417+
cfg.ExcludeTableRegex[1] = ".*\\..*_inner"
418+
419+
c := new(Canal)
420+
c.cfg = cfg
421+
require.Nil(t, c.initTableFilter())
422+
require.True(t, c.checkTableMatch("test.canal_test"))
423+
require.False(t, c.checkTableMatch("test.canal_test_inner"))
424+
require.False(t, c.checkTableMatch("mysql.canal_test_inner"))
425+
426+
cfg.IncludeTableRegex = nil
427+
c = new(Canal)
428+
c.cfg = cfg
429+
require.Nil(t, c.initTableFilter())
430+
require.True(t, c.checkTableMatch("test.canal_test"))
431+
require.False(t, c.checkTableMatch("test.canal_test_inner"))
432+
require.False(t, c.checkTableMatch("mysql.canal_test_inner"))
433+
}

canal/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ type Config struct {
6161
HeartbeatPeriod time.Duration `toml:"heartbeat_period"`
6262
ReadTimeout time.Duration `toml:"read_timeout"`
6363

64-
// IncludeTableRegex or ExcludeTableRegex should contain database name
64+
// IncludeTableRegex or ExcludeTableRegex should contain database name.
65+
// IncludeTableRegex defines the tables that will be included, if empty, all tables will be included.
66+
// ExcludeTableRegex defines the tables that will be excluded from the ones defined by IncludeTableRegex.
6567
// Only a table which matches IncludeTableRegex and dismatches ExcludeTableRegex will be processed
6668
// eg, IncludeTableRegex : [".*\\.canal"], ExcludeTableRegex : ["mysql\\..*"]
67-
// this will include all database's 'canal' table, except database 'mysql'
69+
// this will include all database's 'canal' table, except database 'mysql'.
6870
// Default IncludeTableRegex and ExcludeTableRegex are empty, this will include all tables
6971
IncludeTableRegex []string `toml:"include_table_regex"`
7072
ExcludeTableRegex []string `toml:"exclude_table_regex"`

0 commit comments

Comments
 (0)