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: wait for heartbeat lag in gh-ost cutover task (#2092)
p0ny659486d2016e
diff --git a/server/task_executor_schema_update_ghost_cutover.go b/server/task_executor_schema_update_ghost_cutover.goindex 6684fd1274..bffc7fd592 100644--- a/server/task_executor_schema_update_ghost_cutover.go+++ b/server/task_executor_schema_update_ghost_cutover.go@@ -10,6 +10,7 @@ import ("sync/atomic""time"+ "github.com/github/gh-ost/go/base""go.uber.org/zap""github.com/bytebase/bytebase/api"@@ -64,10 +65,10 @@ func (exec *SchemaUpdateGhostCutoverTaskExecutor) RunOnce(ctx context.Context, s}sharedGhost := value.(sharedGhostState)- return cutover(ctx, server, task, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent, postponeFilename, sharedGhost.errCh)+ return cutover(ctx, server, task, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent, postponeFilename, sharedGhost.migrationContext, sharedGhost.errCh)}-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) {+func cutover(ctx context.Context, server *Server, task *api.Task, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent, postponeFilename string, migrationContext *base.MigrationContext, 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)@@ -95,6 +96,13 @@ func cutover(ctx context.Context, server *Server, task *api.Task, statement, schreturn -1, "", err}+ // wait for heartbeat lag.+ // try to make the time gap between the migration history insertion and the actual cutover as close as possible.+ cancelled := waitForCutover(ctx, migrationContext)+ if cancelled {+ return -1, "", fmt.Errorf("cutover poller cancelled")+ }+insertedID, err := util.BeginMigration(ctx, executor, mi, prevSchemaBuf.String(), statement, db.BytebaseDatabase)if err != nil {if common.ErrorCode(err) == common.MigrationAlreadyApplied {@@ -135,6 +143,24 @@ func cutover(ctx context.Context, server *Server, task *api.Task, statement, schreturn postMigration(ctx, server, task, vcsPushEvent, mi, migrationID, schema)}+func waitForCutover(ctx context.Context, migrationContext *base.MigrationContext) bool {+ ticker := time.NewTicker(time.Second)+ defer ticker.Stop()+ for {+ select {+ case <-ticker.C:+ heartbeatLag := migrationContext.TimeSinceLastHeartbeatOnChangelog()+ maxLagMillisecondsThrottle := time.Duration(atomic.LoadInt64(&migrationContext.MaxLagMillisecondsThrottleThreshold)) * time.Millisecond+ cutOverLockTimeout := time.Duration(migrationContext.CutOverLockTimeoutSeconds) * time.Second+ if heartbeatLag <= maxLagMillisecondsThrottle && heartbeatLag <= cutOverLockTimeout {+ return false+ }+ case <-ctx.Done(): // if cancel() execute+ return true+ }+ }+}+// IsCompleted tells the scheduler if the task execution has completed.func (exec *SchemaUpdateGhostCutoverTaskExecutor) IsCompleted() bool {return atomic.LoadInt32(&exec.completed) == 1diff --git a/server/task_executor_schema_update_ghost_sync.go b/server/task_executor_schema_update_ghost_sync.goindex bba38b8923..d5d0955f88 100644--- a/server/task_executor_schema_update_ghost_sync.go+++ b/server/task_executor_schema_update_ghost_sync.go@@ -74,8 +74,8 @@ func getTableNameFromStatement(statement string) (string, error) {}type sharedGhostState struct {- migrator *logic.Migrator- errCh <-chan error+ migrationContext *base.MigrationContext+ errCh <-chan error}type ghostConfig struct {@@ -259,7 +259,7 @@ func (exec *SchemaUpdateGhostSyncTaskExecutor) runGhostMigration(_ context.Conteselect {case <-syncDone:- server.TaskScheduler.sharedTaskState.Store(task.ID, sharedGhostState{migrator: migrator, errCh: migrationError})+ server.TaskScheduler.sharedTaskState.Store(task.ID, sharedGhostState{migrationContext: migrationContext, errCh: migrationError})return true, &api.TaskRunResultPayload{Detail: "sync done"}, nilcase err := <-migrationError:return true, nil, err