Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binlog-filter: add new event type #655

Merged
merged 15 commits into from
Jul 13, 2022
27 changes: 20 additions & 7 deletions pkg/binlog-filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const (

CreateDatabase EventType = "create database"
DropDatabase EventType = "drop database"
AlterDatabase EventType = "alter database"
CreateTable EventType = "create table"
DropTable EventType = "drop table"
TruncateTable EventType = "truncate table"
Expand All @@ -63,9 +64,16 @@ const (
DropIndex EventType = "drop index"
CreateView EventType = "create view"
DropView EventType = "drop view"
AlertTable EventType = "alter table"
AlterTable EventType = "alter table"

CreateSchema EventType = "create schema" // alias of CreateDatabase
DropSchema EventType = "drop schema" // alias of DropDatabase
asddongmen marked this conversation as resolved.
Show resolved Hide resolved
AlterSchema EventType = "alter schema" // alias of AlterDatabase
AddIndex EventType = "add index" // alias of CreateIndex
// if need, add more AlertTableOption = "alert table option"

// NullEvent is used to represents unsupported ddl event type when we
// convert a ast.StmtNode or a string to EventType.
NullEvent EventType = ""
)

Expand All @@ -74,13 +82,16 @@ func ClassifyEvent(event EventType) (EventType, error) {
switch event {
case InsertEvent, UpdateEvent, DeleteEvent:
return dml, nil
case CreateDatabase, DropDatabase, CreateTable, DropTable, TruncateTable, RenameTable,
CreateIndex, DropIndex, CreateView, DropView, AlertTable:
case CreateDatabase, DropDatabase, CreateTable,
DropTable, TruncateTable, RenameTable,
CreateIndex, DropIndex, CreateView,
DropView, AlterTable,
CreateSchema, DropSchema, AddIndex:
return ddl, nil
case NullEvent:
return NullEvent, nil
default:
return NoneEvent, errors.NotValidf("event type %s", event)
return NullEvent, errors.NotValidf("event type %s", event)
}
}

Expand Down Expand Up @@ -115,11 +126,13 @@ func (b *BinlogEventRule) Valid() error {
return errors.Errorf("action of binlog event rule %+v should not be empty", b)
}

// TODO: check validity of dml/ddl event.
for i := range b.Events {
b.Events[i] = EventType(strings.ToLower(string(b.Events[i])))
et, err := toEventType(string(b.Events[i]))
if err != nil {
return errors.NotValidf("event type %s", b.Events[i])
}
b.Events[i] = et
}

return nil
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/binlog-filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/errors"
selector "github.com/pingcap/tidb-tools/pkg/table-rule-selector"
)

Expand Down Expand Up @@ -125,6 +126,7 @@ func (t *testFilterSuite) TestFilter(c *C) {

// mismatched
action, err := filter.Filter("xxx_a", "", InsertEvent, "")
c.Assert(err, IsNil)
c.Assert(action, Equals, Do)

// invalid rule
Expand Down Expand Up @@ -245,3 +247,39 @@ func (t *testFilterSuite) TestGlobalFilter(c *C) {
c.Assert(action, Equals, cs.action)
}
}

func (t *testFilterSuite) TestToEventType(c *C) {
cases := []struct {
eventStr string
event EventType
err error
}{
{"", NullEvent, nil},
{"insert", InsertEvent, nil},
asddongmen marked this conversation as resolved.
Show resolved Hide resolved
{"Insert", InsertEvent, nil},
{"update", UpdateEvent, nil},
{"UPDATE", UpdateEvent, nil},
{"delete", DeleteEvent, nil},
{"create", NullEvent, errors.NotValidf("event type %s", "create")},
{"create schema", CreateDatabase, nil},
{"create SCHEMA", CreateDatabase, nil},
{"create database", CreateDatabase, nil},
{"drop schema", DropDatabase, nil},
{"drop Schema", DropDatabase, nil},
{"drop database", DropDatabase, nil},
{"alter database", AlterDatabase, nil},
{"alter schema", AlterDatabase, nil},
{"add index", CreateIndex, nil},
{"create index", CreateIndex, nil},
{"xxx", NullEvent, errors.NotValidf("event type %s", "xxx")},
{"I don't know", NullEvent, errors.NotValidf("event type %s", "I don't know")},
}

for _, cs := range cases {
event, err := toEventType(cs.eventStr)
c.Assert(cs.event, Equals, event)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when err == nil, should also check cs.err is also nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

c.Assert(cs.err.Error(), Equals, err.Error())
}
}
}
51 changes: 49 additions & 2 deletions pkg/binlog-filter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

package filter

import "github.com/pingcap/tidb/parser/ast"
import (
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/parser/ast"
)

// AstToDDLEvent returns filter.DDLEvent
func AstToDDLEvent(node ast.StmtNode) EventType {
Expand All @@ -38,10 +43,52 @@ func AstToDDLEvent(node ast.StmtNode) EventType {
case *ast.DropIndexStmt:
return DropIndex
case *ast.AlterTableStmt:
return AlertTable
return AlterTable
case *ast.CreateViewStmt:
return CreateView
case *ast.AlterDatabaseStmt:
asddongmen marked this conversation as resolved.
Show resolved Hide resolved
return AlterDatabase
}

return NullEvent
}

// toEventType converts event type string to EventType and check if it is valid.
func toEventType(es string) (EventType, error) {
event := EventType(strings.ToLower(es))
switch event {
case AllEvent,
AllDDL,
AllDML,
NullEvent,
NoneEvent,
NoneDDL,
NoneDML,
InsertEvent,
UpdateEvent,
DeleteEvent,
CreateDatabase,
DropDatabase,
AlterDatabase,
CreateTable,
DropTable,
TruncateTable,
RenameTable,
CreateIndex,
DropIndex,
CreateView,
DropView,
AlterTable:
return event, nil
case CreateSchema: // alias of CreateDatabase
return CreateDatabase, nil
case DropSchema: // alias of DropDatabase
return DropDatabase, nil
case AddIndex: // alias of CreateIndex
return CreateIndex, nil
case AlterSchema:
return AlterDatabase, nil
default:
return NullEvent, errors.NotValidf("event type %s", es)
}
}