Skip to content

Commit a1473df

Browse files
author
Nikhil Mathew
committed
Stop infinite goroutines
1 parent 7412f42 commit a1473df

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

go/logic/applier.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ const (
3030
// Applier is the one to actually write row data and apply binlog events onto the ghost table.
3131
// It is where the ghost & changelog tables get created. It is where the cut-over phase happens.
3232
type Applier struct {
33-
connectionConfig *mysql.ConnectionConfig
34-
db *gosql.DB
35-
singletonDB *gosql.DB
36-
migrationContext *base.MigrationContext
33+
connectionConfig *mysql.ConnectionConfig
34+
db *gosql.DB
35+
singletonDB *gosql.DB
36+
migrationContext *base.MigrationContext
37+
finishedMigrating bool
3738
}
3839

3940
func NewApplier(migrationContext *base.MigrationContext) *Applier {
4041
return &Applier{
41-
connectionConfig: migrationContext.ApplierConnectionConfig,
42-
migrationContext: migrationContext,
42+
connectionConfig: migrationContext.ApplierConnectionConfig,
43+
migrationContext: migrationContext,
44+
finishedMigrating: false,
4345
}
4446
}
4547

@@ -288,6 +290,10 @@ func (this *Applier) WriteChangelogState(value string) (string, error) {
288290
return this.WriteAndLogChangelog("state", value)
289291
}
290292

293+
func (this *Applier) FinalCleanup() {
294+
this.finishedMigrating = true
295+
}
296+
291297
// InitiateHeartbeat creates a heartbeat cycle, writing to the changelog table.
292298
// This is done asynchronously
293299
func (this *Applier) InitiateHeartbeat() {
@@ -310,6 +316,9 @@ func (this *Applier) InitiateHeartbeat() {
310316

311317
heartbeatTick := time.Tick(time.Duration(this.migrationContext.HeartbeatIntervalMilliseconds) * time.Millisecond)
312318
for range heartbeatTick {
319+
if this.finishedMigrating {
320+
return
321+
}
313322
// Generally speaking, we would issue a goroutine, but I'd actually rather
314323
// have this block the loop rather than spam the master in the event something
315324
// goes wrong

go/logic/migrator.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ type Migrator struct {
8383
applyEventsQueue chan *applyEventStruct
8484

8585
handledChangelogStates map[string]bool
86+
87+
finishedMigrating bool
8688
}
8789

8890
func NewMigrator(context *base.MigrationContext) *Migrator {
@@ -97,6 +99,7 @@ func NewMigrator(context *base.MigrationContext) *Migrator {
9799
copyRowsQueue: make(chan tableWriteFunc),
98100
applyEventsQueue: make(chan *applyEventStruct, base.MaxEventsBatchSize),
99101
handledChangelogStates: make(map[string]bool),
102+
finishedMigrating: false,
100103
}
101104
return migrator
102105
}
@@ -718,6 +721,9 @@ func (this *Migrator) initiateStatus() error {
718721
this.printStatus(ForcePrintStatusAndHintRule)
719722
statusTick := time.Tick(1 * time.Second)
720723
for range statusTick {
724+
if this.finishedMigrating {
725+
return nil
726+
}
721727
go this.printStatus(HeuristicPrintStatusRule)
722728
}
723729

@@ -942,6 +948,9 @@ func (this *Migrator) initiateStreaming() error {
942948
go func() {
943949
ticker := time.Tick(1 * time.Second)
944950
for range ticker {
951+
if this.finishedMigrating {
952+
return
953+
}
945954
this.migrationContext.SetRecentBinlogCoordinates(*this.eventsStreamer.GetCurrentBinlogCoordinates())
946955
}
947956
}()
@@ -1132,6 +1141,10 @@ func (this *Migrator) executeWriteFuncs() error {
11321141
return nil
11331142
}
11341143
for {
1144+
if this.finishedMigrating {
1145+
return nil
1146+
}
1147+
11351148
this.throttler.throttle(nil)
11361149

11371150
// We give higher priority to event processing, then secondary priority to
@@ -1209,5 +1222,8 @@ func (this *Migrator) finalCleanup() error {
12091222
}
12101223
}
12111224

1225+
this.finishedMigrating = true
1226+
this.applier.FinalCleanup()
1227+
12121228
return nil
12131229
}

0 commit comments

Comments
 (0)