forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_change_test.go
385 lines (356 loc) · 10.9 KB
/
index_change_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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright 2016 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 (
"context"
"testing"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
)
func TestIndexChange(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
ddl.SetWaitTimeWhenErrorOccurred(1 * time.Microsecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (c1 int primary key, c2 int)")
tk.MustExec("insert t values (1, 1), (2, 2), (3, 3);")
d := dom.DDL()
tc := &ddl.TestDDLCallback{Do: dom}
// set up hook
prevState := model.StateNone
addIndexDone := false
var jobID int64
var (
deleteOnlyTable table.Table
writeOnlyTable table.Table
publicTable table.Table
)
tc.OnJobUpdatedExported = func(job *model.Job) {
if job.SchemaState == prevState {
return
}
jobID = job.ID
ctx1 := testNewContext(store)
prevState = job.SchemaState
require.NoError(t, dom.Reload())
tbl, exist := dom.InfoSchema().TableByID(job.TableID)
require.True(t, exist)
switch job.SchemaState {
case model.StateDeleteOnly:
deleteOnlyTable = tbl
case model.StateWriteOnly:
writeOnlyTable = tbl
err := checkAddWriteOnlyForAddIndex(ctx1, deleteOnlyTable, writeOnlyTable)
require.NoError(t, err)
case model.StatePublic:
require.Equalf(t, int64(3), job.GetRowCount(), "job's row count %d != 3", job.GetRowCount())
publicTable = tbl
err := checkAddPublicForAddIndex(ctx1, writeOnlyTable, publicTable)
require.NoError(t, err)
if job.State == model.JobStateSynced {
addIndexDone = true
}
}
}
d.SetHook(tc)
tk.MustExec("alter table t add index c2(c2)")
// We need to make sure onJobUpdated is called in the first hook.
// After testCreateIndex(), onJobUpdated() may not be called when job.state is Sync.
// If we skip this check, prevState may wrongly set to StatePublic.
for i := 0; i <= 10; i++ {
if addIndexDone {
break
}
time.Sleep(50 * time.Millisecond)
}
v := getSchemaVer(t, tk.Session())
checkHistoryJobArgs(t, tk.Session(), jobID, &historyJobArgs{ver: v, tbl: publicTable.Meta()})
prevState = model.StateNone
var noneTable table.Table
tc.OnJobUpdatedExported = func(job *model.Job) {
jobID = job.ID
if job.SchemaState == prevState {
return
}
prevState = job.SchemaState
var err error
require.NoError(t, dom.Reload())
tbl, exist := dom.InfoSchema().TableByID(job.TableID)
require.True(t, exist)
ctx1 := testNewContext(store)
switch job.SchemaState {
case model.StateWriteOnly:
writeOnlyTable = tbl
err = checkDropWriteOnly(ctx1, publicTable, writeOnlyTable)
require.NoError(t, err)
case model.StateDeleteOnly:
deleteOnlyTable = tbl
err = checkDropDeleteOnly(ctx1, writeOnlyTable, deleteOnlyTable)
require.NoError(t, err)
case model.StateNone:
noneTable = tbl
require.Equalf(t, 0, len(noneTable.Indices()), "index should have been dropped")
}
}
tk.MustExec("alter table t drop index c2")
v = getSchemaVer(t, tk.Session())
checkHistoryJobArgs(t, tk.Session(), jobID, &historyJobArgs{ver: v, tbl: noneTable.Meta()})
}
func checkIndexExists(ctx sessionctx.Context, tbl table.Table, indexValue interface{}, handle int64, exists bool) error {
idx := tbl.Indices()[0]
txn, err := ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
doesExist, _, err := idx.Exist(ctx.GetSessionVars().StmtCtx, txn, types.MakeDatums(indexValue), kv.IntHandle(handle))
if err != nil {
return errors.Trace(err)
}
if exists != doesExist {
if exists {
return errors.New("index should exists")
}
return errors.New("index should not exists")
}
return nil
}
func checkAddWriteOnlyForAddIndex(ctx sessionctx.Context, delOnlyTbl, writeOnlyTbl table.Table) error {
// DeleteOnlyTable: insert t values (4, 4);
err := sessiontxn.NewTxn(context.Background(), ctx)
if err != nil {
return errors.Trace(err)
}
_, err = delOnlyTbl.AddRecord(ctx, types.MakeDatums(4, 4))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, writeOnlyTbl, 4, 4, false)
if err != nil {
return errors.Trace(err)
}
// WriteOnlyTable: insert t values (5, 5);
_, err = writeOnlyTbl.AddRecord(ctx, types.MakeDatums(5, 5))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, writeOnlyTbl, 5, 5, true)
if err != nil {
return errors.Trace(err)
}
// WriteOnlyTable: update t set c2 = 1 where c1 = 4 and c2 = 4
err = writeOnlyTbl.UpdateRecord(context.Background(), ctx, kv.IntHandle(4), types.MakeDatums(4, 4), types.MakeDatums(4, 1), touchedSlice(writeOnlyTbl))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, writeOnlyTbl, 1, 4, true)
if err != nil {
return errors.Trace(err)
}
// DeleteOnlyTable: update t set c2 = 3 where c1 = 4 and c2 = 1
err = delOnlyTbl.UpdateRecord(context.Background(), ctx, kv.IntHandle(4), types.MakeDatums(4, 1), types.MakeDatums(4, 3), touchedSlice(writeOnlyTbl))
if err != nil {
return errors.Trace(err)
}
// old value index not exists.
err = checkIndexExists(ctx, writeOnlyTbl, 1, 4, false)
if err != nil {
return errors.Trace(err)
}
// new value index not exists.
err = checkIndexExists(ctx, writeOnlyTbl, 3, 4, false)
if err != nil {
return errors.Trace(err)
}
// WriteOnlyTable: delete t where c1 = 4 and c2 = 3
err = writeOnlyTbl.RemoveRecord(ctx, kv.IntHandle(4), types.MakeDatums(4, 3))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, writeOnlyTbl, 3, 4, false)
if err != nil {
return errors.Trace(err)
}
// DeleteOnlyTable: delete t where c1 = 5
err = delOnlyTbl.RemoveRecord(ctx, kv.IntHandle(5), types.MakeDatums(5, 5))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, writeOnlyTbl, 5, 5, false)
if err != nil {
return errors.Trace(err)
}
return nil
}
func checkAddPublicForAddIndex(ctx sessionctx.Context, writeTbl, publicTbl table.Table) error {
// WriteOnlyTable: insert t values (6, 6)
err := sessiontxn.NewTxn(context.Background(), ctx)
if err != nil {
return errors.Trace(err)
}
_, err = writeTbl.AddRecord(ctx, types.MakeDatums(6, 6))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, publicTbl, 6, 6, true)
if err != nil {
return errors.Trace(err)
}
// PublicTable: insert t values (7, 7)
_, err = publicTbl.AddRecord(ctx, types.MakeDatums(7, 7))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, publicTbl, 7, 7, true)
if err != nil {
return errors.Trace(err)
}
// WriteOnlyTable: update t set c2 = 5 where c1 = 7 and c2 = 7
err = writeTbl.UpdateRecord(context.Background(), ctx, kv.IntHandle(7), types.MakeDatums(7, 7), types.MakeDatums(7, 5), touchedSlice(writeTbl))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, publicTbl, 5, 7, true)
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, publicTbl, 7, 7, false)
if err != nil {
return errors.Trace(err)
}
// WriteOnlyTable: delete t where c1 = 6
err = writeTbl.RemoveRecord(ctx, kv.IntHandle(6), types.MakeDatums(6, 6))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, publicTbl, 6, 6, false)
if err != nil {
return errors.Trace(err)
}
var rows [][]types.Datum
err = tables.IterRecords(publicTbl, ctx, publicTbl.Cols(),
func(_ kv.Handle, data []types.Datum, cols []*table.Column) (bool, error) {
rows = append(rows, data)
return true, nil
})
if err != nil {
return errors.Trace(err)
}
if len(rows) == 0 {
return errors.New("table is empty")
}
for _, row := range rows {
idxVal := row[1].GetInt64()
handle := row[0].GetInt64()
err = checkIndexExists(ctx, publicTbl, idxVal, handle, true)
if err != nil {
return errors.Trace(err)
}
}
txn, err := ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
return txn.Commit(context.Background())
}
func checkDropWriteOnly(ctx sessionctx.Context, publicTbl, writeTbl table.Table) error {
// WriteOnlyTable insert t values (8, 8)
err := sessiontxn.NewTxn(context.Background(), ctx)
if err != nil {
return errors.Trace(err)
}
_, err = writeTbl.AddRecord(ctx, types.MakeDatums(8, 8))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, publicTbl, 8, 8, true)
if err != nil {
return errors.Trace(err)
}
// WriteOnlyTable update t set c2 = 7 where c1 = 8 and c2 = 8
err = writeTbl.UpdateRecord(context.Background(), ctx, kv.IntHandle(8), types.MakeDatums(8, 8), types.MakeDatums(8, 7), touchedSlice(writeTbl))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, publicTbl, 7, 8, true)
if err != nil {
return errors.Trace(err)
}
// WriteOnlyTable delete t where c1 = 8
err = writeTbl.RemoveRecord(ctx, kv.IntHandle(8), types.MakeDatums(8, 7))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, publicTbl, 7, 8, false)
if err != nil {
return errors.Trace(err)
}
txn, err := ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
return txn.Commit(context.Background())
}
func checkDropDeleteOnly(ctx sessionctx.Context, writeTbl, delTbl table.Table) error {
// WriteOnlyTable insert t values (9, 9)
err := sessiontxn.NewTxn(context.Background(), ctx)
if err != nil {
return errors.Trace(err)
}
_, err = writeTbl.AddRecord(ctx, types.MakeDatums(9, 9))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, writeTbl, 9, 9, true)
if err != nil {
return errors.Trace(err)
}
// DeleteOnlyTable insert t values (10, 10)
_, err = delTbl.AddRecord(ctx, types.MakeDatums(10, 10))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, writeTbl, 10, 10, false)
if err != nil {
return errors.Trace(err)
}
// DeleteOnlyTable update t set c2 = 10 where c1 = 9
err = delTbl.UpdateRecord(context.Background(), ctx, kv.IntHandle(9), types.MakeDatums(9, 9), types.MakeDatums(9, 10), touchedSlice(delTbl))
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, writeTbl, 9, 9, false)
if err != nil {
return errors.Trace(err)
}
err = checkIndexExists(ctx, writeTbl, 10, 9, false)
if err != nil {
return errors.Trace(err)
}
txn, err := ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
return txn.Commit(context.Background())
}