Skip to content

Commit 49262c5

Browse files
committed
use mutex to avoid to send drop cutover sentry table to mysql twice
1 parent b38814f commit 49262c5

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

go/logic/applier.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/outbrain/golib/log"
2020
"github.com/outbrain/golib/sqlutils"
21+
"sync"
2122
)
2223

2324
const (
@@ -52,11 +53,12 @@ func newDmlBuildResultError(err error) *dmlBuildResult {
5253
// Applier is the one to actually write row data and apply binlog events onto the ghost table.
5354
// It is where the ghost & changelog tables get created. It is where the cut-over phase happens.
5455
type Applier struct {
55-
connectionConfig *mysql.ConnectionConfig
56-
db *gosql.DB
57-
singletonDB *gosql.DB
58-
migrationContext *base.MigrationContext
59-
finishedMigrating int64
56+
connectionConfig *mysql.ConnectionConfig
57+
db *gosql.DB
58+
singletonDB *gosql.DB
59+
migrationContext *base.MigrationContext
60+
finishedMigrating int64
61+
dropAtomicCutOverSentryTableMutex sync.Mutex
6062
}
6163

6264
func NewApplier(migrationContext *base.MigrationContext) *Applier {
@@ -738,6 +740,9 @@ func (this *Applier) ExpectProcess(sessionId int64, stateHint, infoHint string)
738740
// DropAtomicCutOverSentryTableIfExists checks if the "old" table name
739741
// happens to be a cut-over magic table; if so, it drops it.
740742
func (this *Applier) DropAtomicCutOverSentryTableIfExists() error {
743+
this.dropAtomicCutOverSentryTableMutex.Lock()
744+
defer this.dropAtomicCutOverSentryTableMutex.Unlock()
745+
741746
log.Infof("Looking for magic cut-over table")
742747
tableName := this.migrationContext.GetOldTableName()
743748
rowMap := this.showTableStatus(tableName)
@@ -854,6 +859,7 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
854859

855860
// The magic table is here because we locked it. And we are the only ones allowed to drop it.
856861
// And in fact, we will:
862+
this.dropAtomicCutOverSentryTableMutex.Lock()
857863
log.Infof("Dropping magic cut-over table")
858864
query = fmt.Sprintf(`drop /* gh-ost */ table if exists %s.%s`,
859865
sql.EscapeName(this.migrationContext.DatabaseName),
@@ -863,6 +869,7 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
863869
log.Errore(err)
864870
// We DO NOT return here because we must `UNLOCK TABLES`!
865871
}
872+
this.dropAtomicCutOverSentryTableMutex.Unlock()
866873

867874
// Tables still locked
868875
log.Infof("Releasing lock from %s.%s, %s.%s",

0 commit comments

Comments
 (0)