Skip to content

Commit 982b8ee

Browse files
author
Nikhil Mathew
committed
Refactor global migration context
1 parent ed5c804 commit 982b8ee

11 files changed

Lines changed: 34 additions & 45 deletions

File tree

go/base/context.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,7 @@ type ContextConfig struct {
212212
}
213213
}
214214

215-
var context *MigrationContext
216-
217-
func init() {
218-
context = newMigrationContext()
219-
}
220-
221-
func newMigrationContext() *MigrationContext {
215+
func NewMigrationContext() *MigrationContext {
222216
return &MigrationContext{
223217
defaultNumRetries: 60,
224218
ChunkSize: 1000,
@@ -239,11 +233,6 @@ func newMigrationContext() *MigrationContext {
239233
}
240234
}
241235

242-
// GetMigrationContext
243-
func GetMigrationContext() *MigrationContext {
244-
return context
245-
}
246-
247236
func getSafeTableName(baseName string, suffix string) string {
248237
name := fmt.Sprintf("_%s_%s", baseName, suffix)
249238
if len(name) <= mysql.MaxTableNameLength {

go/base/context_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@ func init() {
1919

2020
func TestGetTableNames(t *testing.T) {
2121
{
22-
context = newMigrationContext()
22+
context := NewMigrationContext()
2323
context.OriginalTableName = "some_table"
2424
test.S(t).ExpectEquals(context.GetOldTableName(), "_some_table_del")
2525
test.S(t).ExpectEquals(context.GetGhostTableName(), "_some_table_gho")
2626
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_some_table_ghc")
2727
}
2828
{
29-
context = newMigrationContext()
29+
context := NewMigrationContext()
3030
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890"
3131
test.S(t).ExpectEquals(context.GetOldTableName(), "_a1234567890123456789012345678901234567890123456789012345678_del")
3232
test.S(t).ExpectEquals(context.GetGhostTableName(), "_a1234567890123456789012345678901234567890123456789012345678_gho")
3333
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_a1234567890123456789012345678901234567890123456789012345678_ghc")
3434
}
3535
{
36-
context = newMigrationContext()
36+
context := NewMigrationContext()
3737
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123"
3838
oldTableName := context.GetOldTableName()
3939
test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123456789012345678_del")
4040
}
4141
{
42-
context = newMigrationContext()
42+
context := NewMigrationContext()
4343
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123"
4444
context.TimestampOldTable = true
4545
longForm := "Jan 2, 2006 at 3:04pm (MST)"
@@ -48,7 +48,7 @@ func TestGetTableNames(t *testing.T) {
4848
test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123_20130203195400_del")
4949
}
5050
{
51-
context = newMigrationContext()
51+
context := NewMigrationContext()
5252
context.OriginalTableName = "foo_bar_baz"
5353
context.ForceTmpTableName = "tmp"
5454
test.S(t).ExpectEquals(context.GetOldTableName(), "_tmp_del")

go/binlog/gomysql_reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ type GoMySQLReader struct {
2929
MigrationContext *base.MigrationContext
3030
}
3131

32-
func NewGoMySQLReader(connectionConfig *mysql.ConnectionConfig) (binlogReader *GoMySQLReader, err error) {
32+
func NewGoMySQLReader(migrationContext *base.MigrationContext, connectionConfig *mysql.ConnectionConfig) (binlogReader *GoMySQLReader, err error) {
3333
binlogReader = &GoMySQLReader{
3434
connectionConfig: connectionConfig,
3535
currentCoordinates: mysql.BinlogCoordinates{},
3636
currentCoordinatesMutex: &sync.Mutex{},
3737
binlogSyncer: nil,
3838
binlogStreamer: nil,
39-
MigrationContext: base.GetMigrationContext(),
39+
MigrationContext: migrationContext,
4040
}
4141

4242
serverId := uint32(binlogReader.MigrationContext.ReplicaServerId)

go/cmd/gh-ost/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func acceptSignals(migrationContext *base.MigrationContext) {
4343

4444
// main is the application's entry point. It will either spawn a CLI or HTTP interfaces.
4545
func main() {
46-
migrationContext := base.GetMigrationContext()
46+
migrationContext := base.NewMigrationContext()
4747

4848
flag.StringVar(&migrationContext.InspectorConnectionConfig.Key.Hostname, "host", "127.0.0.1", "MySQL hostname (preferably a replica, not the master)")
4949
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 unabel to determine the master")
@@ -241,7 +241,7 @@ func main() {
241241
log.Infof("starting gh-ost %+v", AppVersion)
242242
acceptSignals(migrationContext)
243243

244-
migrator := logic.NewMigrator()
244+
migrator := logic.NewMigrator(migrationContext)
245245
err := migrator.Migrate()
246246
if err != nil {
247247
migrator.ExecOnFailureHook()

go/logic/applier.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ type Applier struct {
3636
migrationContext *base.MigrationContext
3737
}
3838

39-
func NewApplier() *Applier {
39+
func NewApplier(migrationContext *base.MigrationContext) *Applier {
4040
return &Applier{
41-
connectionConfig: base.GetMigrationContext().ApplierConnectionConfig,
42-
migrationContext: base.GetMigrationContext(),
41+
connectionConfig: migrationContext.ApplierConnectionConfig,
42+
migrationContext: migrationContext,
4343
}
4444
}
4545

go/logic/hooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ type HooksExecutor struct {
3737
migrationContext *base.MigrationContext
3838
}
3939

40-
func NewHooksExecutor() *HooksExecutor {
40+
func NewHooksExecutor(migrationContext *base.MigrationContext) *HooksExecutor {
4141
return &HooksExecutor{
42-
migrationContext: base.GetMigrationContext(),
42+
migrationContext: migrationContext,
4343
}
4444
}
4545

go/logic/inspect.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ type Inspector struct {
3131
migrationContext *base.MigrationContext
3232
}
3333

34-
func NewInspector() *Inspector {
34+
func NewInspector(migrationContext *base.MigrationContext) *Inspector {
3535
return &Inspector{
36-
connectionConfig: base.GetMigrationContext().InspectorConnectionConfig,
37-
migrationContext: base.GetMigrationContext(),
36+
connectionConfig: migrationContext.InspectorConnectionConfig,
37+
migrationContext: migrationContext,
3838
}
3939
}
4040

go/logic/migrator.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ type Migrator struct {
8787
handledChangelogStates map[string]bool
8888
}
8989

90-
func NewMigrator() *Migrator {
90+
func NewMigrator(context *base.MigrationContext) *Migrator {
9191
migrator := &Migrator{
92-
migrationContext: base.GetMigrationContext(),
92+
migrationContext: context,
9393
parser: sql.NewParser(),
9494
ghostTableMigrated: make(chan bool),
9595
firstThrottlingCollected: make(chan bool, 3),
@@ -120,7 +120,7 @@ func (this *Migrator) acceptSignals() {
120120

121121
// initiateHooksExecutor
122122
func (this *Migrator) initiateHooksExecutor() (err error) {
123-
this.hooksExecutor = NewHooksExecutor()
123+
this.hooksExecutor = NewHooksExecutor(this.migrationContext)
124124
if err := this.hooksExecutor.initHooks(); err != nil {
125125
return err
126126
}
@@ -655,7 +655,7 @@ func (this *Migrator) initiateServer() (err error) {
655655
var f printStatusFunc = func(rule PrintStatusRule, writer io.Writer) {
656656
this.printStatus(rule, writer)
657657
}
658-
this.server = NewServer(this.hooksExecutor, f)
658+
this.server = NewServer(this.migrationContext, this.hooksExecutor, f)
659659
if err := this.server.BindSocketFile(); err != nil {
660660
return err
661661
}
@@ -675,7 +675,7 @@ func (this *Migrator) initiateServer() (err error) {
675675
// - heartbeat
676676
// When `--allow-on-master` is supplied, the inspector is actually the master.
677677
func (this *Migrator) initiateInspector() (err error) {
678-
this.inspector = NewInspector()
678+
this.inspector = NewInspector(this.migrationContext)
679679
if err := this.inspector.InitDBConnections(); err != nil {
680680
return err
681681
}
@@ -934,7 +934,7 @@ func (this *Migrator) printStatus(rule PrintStatusRule, writers ...io.Writer) {
934934

935935
// initiateStreaming begins treaming of binary log events and registers listeners for such events
936936
func (this *Migrator) initiateStreaming() error {
937-
this.eventsStreamer = NewEventsStreamer()
937+
this.eventsStreamer = NewEventsStreamer(this.migrationContext)
938938
if err := this.eventsStreamer.InitDBConnections(); err != nil {
939939
return err
940940
}
@@ -982,7 +982,7 @@ func (this *Migrator) addDMLEventsListener() error {
982982

983983
// initiateThrottler kicks in the throttling collection and the throttling checks.
984984
func (this *Migrator) initiateThrottler() error {
985-
this.throttler = NewThrottler(this.applier, this.inspector)
985+
this.throttler = NewThrottler(this.migrationContext, this.applier, this.inspector)
986986

987987
go this.throttler.initiateThrottlerCollection(this.firstThrottlingCollected)
988988
log.Infof("Waiting for first throttle metrics to be collected")
@@ -996,7 +996,7 @@ func (this *Migrator) initiateThrottler() error {
996996
}
997997

998998
func (this *Migrator) initiateApplier() error {
999-
this.applier = NewApplier()
999+
this.applier = NewApplier(this.migrationContext)
10001000
if err := this.applier.InitDBConnections(); err != nil {
10011001
return err
10021002
}

go/logic/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ type Server struct {
3030
printStatus printStatusFunc
3131
}
3232

33-
func NewServer(hooksExecutor *HooksExecutor, printStatus printStatusFunc) *Server {
33+
func NewServer(migrationContext *base.MigrationContext, hooksExecutor *HooksExecutor, printStatus printStatusFunc) *Server {
3434
return &Server{
35-
migrationContext: base.GetMigrationContext(),
35+
migrationContext: migrationContext,
3636
hooksExecutor: hooksExecutor,
3737
printStatus: printStatus,
3838
}

go/logic/streamer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ type EventsStreamer struct {
4545
binlogReader *binlog.GoMySQLReader
4646
}
4747

48-
func NewEventsStreamer() *EventsStreamer {
48+
func NewEventsStreamer(migrationContext *base.MigrationContext) *EventsStreamer {
4949
return &EventsStreamer{
50-
connectionConfig: base.GetMigrationContext().InspectorConnectionConfig,
51-
migrationContext: base.GetMigrationContext(),
50+
connectionConfig: migrationContext.InspectorConnectionConfig,
51+
migrationContext: migrationContext,
5252
listeners: [](*BinlogEventListener){},
5353
listenersMutex: &sync.Mutex{},
5454
eventsChannel: make(chan *binlog.BinlogEntry, EventsChannelBufferSize),
@@ -122,7 +122,7 @@ func (this *EventsStreamer) InitDBConnections() (err error) {
122122

123123
// initBinlogReader creates and connects the reader: we hook up to a MySQL server as a replica
124124
func (this *EventsStreamer) initBinlogReader(binlogCoordinates *mysql.BinlogCoordinates) error {
125-
goMySQLReader, err := binlog.NewGoMySQLReader(this.migrationContext.InspectorConnectionConfig)
125+
goMySQLReader, err := binlog.NewGoMySQLReader(this.migrationContext, this.migrationContext.InspectorConnectionConfig)
126126
if err != nil {
127127
return err
128128
}

0 commit comments

Comments
 (0)