Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(flashback): store binlog coordinates for MySQL DML task (#3278)
Yilong Li85c716881c23
diff --git a/api/task.go b/api/task.goindex 609e11b5a9..54962a1370 100644--- a/api/task.go+++ b/api/task.go@@ -138,9 +138,17 @@ type TaskDatabaseDataUpdatePayload struct {SchemaVersion string `json:"schemaVersion,omitempty"`VCSPushEvent *vcs.PushEvent `json:"pushEvent,omitempty"`- // ThreadID is the ID of the connection executing the migration.// MySQL rollback SQL related.++ // ThreadID is the ID of the connection executing the migration.+ // We use it to filter the binlog events of the migration transaction.ThreadID string `json:"threadID,omitempty"`+ // BinlogXxx are obtained before and after executing the migration.+ // We use them to locate the range of binlog for the migration transaction.+ BinlogFileStart string `json:"binlogFileStart,omitempty"`+ BinlogFileEnd string `json:"binlogFileEnd,omitempty"`+ BinlogPosStart int64 `json:"binlogPosStart,omitempty"`+ BinlogPosEnd int64 `json:"binlogPosEnd,omitempty"`}// TaskDatabaseBackupPayload is the task payload for database backup.diff --git a/plugin/db/mysql/dump.go b/plugin/db/mysql/dump.goindex 8ba1bf5091..6f2c859760 100644--- a/plugin/db/mysql/dump.go+++ b/plugin/db/mysql/dump.go@@ -106,7 +106,7 @@ func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer,return "", err}- binlog, err := GetBinlogInfo(ctx, conn)+ binlog, err := GetBinlogInfo(ctx, driver.db)if err != nil {return "", err}@@ -357,11 +357,11 @@ func excludeSchemaAutoIncrementValue(s string) string {}// GetBinlogInfo queries current binlog info from MySQL server.-func GetBinlogInfo(ctx context.Context, conn *sql.Conn) (api.BinlogInfo, error) {+func GetBinlogInfo(ctx context.Context, db *sql.DB) (api.BinlogInfo, error) {query := "SHOW MASTER STATUS"binlogInfo := api.BinlogInfo{}var unused interface{}- if err := conn.QueryRowContext(ctx, query).Scan(+ if err := db.QueryRowContext(ctx, query).Scan(&binlogInfo.FileName,&binlogInfo.Position,&unused,diff --git a/server/task_executor.go b/server/task_executor.goindex 448aaaf745..3547753f35 100644--- a/server/task_executor.go+++ b/server/task_executor.go@@ -153,7 +153,7 @@ func executeMigration(ctx context.Context, server *Server, task *api.Task, state}if task.Type == api.TaskDatabaseDataUpdate && task.Instance.Engine == db.MySQL {- if err := updateTaskPayloadForMySQLThreadID(ctx, driver, task, server.store); err != nil {+ if err := setThreadIDAndStartBinlogCoordinate(ctx, driver, task, server.store); err != nil {return 0, "", errors.Wrap(err, "failed to update the task payload for MySQL rollback SQL")}}@@ -163,10 +163,16 @@ func executeMigration(ctx context.Context, server *Server, task *api.Task, statereturn 0, "", err}+ if task.Type == api.TaskDatabaseDataUpdate && task.Instance.Engine == db.MySQL {+ if err := setEndBinlogCoordinate(ctx, driver, task, server.store); err != nil {+ return 0, "", errors.Wrap(err, "failed to update the task payload for MySQL rollback SQL")+ }+ }+return migrationID, schema, nil}-func updateTaskPayloadForMySQLThreadID(ctx context.Context, driver db.Driver, task *api.Task, store *store.Store) error {+func setThreadIDAndStartBinlogCoordinate(ctx context.Context, driver db.Driver, task *api.Task, store *store.Store) error {mysqlDriver, ok := driver.(*mysql.Driver)if !ok {return errors.Errorf("failed to cast driver to mysql.Driver")@@ -180,6 +186,51 @@ func updateTaskPayloadForMySQLThreadID(ctx context.Context, driver db.Driver, tareturn err}payload.ThreadID = connID++ db, err := driver.GetDBConnection(ctx, "")+ if err != nil {+ return errors.Wrap(err, "failed to get the DB connection")+ }+ binlogInfo, err := mysql.GetBinlogInfo(ctx, db)+ if err != nil {+ return errors.Wrap(err, "failed to get the binlog info before executing the migration transaction")+ }+ payload.BinlogFileStart = binlogInfo.FileName+ payload.BinlogPosStart = binlogInfo.Position++ payloadBytes, err := json.Marshal(payload)+ if err != nil {+ return errors.Wrap(err, "failed to marshal task payload")+ }+ payloadString := string(payloadBytes)+ patch := &api.TaskPatch{+ ID: task.ID,+ UpdaterID: api.SystemBotID,+ Payload: &payloadString,+ }+ if _, err := store.PatchTask(ctx, patch); err != nil {+ return errors.Wrapf(err, "failed to patch task %d with the MySQL thread ID", task.ID)+ }+ return nil+}++func setEndBinlogCoordinate(ctx context.Context, driver db.Driver, task *api.Task, store *store.Store) error {+ payload := &api.TaskDatabaseDataUpdatePayload{}+ if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {+ return errors.Wrap(err, "invalid database data update payload")+ }++ db, err := driver.GetDBConnection(ctx, "")+ if err != nil {+ return errors.Wrap(err, "failed to get the DB connection")+ }+ binlogInfo, err := mysql.GetBinlogInfo(ctx, db)+ if err != nil {+ return errors.Wrap(err, "failed to get the binlog info before executing the migration transaction")+ }+ payload.BinlogFileEnd = binlogInfo.FileName+ payload.BinlogPosEnd = binlogInfo.Position+payloadBytes, err := json.Marshal(payload)if err != nil {return errors.Wrap(err, "failed to marshal task payload")