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: share gh-ost states between sync and cutover task (#2068)
p0ny183faf457841
diff --git a/server/task_executor_schema_update_ghost_cutover.go b/server/task_executor_schema_update_ghost_cutover.goindex 37b3c62dcc..6684fd1274 100644--- a/server/task_executor_schema_update_ghost_cutover.go+++ b/server/task_executor_schema_update_ghost_cutover.go@@ -38,7 +38,10 @@ func (exec *SchemaUpdateGhostCutoverTaskExecutor) RunOnce(ctx context.Context, sif err != nil {return true, nil, fmt.Errorf("failed to get a single taskDAG for schema update gh-ost cutover task, id: %v, error: %w", task.ID, err)}+syncTaskID := taskDAG.FromTaskID+ defer server.TaskScheduler.sharedTaskState.Delete(syncTaskID)+syncTask, err := server.store.GetTaskByID(ctx, syncTaskID)if err != nil {return true, nil, fmt.Errorf("failed to get schema update gh-ost sync task for cutover task, error: %w", err)@@ -53,13 +56,18 @@ func (exec *SchemaUpdateGhostCutoverTaskExecutor) RunOnce(ctx context.Context, sreturn true, nil, fmt.Errorf("failed to parse table name from statement, error: %w", err)}- socketFilename := getSocketFilename(syncTaskID, task.Database.ID, task.Database.Name, tableName)postponeFilename := getPostponeFlagFilename(syncTaskID, task.Database.ID, task.Database.Name, tableName)- return cutover(ctx, server, task, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent, socketFilename, postponeFilename)+ value, ok := server.TaskScheduler.sharedTaskState.Load(syncTaskID)+ if !ok {+ return true, nil, fmt.Errorf("failed to get gh-ost state from sync task")+ }+ sharedGhost := value.(sharedGhostState)++ return cutover(ctx, server, task, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent, postponeFilename, sharedGhost.errCh)}-func cutover(ctx context.Context, server *Server, task *api.Task, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent, socketFilename, postponeFilename string) (terminated bool, result *api.TaskRunResultPayload, err error) {+func cutover(ctx context.Context, server *Server, task *api.Task, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent, postponeFilename string, errCh <-chan error) (terminated bool, result *api.TaskRunResultPayload, err error) {statement = strings.TrimSpace(statement)mi, err := preMigration(ctx, server, task, db.Migrate, statement, schemaVersion, vcsPushEvent)@@ -109,13 +117,8 @@ func cutover(ctx context.Context, server *Server, task *api.Task, statement, schreturn -1, "", fmt.Errorf("failed to remove postpone flag file, error: %w", err)}- ticker := time.NewTicker(time.Second * 1)- defer ticker.Stop()-- for range ticker.C {- if _, err := os.Stat(socketFilename); err != nil {- break- }+ if migrationErr := <-errCh; migrationErr != nil {+ return -1, "", fmt.Errorf("failed to run gh-ost migration, err: %w", migrationErr)}var afterSchemaBuf bytes.Bufferdiff --git a/server/task_executor_schema_update_ghost_sync.go b/server/task_executor_schema_update_ghost_sync.goindex c61228ba3a..bba38b8923 100644--- a/server/task_executor_schema_update_ghost_sync.go+++ b/server/task_executor_schema_update_ghost_sync.go@@ -73,6 +73,11 @@ func getTableNameFromStatement(statement string) (string, error) {return parser.GetExplicitTable(), nil}+type sharedGhostState struct {+ migrator *logic.Migrator+ errCh <-chan error+}+type ghostConfig struct {// serverID should be uniqueserverID uint@@ -169,9 +174,9 @@ func newMigrationContext(config ghostConfig) (*base.MigrationContext, error) {return migrationContext, nil}-func (exec *SchemaUpdateGhostSyncTaskExecutor) runGhostMigration(_ context.Context, _ *Server, task *api.Task, statement string) (terminated bool, result *api.TaskRunResultPayload, err error) {+func (exec *SchemaUpdateGhostSyncTaskExecutor) runGhostMigration(_ context.Context, server *Server, task *api.Task, statement string) (terminated bool, result *api.TaskRunResultPayload, err error) {syncDone := make(chan struct{})- syncError := make(chan error)+ migrationError := make(chan error)instance := task.InstancedatabaseName := task.Database.Name@@ -243,21 +248,20 @@ func (exec *SchemaUpdateGhostSyncTaskExecutor) runGhostMigration(_ context.Contego func() {if err := migrator.Migrate(); err != nil {log.Error("failed to run gh-ost migration", zap.Error(err))- // `migrator.Migrate` can return an error.- // 1. If the error is returned before closing `syncDone`,`runGhostMigration` will receive from `syncError`.- // 2. If it is returned after `syncDone` is closed, `runGhostMigration` would have received from `syncDone` and returned,- // and there's no consumer for `syncError`, so we must send to `syncError` without blocking.- select {- case syncError <- fmt.Errorf("failed to run gh-ost migration, error: %w", err):- default:- }+ migrationError <- err+ return}+ migrationError <- nil+ // we send to migrationError channel anyway because:+ // 1. before syncDone, the gh-ost sync task will receive it.+ // 2. after syncDone, the gh-ost cutover task will receive it.}()select {case <-syncDone:+ server.TaskScheduler.sharedTaskState.Store(task.ID, sharedGhostState{migrator: migrator, errCh: migrationError})return true, &api.TaskRunResultPayload{Detail: "sync done"}, nil- case err := <-syncError:+ case err := <-migrationError:return true, nil, err}}diff --git a/server/task_scheduler.go b/server/task_scheduler.goindex e0720db3ca..22a5b9a558 100644--- a/server/task_scheduler.go+++ b/server/task_scheduler.go@@ -32,7 +32,8 @@ func NewTaskScheduler(server *Server) *TaskScheduler {type TaskScheduler struct {executorGetters map[api.TaskType]func() TaskExecutorrunningExecutors map[int]TaskExecutor- taskProgress sync.Map+ taskProgress sync.Map // map[taskID]api.Progress+ sharedTaskState sync.Map // map[taskID]interface{}server *Server}