Skip to content

Commit de1ced3

Browse files
author
dbking
committed
fix: add AllowSetupMetadataLockInstruments Compatible with MySQL 5.7 and previous versions
1 parent 6e3a0ed commit de1ced3

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

go/base/context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ type MigrationContext struct {
247247

248248
recentBinlogCoordinates mysql.BinlogCoordinates
249249

250-
BinlogSyncerMaxReconnectAttempts int
250+
BinlogSyncerMaxReconnectAttempts int
251+
AllowSetupMetadataLockInstruments bool
252+
IsOpenMetadataLockInstruments bool
251253

252254
Log Logger
253255
}

go/cmd/gh-ost/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func main() {
137137
flag.Int64Var(&migrationContext.HooksStatusIntervalSec, "hooks-status-interval", 60, "how many seconds to wait between calling onStatus hook")
138138

139139
flag.UintVar(&migrationContext.ReplicaServerId, "replica-server-id", 99999, "server id used by gh-ost process. Default: 99999")
140+
flag.BoolVar(&migrationContext.AllowSetupMetadataLockInstruments, "allow-setup-metadata-lock-instruments", false, "validate rename session hold the MDL of original table before unlock tables in cut-over phase")
140141
flag.IntVar(&migrationContext.BinlogSyncerMaxReconnectAttempts, "binlogsyncer-max-reconnect-attempts", 0, "when master node fails, the maximum number of binlog synchronization attempts to reconnect. 0 is unlimited")
141142

142143
flag.BoolVar(&migrationContext.IncludeTriggers, "include-triggers", false, "When true, the triggers (if exist) will be created on the new table")

go/logic/applier.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,28 @@ func (this *Applier) dropTable(tableName string) error {
416416
return nil
417417
}
418418

419+
func (this *Applier) StateMetadataLockInstrument() error {
420+
query := `select /*+ MAX_EXECUTION_TIME(300) */ ENABLED, TIMED from performance_schema.setup_instruments WHERE NAME = 'wait/lock/metadata/sql/mdl'`
421+
var enabled, timed string
422+
if err := this.db.QueryRow(query).Scan(&enabled, &timed); err != nil {
423+
return this.migrationContext.Log.Errorf("query performance_schema.setup_instruments with name wait/lock/metadata/sql/mdl error: %s", err)
424+
}
425+
if strings.EqualFold(enabled, "YES") && strings.EqualFold(timed, "YES") {
426+
this.migrationContext.IsOpenMetadataLockInstruments = true
427+
return nil
428+
}
429+
if !this.migrationContext.AllowSetupMetadataLockInstruments {
430+
return nil
431+
}
432+
this.migrationContext.Log.Infof("instrument wait/lock/metadata/sql/mdl state: enabled %s, timed %s", enabled, timed)
433+
if _, err := this.db.Exec(`UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES' WHERE NAME = 'wait/lock/metadata/sql/mdl'`); err != nil {
434+
return this.migrationContext.Log.Errorf("enable instrument wait/lock/metadata/sql/mdl error: %s", err)
435+
}
436+
this.migrationContext.IsOpenMetadataLockInstruments = true
437+
this.migrationContext.Log.Infof("instrument wait/lock/metadata/sql/mdl enabled")
438+
return nil
439+
}
440+
419441
// dropTriggers drop the triggers on the applied host
420442
func (this *Applier) DropTriggersFromGhost() error {
421443
if len(this.migrationContext.Triggers) > 0 {
@@ -1192,7 +1214,7 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
11921214

11931215
this.migrationContext.Log.Infof("Session renameLockSessionId is %+v", *renameLockSessionId)
11941216
// checking the lock holded by rename session
1195-
if *renameLockSessionId > 0 {
1217+
if *renameLockSessionId > 0 && this.migrationContext.IsOpenMetadataLockInstruments {
11961218
sleepDuration := time.Duration(10*this.migrationContext.CutOverLockTimeoutSeconds) * time.Millisecond
11971219
for i := 1; i <= 100; i++ {
11981220
err := this.ExpectMetadataLock(*renameLockSessionId)

go/logic/migrator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,10 @@ func (this *Migrator) initiateApplier() error {
12051205
}
12061206
}
12071207
this.applier.WriteChangelogState(string(GhostTableMigrated))
1208+
if err := this.applier.StateMetadataLockInstrument(); err != nil {
1209+
this.migrationContext.Log.Errorf("Unable to enable metadata lock instrument, see further error details. Bailing out")
1210+
return err
1211+
}
12081212
go this.applier.InitiateHeartbeat()
12091213
return nil
12101214
}

go/logic/migrator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,18 @@ func (suite *MigratorTestSuite) TestCutOverLossDataCaseLockGhostBeforeRename() {
477477
migrationContext.ThrottleHTTPTimeoutMillis = 1000
478478
migrationContext.CutOverLockTimeoutSeconds = 4
479479

480-
//nolint:dogsled
481480
_, filename, _, _ := runtime.Caller(0)
482481
migrationContext.ServeSocketFile = filepath.Join(filepath.Dir(filename), "../../tmp/gh-ost.sock")
483482
migrationContext.PostponeCutOverFlagFile = filepath.Join(filepath.Dir(filename), "../../tmp/ghost.postpone.flag")
484483

485484
migrator := NewMigrator(migrationContext, "0.0.0")
486485

486+
//nolint:contextcheck
487487
done <- migrator.Migrate()
488488
}()
489489

490490
time.Sleep(2 * time.Second)
491+
//nolint:dogsled
491492
_, filename, _, _ := runtime.Caller(0)
492493
err = os.Remove(filepath.Join(filepath.Dir(filename), "../../tmp/ghost.postpone.flag"))
493494
if err != nil {
@@ -528,7 +529,6 @@ func (suite *MigratorTestSuite) TestCutOverLossDataCaseLockGhostBeforeRename() {
528529

529530
suite.Require().Equal("testing", tableName)
530531
suite.Require().Equal("CREATE TABLE `testing` (\n `id` int NOT NULL,\n `name` varchar(64) DEFAULT NULL,\n `foobar` varchar(255) DEFAULT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", createTableSQL)
531-
532532
}
533533

534534
func TestMigrator(t *testing.T) {

0 commit comments

Comments
 (0)