-
Notifications
You must be signed in to change notification settings - Fork 131
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
pb: update pb parser to avoid drainer failure (#1093) #1123
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,16 @@ import ( | |
"fmt" | ||
|
||
"github.com/pingcap/check" | ||
<<<<<<< HEAD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 'STRING', found '<<' (and 9 more errors) |
||
"github.com/pingcap/parser/mysql" | ||
pb "github.com/pingcap/tidb-binlog/proto/binlog" | ||
======= | ||
"github.com/pingcap/tidb/parser/mysql" | ||
>>>>>>> 194d4ac1 (pb: update pb parser to avoid drainer failure (#1093)) | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/codec" | ||
|
||
pb "github.com/pingcap/tidb-binlog/proto/binlog" | ||
) | ||
|
||
type testPbSuite struct { | ||
|
@@ -55,6 +61,32 @@ func (t *testPbSuite) TestDDL(c *check.C) { | |
CommitTs: t.TiBinlog.GetCommitTs(), | ||
DdlQuery: []byte(expected), | ||
}) | ||
|
||
// test PB shouldn't reports an error when meeting unsupported DDL | ||
t.TiBinlog.DdlQuery = []byte("invalid sql") | ||
pbBinog, err = TiBinlogToPbBinlog(t, t.Schema, t.Table, t.TiBinlog, nil) | ||
c.Assert(err, check.IsNil) | ||
|
||
c.Log("get ddl: ", string(pbBinog.GetDdlQuery())) | ||
expected = fmt.Sprintf("use `%s`; %s;", t.Schema, string(t.TiBinlog.GetDdlQuery())) | ||
c.Assert(pbBinog, check.DeepEquals, &pb.Binlog{ | ||
Tp: pb.BinlogType_DDL, | ||
CommitTs: t.TiBinlog.GetCommitTs(), | ||
DdlQuery: []byte(expected), | ||
}) | ||
|
||
// test PB shouldn't reports an error when meeting unsupported DDL | ||
t.TiBinlog.DdlQuery = []byte("invalid sql") | ||
t.Schema = "" | ||
pbBinog, err = TiBinlogToPbBinlog(t, t.Schema, t.Table, t.TiBinlog, nil) | ||
c.Assert(err, check.IsNil) | ||
|
||
c.Log("get ddl: ", string(pbBinog.GetDdlQuery())) | ||
c.Assert(pbBinog, check.DeepEquals, &pb.Binlog{ | ||
Tp: pb.BinlogType_DDL, | ||
CommitTs: t.TiBinlog.GetCommitTs(), | ||
DdlQuery: t.TiBinlog.GetDdlQuery(), | ||
}) | ||
} | ||
|
||
func (t *testPbSuite) testDML(c *check.C, tp pb.EventType) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,15 +19,22 @@ import ( | |
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/log" | ||
<<<<<<< HEAD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 'STRING', found '<<' (and 10 more errors) |
||
"github.com/pingcap/parser" | ||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/parser/mysql" | ||
"github.com/pingcap/tidb-binlog/pkg/util" | ||
======= | ||
"github.com/pingcap/tidb/parser/model" | ||
"github.com/pingcap/tidb/parser/mysql" | ||
>>>>>>> 194d4ac1 (pb: update pb parser to avoid drainer failure (#1093)) | ||
"github.com/pingcap/tidb/table" | ||
"github.com/pingcap/tidb/tablecodec" | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/codec" | ||
"go.uber.org/zap" | ||
|
||
"github.com/pingcap/tidb-binlog/pkg/util" | ||
) | ||
|
||
var sqlMode mysql.SQLMode | ||
|
@@ -37,13 +44,6 @@ func SetSQLMode(mode mysql.SQLMode) { | |
sqlMode = mode | ||
} | ||
|
||
func getParser() (p *parser.Parser) { | ||
p = parser.New() | ||
p.SetSQLMode(sqlMode) | ||
|
||
return | ||
} | ||
|
||
func insertRowToDatums(table *model.TableInfo, row []byte) (datums map[int64]types.Datum, err error) { | ||
colsTypeMap := util.ToColumnTypeMap(table.Columns) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,14 +26,28 @@ import ( | |
"github.com/BurntSushi/toml" | ||
"github.com/pingcap/errors" | ||
"github.com/pingcap/log" | ||
<<<<<<< HEAD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 'STRING', found '<<' (and 10 more errors) |
||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/tidb-binlog/pkg/flags" | ||
"github.com/pingcap/tidb-binlog/pkg/security" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/store/tikv/oracle" | ||
"github.com/pingcap/tidb/types" | ||
======= | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/parser" | ||
"github.com/pingcap/tidb/parser/ast" | ||
"github.com/pingcap/tidb/parser/model" | ||
"github.com/pingcap/tidb/parser/mysql" | ||
"github.com/pingcap/tidb/types" | ||
_ "github.com/pingcap/tidb/types/parser_driver" // for parser driver | ||
"github.com/tikv/client-go/v2/oracle" | ||
>>>>>>> 194d4ac1 (pb: update pb parser to avoid drainer failure (#1093)) | ||
pd "github.com/tikv/pd/client" | ||
"go.uber.org/zap" | ||
|
||
"github.com/pingcap/tidb-binlog/pkg/flags" | ||
"github.com/pingcap/tidb-binlog/pkg/security" | ||
) | ||
|
||
// DefaultIP get a default non local ip, err is not nil, ip return 127.0.0.1 | ||
|
@@ -333,3 +347,17 @@ func WriteFileAtomic(filename string, data []byte, perm os.FileMode) error { | |
|
||
return os.Rename(f.Name(), filename) | ||
} | ||
|
||
// IsCreateDatabaseDDL checks whether ddl is a create database statement | ||
func IsCreateDatabaseDDL(sql string, sqlMode mysql.SQLMode) bool { | ||
p := parser.New() | ||
p.SetSQLMode(sqlMode) | ||
stmt, err := p.ParseOneStmt(sql, "", "") | ||
if err != nil { | ||
log.Error("parse sql failed", zap.String("sql", sql), zap.Error(err)) | ||
return false | ||
} | ||
|
||
_, isCreateDatabase := stmt.(*ast.CreateDatabaseStmt) | ||
return isCreateDatabase | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,18 @@ import ( | |
|
||
. "github.com/pingcap/check" | ||
"github.com/pingcap/log" | ||
<<<<<<< HEAD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 'STRING', found '<<' (and 10 more errors) |
||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/parser/mysql" | ||
"github.com/pingcap/tidb-binlog/pkg/security" | ||
======= | ||
>>>>>>> 194d4ac1 (pb: update pb parser to avoid drainer failure (#1093)) | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/types" | ||
pd "github.com/tikv/pd/client" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/pingcap/tidb-binlog/pkg/security" | ||
) | ||
|
||
// Hook up gocheck into the "go test" runner. | ||
|
@@ -340,3 +345,22 @@ func (s *waitUntilTimeoutSuit) TestGoAndAbortGoroutine(c *C) { | |
}, time.Second) | ||
c.Assert(called, IsTrue) | ||
} | ||
|
||
type isCreateDBDDLSuite struct{} | ||
|
||
var _ = Suite(&isCreateDBDDLSuite{}) | ||
|
||
func (s *isCreateDBDDLSuite) TestInvalidSQL(c *C) { | ||
c.Assert(IsCreateDatabaseDDL("INSERT INTO Y a b c;", mysql.ModeNone), IsFalse) | ||
c.Assert(IsCreateDatabaseDDL("Invalid SQL", mysql.ModeNone), IsFalse) | ||
} | ||
|
||
func (s *isCreateDBDDLSuite) TestNonCreateDBSQL(c *C) { | ||
c.Assert(IsCreateDatabaseDDL("SELECT 1;", mysql.ModeNone), IsFalse) | ||
c.Assert(IsCreateDatabaseDDL(`INSERT INTO tbl(id, name) VALUES(1, "test";`, mysql.ModeNone), IsFalse) | ||
} | ||
|
||
func (s *isCreateDBDDLSuite) TestCreateDBSQL(c *C) { | ||
c.Assert(IsCreateDatabaseDDL("CREATE DATABASE test;", mysql.ModeNone), IsTrue) | ||
c.Assert(IsCreateDatabaseDDL("create database `db2`;", mysql.ModeNone), IsTrue) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 'STRING', found '<<' (and 10 more errors)