Skip to content

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

refactor: ghost default args (#9081)

p0ny2fe6cbd4a11f
diff --git a/backend/component/ghost/config.go b/backend/component/ghost/config.go
index fbf29a5ae5..809237813b 100644
--- a/backend/component/ghost/config.go
+++ b/backend/component/ghost/config.go
@@ -13,6 +13,38 @@ import (
"github.com/bytebase/bytebase/backend/store"
)
+var defaultConfig = struct {
+ allowedRunningOnMaster bool
+ concurrentCountTableRows bool
+ timestampAllTable bool
+ hooksStatusIntervalSec int64
+ heartbeatIntervalMilliseconds int64
+ niceRatio float64
+ chunkSize int64
+ dmlBatchSize int64
+ maxLagMillisecondsThrottleThreshold int64
+ defaultNumRetries int64
+ cutoverLockTimeoutSeconds int64
+ exponentialBackoffMaxInterval int64
+ throttleHTTPIntervalMillis int64
+ throttleHTTPTimeoutMillis int64
+}{
+ allowedRunningOnMaster: true, // allow-on-master
+ concurrentCountTableRows: true, // concurrent-rowcount
+ timestampAllTable: true, // doesn't have a gh-ost cli flag counterpart
+ hooksStatusIntervalSec: 60, // hooks-status-interval
+ heartbeatIntervalMilliseconds: 100, // heartbeat-interval-millis
+ niceRatio: 0, // nice-ration
+ chunkSize: 1000, // chunk-size
+ dmlBatchSize: 10, // dml-batch-size
+ maxLagMillisecondsThrottleThreshold: 1500, // max-lag-millis
+ defaultNumRetries: 60, // default-retries
+ cutoverLockTimeoutSeconds: 60, // cut-over-lock-timeout-seconds
+ exponentialBackoffMaxInterval: 64, // exponential-backoff-max-interval
+ throttleHTTPIntervalMillis: 100, // throttle-http-interval-millis
+ throttleHTTPTimeoutMillis: 1000, // throttle-http-timeout-millis
+}
+
type UserFlags struct {
maxLoad *string
chunkSize *int
@@ -83,58 +115,6 @@ func GetUserFlags(flags map[string]string) (*UserFlags, error) {
return f, nil
}
-// Config is the configuration for gh-ost migration.
-type Config struct {
- // serverID should be unique
- ServerID uint
- Host string
- Port string
- User string
- Password string
- Database string
- Table string
- AlterStatement string
- SocketFilename string
- PostponeFlagFilename string
- Noop bool
-
- // vendor related
- IsAWS bool
-}
-
-// getGhostConfig returns a gh-ost configuration for migration.
-func getGhostConfig(taskID int, database *store.DatabaseMessage, dataSource *store.DataSourceMessage, secret string, instanceUsers []*store.InstanceUserMessage, tableName string, statement string, noop bool, serverIDOffset uint) (Config, error) {
- var isAWS bool
- for _, user := range instanceUsers {
- if user.Name == "'rdsadmin'@'localhost'" && strings.Contains(user.Grant, "SUPER") {
- isAWS = true
- break
- }
- }
- password, err := common.Unobfuscate(dataSource.ObfuscatedPassword, secret)
- if err != nil {
- return Config{}, err
- }
- return Config{
- Host: dataSource.Host,
- Port: dataSource.Port,
- User: dataSource.Username,
- Password: password,
- Database: database.DatabaseName,
- Table: tableName,
- AlterStatement: statement,
- SocketFilename: getSocketFilename(taskID, database.UID, database.DatabaseName, tableName),
- PostponeFlagFilename: GetPostponeFlagFilename(taskID, database.UID, database.DatabaseName, tableName),
- Noop: noop,
- // On the source and each replica, you must set the server_id system variable to establish a unique replication ID. For each server, you should pick a unique positive integer in the range from 1 to 2^32 − 1, and each ID must be different from every other ID in use by any other source or replica in the replication topology. Example: server-id=3.
- // https://dev.mysql.com/doc/refman/5.7/en/replication-options-source.html
- // Here we use serverID = offset + task.ID to avoid potential conflicts.
- ServerID: serverIDOffset + uint(taskID),
- // https://github.com/github/gh-ost/blob/master/doc/rds.md
- IsAWS: isAWS,
- }, nil
-}
-
func getSocketFilename(taskID int, databaseID int, databaseName string, tableName string) string {
return fmt.Sprintf("/tmp/gh-ost.%v.%v.%v.%v.sock", taskID, databaseID, databaseName, tableName)
}
@@ -145,60 +125,43 @@ func GetPostponeFlagFilename(taskID int, databaseID int, databaseName string, ta
}
// NewMigrationContext is the context for gh-ost migration.
-func NewMigrationContext(taskID int, database *store.DatabaseMessage, dataSource *store.DataSourceMessage, secret string, instanceUsers []*store.InstanceUserMessage, tableName string, statement string, noop bool, serverIDOffset uint) (*base.MigrationContext, error) {
- config, err := getGhostConfig(taskID, database, dataSource, secret, instanceUsers, tableName, statement, noop, serverIDOffset)
+func NewMigrationContext(taskID int, database *store.DatabaseMessage, dataSource *store.DataSourceMessage, secret string, tableName string, statement string, noop bool, serverIDOffset uint) (*base.MigrationContext, error) {
+ password, err := common.Unobfuscate(dataSource.ObfuscatedPassword, secret)
if err != nil {
- return nil, errors.Wrapf(err, "failed to get ghost config")
+ return nil, errors.Wrapf(err, "failed to get password")
}
- const (
- allowedRunningOnMaster = true
- concurrentCountTableRows = true
- timestampAllTable = true
- hooksStatusIntervalSec = 60
- heartbeatIntervalMilliseconds = 100
- niceRatio = 0
- chunkSize = 1000
- dmlBatchSize = 10
- maxLagMillisecondsThrottleThreshold = 1500
- defaultNumRetries = 60
- cutoverLockTimeoutSeconds = 60
- exponentialBackoffMaxInterval = 64
- throttleHTTPIntervalMillis = 100
- throttleHTTPTimeoutMillis = 1000
- )
- statement = strings.Join(strings.Fields(config.AlterStatement), " ")
migrationContext := base.NewMigrationContext()
- migrationContext.InspectorConnectionConfig.Key.Hostname = config.Host
+ migrationContext.InspectorConnectionConfig.Key.Hostname = dataSource.Host
port := 3306
- if config.Port != "" {
- configPort, err := strconv.Atoi(config.Port)
+ if dataSource.Port != "" {
+ dsPort, err := strconv.Atoi(dataSource.Port)
if err != nil {
return nil, errors.Wrap(err, "failed to convert port from string to int")
}
- port = configPort
+ port = dsPort
}
migrationContext.InspectorConnectionConfig.Key.Port = port
- migrationContext.CliUser = config.User
- migrationContext.CliPassword = config.Password
- migrationContext.DatabaseName = config.Database
- migrationContext.OriginalTableName = config.Table
- migrationContext.AlterStatement = statement
- migrationContext.Noop = config.Noop
- migrationContext.ReplicaServerId = config.ServerID
- if config.IsAWS {
- migrationContext.AssumeRBR = true
- }
+ migrationContext.CliUser = dataSource.Username
+ migrationContext.CliPassword = password
+ migrationContext.DatabaseName = database.DatabaseName
+ migrationContext.OriginalTableName = tableName
+ migrationContext.AlterStatement = strings.Join(strings.Fields(statement), " ")
+ migrationContext.Noop = noop
+ // On the source and each replica, you must set the server_id system variable to establish a unique replication ID. For each server, you should pick a unique positive integer in the range from 1 to 2^32 − 1, and each ID must be different from every other ID in use by any other source or replica in the replication topology. Example: server-id=3.
+ // https://dev.mysql.com/doc/refman/5.7/en/replication-options-source.html
+ // Here we use serverID = offset + task.ID to avoid potential conflicts.
+ migrationContext.ReplicaServerId = serverIDOffset + uint(taskID)
// set defaults
if err := migrationContext.SetConnectionConfig(""); err != nil {
return nil, err
}
- migrationContext.AllowedRunningOnMaster = allowedRunningOnMaster
- migrationContext.ConcurrentCountTableRows = concurrentCountTableRows
- migrationContext.HooksStatusIntervalSec = hooksStatusIntervalSec
+ migrationContext.AllowedRunningOnMaster = defaultConfig.allowedRunningOnMaster
+ migrationContext.ConcurrentCountTableRows = defaultConfig.concurrentCountTableRows
+ migrationContext.HooksStatusIntervalSec = defaultConfig.hooksStatusIntervalSec
migrationContext.CutOverType = base.CutOverAtomic
- migrationContext.ThrottleHTTPIntervalMillis = throttleHTTPIntervalMillis
- migrationContext.ThrottleHTTPTimeoutMillis = throttleHTTPTimeoutMillis
+ migrationContext.ThrottleHTTPIntervalMillis = defaultConfig.throttleHTTPIntervalMillis
+ migrationContext.ThrottleHTTPTimeoutMillis = defaultConfig.throttleHTTPTimeoutMillis
if migrationContext.AlterStatement == "" {
return nil, errors.Errorf("alterStatement must be provided and must not be empty")
@@ -218,20 +181,20 @@ func NewMigrationContext(taskID int, database *store.DatabaseMessage, dataSource
}
migrationContext.OriginalTableName = parser.GetExplicitTable()
}
- migrationContext.ServeSocketFile = config.SocketFilename
- migrationContext.PostponeCutOverFlagFile = config.PostponeFlagFilename
- migrationContext.TimestampAllTable = timestampAllTable
- migrationContext.SetHeartbeatIntervalMilliseconds(heartbeatIntervalMilliseconds)
- migrationContext.SetNiceRatio(niceRatio)
- migrationContext.SetChunkSize(chunkSize)
- migrationContext.SetDMLBatchSize(dmlBatchSize)
- migrationContext.SetMaxLagMillisecondsThrottleThreshold(maxLagMillisecondsThrottleThreshold)
- migrationContext.SetDefaultNumRetries(defaultNumRetries)
+ migrationContext.ServeSocketFile = getSocketFilename(taskID, database.UID, database.DatabaseName, tableName)
+ migrationContext.PostponeCutOverFlagFile = GetPostponeFlagFilename(taskID, database.UID, database.DatabaseName, tableName)
+ migrationContext.TimestampAllTable = defaultConfig.timestampAllTable
+ migrationContext.SetHeartbeatIntervalMilliseconds(defaultConfig.heartbeatIntervalMilliseconds)
+ migrationContext.SetNiceRatio(defaultConfig.niceRatio)
+ migrationContext.SetChunkSize(defaultConfig.chunkSize)
+ migrationContext.SetDMLBatchSize(defaultConfig.dmlBatchSize)
+ migrationContext.SetMaxLagMillisecondsThrottleThreshold(defaultConfig.maxLagMillisecondsThrottleThreshold)
+ migrationContext.SetDefaultNumRetries(defaultConfig.defaultNumRetries)
migrationContext.ApplyCredentials()
- if err := migrationContext.SetCutOverLockTimeoutSeconds(cutoverLockTimeoutSeconds); err != nil {
+ if err := migrationContext.SetCutOverLockTimeoutSeconds(defaultConfig.cutoverLockTimeoutSeconds); err != nil {
return nil, err
}
- if err := migrationContext.SetExponentialBackoffMaxInterval(exponentialBackoffMaxInterval); err != nil {
+ if err := migrationContext.SetExponentialBackoffMaxInterval(defaultConfig.exponentialBackoffMaxInterval); err != nil {
return nil, err
}
return migrationContext, nil
diff --git a/backend/runner/plancheck/ghost_sync_executor.go b/backend/runner/plancheck/ghost_sync_executor.go
index aac3da790a..23accf9768 100644
--- a/backend/runner/plancheck/ghost_sync_executor.go
+++ b/backend/runner/plancheck/ghost_sync_executor.go
@@ -80,11 +80,6 @@ func (e *GhostSyncExecutor) Run(ctx context.Context, config *storepb.PlanCheckRu
return nil, common.Errorf(common.Internal, "admin data source not found for instance %d", instance.UID)
}
- instanceUsers, err := e.store.ListInstanceUsers(ctx, &store.FindInstanceUserMessage{InstanceUID: instance.UID})
- if err != nil {
- return nil, common.Errorf(common.Internal, "failed to find instance user by instanceID %d", instance.UID)
- }
-
sheetUID := int(config.SheetUid)
sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
if err != nil {
@@ -107,7 +102,7 @@ func (e *GhostSyncExecutor) Run(ctx context.Context, config *storepb.PlanCheckRu
return nil, common.Wrapf(err, common.Internal, "failed to parse table name from statement, statement: %v", statement)
}
- migrationContext, err := ghost.NewMigrationContext(rand.Intn(10000000), database, adminDataSource, e.secret, instanceUsers, tableName, renderedStatement, true, 20000000)
+ migrationContext, err := ghost.NewMigrationContext(rand.Intn(10000000), database, adminDataSource, e.secret, tableName, renderedStatement, true, 20000000)
if err != nil {
return nil, common.Wrapf(err, common.Internal, "failed to create migration context")
}
diff --git a/backend/runner/taskrun/schema_update_ghost_sync_executor.go b/backend/runner/taskrun/schema_update_ghost_sync_executor.go
index a67df3313e..37c100f6f4 100644
--- a/backend/runner/taskrun/schema_update_ghost_sync_executor.go
+++ b/backend/runner/taskrun/schema_update_ghost_sync_executor.go
@@ -56,7 +56,7 @@ func (exec *SchemaUpdateGhostSyncExecutor) RunOnce(ctx context.Context, _ contex
return true, nil, err
}
- return exec.runGhostMigration(ctx, exec.store, task, statement)
+ return exec.runGhostMigration(ctx, task, statement)
}
type sharedGhostState struct {
@@ -64,7 +64,7 @@ type sharedGhostState struct {
errCh <-chan error
}
-func (exec *SchemaUpdateGhostSyncExecutor) runGhostMigration(ctx context.Context, stores *store.Store, task *store.TaskMessage, statement string) (terminated bool, result *api.TaskRunResultPayload, err error) {
+func (exec *SchemaUpdateGhostSyncExecutor) runGhostMigration(ctx context.Context, task *store.TaskMessage, statement 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.
@@ -97,16 +97,11 @@ func (exec *SchemaUpdateGhostSyncExecutor) runGhostMigration(ctx context.Context
return true, nil, errors.Errorf("database not found")
}
- instanceUsers, err := stores.ListInstanceUsers(ctx, &store.FindInstanceUserMessage{InstanceUID: task.InstanceID})
- if err != nil {
- return true, nil, common.Errorf(common.Internal, "failed to find instance user by instanceID %d", task.InstanceID)
- }
-
materials := utils.GetSecretMapFromDatabaseMessage(database)
// To avoid leaking the rendered statement, the error message should use the original statement and not the rendered statement.
renderedStatement := utils.RenderStatement(statement, materials)
- migrationContext, err := ghost.NewMigrationContext(task.ID, database, adminDataSource, exec.secret, instanceUsers, tableName, renderedStatement, false, 10000000)
+ migrationContext, err := ghost.NewMigrationContext(task.ID, database, adminDataSource, exec.secret, tableName, renderedStatement, false, 10000000)
if err != nil {
return true, nil, errors.Wrap(err, "failed to init migrationContext for gh-ost")
}