forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddl_db_change_test.go
284 lines (266 loc) · 8.02 KB
/
ddl_db_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
// Copyright 2017 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package ddl_test
import (
"strings"
"time"
"github.com/juju/errors"
. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/util/testleak"
goctx "golang.org/x/net/context"
)
var _ = Suite(&testStateChangeSuite{})
type testStateChangeSuite struct {
lease time.Duration
store kv.Storage
dom *domain.Domain
se tidb.Session
p *parser.Parser
}
func (s *testStateChangeSuite) SetUpSuite(c *C) {
s.lease = 200 * time.Millisecond
var err error
s.store, err = tikv.NewMockTikvStore()
c.Assert(err, IsNil)
tidb.SetSchemaLease(s.lease)
s.dom, err = tidb.BootstrapSession(s.store)
c.Assert(err, IsNil)
s.se, err = tidb.CreateSession4Test(s.store)
c.Assert(err, IsNil)
_, err = s.se.Execute(goctx.Background(), "create database test_db_state")
c.Assert(err, IsNil)
_, err = s.se.Execute(goctx.Background(), "use test_db_state")
c.Assert(err, IsNil)
s.p = parser.New()
}
func (s *testStateChangeSuite) TearDownSuite(c *C) {
s.se.Execute(goctx.Background(), "drop database if exists test_db_state")
s.se.Close()
s.dom.Close()
s.store.Close()
}
func (s *testStateChangeSuite) TestTwoStates(c *C) {
cnt := 5
// New the testExecInfo.
testInfo := &testExecInfo{
execCases: cnt,
sqlInfos: make([]*sqlInfo, 4),
}
for i := 0; i < len(testInfo.sqlInfos); i++ {
sqlInfo := &sqlInfo{cases: make([]*stateCase, cnt)}
for j := 0; j < cnt; j++ {
sqlInfo.cases[j] = new(stateCase)
}
testInfo.sqlInfos[i] = sqlInfo
}
err := testInfo.createSessions(s.store, "test_db_state")
c.Assert(err, IsNil)
// Fill the SQLs and expected error messages.
testInfo.sqlInfos[0].sql = "insert into t (c1, c2, c3, c4) value(2, 'b', 'N', '2017-07-02')"
testInfo.sqlInfos[1].sql = "insert into t (c1, c2, c3, d3, c4) value(3, 'b', 'N', 'a', '2017-07-03')"
unknownColErr := errors.New("unknown column d3")
testInfo.sqlInfos[1].cases[0].expectedErr = unknownColErr
testInfo.sqlInfos[1].cases[1].expectedErr = unknownColErr
testInfo.sqlInfos[1].cases[2].expectedErr = unknownColErr
testInfo.sqlInfos[1].cases[3].expectedErr = unknownColErr
testInfo.sqlInfos[2].sql = "update t set c2 = 'c2_update'"
testInfo.sqlInfos[3].sql = "replace into t values(5, 'e', 'N', '2017-07-05')'"
testInfo.sqlInfos[3].cases[4].expectedErr = errors.New("Column count doesn't match value count at row 1")
alterTableSQL := "alter table t add column d3 enum('a', 'b') not null default 'a' after c3"
s.test(c, "", alterTableSQL, testInfo)
// TODO: Add more DDL statements.
}
func (s *testStateChangeSuite) test(c *C, tableName, alterTableSQL string, testInfo *testExecInfo) {
defer testleak.AfterTest(c)()
_, err := s.se.Execute(goctx.Background(), `create table t (
c1 int,
c2 varchar(64),
c3 enum('N','Y') not null default 'N',
c4 timestamp on update current_timestamp,
key(c1, c2))`)
c.Assert(err, IsNil)
defer s.se.Execute(goctx.Background(), "drop table t")
_, err = s.se.Execute(goctx.Background(), "insert into t values(1, 'a', 'N', '2017-07-01')")
c.Assert(err, IsNil)
callback := &ddl.TestDDLCallback{}
prevState := model.StateNone
var checkErr error
err = testInfo.parseSQLs(s.p)
c.Assert(err, IsNil, Commentf("error stack %v", errors.ErrorStack(err)))
times := 0
callback.OnJobUpdatedExported = func(job *model.Job) {
if job.SchemaState == prevState || checkErr != nil || times >= 3 {
return
}
times++
switch job.SchemaState {
case model.StateDeleteOnly:
// This state we execute every sqlInfo one time using the first session and other information.
err = testInfo.compileSQL(0)
if err != nil {
checkErr = err
break
}
err = testInfo.execSQL(0)
if err != nil {
checkErr = err
}
case model.StateWriteOnly:
// This state we put the schema information to the second case.
err = testInfo.compileSQL(1)
if err != nil {
checkErr = err
}
case model.StateWriteReorganization:
// This state we execute every sqlInfo one time using the third session and other information.
err = testInfo.compileSQL(2)
if err != nil {
checkErr = err
break
}
err = testInfo.execSQL(2)
if err != nil {
checkErr = err
break
}
// Mock the server is in `write only` state.
err = testInfo.execSQL(1)
if err != nil {
checkErr = err
break
}
// This state we put the schema information to the fourth case.
err = testInfo.compileSQL(3)
if err != nil {
checkErr = err
}
}
}
d := s.dom.DDL()
d.SetHook(callback)
_, err = s.se.Execute(goctx.Background(), alterTableSQL)
c.Assert(err, IsNil)
err = testInfo.compileSQL(4)
c.Assert(err, IsNil)
err = testInfo.execSQL(4)
c.Assert(err, IsNil)
// Mock the server is in `write reorg` state.
err = testInfo.execSQL(3)
c.Assert(err, IsNil)
c.Assert(errors.ErrorStack(checkErr), Equals, "")
callback = &ddl.TestDDLCallback{}
d.SetHook(callback)
}
type stateCase struct {
session tidb.Session
rawStmt ast.StmtNode
stmt ast.Statement
expectedErr error
}
type sqlInfo struct {
sql string
// cases is multiple stateCases.
// Every case need to be executed with the different schema state.
cases []*stateCase
}
// testExecInfo contains some SQL information and the number of times each SQL is executed
// in a DDL statement.
type testExecInfo struct {
// execCases represents every SQL need to be executed execCases times.
// And the schema state is different at each execution.
execCases int
// sqlInfos represents this test information has multiple SQLs to test.
sqlInfos []*sqlInfo
}
func (t *testExecInfo) createSessions(store kv.Storage, useDB string) error {
var err error
for i, info := range t.sqlInfos {
for j, c := range info.cases {
c.session, err = tidb.CreateSession4Test(store)
if err != nil {
return errors.Trace(err)
}
_, err = c.session.Execute(goctx.Background(), "use "+useDB)
if err != nil {
return errors.Trace(err)
}
// It's used to debug.
c.session.SetConnectionID(uint64(i*10 + j))
}
}
return nil
}
func (t *testExecInfo) parseSQLs(p *parser.Parser) error {
if t.execCases <= 0 {
return nil
}
var err error
for _, sqlInfo := range t.sqlInfos {
seVars := sqlInfo.cases[0].session.GetSessionVars()
charset, collation := seVars.GetCharsetInfo()
for j := 0; j < t.execCases; j++ {
sqlInfo.cases[j].rawStmt, err = p.ParseOneStmt(sqlInfo.sql, charset, collation)
if err != nil {
return errors.Trace(err)
}
}
}
return nil
}
func (t *testExecInfo) compileSQL(idx int) (err error) {
for _, info := range t.sqlInfos {
c := info.cases[idx]
compiler := executor.Compiler{Ctx: c.session}
se := c.session
goCtx := goctx.TODO()
se.PrepareTxnCtx(goCtx)
ctx := se.(context.Context)
executor.ResetStmtCtx(ctx, c.rawStmt)
c.stmt, err = compiler.Compile(goCtx, c.rawStmt)
if err != nil {
return errors.Trace(err)
}
}
return nil
}
func (t *testExecInfo) execSQL(idx int) error {
for _, sqlInfo := range t.sqlInfos {
c := sqlInfo.cases[idx]
_, err := c.stmt.Exec(goctx.TODO())
if c.expectedErr != nil {
if err == nil {
err = errors.Errorf("expected error %s but got nil", c.expectedErr)
} else if strings.Contains(err.Error(), c.expectedErr.Error()) {
err = nil
}
}
if err != nil {
return errors.Trace(err)
}
err = c.session.CommitTxn(goctx.TODO())
if err != nil {
return errors.Trace(err)
}
}
return nil
}