Refactor the code and remove the global variable of

support-aliyun-rds branch.
This commit is contained in:
zj118228 2018-03-29 11:19:20 +08:00
parent b3f599acd8
commit d3b98d9e74
7 changed files with 16 additions and 22 deletions

View File

@ -215,8 +215,6 @@ type ContextConfig struct {
} }
} }
var Context *MigrationContext
func NewMigrationContext() *MigrationContext { func NewMigrationContext() *MigrationContext {
return &MigrationContext{ return &MigrationContext{
Uuid: uuid.NewV4().String(), Uuid: uuid.NewV4().String(),

View File

@ -64,7 +64,7 @@ func StringContainsAll(s string, substrings ...string) bool {
return nonEmptyStringsFound return nonEmptyStringsFound
} }
func ValidateConnection(db *gosql.DB, connectionConfig *mysql.ConnectionConfig) (string, error) { func ValidateConnection(db *gosql.DB, connectionConfig *mysql.ConnectionConfig, migrationContext *MigrationContext) (string, error) {
versionQuery := `select @@global.version` versionQuery := `select @@global.version`
var port, extraPort int var port, extraPort int
var version string var version string
@ -76,7 +76,7 @@ func ValidateConnection(db *gosql.DB, connectionConfig *mysql.ConnectionConfig)
// swallow this error. not all servers support extra_port // swallow this error. not all servers support extra_port
} }
// AliyunRDS set users port to "NULL", replace it by gh-ost param // AliyunRDS set users port to "NULL", replace it by gh-ost param
if Context.AliyunRDS { if migrationContext.AliyunRDS {
port = connectionConfig.Key.Port port = connectionConfig.Key.Port
} else { } else {
portQuery := `select @@global.port` portQuery := `select @@global.port`

View File

@ -44,8 +44,6 @@ func acceptSignals(migrationContext *base.MigrationContext) {
// main is the application's entry point. It will either spawn a CLI or HTTP interfaces. // main is the application's entry point. It will either spawn a CLI or HTTP interfaces.
func main() { func main() {
migrationContext := base.NewMigrationContext() migrationContext := base.NewMigrationContext()
base.Context = migrationContext
flag.StringVar(&migrationContext.InspectorConnectionConfig.Key.Hostname, "host", "127.0.0.1", "MySQL hostname (preferably a replica, not the master)") flag.StringVar(&migrationContext.InspectorConnectionConfig.Key.Hostname, "host", "127.0.0.1", "MySQL hostname (preferably a replica, not the master)")
flag.StringVar(&migrationContext.AssumeMasterHostname, "assume-master-host", "", "(optional) explicitly tell gh-ost the identity of the master. Format: some.host.com[:port] This is useful in master-master setups where you wish to pick an explicit master, or in a tungsten-replicator where gh-ost is unable to determine the master") flag.StringVar(&migrationContext.AssumeMasterHostname, "assume-master-host", "", "(optional) explicitly tell gh-ost the identity of the master. Format: some.host.com[:port] This is useful in master-master setups where you wish to pick an explicit master, or in a tungsten-replicator where gh-ost is unable to determine the master")
flag.IntVar(&migrationContext.InspectorConnectionConfig.Key.Port, "port", 3306, "MySQL port (preferably a replica, not the master)") flag.IntVar(&migrationContext.InspectorConnectionConfig.Key.Port, "port", 3306, "MySQL port (preferably a replica, not the master)")

View File

@ -78,21 +78,21 @@ func (this *Applier) InitDBConnections() (err error) {
return err return err
} }
this.singletonDB.SetMaxOpenConns(1) this.singletonDB.SetMaxOpenConns(1)
version, err := base.ValidateConnection(this.db, this.connectionConfig) version, err := base.ValidateConnection(this.db, this.connectionConfig, this.migrationContext)
if err != nil { if err != nil {
return err return err
} }
if _, err := base.ValidateConnection(this.singletonDB, this.connectionConfig); err != nil { if _, err := base.ValidateConnection(this.singletonDB, this.connectionConfig, this.migrationContext); err != nil {
return err return err
} }
this.migrationContext.ApplierMySQLVersion = version this.migrationContext.ApplierMySQLVersion = version
if err := this.validateAndReadTimeZone(); err != nil { if err := this.validateAndReadTimeZone(); err != nil {
return err return err
} }
if impliedKey, err := mysql.GetInstanceKey(this.db, this.migrationContext.AliyunRDS); err != nil { if !this.migrationContext.AliyunRDS {
return err if impliedKey, err := mysql.GetInstanceKey(this.db); err != nil {
} else { return err
if this.migrationContext.AliyunRDS != true { } else {
this.connectionConfig.ImpliedKey = impliedKey this.connectionConfig.ImpliedKey = impliedKey
} }
} }

View File

@ -53,10 +53,10 @@ func (this *Inspector) InitDBConnections() (err error) {
if err := this.validateConnection(); err != nil { if err := this.validateConnection(); err != nil {
return err return err
} }
if impliedKey, err := mysql.GetInstanceKey(this.db, this.migrationContext.AliyunRDS); err != nil { if !this.migrationContext.AliyunRDS {
return err if impliedKey, err := mysql.GetInstanceKey(this.db); err != nil {
} else { return err
if this.migrationContext.AliyunRDS != true { } else {
this.connectionConfig.ImpliedKey = impliedKey this.connectionConfig.ImpliedKey = impliedKey
} }
} }
@ -205,7 +205,7 @@ func (this *Inspector) validateConnection() error {
return fmt.Errorf("MySQL replication length limited to 32 characters. See https://dev.mysql.com/doc/refman/5.7/en/assigning-passwords.html") return fmt.Errorf("MySQL replication length limited to 32 characters. See https://dev.mysql.com/doc/refman/5.7/en/assigning-passwords.html")
} }
version, err := base.ValidateConnection(this.db, this.connectionConfig) version, err := base.ValidateConnection(this.db, this.connectionConfig, this.migrationContext)
this.migrationContext.InspectorMySQLVersion = version this.migrationContext.InspectorMySQLVersion = version
return err return err
} }

View File

@ -107,7 +107,7 @@ func (this *EventsStreamer) InitDBConnections() (err error) {
if this.db, _, err = mysql.GetDB(this.migrationContext.Uuid, EventsStreamerUri); err != nil { if this.db, _, err = mysql.GetDB(this.migrationContext.Uuid, EventsStreamerUri); err != nil {
return err return err
} }
if _, err := base.ValidateConnection(this.db, this.connectionConfig); err != nil { if _, err := base.ValidateConnection(this.db, this.connectionConfig, this.migrationContext); err != nil {
return err return err
} }
if err := this.readCurrentBinlogCoordinates(); err != nil { if err := this.readCurrentBinlogCoordinates(); err != nil {

View File

@ -171,11 +171,9 @@ func GetSelfBinlogCoordinates(db *gosql.DB) (selfBinlogCoordinates *BinlogCoordi
} }
// GetInstanceKey reads hostname and port on given DB // GetInstanceKey reads hostname and port on given DB
func GetInstanceKey(db *gosql.DB, isAliyunRDS bool) (instanceKey *InstanceKey, err error) { func GetInstanceKey(db *gosql.DB) (instanceKey *InstanceKey, err error) {
instanceKey = &InstanceKey{} instanceKey = &InstanceKey{}
if isAliyunRDS != true { err = db.QueryRow(`select @@global.hostname, @@global.port`).Scan(&instanceKey.Hostname, &instanceKey.Port)
err = db.QueryRow(`select @@global.hostname, @@global.port`).Scan(&instanceKey.Hostname, &instanceKey.Port)
}
return instanceKey, err return instanceKey, err
} }