Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(flashback): filter binlog transactions by thread ID (#3310)

Yilong Li646ee0f910d7
diff --git a/plugin/db/mysql/binlog_stream_parser.go b/plugin/db/mysql/binlog_stream_parser.go
index 63bbbe152b..0ec7f1315c 100644
--- a/plugin/db/mysql/binlog_stream_parser.go
+++ b/plugin/db/mysql/binlog_stream_parser.go
@@ -3,6 +3,7 @@ package mysql
import (
"bufio"
"io"
+ "regexp"
"strings"
"github.com/pkg/errors"
@@ -108,6 +109,37 @@ func ParseBinlogStream(stream io.Reader) ([]BinlogTransaction, error) {
return txns, nil
}
+// FilterBinlogTransactionsByThreadID filters the binlog transaction by thread ID.
+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)
+ }
+ }
+ return ret, nil
+}
+
+var (
+ reThreadID = regexp.MustCompile(`thread_id=(\d+)`)
+)
+
+func parseQueryEventThreadID(header string) (string, error) {
+ matches := reThreadID.FindStringSubmatch(header)
+ if len(matches) != 2 {
+ return "", errors.Errorf("invalid query header %q", header)
+ }
+ return matches[1], nil
+}
+
func getEventType(header string) BinlogEventType {
if strings.Contains(header, "Query") {
return QueryEventType
diff --git a/plugin/db/mysql/binlog_stream_parser_test.go b/plugin/db/mysql/binlog_stream_parser_test.go
index 28287d6629..f78a074a6a 100644
--- a/plugin/db/mysql/binlog_stream_parser_test.go
+++ b/plugin/db/mysql/binlog_stream_parser_test.go
@@ -458,3 +458,164 @@ DELIMITER ;
})
}
}
+
+func TestFilterBinlogTransactionsByThreadID(t *testing.T) {
+ tests := []struct {
+ name string
+ txnList []BinlogTransaction
+ threadID string
+ want []BinlogTransaction
+ err bool
+ }{
+ {
+ name: "empty",
+ err: false,
+ },
+ {
+ name: "invalid transaction",
+ txnList: []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`" + `
+ ### WHERE
+ ### @1=1
+ ### @2='alice'
+ ### @3=100
+ ### SET
+ ### @1=1
+ ### @2='alice'
+ ### @3=90`,
+ },
+ },
+ },
+ err: true,
+ },
+ {
+ name: "real world",
+ txnList: []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/*!*/;
+ `,
+ },
+ },
+ {
+ {
+ 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*/;`,
+ },
+ },
+ },
+ 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,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ a := require.New(t)
+ _, err := FilterBinlogTransactionsByThreadID(test.txnList, test.threadID)
+ if test.err {
+ a.Error(err)
+ } else {
+ a.NoError(err)
+ }
+ })
+ }
+}