Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
fix: merge fix that removed deadlock issue when waiting for event up …
Browse files Browse the repository at this point in the history
…to lock

This PR contains a fix for the deadlock issue described in github#992 (which is also discussed here github#1171). The fix is introduced in the original repo here https://github.com/github/gh-ost/pull/1180/files. This PR contains a cherry-pick of that fix
  • Loading branch information
btruhand authored Oct 20, 2023
2 parents 4f3a6b2 + 7b9c256 commit 12cd76b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
14 changes: 6 additions & 8 deletions go/logic/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package logic
import (
gosql "database/sql"
"fmt"
"sync"
"strings"
"sync/atomic"
"time"
Expand Down Expand Up @@ -885,7 +884,7 @@ func (this *Applier) CreateAtomicCutOverSentryTable() error {
}

// AtomicCutOverMagicLock
func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocked chan<- error, okToUnlockTable <-chan bool, tableUnlocked chan<- error, dropCutOverSentryTableOnce *sync.Once) error {
func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocked chan<- error, okToUnlockTable <-chan bool, tableUnlocked chan<- error) error {
tx, err := this.db.Begin()
if err != nil {
tableLocked <- err
Expand All @@ -896,6 +895,7 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
tableLocked <- fmt.Errorf("Unexpected error in AtomicCutOverMagicLock(), injected to release blocking channel reads")
tableUnlocked <- fmt.Errorf("Unexpected error in AtomicCutOverMagicLock(), injected to release blocking channel reads")
tx.Rollback()
this.DropAtomicCutOverSentryTableIfExists()
}()

var sessionId int64
Expand Down Expand Up @@ -964,12 +964,10 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
sql.EscapeName(this.migrationContext.GetOldTableName()),
)

dropCutOverSentryTableOnce.Do(func() {
if _, err := tx.Exec(query); err != nil {
this.migrationContext.Log.Errore(err)
// We DO NOT return here because we must `UNLOCK TABLES`!
}
})
if _, err := tx.Exec(query); err != nil {
this.migrationContext.Log.Errore(err)
// We DO NOT return here because we must `UNLOCK TABLES`!
}

// Tables still locked
this.migrationContext.Log.Infof("Releasing lock from %s.%s, %s.%s",
Expand Down
7 changes: 1 addition & 6 deletions go/logic/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"math"
"os"
"strings"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -650,12 +649,8 @@ func (this *Migrator) atomicCutOver() (err error) {
defer atomic.StoreInt64(&this.migrationContext.InCutOverCriticalSectionFlag, 0)

okToUnlockTable := make(chan bool, 4)
var dropCutOverSentryTableOnce sync.Once
defer func() {
okToUnlockTable <- true
dropCutOverSentryTableOnce.Do(func() {
this.applier.DropAtomicCutOverSentryTableIfExists()
})
}()

atomic.StoreInt64(&this.migrationContext.AllEventsUpToLockProcessedInjectedFlag, 0)
Expand All @@ -664,7 +659,7 @@ func (this *Migrator) atomicCutOver() (err error) {
tableLocked := make(chan error, 2)
tableUnlocked := make(chan error, 2)
go func() {
if err := this.applier.AtomicCutOverMagicLock(lockOriginalSessionIdChan, tableLocked, okToUnlockTable, tableUnlocked, &dropCutOverSentryTableOnce); err != nil {
if err := this.applier.AtomicCutOverMagicLock(lockOriginalSessionIdChan, tableLocked, okToUnlockTable, tableUnlocked); err != nil {
this.migrationContext.Log.Errore(err)
}
}()
Expand Down

0 comments on commit 12cd76b

Please sign in to comment.