@@ -1099,7 +1099,7 @@ func (this *Applier) RevertAtomicCutOverWaitTimeout() {
1099
1099
}
1100
1100
1101
1101
// AtomicCutOverMagicLock
1102
- func (this * Applier ) AtomicCutOverMagicLock (sessionIdChan chan int64 , tableLocked chan <- error , okToUnlockTable <- chan bool , tableUnlocked chan <- error ) error {
1102
+ func (this * Applier ) AtomicCutOverMagicLock (sessionIdChan chan int64 , tableLocked chan <- error , okToUnlockTable <- chan bool , tableUnlocked chan <- error , renameLockSessionId * int64 ) error {
1103
1103
tx , err := this .db .Begin ()
1104
1104
if err != nil {
1105
1105
tableLocked <- err
@@ -1190,6 +1190,23 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
1190
1190
// We DO NOT return here because we must `UNLOCK TABLES`!
1191
1191
}
1192
1192
1193
+ this .migrationContext .Log .Infof ("Session renameLockSessionId is %+v" , * renameLockSessionId )
1194
+ // checking the lock holded by rename session
1195
+ if * renameLockSessionId > 0 {
1196
+ for i := 0 ; i <= 50 ; i ++ {
1197
+ err := this .ExpectMetadataLock (* renameLockSessionId )
1198
+ if err == nil {
1199
+ this .migrationContext .Log .Infof ("Rename session is pending lock on the origin table !" )
1200
+ break
1201
+ } else {
1202
+ if i == 50 {
1203
+ return err
1204
+ }
1205
+ time .Sleep (10 * time .Millisecond )
1206
+ }
1207
+ }
1208
+ }
1209
+
1193
1210
// Tables still locked
1194
1211
this .migrationContext .Log .Infof ("Releasing lock from %s.%s, %s.%s" ,
1195
1212
sql .EscapeName (this .migrationContext .DatabaseName ),
@@ -1409,3 +1426,27 @@ func (this *Applier) Teardown() {
1409
1426
this .singletonDB .Close ()
1410
1427
atomic .StoreInt64 (& this .finishedMigrating , 1 )
1411
1428
}
1429
+
1430
+ func (this * Applier ) ExpectMetadataLock (sessionId int64 ) error {
1431
+ found := false
1432
+ query := `
1433
+ select /* gh-ost */ m.owner_thread_id
1434
+ from performance_schema.metadata_locks m join performance_schema.threads t
1435
+ on m.owner_thread_id=t.thread_id
1436
+ where m.object_type = 'TABLE' and m.object_schema = ? and m.object_name = ?
1437
+ and m.lock_type = 'EXCLUSIVE' and m.lock_status = 'PENDING'
1438
+ and t.processlist_id = ?
1439
+ `
1440
+ err := sqlutils .QueryRowsMap (this .db , query , func (m sqlutils.RowMap ) error {
1441
+ found = true
1442
+ return nil
1443
+ }, this .migrationContext .DatabaseName , this .migrationContext .OriginalTableName , sessionId )
1444
+ if err != nil {
1445
+ return err
1446
+ }
1447
+ if ! found {
1448
+ err = fmt .Errorf ("cannot find PENDING metadata lock on original table: `%s`.`%s`" , this .migrationContext .DatabaseName , this .migrationContext .OriginalTableName )
1449
+ return this .migrationContext .Log .Errore (err )
1450
+ }
1451
+ return nil
1452
+ }
0 commit comments