Skip to content

Commit 0a7e713

Browse files
committed
Ensure cleanup happens, even on error
1 parent e4bb70d commit 0a7e713

6 files changed

Lines changed: 40 additions & 12 deletions

File tree

go/logic/applier.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,6 @@ func (this *Applier) WriteChangelogState(value string) (string, error) {
290290
return this.WriteAndLogChangelog("state", value)
291291
}
292292

293-
func (this *Applier) FinalCleanup() {
294-
this.db.Close()
295-
this.singletonDB.Close()
296-
this.finishedMigrating = true
297-
}
298-
299293
// InitiateHeartbeat creates a heartbeat cycle, writing to the changelog table.
300294
// This is done asynchronously
301295
func (this *Applier) InitiateHeartbeat() {
@@ -1044,3 +1038,10 @@ func (this *Applier) ApplyDMLEventQueries(dmlEvents [](*binlog.BinlogDMLEvent))
10441038
log.Debugf("ApplyDMLEventQueries() applied %d events in one transaction", len(dmlEvents))
10451039
return nil
10461040
}
1041+
1042+
func (this *Applier) Teardown() {
1043+
log.Debugf("Tearing down...")
1044+
this.db.Close()
1045+
this.singletonDB.Close()
1046+
this.finishedMigrating = true
1047+
}

go/logic/inspect.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,8 @@ func (this *Inspector) getReplicationLag() (replicationLag time.Duration, err er
723723
)
724724
return replicationLag, err
725725
}
726+
727+
func (this *Inspector) Teardown() {
728+
this.db.Close()
729+
return
730+
}

go/logic/migrator.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ func (this *Migrator) Migrate() (err error) {
291291
if err := this.validateStatement(); err != nil {
292292
return err
293293
}
294+
295+
// After this point, we'll need to teardown anything that's been started
296+
// so we don't leave things hanging around
297+
defer this.teardown()
298+
294299
if err := this.initiateInspector(); err != nil {
295300
return err
296301
}
@@ -1223,10 +1228,26 @@ func (this *Migrator) finalCleanup() error {
12231228
}
12241229
}
12251230

1231+
return nil
1232+
}
1233+
1234+
func (this *Migrator) teardown() {
12261235
this.finishedMigrating = true
1227-
this.applier.FinalCleanup()
1228-
this.eventsStreamer.FinalCleanup()
1229-
sqlutils.ResetDBCache()
12301236

1231-
return nil
1237+
if this.inspector != nil {
1238+
log.Infof("Tearing down inspector")
1239+
this.inspector.Teardown()
1240+
}
1241+
1242+
if this.applier != nil {
1243+
log.Infof("Tearing down applier")
1244+
this.applier.Teardown()
1245+
}
1246+
1247+
if this.eventsStreamer != nil {
1248+
log.Infof("Tearing down streamer")
1249+
this.eventsStreamer.Teardown()
1250+
}
1251+
1252+
sqlutils.ResetDBCache()
12321253
}

go/logic/streamer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func (this *EventsStreamer) Close() (err error) {
231231
return err
232232
}
233233

234-
func (this *EventsStreamer) FinalCleanup() {
234+
func (this *EventsStreamer) Teardown() {
235235
this.db.Close()
236236
return
237237
}

go/logic/throttler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func (this *Throttler) collectControlReplicasLag() {
187187
} else if err = db.QueryRow(replicationLagQuery).Scan(&heartbeatValue); err != nil {
188188
return lag, err
189189
}
190+
190191
lag, err = parseChangelogHeartbeat(heartbeatValue)
191192
return lag, err
192193
}

go/mysql/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func GetReplicationLag(connectionConfig *ConnectionConfig) (replicationLag time.
4040
if db, _, err = sqlutils.GetDB(dbUri); err != nil {
4141
return replicationLag, err
4242
}
43+
defer db.Close()
4344

4445
err = sqlutils.QueryRowsMap(db, `show slave status`, func(m sqlutils.RowMap) error {
4546
slaveIORunning := m.GetString("Slave_IO_Running")
@@ -52,7 +53,6 @@ func GetReplicationLag(connectionConfig *ConnectionConfig) (replicationLag time.
5253
return nil
5354
})
5455

55-
db.Close()
5656
return replicationLag, err
5757
}
5858

0 commit comments

Comments
 (0)