forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestart_test.go
209 lines (180 loc) · 4.9 KB
/
restart_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
// Copyright 2020 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.
// +build !race
package ddl
import (
"context"
"errors"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/util/mock"
)
// this test file include some test that will cause data race, mainly because restartWorkers modify d.ctx
// restartWorkers is like the function of d.start. But it won't initialize the "workers" and create a new worker.
// It only starts the original workers.
func (d *ddl) restartWorkers(ctx context.Context) {
d.ctx, d.cancel = context.WithCancel(ctx)
d.wg.Add(1)
go d.limitDDLJobs()
if !RunWorker {
return
}
err := d.ownerManager.CampaignOwner()
terror.Log(err)
for _, worker := range d.workers {
worker.wg.Add(1)
worker.ctx = d.ctx
w := worker
go w.start(d.ddlCtx)
asyncNotify(worker.ddlJobCh)
}
}
// runInterruptedJob should be called concurrently with restartWorkers
func runInterruptedJob(c *C, d *ddl, job *model.Job, doneCh chan error) {
ctx := mock.NewContext()
ctx.Store = d.store
var (
history *model.Job
err error
)
err = d.doDDLJob(ctx, job)
if errors.Is(err, context.Canceled) {
endlessLoopTime := time.Now().Add(time.Minute)
for history == nil {
// imitate doDDLJob's logic, quit only find history
history, _ = d.getHistoryDDLJob(job.ID)
if history != nil {
err = history.Error
}
time.Sleep(10 * testLease)
if time.Now().After(endlessLoopTime) {
err = errors.New("runInterruptedJob may enter endless loop")
break
}
}
}
doneCh <- err
}
func testRunInterruptedJob(c *C, d *ddl, job *model.Job) {
done := make(chan error, 1)
go runInterruptedJob(c, d, job, done)
ticker := time.NewTicker(d.lease * 1)
defer ticker.Stop()
LOOP:
for {
select {
case <-ticker.C:
d.Stop()
d.restartWorkers(context.Background())
time.Sleep(time.Millisecond * 20)
case err := <-done:
c.Assert(err, IsNil)
break LOOP
}
}
}
func (s *testSchemaSuite) TestSchemaResume(c *C) {
store := testCreateStore(c, "test_schema_resume")
defer store.Close()
d1 := testNewDDLAndStart(
context.Background(),
c,
WithStore(store),
WithLease(testLease),
)
defer d1.Stop()
testCheckOwner(c, d1, true)
dbInfo := testSchemaInfo(c, d1, "test")
job := &model.Job{
SchemaID: dbInfo.ID,
Type: model.ActionCreateSchema,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{dbInfo},
}
testRunInterruptedJob(c, d1, job)
testCheckSchemaState(c, d1, dbInfo, model.StatePublic)
job = &model.Job{
SchemaID: dbInfo.ID,
Type: model.ActionDropSchema,
BinlogInfo: &model.HistoryInfo{},
}
testRunInterruptedJob(c, d1, job)
testCheckSchemaState(c, d1, dbInfo, model.StateNone)
}
func (s *testStatSuite) TestStat(c *C) {
store := testCreateStore(c, "test_stat")
defer store.Close()
d := testNewDDLAndStart(
context.Background(),
c,
WithStore(store),
WithLease(testLease),
)
defer d.Stop()
dbInfo := testSchemaInfo(c, d, "test")
testCreateSchema(c, testNewContext(d), d, dbInfo)
// TODO: Get this information from etcd.
// m, err := d.Stats(nil)
// c.Assert(err, IsNil)
// c.Assert(m[ddlOwnerID], Equals, d.uuid)
job := &model.Job{
SchemaID: dbInfo.ID,
Type: model.ActionDropSchema,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{dbInfo.Name},
}
done := make(chan error, 1)
go runInterruptedJob(c, d, job, done)
ticker := time.NewTicker(d.lease * 1)
defer ticker.Stop()
ver := s.getDDLSchemaVer(c, d)
LOOP:
for {
select {
case <-ticker.C:
d.Stop()
c.Assert(s.getDDLSchemaVer(c, d), GreaterEqual, ver)
d.restartWorkers(context.Background())
time.Sleep(time.Millisecond * 20)
case err := <-done:
// TODO: Get this information from etcd.
// m, err := d.Stats(nil)
c.Assert(err, IsNil)
break LOOP
}
}
}
func (s *testTableSuite) TestTableResume(c *C) {
d := s.d
testCheckOwner(c, d, true)
tblInfo := testTableInfo(c, d, "t1", 3)
job := &model.Job{
SchemaID: s.dbInfo.ID,
TableID: tblInfo.ID,
Type: model.ActionCreateTable,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{tblInfo},
}
testRunInterruptedJob(c, d, job)
testCheckTableState(c, d, s.dbInfo, tblInfo, model.StatePublic)
job = &model.Job{
SchemaID: s.dbInfo.ID,
TableID: tblInfo.ID,
Type: model.ActionDropTable,
BinlogInfo: &model.HistoryInfo{},
}
testRunInterruptedJob(c, d, job)
testCheckTableState(c, d, s.dbInfo, tblInfo, model.StateNone)
}