-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
delete_range.go
278 lines (254 loc) · 7.87 KB
/
delete_range.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
// 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
import (
"bytes"
"encoding/hex"
"fmt"
"math"
"github.com/juju/errors"
"github.com/ngaut/pools"
"github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/sqlexec"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
const (
insertDeleteRangeSQL = `INSERT IGNORE INTO mysql.gc_delete_range VALUES ("%d", "%d", "%s", "%s", "%d")`
delBatchSize = 65536
delBackLog = 128
)
type delRangeManager interface {
// addDelRangeJob add a DDL job into gc_delete_range table.
addDelRangeJob(job *model.Job) error
start()
clear()
}
type delRange struct {
d *ddl
ctxPool *pools.ResourcePool
storeSupport bool
emulatorCh chan struct{}
keys []kv.Key
}
// newDelRangeManager returns a delRangeManager.
func newDelRangeManager(d *ddl, ctxPool *pools.ResourcePool, supportDelRange bool) delRangeManager {
dr := &delRange{
d: d,
ctxPool: ctxPool,
storeSupport: supportDelRange,
}
if !dr.storeSupport {
dr.emulatorCh = make(chan struct{}, delBackLog)
dr.keys = make([]kv.Key, 0, delBatchSize)
}
return dr
}
// addDelRangeJob implements delRangeManager interface.
func (dr *delRange) addDelRangeJob(job *model.Job) error {
resource, err := dr.ctxPool.Get()
if err != nil {
return errors.Trace(err)
}
defer dr.ctxPool.Put(resource)
ctx := resource.(sessionctx.Context)
ctx.GetSessionVars().SetStatusFlag(mysql.ServerStatusAutocommit, true)
ctx.GetSessionVars().InRestrictedSQL = true
err = insertJobIntoDeleteRangeTable(ctx, job)
if err != nil {
return errors.Trace(err)
}
if !dr.storeSupport {
dr.emulatorCh <- struct{}{}
}
log.Infof("[ddl] add job (%d,%s) into delete-range table", job.ID, job.Type.String())
return nil
}
// start implements delRangeManager interface.
func (dr *delRange) start() {
if !dr.storeSupport {
dr.d.wait.Add(1)
go dr.startEmulator()
}
}
// clear implements delRangeManager interface.
func (dr *delRange) clear() {
log.Infof("[ddl] closing delRange session pool")
dr.ctxPool.Close()
}
// startEmulator is only used for those storage engines which don't support
// delete-range. The emulator fetches records from gc_delete_range table and
// deletes all keys in each DelRangeTask.
func (dr *delRange) startEmulator() {
defer dr.d.wait.Done()
log.Infof("[ddl] start delRange emulator")
for {
select {
case <-dr.emulatorCh:
case <-dr.d.quitCh:
return
}
err := dr.doDelRangeWork()
terror.Log(errors.Trace(err))
}
}
func (dr *delRange) doDelRangeWork() error {
resource, err := dr.ctxPool.Get()
if err != nil {
log.Errorf("[ddl] delRange emulator get session fail: %s", err)
return errors.Trace(err)
}
defer dr.ctxPool.Put(resource)
ctx := resource.(sessionctx.Context)
ctx.GetSessionVars().SetStatusFlag(mysql.ServerStatusAutocommit, true)
ctx.GetSessionVars().InRestrictedSQL = true
ranges, err := util.LoadDeleteRanges(ctx, math.MaxInt64)
if err != nil {
log.Errorf("[dd] delRange emulator load tasks fail: %s", err)
return errors.Trace(err)
}
for _, r := range ranges {
if err := dr.doTask(ctx, r); err != nil {
log.Errorf("[ddl] delRange emulator do task fail: %s", err)
return errors.Trace(err)
}
}
return nil
}
func (dr *delRange) doTask(ctx sessionctx.Context, r util.DelRangeTask) error {
var oldStartKey, newStartKey kv.Key
oldStartKey = r.StartKey
for {
finish := true
dr.keys = dr.keys[:0]
err := kv.RunInNewTxn(dr.d.store, false, func(txn kv.Transaction) error {
iter, err := txn.Seek(oldStartKey)
if err != nil {
return errors.Trace(err)
}
defer iter.Close()
for i := 0; i < delBatchSize; i++ {
if !iter.Valid() {
break
}
finish = bytes.Compare(iter.Key(), r.EndKey) >= 0
if finish {
break
}
dr.keys = append(dr.keys, iter.Key().Clone())
newStartKey = iter.Key().Next()
if err := iter.Next(); err != nil {
return errors.Trace(err)
}
}
for _, key := range dr.keys {
err := txn.Delete(key)
if err != nil && !kv.ErrNotExist.Equal(err) {
return errors.Trace(err)
}
}
return nil
})
if err != nil {
return errors.Trace(err)
}
if finish {
if err := util.CompleteDeleteRange(ctx, r); err != nil {
log.Errorf("[ddl] delRange emulator complete task fail: %s", err)
return errors.Trace(err)
}
log.Infof("[ddl] delRange emulator complete task: (%d, %d)", r.JobID, r.ElementID)
break
} else {
if err := util.UpdateDeleteRange(ctx, r, newStartKey, oldStartKey); err != nil {
log.Errorf("[ddl] delRange emulator update task fail: %s", err)
}
oldStartKey = newStartKey
}
}
return nil
}
// insertJobIntoDeleteRangeTable parses the job into delete-range arguments,
// and inserts a new record into gc_delete_range table. The primary key is
// job ID, so we ignore key conflict error.
func insertJobIntoDeleteRangeTable(ctx sessionctx.Context, job *model.Job) error {
now, err := getNowTS(ctx)
if err != nil {
return errors.Trace(err)
}
s := ctx.(sqlexec.SQLExecutor)
switch job.Type {
case model.ActionDropSchema:
var tableIDs []int64
if err := job.DecodeArgs(&tableIDs); err != nil {
return errors.Trace(err)
}
for _, tableID := range tableIDs {
startKey := tablecodec.EncodeTablePrefix(tableID)
endKey := tablecodec.EncodeTablePrefix(tableID + 1)
if err := doInsert(s, job.ID, tableID, startKey, endKey, now); err != nil {
return errors.Trace(err)
}
}
case model.ActionDropTable, model.ActionTruncateTable:
tableID := job.TableID
startKey := tablecodec.EncodeTablePrefix(tableID)
endKey := tablecodec.EncodeTablePrefix(tableID + 1)
return doInsert(s, job.ID, tableID, startKey, endKey, now)
// ActionAddIndex needs do it, because it needs to be rolled back when it's canceled.
case model.ActionAddIndex:
tableID := job.TableID
var indexID int64
if err := job.DecodeArgs(&indexID); err != nil {
return errors.Trace(err)
}
startKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID+1)
return doInsert(s, job.ID, indexID, startKey, endKey, now)
case model.ActionDropIndex:
tableID := job.TableID
var indexName interface{}
var indexID int64
if err := job.DecodeArgs(&indexName, &indexID); err != nil {
return errors.Trace(err)
}
startKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID+1)
return doInsert(s, job.ID, indexID, startKey, endKey, now)
}
return nil
}
func doInsert(s sqlexec.SQLExecutor, jobID int64, elementID int64, startKey, endKey kv.Key, ts int64) error {
log.Infof("[ddl] insert into delete-range table with key: (%d,%d)", jobID, elementID)
startKeyEncoded := hex.EncodeToString(startKey)
endKeyEncoded := hex.EncodeToString(endKey)
sql := fmt.Sprintf(insertDeleteRangeSQL, jobID, elementID, startKeyEncoded, endKeyEncoded, ts)
_, err := s.Execute(context.Background(), sql)
return errors.Trace(err)
}
// getNowTS gets the current timestamp, in second.
func getNowTS(ctx sessionctx.Context) (int64, error) {
currVer, err := ctx.GetStore().CurrentVersion()
if err != nil {
return 0, errors.Trace(err)
}
physical := oracle.ExtractPhysical(currVer.Ver)
return physical / 1e3, nil
}