forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.go
262 lines (224 loc) · 6.49 KB
/
checksum.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
// Copyright 2018 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 executor
import (
"strconv"
"github.com/juju/errors"
"github.com/pingcap/tidb/distsql"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/ranger"
"github.com/pingcap/tipb/go-tipb"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
var _ Executor = &ChecksumTableExec{}
// ChecksumTableExec represents ChecksumTable executor.
type ChecksumTableExec struct {
baseExecutor
tables map[int64]*checksumContext
done bool
}
// Open implements the Executor Open interface.
func (e *ChecksumTableExec) Open(ctx context.Context) error {
if err := e.baseExecutor.Open(ctx); err != nil {
return errors.Trace(err)
}
concurrency, err := getChecksumTableConcurrency(e.ctx)
if err != nil {
return errors.Trace(err)
}
tasks, err := e.buildTasks()
if err != nil {
return errors.Trace(err)
}
taskCh := make(chan *checksumTask, len(tasks))
resultCh := make(chan *checksumResult, len(tasks))
for i := 0; i < concurrency; i++ {
go e.checksumWorker(taskCh, resultCh)
}
for _, task := range tasks {
taskCh <- task
}
close(taskCh)
for i := 0; i < len(tasks); i++ {
result := <-resultCh
if result.Error != nil {
err = result.Error
log.Error(errors.ErrorStack(err))
continue
}
e.handleResult(result)
}
if err != nil {
return errors.Trace(err)
}
return nil
}
// Next implements the Executor Next interface.
func (e *ChecksumTableExec) Next(ctx context.Context, chk *chunk.Chunk) error {
chk.Reset()
if e.done {
return nil
}
for _, t := range e.tables {
chk.AppendString(0, t.DBInfo.Name.O)
chk.AppendString(1, t.TableInfo.Name.O)
chk.AppendUint64(2, t.Response.Checksum)
chk.AppendUint64(3, t.Response.TotalKvs)
chk.AppendUint64(4, t.Response.TotalBytes)
}
e.done = true
return nil
}
func (e *ChecksumTableExec) buildTasks() ([]*checksumTask, error) {
var tasks []*checksumTask
for id, t := range e.tables {
reqs, err := t.BuildRequests(e.ctx)
if err != nil {
return nil, errors.Trace(err)
}
for _, req := range reqs {
tasks = append(tasks, &checksumTask{id, req})
}
}
return tasks, nil
}
func (e *ChecksumTableExec) handleResult(result *checksumResult) {
table := e.tables[result.TableID]
table.HandleResponse(result.Response)
}
func (e *ChecksumTableExec) checksumWorker(taskCh <-chan *checksumTask, resultCh chan<- *checksumResult) {
for task := range taskCh {
result := &checksumResult{TableID: task.TableID}
result.Response, result.Error = e.handleChecksumRequest(task.Request)
resultCh <- result
}
}
func (e *ChecksumTableExec) handleChecksumRequest(req *kv.Request) (resp *tipb.ChecksumResponse, err error) {
ctx := context.TODO()
res, err := distsql.Checksum(ctx, e.ctx.GetClient(), req, e.ctx.GetSessionVars().KVVars)
if err != nil {
return nil, errors.Trace(err)
}
res.Fetch(ctx)
defer func() {
if err1 := res.Close(); err1 != nil {
err = errors.Trace(err1)
}
}()
resp = &tipb.ChecksumResponse{}
for {
data, err := res.NextRaw(ctx)
if err != nil {
return nil, errors.Trace(err)
}
if data == nil {
break
}
checksum := &tipb.ChecksumResponse{}
if err = checksum.Unmarshal(data); err != nil {
return nil, errors.Trace(err)
}
updateChecksumResponse(resp, checksum)
}
return resp, nil
}
type checksumTask struct {
TableID int64
Request *kv.Request
}
type checksumResult struct {
Error error
TableID int64
Response *tipb.ChecksumResponse
}
type checksumContext struct {
DBInfo *model.DBInfo
TableInfo *model.TableInfo
StartTs uint64
Response *tipb.ChecksumResponse
}
func newChecksumContext(db *model.DBInfo, table *model.TableInfo, startTs uint64) *checksumContext {
return &checksumContext{
DBInfo: db,
TableInfo: table,
StartTs: startTs,
Response: &tipb.ChecksumResponse{},
}
}
func (c *checksumContext) BuildRequests(ctx sessionctx.Context) ([]*kv.Request, error) {
var reqs []*kv.Request
req, err := c.buildTableRequest(ctx)
if err != nil {
return nil, errors.Trace(err)
}
reqs = append(reqs, req)
for _, indexInfo := range c.TableInfo.Indices {
if indexInfo.State != model.StatePublic {
continue
}
req, err = c.buildIndexRequest(ctx, indexInfo)
if err != nil {
return nil, errors.Trace(err)
}
reqs = append(reqs, req)
}
return reqs, nil
}
func (c *checksumContext) buildTableRequest(ctx sessionctx.Context) (*kv.Request, error) {
checksum := &tipb.ChecksumRequest{
StartTs: c.StartTs,
ScanOn: tipb.ChecksumScanOn_Table,
Algorithm: tipb.ChecksumAlgorithm_Crc64_Xor,
}
ranges := ranger.FullIntRange(false)
var builder distsql.RequestBuilder
return builder.SetTableRanges(c.TableInfo.ID, ranges, nil).
SetChecksumRequest(checksum).
SetConcurrency(ctx.GetSessionVars().DistSQLScanConcurrency).
Build()
}
func (c *checksumContext) buildIndexRequest(ctx sessionctx.Context, indexInfo *model.IndexInfo) (*kv.Request, error) {
checksum := &tipb.ChecksumRequest{
StartTs: c.StartTs,
ScanOn: tipb.ChecksumScanOn_Index,
Algorithm: tipb.ChecksumAlgorithm_Crc64_Xor,
}
ranges := ranger.FullRange()
var builder distsql.RequestBuilder
return builder.SetIndexRanges(ctx.GetSessionVars().StmtCtx, c.TableInfo.ID, indexInfo.ID, ranges).
SetChecksumRequest(checksum).
SetConcurrency(ctx.GetSessionVars().DistSQLScanConcurrency).
Build()
}
func (c *checksumContext) HandleResponse(update *tipb.ChecksumResponse) {
updateChecksumResponse(c.Response, update)
}
func getChecksumTableConcurrency(ctx sessionctx.Context) (int, error) {
sessionVars := ctx.GetSessionVars()
concurrency, err := variable.GetSessionSystemVar(sessionVars, variable.TiDBChecksumTableConcurrency)
if err != nil {
return 0, errors.Trace(err)
}
c, err := strconv.ParseInt(concurrency, 10, 64)
return int(c), errors.Trace(err)
}
func updateChecksumResponse(resp, update *tipb.ChecksumResponse) {
resp.Checksum ^= update.Checksum
resp.TotalKvs += update.TotalKvs
resp.TotalBytes += update.TotalBytes
}