Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: fix data race in GetDirtyTable() #12767

Merged
Merged
5 changes: 3 additions & 2 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,9 @@ func (b *executorBuilder) buildUnionScanFromReader(reader Executor, v *plannerco
// GetDirtyDB() is safe here. If this table has been modified in the transaction, non-nil DirtyTable
// can be found in DirtyDB now, so GetDirtyTable is safe; if this table has not been modified in the
// transaction, empty DirtyTable would be inserted into DirtyDB, it does not matter when multiple
// goroutines write empty DirtyTable to DirtyDB for this table concurrently. Thus we don't use lock
// to synchronize here.
// goroutines write empty DirtyTable to DirtyDB for this table concurrently. Although the DirtyDB looks
// safe for data race in all the cases, the map of golang will throw panic when it's accessed in parallel.
// So we lock it when getting dirty table.
physicalTableID := getPhysicalTableID(x.table)
us.dirty = GetDirtyDB(b.ctx).GetDirtyTable(physicalTableID)
us.conditions = v.Conditions
Expand Down
7 changes: 7 additions & 0 deletions executor/union_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package executor

import (
"context"
"sync"

"github.com/pingcap/errors"
"github.com/pingcap/parser/model"
Expand All @@ -28,12 +29,17 @@ import (
// DirtyDB stores uncommitted write operations for a transaction.
// It is stored and retrieved by context.Value and context.SetValue method.
type DirtyDB struct {
sync.Mutex

// tables is a map whose key is tableID.
tables map[int64]*DirtyTable
}

// GetDirtyTable gets the DirtyTable by id from the DirtyDB.
func (udb *DirtyDB) GetDirtyTable(tid int64) *DirtyTable {
// The index join access the tables map parallelly.
// But the map throws panic in this case. So it's locked.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also update the comment in table reader case of buildUnionScanFromReader? It is outdated now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

udb.Lock()
dt, ok := udb.tables[tid]
if !ok {
dt = &DirtyTable{
Expand All @@ -43,6 +49,7 @@ func (udb *DirtyDB) GetDirtyTable(tid int64) *DirtyTable {
}
udb.tables[tid] = dt
}
udb.Unlock()
return dt
}

Expand Down