forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_table_test.go
244 lines (218 loc) · 6.67 KB
/
job_table_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ddl_test
import (
"sync"
"testing"
"time"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)
// TestDDLScheduling tests the DDL scheduling. See Concurrent DDL RFC for the rules of DDL scheduling.
// This test checks the chosen job records to see if there are wrong scheduling, if job A and job B cannot run concurrently,
// then the all the record of job A must before or after job B, no cross record between these 2 jobs should be in between.
func TestDDLScheduling(t *testing.T) {
if !variable.EnableConcurrentDDL.Load() {
t.Skipf("test requires concurrent ddl")
}
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("CREATE TABLE e (id INT NOT NULL) PARTITION BY RANGE (id) (PARTITION p1 VALUES LESS THAN (50), PARTITION p2 VALUES LESS THAN (100));")
tk.MustExec("CREATE TABLE e2 (id INT NOT NULL);")
tk.MustExec("CREATE TABLE e3 (id INT NOT NULL);")
d := dom.DDL()
ddlJobs := []string{
"alter table e2 add index idx(id)",
"alter table e2 add index idx1(id)",
"alter table e2 add index idx2(id)",
"create table e5 (id int)",
"ALTER TABLE e EXCHANGE PARTITION p1 WITH TABLE e2;",
"alter table e add index idx(id)",
"alter table e add partition (partition p3 values less than (150))",
"create table e4 (id int)",
"alter table e3 add index idx1(id)",
"ALTER TABLE e EXCHANGE PARTITION p1 WITH TABLE e3;",
}
hook := &ddl.TestDDLCallback{}
var wg util.WaitGroupWrapper
wg.Add(1)
var once sync.Once
hook.OnGetJobBeforeExported = func(jobType string) {
once.Do(func() {
for i, job := range ddlJobs {
wg.Run(func() {
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_exchange_partition=1")
recordSet, _ := tk.Exec(job)
if recordSet != nil {
require.NoError(t, recordSet.Close())
}
})
for {
time.Sleep(time.Millisecond * 100)
jobs, err := ddl.GetAllDDLJobs(testkit.NewTestKit(t, store).Session(), nil)
require.NoError(t, err)
if len(jobs) == i+1 {
break
}
}
}
wg.Done()
})
}
record := make([]int64, 0, 16)
hook.OnGetJobAfterExported = func(jobType string, job *model.Job) {
// record the job schedule order
record = append(record, job.ID)
}
err := failpoint.Enable("github.com/pingcap/tidb/ddl/mockRunJobTime", `return(true)`)
require.NoError(t, err)
defer func() {
err := failpoint.Disable("github.com/pingcap/tidb/ddl/mockRunJobTime")
require.NoError(t, err)
}()
d.SetHook(hook)
wg.Wait()
// sort all the job id.
ids := make(map[int64]struct{}, 16)
for _, id := range record {
ids[id] = struct{}{}
}
sortedIDs := make([]int64, 0, 16)
for id := range ids {
sortedIDs = append(sortedIDs, id)
}
slices.Sort(sortedIDs)
// map the job id to the DDL sequence.
// sortedIDs may looks like [30, 32, 34, 36, ...], it is the same order with the job in `ddlJobs`, 30 is the first job in `ddlJobs`, 32 is second...
// record may looks like [30, 30, 32, 32, 34, 32, 36, 34, ...]
// and the we map the record to the DDL sequence, [0, 0, 1, 1, 2, 1, 3, 2, ...]
for i := range record {
idx, b := slices.BinarySearch(sortedIDs, record[i])
require.True(t, b)
record[i] = int64(idx)
}
check(t, record, 0, 1, 2)
check(t, record, 0, 4)
check(t, record, 1, 4)
check(t, record, 2, 4)
check(t, record, 4, 5)
check(t, record, 4, 6)
check(t, record, 4, 9)
check(t, record, 5, 6)
check(t, record, 5, 9)
check(t, record, 6, 9)
check(t, record, 8, 9)
}
// check will check if there are any cross between ids.
// e.g. if ids is [1, 2] this function checks all `1` is before or after than `2` in record.
func check(t *testing.T, record []int64, ids ...int64) {
// have return true if there are any `i` is before `j`, false if there are any `j` is before `i`.
have := func(i, j int64) bool {
for _, id := range record {
if id == i {
return true
}
if id == j {
return false
}
}
require.FailNow(t, "should not reach here", record)
return false
}
// all checks if all `i` is before `j`.
all := func(i, j int64) {
meet := false
for _, id := range record {
if id == j {
meet = true
}
require.False(t, meet && id == i, record)
}
}
for i := 0; i < len(ids)-1; i++ {
for j := i + 1; j < len(ids); j++ {
if have(ids[i], ids[j]) {
all(ids[i], ids[j])
} else {
all(ids[j], ids[i])
}
}
}
}
func TestAlwaysChoiceProcessingJob(t *testing.T) {
if !variable.EnableConcurrentDDL.Load() {
t.Skipf("test requires concurrent ddl")
}
store, dom := testkit.CreateMockStoreAndDomain(t)
d := dom.DDL()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int, b int)")
ddlJobs := []string{
"alter table t add index idx(a)",
"alter table t add index idx(b)",
}
hook := &ddl.TestDDLCallback{}
var wg util.WaitGroupWrapper
wg.Add(1)
var once sync.Once
var idxa, idxb int64
hook.OnGetJobBeforeExported = func(jobType string) {
once.Do(func() {
var jobs []*model.Job
for i, job := range ddlJobs {
wg.Run(func() {
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
recordSet, _ := tk.Exec(job)
if recordSet != nil {
require.NoError(t, recordSet.Close())
}
})
for {
time.Sleep(time.Millisecond * 100)
var err error
jobs, err = ddl.GetAllDDLJobs(testkit.NewTestKit(t, store).Session(), nil)
require.NoError(t, err)
if len(jobs) == i+1 {
break
}
}
}
idxa = jobs[0].ID
idxb = jobs[1].ID
require.Greater(t, idxb, idxa)
tk := testkit.NewTestKit(t, store)
tk.MustExec("update mysql.tidb_ddl_job set processing = 1 where job_id = ?", idxb)
wg.Done()
})
}
record := make([]int64, 0, 16)
hook.OnGetJobAfterExported = func(jobType string, job *model.Job) {
// record the job schedule order
record = append(record, job.ID)
}
d.SetHook(hook)
wg.Wait()
check(t, record, idxb, idxa)
}