forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorg_test.go
194 lines (161 loc) · 4.71 KB
/
reorg_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
// Copyright 2015 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
import (
"time"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/testleak"
"golang.org/x/net/context"
)
type testCtxKeyType int
func (k testCtxKeyType) String() string {
return "test_ctx_key"
}
const testCtxKey testCtxKeyType = 0
func (s *testDDLSuite) TestReorg(c *C) {
defer testleak.AfterTest(c)()
store := testCreateStore(c, "test_reorg")
defer store.Close()
d := testNewDDL(context.Background(), nil, store, nil, nil, testLease)
defer d.Stop()
time.Sleep(testLease)
ctx := testNewContext(d)
ctx.SetValue(testCtxKey, 1)
c.Assert(ctx.Value(testCtxKey), Equals, 1)
ctx.ClearValue(testCtxKey)
err := ctx.NewTxn()
c.Assert(err, IsNil)
ctx.Txn().Set([]byte("a"), []byte("b"))
err = ctx.Txn().Rollback()
c.Assert(err, IsNil)
err = ctx.NewTxn()
c.Assert(err, IsNil)
ctx.Txn().Set([]byte("a"), []byte("b"))
err = ctx.Txn().Commit(context.Background())
c.Assert(err, IsNil)
rowCount := int64(10)
handle := int64(100)
f := func() error {
d.reorgCtx.setRowCountAndHandle(rowCount, handle)
time.Sleep(20 * testLease)
return nil
}
job := &model.Job{
ID: 1,
SnapshotVer: 1, // Make sure it is not zero. So the reorgInfo's frist is false.
}
err = ctx.NewTxn()
c.Assert(err, IsNil)
m := meta.NewMeta(ctx.Txn())
err = d.runReorgJob(m, job, f)
c.Assert(err, NotNil)
// The longest to wait for 5 seconds to make sure the function of f is returned.
for i := 0; i < 1000; i++ {
time.Sleep(5 * time.Millisecond)
err = d.runReorgJob(m, job, f)
if err == nil {
c.Assert(job.RowCount, Equals, rowCount)
c.Assert(d.reorgCtx.rowCount, Equals, int64(0))
// Test whether reorgInfo's Handle is update.
err = ctx.Txn().Commit(context.Background())
c.Assert(err, IsNil)
err = ctx.NewTxn()
c.Assert(err, IsNil)
m = meta.NewMeta(ctx.Txn())
info, err1 := d.getReorgInfo(m, job)
c.Assert(err1, IsNil)
c.Assert(info.Handle, Equals, handle)
c.Assert(d.reorgCtx.doneHandle, Equals, int64(0))
break
}
}
c.Assert(err, IsNil)
d.Stop()
err = d.runReorgJob(m, job, func() error {
time.Sleep(4 * testLease)
return nil
})
c.Assert(err, NotNil)
err = ctx.Txn().Commit(context.Background())
c.Assert(err, IsNil)
d.start(context.Background())
job = &model.Job{
ID: 2,
SchemaID: 1,
Type: model.ActionCreateSchema,
Args: []interface{}{model.NewCIStr("test")},
}
var info *reorgInfo
err = kv.RunInNewTxn(d.store, false, func(txn kv.Transaction) error {
t := meta.NewMeta(txn)
var err1 error
info, err1 = d.getReorgInfo(t, job)
c.Assert(err1, IsNil)
err1 = info.UpdateHandle(txn, 1)
c.Assert(err1, IsNil)
return nil
})
c.Assert(err, IsNil)
err = kv.RunInNewTxn(d.store, false, func(txn kv.Transaction) error {
t := meta.NewMeta(txn)
var err1 error
info, err1 = d.getReorgInfo(t, job)
c.Assert(err1, IsNil)
c.Assert(info.Handle, Greater, int64(0))
return nil
})
c.Assert(err, IsNil)
}
func (s *testDDLSuite) TestReorgOwner(c *C) {
defer testleak.AfterTest(c)()
store := testCreateStore(c, "test_reorg_owner")
defer store.Close()
d1 := testNewDDL(context.Background(), nil, store, nil, nil, testLease)
defer d1.Stop()
ctx := testNewContext(d1)
testCheckOwner(c, d1, true)
d2 := testNewDDL(context.Background(), nil, store, nil, nil, testLease)
defer d2.Stop()
dbInfo := testSchemaInfo(c, d1, "test")
testCreateSchema(c, ctx, d1, dbInfo)
tblInfo := testTableInfo(c, d1, "t", 3)
testCreateTable(c, ctx, d1, dbInfo, tblInfo)
t := testGetTable(c, d1, dbInfo.ID, tblInfo.ID)
num := 10
for i := 0; i < num; i++ {
_, err := t.AddRecord(ctx, types.MakeDatums(i, i, i), false)
c.Assert(err, IsNil)
}
err := ctx.Txn().Commit(context.Background())
c.Assert(err, IsNil)
tc := &TestDDLCallback{}
tc.onJobRunBefore = func(job *model.Job) {
if job.SchemaState == model.StateDeleteReorganization {
d1.Stop()
}
}
d1.SetHook(tc)
testDropSchema(c, ctx, d1, dbInfo)
err = kv.RunInNewTxn(d1.store, false, func(txn kv.Transaction) error {
t := meta.NewMeta(txn)
db, err1 := t.GetDatabase(dbInfo.ID)
c.Assert(err1, IsNil)
c.Assert(db, IsNil)
return nil
})
c.Assert(err, IsNil)
}