Skip to content

Commit

Permalink
ddl: let concurrent truncate on the same table depend on the previous…
Browse files Browse the repository at this point in the history
… one (#40501) (#40510)

close #40484
  • Loading branch information
ti-chi-bot authored Feb 1, 2023
1 parent 4a78e5e commit b7a212e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"math"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -1763,3 +1764,55 @@ func TestDDLBlockedCreateView(t *testing.T) {
dom.DDL().SetHook(hook)
tk.MustExec("alter table t modify column a char(10)")
}

func TestMDLTruncateTable(t *testing.T) {
if !variable.EnableConcurrentDDL.Load() {
t.Skipf("test requires concurrent ddl")
}
store, dom := testkit.CreateMockStoreAndDomain(t)

tk := testkit.NewTestKit(t, store)
tk2 := testkit.NewTestKit(t, store)
tk3 := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int);")
tk.MustExec("begin")
tk.MustExec("select * from t for update")

var wg sync.WaitGroup

hook := &ddl.TestDDLCallback{Do: dom}
wg.Add(2)
var timetk2 time.Time
var timetk3 time.Time

one := false
f := func(job *model.Job) {
if !one {
one = true
} else {
return
}
go func() {
tk3.MustExec("truncate table test.t")
timetk3 = time.Now()
wg.Done()
}()
}

hook.OnJobUpdatedExported.Store(&f)
dom.DDL().SetHook(hook)

go func() {
tk2.MustExec("truncate table test.t")
timetk2 = time.Now()
wg.Done()
}()

time.Sleep(2 * time.Second)
timeMain := time.Now()
tk.MustExec("commit")
wg.Wait()
require.True(t, timetk2.After(timeMain))
require.True(t, timetk3.After(timeMain))
}
2 changes: 2 additions & 0 deletions ddl/job_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ func job2UniqueIDs(job *model.Job, schema bool) string {
}
slices.Sort(s)
return strings.Join(s, ",")
case model.ActionTruncateTable:
return strconv.FormatInt(job.TableID, 10) + "," + strconv.FormatInt(job.Args[0].(int64), 10)
}
if schema {
return strconv.FormatInt(job.SchemaID, 10)
Expand Down

0 comments on commit b7a212e

Please sign in to comment.