Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(rollback): filter transaction by thread id in stream (#3494)
Yilong Li494b7120bc8f
diff --git a/plugin/db/mysql/binlog_stream_parser.go b/plugin/db/mysql/binlog_stream_parser.goindex ac7887d009..3f6fe1951d 100644--- a/plugin/db/mysql/binlog_stream_parser.go+++ b/plugin/db/mysql/binlog_stream_parser.go@@ -40,7 +40,7 @@ type BinlogEvent struct {type BinlogTransaction []BinlogEvent// ParseBinlogStream splits the mysqlbinlog output stream to a list of transactions.-func ParseBinlogStream(stream io.Reader) ([]BinlogTransaction, error) {+func ParseBinlogStream(stream io.Reader, threadID string) ([]BinlogTransaction, error) {reader := bufio.NewReader(stream)var event BinlogEventvar txns []BinlogTransaction@@ -89,7 +89,13 @@ func ParseBinlogStream(stream io.Reader) ([]BinlogTransaction, error) {} else if event.Type == XidEventType {// The current transaction ends with an Xid event, which means it's a complete transaction.txn = append(txn, event)- txns = append(txns, txn)+ ok, err := filterBinlogTransactionsByThreadID(txn, threadID)+ if err != nil {+ return nil, err+ }+ if ok {+ txns = append(txns, txn)+ }txn = nil} else {// This is a DML event. Append it to the current transaction.@@ -99,7 +105,11 @@ func ParseBinlogStream(stream io.Reader) ([]BinlogTransaction, error) {event = BinlogEvent{}bodyBuf.Reset()if err == io.EOF {- if len(txn) > 0 {+ ok, err := filterBinlogTransactionsByThreadID(txn, threadID)+ if err != nil {+ return nil, err+ }+ if ok {txns = append(txns, txn)}break@@ -109,24 +119,19 @@ func ParseBinlogStream(stream io.Reader) ([]BinlogTransaction, error) {return txns, nil}-// FilterBinlogTransactionsByThreadID filters the binlog transaction by thread ID.-// TODO(dragonly): refactor to streaming process.-func FilterBinlogTransactionsByThreadID(txnList []BinlogTransaction, threadID string) ([]BinlogTransaction, error) {- var ret []BinlogTransaction- for _, txn := range txnList {- event := txn[0]- if event.Type != QueryEventType {- return nil, errors.Errorf("invalid binlog transaction, the first event must be an query event, but got %s", event.Type.String())- }- parsed, err := parseQueryEventThreadID(event.Header)- if err != nil {- return nil, errors.WithMessage(err, "failed to parse thread ID from query event")- }- if parsed == threadID {- ret = append(ret, txn)- }+func filterBinlogTransactionsByThreadID(txn BinlogTransaction, threadID string) (bool, error) {+ if len(txn) == 0 {+ return false, nil+ }+ event := txn[0]+ if event.Type != QueryEventType {+ return false, errors.Errorf("invalid binlog transaction, the first event must be an query event, but got %s", event.Type.String())+ }+ eventThreadID, err := parseQueryEventThreadID(event.Header)+ if err != nil {+ return false, errors.WithMessage(err, "failed to parse thread ID from query event")}- return ret, nil+ return eventThreadID == threadID, nil}var (diff --git a/plugin/db/mysql/binlog_stream_parser_test.go b/plugin/db/mysql/binlog_stream_parser_test.goindex 8749488e1c..a2622ddab5 100644--- a/plugin/db/mysql/binlog_stream_parser_test.go+++ b/plugin/db/mysql/binlog_stream_parser_test.go@@ -366,80 +366,6 @@ BEGIN`,},},- {- {- Type: QueryEventType,- Header: "#221018 16:21:19 server id 1 end_log_pos 1886 CRC32 0xd95a1592 Query thread_id=58599 exec_time=0 error_code=0\n",- Body: `SET TIMESTAMP=1666081279/*!*/;-BEGIN-/*!*/;-`,- },- {- Type: UpdateRowsEventType,- Header: "#221018 16:21:19 server id 1 end_log_pos 2044 CRC32 0x9dbbb766 Update_rows: table id 259 flags: STMT_END_F\n",- Body: `### UPDATE ` + "`binlog_test`.`user`" + `-### WHERE-### @1=1-### @2='alice'-### @3=90-### SET-### @1=1-### @2='alice'-### @3=0-### UPDATE ` + "`binlog_test`.`user`" + `-### WHERE-### @1=2-### @2='bob'-### @3=110-### SET-### @1=2-### @2='bob'-### @3=0-`,- },- {- Type: XidEventType,- Header: "#221018 16:21:19 server id 1 end_log_pos 2075 CRC32 0x64944ac3 Xid = 349592\n",- Body: `COMMIT/*!*/;-`,- },- },- {- {- Type: QueryEventType,- Header: "#221018 16:21:45 server id 1 end_log_pos 2236 CRC32 0x965db1d1 Query thread_id=58599 exec_time=0 error_code=0\n",- Body: `SET TIMESTAMP=1666081305/*!*/;-BEGIN-/*!*/;-`,- },- {- Type: DeleteRowsEventType,- Header: "#221018 16:21:45 server id 1 end_log_pos 2365 CRC32 0xf759c90c Delete_rows: table id 259 flags: STMT_END_F\n",- Body: `### DELETE FROM ` + "`binlog_test`.`user`" + `-### WHERE-### @1=1-### @2='alice'-### @3=0-### DELETE FROM ` + "`binlog_test`.`user`" + `-### WHERE-### @1=2-### @2='bob'-### @3=0-`,- },- {- Type: XidEventType,- Header: "#221018 16:21:45 server id 1 end_log_pos 2396 CRC32 0x816695ae Xid = 349604\n",- Body: `COMMIT/*!*/;-SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;-DELIMITER ;-# End of log file-/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;-/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;`,- },- },},err: false,},@@ -450,7 +376,7 @@ DELIMITER ;test := testt.Run(test.name, func(t *testing.T) {a := require.New(t)- txns, err := ParseBinlogStream(strings.NewReader(test.stream))+ txns, err := ParseBinlogStream(strings.NewReader(test.stream), "53771")if test.err {a.Error(err)} else {@@ -464,9 +390,9 @@ DELIMITER ;func TestFilterBinlogTransactionsByThreadID(t *testing.T) {tests := []struct {name string- txnList []BinlogTransaction+ txn BinlogTransactionthreadID string- want []BinlogTransaction+ ok boolerr bool}{{@@ -475,12 +401,11 @@ func TestFilterBinlogTransactionsByThreadID(t *testing.T) {},{name: "invalid transaction",- txnList: []BinlogTransaction{+ txn: BinlogTransaction{{- {- Type: UpdateRowsEventType,- Header: "#221017 14:25:53 server id 1 end_log_pos 1249 CRC32 0x3d8fa43e Update_rows: table id 259 flags: STMT_END_F\n",- Body: `### UPDATE ` + "`binlog_test`.`user`" + `+ Type: UpdateRowsEventType,+ Header: "#221017 14:25:53 server id 1 end_log_pos 1249 CRC32 0x3d8fa43e Update_rows: table id 259 flags: STMT_END_F\n",+ Body: `### UPDATE ` + "`binlog_test`.`user`" + `### WHERE### @1=1### @2='alice'@@ -489,27 +414,27 @@ func TestFilterBinlogTransactionsByThreadID(t *testing.T) {### @1=1### @2='alice'### @3=90`,- },},},+ ok: false,err: true,},{- name: "real world",- txnList: []BinlogTransaction{+ name: "real world, ok",+ txn: BinlogTransaction{+{- {- Type: QueryEventType,- Header: "#221017 14:25:24 server id 1 end_log_pos 772 CRC32 0x37cb53f6 Query thread_id=53771 exec_time=0 error_code=0\n",- Body: `SET TIMESTAMP=1665987924/*!*/;+ Type: QueryEventType,+ Header: "#221017 14:25:24 server id 1 end_log_pos 772 CRC32 0x37cb53f6 Query thread_id=53771 exec_time=0 error_code=0\n",+ Body: `SET TIMESTAMP=1665987924/*!*/;BEGIN/*!*/;`,- },- {- Type: WriteRowsEventType,- Header: "#221017 14:25:24 server id 1 end_log_pos 916 CRC32 0x896854fc Write_rows: table id 259 flags: STMT_END_F\n",- Body: `### INSERT INTO ` + "`binlog_test`.`user`" + `+ },+ {+ Type: WriteRowsEventType,+ Header: "#221017 14:25:24 server id 1 end_log_pos 916 CRC32 0x896854fc Write_rows: table id 259 flags: STMT_END_F\n",+ Body: `### INSERT INTO ` + "`binlog_test`.`user`" + `### SET### @1=1### @2='alice'@@ -524,27 +449,33 @@ func TestFilterBinlogTransactionsByThreadID(t *testing.T) {### @1=3### @2='cindy'### @3=100`,- },- {- Type: XidEventType,- Header: "#221017 14:25:24 server id 1 end_log_pos 947 CRC32 0xaf8e8303 Xid = 327602\n",- Body: `COMMIT/*!*/;+ },+ {+ Type: XidEventType,+ Header: "#221017 14:25:24 server id 1 end_log_pos 947 CRC32 0xaf8e8303 Xid = 327602\n",+ Body: `COMMIT/*!*/;`,- },},+ },+ threadID: "53771",+ ok: true,+ err: false,+ },+ {+ name: "real world, thread id not match",+ txn: BinlogTransaction{{- {- Type: QueryEventType,- Header: "#221018 16:21:45 server id 1 end_log_pos 2236 CRC32 0x965db1d1 Query thread_id=58599 exec_time=0 error_code=0\n",- Body: `SET TIMESTAMP=1666081305/*!*/;+ Type: QueryEventType,+ Header: "#221018 16:21:45 server id 1 end_log_pos 2236 CRC32 0x965db1d1 Query thread_id=58599 exec_time=0 error_code=0\n",+ Body: `SET TIMESTAMP=1666081305/*!*/;BEGIN/*!*/;`,- },- {- Type: DeleteRowsEventType,- Header: "#221018 16:21:45 server id 1 end_log_pos 2365 CRC32 0xf759c90c Delete_rows: table id 259 flags: STMT_END_F\n",- Body: `### DELETE FROM ` + "`binlog_test`.`user`" + `+ },+ {+ Type: DeleteRowsEventType,+ Header: "#221018 16:21:45 server id 1 end_log_pos 2365 CRC32 0xf759c90c Delete_rows: table id 259 flags: STMT_END_F\n",+ Body: `### DELETE FROM ` + "`binlog_test`.`user`" + `### WHERE### @1=1### @2='alice'@@ -554,58 +485,21 @@ func TestFilterBinlogTransactionsByThreadID(t *testing.T) {### @1=2### @2='bob'### @3=0`,- },- {- Type: XidEventType,- Header: "#221018 16:21:45 server id 1 end_log_pos 2396 CRC32 0x816695ae Xid = 349604\n",- Body: `COMMIT/*!*/;+ },+ {+ Type: XidEventType,+ Header: "#221018 16:21:45 server id 1 end_log_pos 2396 CRC32 0x816695ae Xid = 349604\n",+ Body: `COMMIT/*!*/;SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;DELIMITER ;# End of log file/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;`,- },},},threadID: "53771",- want: []BinlogTransaction{- {- {- Type: QueryEventType,- Header: "#221017 14:25:24 server id 1 end_log_pos 772 CRC32 0x37cb53f6 Query thread_id=53771 exec_time=0 error_code=0\n",- Body: `SET TIMESTAMP=1665987924/*!*/;- BEGIN- /*!*/;- `,- },- {- Type: WriteRowsEventType,- Header: "#221017 14:25:24 server id 1 end_log_pos 916 CRC32 0x896854fc Write_rows: table id 259 flags: STMT_END_F\n",- Body: `### INSERT INTO ` + "`binlog_test`.`user`" + `- ### SET- ### @1=1- ### @2='alice'- ### @3=100- ### INSERT INTO ` + "`binlog_test`.`user`" + `- ### SET- ### @1=2- ### @2='bob'- ### @3=100- ### INSERT INTO ` + "`binlog_test`.`user`" + `- ### SET- ### @1=3- ### @2='cindy'- ### @3=100`,- },- {- Type: XidEventType,- Header: "#221017 14:25:24 server id 1 end_log_pos 947 CRC32 0xaf8e8303 Xid = 327602\n",- Body: `COMMIT/*!*/;- `,- },- },- },- err: false,+ ok: false,+ err: false,},}@@ -614,7 +508,8 @@ func TestFilterBinlogTransactionsByThreadID(t *testing.T) {test := testt.Run(test.name, func(t *testing.T) {a := require.New(t)- _, err := FilterBinlogTransactionsByThreadID(test.txnList, test.threadID)+ ok, err := filterBinlogTransactionsByThreadID(test.txn, test.threadID)+ a.Equal(test.ok, ok)if test.err {a.Error(err)} else {diff --git a/plugin/db/mysql/rollback.go b/plugin/db/mysql/rollback.goindex 2f11589148..5833b56b51 100644--- a/plugin/db/mysql/rollback.go+++ b/plugin/db/mysql/rollback.go@@ -97,16 +97,11 @@ func (driver *Driver) GenerateRollbackSQL(ctx context.Context, binlogFileNameLisreturn "", errors.Wrap(err, "failed to run mysqlbinlog")}- txnList, err := ParseBinlogStream(pr)+ txnList, err := ParseBinlogStream(pr, threadID)if err != nil {return "", errors.WithMessage(err, "failed to parse binlog stream")}- txnList, err = FilterBinlogTransactionsByThreadID(txnList, threadID)- if err != nil {- return "", errors.WithMessage(err, "failed to filter binlog transactions by thread ID")- }-var rollbackSQLList []stringfor i := len(txnList) - 1; i >= 0; i-- {sql, err := txnList[i].GetRollbackSQL(tableCatalog)