Skip to content

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

feat: cancel ghost tasks (#10855)

p0ny759708ac7bf7
diff --git a/backend/component/ghost/logger.go b/backend/component/ghost/logger.go
index 104a533a9b..7255d45d60 100644
--- a/backend/component/ghost/logger.go
+++ b/backend/component/ghost/logger.go
@@ -31,34 +31,46 @@ func (*ghostLogger) Infof(format string, args ...any) {
}
func (*ghostLogger) Warning(args ...any) error {
+ slog.Warn(fmt.Sprintf(args[0].(string), args[1:]))
return errors.Errorf(args[0].(string), args[1:])
}
func (*ghostLogger) Warningf(format string, args ...any) error {
+ slog.Warn(format, args...)
return errors.Errorf(format, args...)
}
func (*ghostLogger) Error(args ...any) error {
+ slog.Error(fmt.Sprintf(args[0].(string), args[1:]))
return errors.Errorf(args[0].(string), args[1:])
}
func (*ghostLogger) Errorf(format string, args ...any) error {
+ slog.Error(format, args...)
return errors.Errorf(format, args...)
}
func (*ghostLogger) Errore(err error) error {
+ if err != nil {
+ slog.Error(err.Error())
+ }
return err
}
func (*ghostLogger) Fatal(args ...any) error {
+ slog.Error(fmt.Sprintf(args[0].(string), args[1:]))
return errors.Errorf(args[0].(string), args[1:])
}
func (*ghostLogger) Fatalf(format string, args ...any) error {
+ slog.Error(format, args...)
return errors.Errorf(format, args...)
}
func (*ghostLogger) Fatale(err error) error {
+ if err != nil {
+ slog.Error(err.Error())
+ }
return err
}
diff --git a/backend/runner/taskrun/schema_update_ghost_cutover_executor.go b/backend/runner/taskrun/schema_update_ghost_cutover_executor.go
index 8be115a577..3eeb75f556 100644
--- a/backend/runner/taskrun/schema_update_ghost_cutover_executor.go
+++ b/backend/runner/taskrun/schema_update_ghost_cutover_executor.go
@@ -55,7 +55,7 @@ type SchemaUpdateGhostCutoverExecutor struct {
// RunOnce will run SchemaUpdateGhostCutover task once.
// TODO: support cancellation.
-func (e *SchemaUpdateGhostCutoverExecutor) RunOnce(ctx context.Context, _ context.Context, task *store.TaskMessage, taskRunUID int) (bool, *api.TaskRunResultPayload, error) {
+func (e *SchemaUpdateGhostCutoverExecutor) RunOnce(ctx context.Context, taskContext context.Context, task *store.TaskMessage, taskRunUID int) (bool, *api.TaskRunResultPayload, error) {
e.stateCfg.TaskRunExecutionStatuses.Store(taskRunUID,
state.TaskRunExecutionStatus{
ExecutionStatus: v1pb.TaskRun_PRE_EXECUTING,
@@ -111,7 +111,7 @@ func (e *SchemaUpdateGhostCutoverExecutor) RunOnce(ctx context.Context, _ contex
// not using the rendered statement here because we want to avoid leaking the rendered statement
version := model.Version{Version: payload.SchemaVersion}
- terminated, result, err := cutover(ctx, e.store, e.dbFactory, e.activityManager, e.stateCfg, e.license, e.profile, task, taskRunUID, statement, payload.SheetID, version, postponeFilename, sharedGhost.migrationContext, sharedGhost.errCh)
+ terminated, result, err := cutover(ctx, taskContext, e.store, e.dbFactory, e.activityManager, e.stateCfg, e.license, e.profile, task, taskRunUID, statement, payload.SheetID, version, postponeFilename, sharedGhost.migrationContext, sharedGhost.errCh)
if err := e.schemaSyncer.SyncDatabaseSchema(ctx, database, true /* force */); err != nil {
slog.Error("failed to sync database schema",
slog.String("instanceName", instance.ResourceID),
@@ -123,7 +123,7 @@ func (e *SchemaUpdateGhostCutoverExecutor) RunOnce(ctx context.Context, _ contex
return terminated, result, err
}
-func cutover(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFactory, activityManager *activity.Manager, stateCfg *state.State, license enterprise.LicenseService, profile config.Profile, task *store.TaskMessage, taskRunUID int, statement string, sheetID int, schemaVersion model.Version, postponeFilename string, migrationContext *base.MigrationContext, errCh <-chan error) (terminated bool, result *api.TaskRunResultPayload, err error) {
+func cutover(ctx context.Context, taskContext context.Context, stores *store.Store, dbFactory *dbfactory.DBFactory, activityManager *activity.Manager, stateCfg *state.State, license enterprise.LicenseService, profile config.Profile, task *store.TaskMessage, taskRunUID int, statement string, sheetID int, schemaVersion model.Version, postponeFilename string, migrationContext *base.MigrationContext, errCh <-chan error) (terminated bool, result *api.TaskRunResultPayload, err error) {
statement = strings.TrimSpace(statement)
instance, err := stores.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &task.InstanceID})
if err != nil {
@@ -135,9 +135,11 @@ func cutover(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFa
}
// 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)
+ cancelled := waitForCutover(ctx, taskContext, migrationContext)
if cancelled {
- return true, nil, errors.Errorf("cutover poller cancelled")
+ err := errors.Errorf("cutover context cancelled")
+ migrationContext.PanicAbort <- err
+ return true, nil, err
}
mi, err := getMigrationInfo(ctx, stores, profile, task, db.Migrate, statement, schemaVersion)
@@ -167,7 +169,7 @@ func cutover(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFa
return postMigration(ctx, stores, activityManager, license, task, mi, migrationID, schema, &sheetID)
}
-func waitForCutover(ctx context.Context, migrationContext *base.MigrationContext) bool {
+func waitForCutover(ctx context.Context, taskContext context.Context, migrationContext *base.MigrationContext) bool {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
@@ -181,6 +183,8 @@ func waitForCutover(ctx context.Context, migrationContext *base.MigrationContext
}
case <-ctx.Done(): // if cancel() execute
return true
+ case <-taskContext.Done():
+ return true
}
}
}
diff --git a/backend/runner/taskrun/schema_update_ghost_sync_executor.go b/backend/runner/taskrun/schema_update_ghost_sync_executor.go
index 4fa01cbf4f..8ad1af11b9 100644
--- a/backend/runner/taskrun/schema_update_ghost_sync_executor.go
+++ b/backend/runner/taskrun/schema_update_ghost_sync_executor.go
@@ -40,7 +40,7 @@ type SchemaUpdateGhostSyncExecutor struct {
// RunOnce will run SchemaUpdateGhostSync task once.
// TODO: support cancellation.
-func (exec *SchemaUpdateGhostSyncExecutor) RunOnce(ctx context.Context, _ context.Context, task *store.TaskMessage, taskRunUID int) (terminated bool, result *api.TaskRunResultPayload, err error) {
+func (exec *SchemaUpdateGhostSyncExecutor) RunOnce(ctx context.Context, taskContext context.Context, task *store.TaskMessage, taskRunUID int) (terminated bool, result *api.TaskRunResultPayload, err error) {
exec.stateCfg.TaskRunExecutionStatuses.Store(taskRunUID,
state.TaskRunExecutionStatus{
ExecutionStatus: v1pb.TaskRun_EXECUTING,
@@ -56,7 +56,7 @@ func (exec *SchemaUpdateGhostSyncExecutor) RunOnce(ctx context.Context, _ contex
return true, nil, err
}
- return exec.runGhostMigration(ctx, task, statement, payload.Flags)
+ return exec.runGhostMigration(ctx, taskContext, task, statement, payload.Flags)
}
type sharedGhostState struct {
@@ -64,7 +64,7 @@ type sharedGhostState struct {
errCh <-chan error
}
-func (exec *SchemaUpdateGhostSyncExecutor) runGhostMigration(ctx context.Context, task *store.TaskMessage, statement string, flags map[string]string) (terminated bool, result *api.TaskRunResultPayload, err error) {
+func (exec *SchemaUpdateGhostSyncExecutor) runGhostMigration(ctx context.Context, taskContext context.Context, task *store.TaskMessage, statement string, flags map[string]string) (terminated bool, result *api.TaskRunResultPayload, err error) {
syncDone := make(chan struct{})
// set buffer size to 1 to unblock the sender because there is no listner if the task is canceled.
// see PR #2919.
@@ -160,5 +160,8 @@ func (exec *SchemaUpdateGhostSyncExecutor) runGhostMigration(ctx context.Context
case <-ctx.Done():
migrationContext.PanicAbort <- errors.New("task canceled")
return true, nil, errors.New("task canceled")
+ case <-taskContext.Done():
+ migrationContext.PanicAbort <- errors.New("task canceled")
+ return true, nil, errors.New("task canceled")
}
}
diff --git a/go.mod b/go.mod
index 2ed9062d2a..51a517ab53 100644
--- a/go.mod
+++ b/go.mod
@@ -300,7 +300,7 @@ require (
// fix potential security issue(CVE-2020-26160) introduced by indirect dependency.
replace github.com/dgrijalva/jwt-go => github.com/form3tech-oss/jwt-go v3.2.6-0.20210809144907-32ab6a8243d7+incompatible
-replace github.com/github/gh-ost => github.com/bytebase/gh-ost2 v0.0.0-20240226063900-2a717ebc4d9c
+replace github.com/github/gh-ost => github.com/bytebase/gh-ost2 v0.0.0-20240226091811-8bc079577744
replace github.com/pingcap/tidb => github.com/bytebase/tidb2 v0.0.0-20231129002249-5bbb6bb83940
diff --git a/go.sum b/go.sum
index 341abbb379..04044cef60 100644
--- a/go.sum
+++ b/go.sum
@@ -144,8 +144,8 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bytebase/antlr/v4 v4.0.0-20231103101006-5fe1a93b199f h1:5O8DZOEloYvnAZIdHb31MT9L1Xn163rWlZqRsKqTEFM=
github.com/bytebase/antlr/v4 v4.0.0-20231103101006-5fe1a93b199f/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
-github.com/bytebase/gh-ost2 v0.0.0-20240226063900-2a717ebc4d9c h1:1pT/pMN5AlCtC5Imu043SVgXlccikv0ve9PW3VhgHHE=
-github.com/bytebase/gh-ost2 v0.0.0-20240226063900-2a717ebc4d9c/go.mod h1:VRPqaDM7UYxJbcCtQkjBcNkxW17p6LJJdkM/nYwQhqg=
+github.com/bytebase/gh-ost2 v0.0.0-20240226091811-8bc079577744 h1:quO7ADa+58XcEOXBqAB0Ofd0RJbzal94TmXd60ezrx4=
+github.com/bytebase/gh-ost2 v0.0.0-20240226091811-8bc079577744/go.mod h1:VRPqaDM7UYxJbcCtQkjBcNkxW17p6LJJdkM/nYwQhqg=
github.com/bytebase/go-lsp v0.0.0-20240130071507-c04b5c75010c h1:W832GNANCst4C6TgYbQnKKxbuMZKQxTD4++b3HOuzBo=
github.com/bytebase/go-lsp v0.0.0-20240130071507-c04b5c75010c/go.mod h1:SULmZY7YNBsvNiQbrb/BEDdEJ84TGnfyUQxaHt8t8rY=
github.com/bytebase/go-obo v0.0.0-20231026081615-705a7fffbfd2 h1:nEHZi8S7PHT4GxP9GfJiuhO8KkYcHmePvy5BfiYxwNk=