@@ -100,7 +100,7 @@ func (this *Applier) InitDBConnections() (err error) {
100100 if err := this .validateAndReadGlobalVariables (); err != nil {
101101 return err
102102 }
103- if ! this .migrationContext .AliyunRDS && ! this .migrationContext .GoogleCloudPlatform && ! this .migrationContext .AzureMySQL {
103+ if ! this .migrationContext .AliyunRDS && ! this .migrationContext .GoogleCloudPlatform && ! this .migrationContext .AzureMySQL && ! this . migrationContext . OceanBase {
104104 if impliedKey , err := mysql .GetInstanceKey (this .db ); err != nil {
105105 return err
106106 } else {
@@ -773,24 +773,28 @@ func (this *Applier) ApplyIterationInsertQuery() (chunkSize int64, rowsAffected
773773 return chunkSize , rowsAffected , duration , nil
774774}
775775
776- // LockOriginalTable places a write lock on the original table
777- func (this * Applier ) LockOriginalTable () error {
778- query := fmt .Sprintf (`lock /* gh-ost */ tables %s.%s write` ,
779- sql .EscapeName (this .migrationContext .DatabaseName ),
780- sql .EscapeName (this .migrationContext .OriginalTableName ),
781- )
782- this .migrationContext .Log .Infof ("Locking %s.%s" ,
783- sql .EscapeName (this .migrationContext .DatabaseName ),
784- sql .EscapeName (this .migrationContext .OriginalTableName ),
785- )
776+ // lockTable places a write lock on the specific table
777+ func (this * Applier ) lockTable (databaseName , tableName string ) error {
778+ query := fmt .Sprintf (`lock /* gh-ost */ tables %s.%s write` , databaseName , tableName )
779+ this .migrationContext .Log .Infof ("Locking %s.%s" , databaseName , tableName )
786780 this .migrationContext .LockTablesStartTime = time .Now ()
787781 if _ , err := sqlutils .ExecNoPrepare (this .singletonDB , query ); err != nil {
788782 return err
789783 }
790- this .migrationContext .Log .Infof ("Table locked" )
784+ this .migrationContext .Log .Infof ("Table %s.%s locked" , databaseName , tableName )
791785 return nil
792786}
793787
788+ // LockOriginalTable places a write lock on the original table
789+ func (this * Applier ) LockOriginalTable () error {
790+ return this .lockTable (this .migrationContext .DatabaseName , this .migrationContext .OriginalTableName )
791+ }
792+
793+ // LockGhostTable places a write lock on the ghost table
794+ func (this * Applier ) LockGhostTable () error {
795+ return this .lockTable (this .migrationContext .DatabaseName , this .migrationContext .GetGhostTableName ())
796+ }
797+
794798// UnlockTables makes tea. No wait, it unlocks tables.
795799func (this * Applier ) UnlockTables () error {
796800 query := `unlock /* gh-ost */ tables`
@@ -1096,7 +1100,7 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
10961100
10971101 tableLockTimeoutSeconds := this .migrationContext .CutOverLockTimeoutSeconds * 2
10981102 this .migrationContext .Log .Infof ("Setting LOCK timeout as %d seconds" , tableLockTimeoutSeconds )
1099- query = fmt .Sprintf (`set /* gh-ost */ session lock_wait_timeout: =%d` , tableLockTimeoutSeconds )
1103+ query = fmt .Sprintf (`set /* gh-ost */ session lock_wait_timeout=%d` , tableLockTimeoutSeconds )
11001104 if _ , err := tx .Exec (query ); err != nil {
11011105 tableLocked <- err
11021106 return err
@@ -1171,25 +1175,31 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
11711175 return nil
11721176}
11731177
1174- // AtomicCutoverRename
1175- func (this * Applier ) AtomicCutoverRename (sessionIdChan chan int64 , tablesRenamed chan <- error ) error {
1176- tx , err := this .db .Begin ()
1178+ func (this * Applier ) atomicCutoverRename (db * gosql.DB , sessionIdChan chan int64 , tablesRenamed chan <- error ) error {
1179+ tx , err := db .Begin ()
11771180 if err != nil {
11781181 return err
11791182 }
11801183 defer func () {
11811184 tx .Rollback ()
1182- sessionIdChan <- - 1
1183- tablesRenamed <- fmt .Errorf ("Unexpected error in AtomicCutoverRename(), injected to release blocking channel reads" )
1185+ if sessionIdChan != nil {
1186+ sessionIdChan <- - 1
1187+ }
1188+ if tablesRenamed != nil {
1189+ tablesRenamed <- fmt .Errorf ("Unexpected error in AtomicCutoverRename(), injected to release blocking channel reads" )
1190+ }
11841191 }()
1185- var sessionId int64
1186- if err := tx .QueryRow (`select /* gh-ost */ connection_id()` ).Scan (& sessionId ); err != nil {
1187- return err
1192+
1193+ if sessionIdChan != nil {
1194+ var sessionId int64
1195+ if err := tx .QueryRow (`select /* gh-ost */ connection_id()` ).Scan (& sessionId ); err != nil {
1196+ return err
1197+ }
1198+ sessionIdChan <- sessionId
11881199 }
1189- sessionIdChan <- sessionId
11901200
11911201 this .migrationContext .Log .Infof ("Setting RENAME timeout as %d seconds" , this .migrationContext .CutOverLockTimeoutSeconds )
1192- query := fmt .Sprintf (`set /* gh-ost */ session lock_wait_timeout: =%d` , this .migrationContext .CutOverLockTimeoutSeconds )
1202+ query := fmt .Sprintf (`set /* gh-ost */ session lock_wait_timeout=%d` , this .migrationContext .CutOverLockTimeoutSeconds )
11931203 if _ , err := tx .Exec (query ); err != nil {
11941204 return err
11951205 }
@@ -1206,14 +1216,28 @@ func (this *Applier) AtomicCutoverRename(sessionIdChan chan int64, tablesRenamed
12061216 )
12071217 this .migrationContext .Log .Infof ("Issuing and expecting this to block: %s" , query )
12081218 if _ , err := tx .Exec (query ); err != nil {
1209- tablesRenamed <- err
1219+ if tablesRenamed != nil {
1220+ tablesRenamed <- err
1221+ }
12101222 return this .migrationContext .Log .Errore (err )
12111223 }
1212- tablesRenamed <- nil
1224+ if tablesRenamed != nil {
1225+ tablesRenamed <- nil
1226+ }
12131227 this .migrationContext .Log .Infof ("Tables renamed" )
12141228 return nil
12151229}
12161230
1231+ // AtomicCutoverRename renames tables for atomic cut over in non lock session
1232+ func (this * Applier ) AtomicCutoverRename (sessionIdChan chan int64 , tablesRenamed chan <- error ) error {
1233+ return this .atomicCutoverRename (this .db , sessionIdChan , tablesRenamed )
1234+ }
1235+
1236+ // AtomicCutoverRenameWithLock renames tables for atomic cut over in the lock session
1237+ func (this * Applier ) AtomicCutoverRenameWithLock () error {
1238+ return this .atomicCutoverRename (this .singletonDB , nil , nil )
1239+ }
1240+
12171241func (this * Applier ) ShowStatusVariable (variableName string ) (result int64 , err error ) {
12181242 query := fmt .Sprintf (`show /* gh-ost */ global status like '%s'` , variableName )
12191243 if err := this .db .QueryRow (query ).Scan (& variableName , & result ); err != nil {
0 commit comments