forked from pingcap/br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
192 lines (177 loc) · 4.97 KB
/
schema.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
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package backup
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
backuppb "github.com/pingcap/kvproto/pkg/backup"
"github.com/pingcap/log"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tipb/go-tipb"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/pingcap/br/pkg/checksum"
"github.com/pingcap/br/pkg/glue"
"github.com/pingcap/br/pkg/logutil"
"github.com/pingcap/br/pkg/metautil"
"github.com/pingcap/br/pkg/summary"
"github.com/pingcap/br/pkg/utils"
)
const (
// DefaultSchemaConcurrency is the default number of the concurrent
// backup schema tasks.
DefaultSchemaConcurrency = 64
)
type scheamInfo struct {
tableInfo *model.TableInfo
dbInfo *model.DBInfo
crc64xor uint64
totalKvs uint64
totalBytes uint64
stats *handle.JSONTable
}
// Schemas is task for backuping schemas.
type Schemas struct {
// name -> schema
schemas map[string]*scheamInfo
}
func newBackupSchemas() *Schemas {
return &Schemas{
schemas: make(map[string]*scheamInfo),
}
}
func (ss *Schemas) addSchema(
dbInfo *model.DBInfo, tableInfo *model.TableInfo,
) {
name := fmt.Sprintf("%s.%s",
utils.EncloseName(dbInfo.Name.L), utils.EncloseName(tableInfo.Name.L))
ss.schemas[name] = &scheamInfo{
tableInfo: tableInfo,
dbInfo: dbInfo,
}
}
// BackupSchemas backups table info, including checksum and stats.
func (ss *Schemas) BackupSchemas(
ctx context.Context,
metaWriter *metautil.MetaWriter,
store kv.Storage,
statsHandle *handle.Handle,
backupTS uint64,
concurrency uint,
copConcurrency uint,
skipChecksum bool,
updateCh glue.Progress,
) error {
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
span1 := span.Tracer().StartSpan("Schemas.BackupSchemas", opentracing.ChildOf(span.Context()))
defer span1.Finish()
ctx = opentracing.ContextWithSpan(ctx, span1)
}
workerPool := utils.NewWorkerPool(concurrency, "Schemas")
errg, ectx := errgroup.WithContext(ctx)
startAll := time.Now()
op := metautil.AppendSchema
metaWriter.StartWriteMetasAsync(ctx, op)
for _, s := range ss.schemas {
schema := s
workerPool.ApplyOnErrorGroup(errg, func() error {
if utils.IsSysDB(schema.dbInfo.Name.L) {
schema.dbInfo.Name = utils.TemporaryDBName(schema.dbInfo.Name.O)
}
logger := log.With(
zap.String("db", schema.dbInfo.Name.O),
zap.String("table", schema.tableInfo.Name.O),
)
if !skipChecksum {
logger.Info("table checksum start")
start := time.Now()
checksumResp, err := calculateChecksum(
ectx, schema.tableInfo, store.GetClient(), backupTS, copConcurrency)
if err != nil {
return errors.Trace(err)
}
schema.crc64xor = checksumResp.Checksum
schema.totalKvs = checksumResp.TotalKvs
schema.totalBytes = checksumResp.TotalBytes
logger.Info("table checksum finished",
zap.Uint64("Crc64Xor", checksumResp.Checksum),
zap.Uint64("TotalKvs", checksumResp.TotalKvs),
zap.Uint64("TotalBytes", checksumResp.TotalBytes),
zap.Duration("take", time.Since(start)))
}
if statsHandle != nil {
jsonTable, err := statsHandle.DumpStatsToJSON(
schema.dbInfo.Name.String(), schema.tableInfo, nil)
if err != nil {
logger.Error("dump table stats failed", logutil.ShortError(err))
}
schema.stats = jsonTable
}
// Send schema to metawriter
dbBytes, err := json.Marshal(schema.dbInfo)
if err != nil {
return errors.Trace(err)
}
tableBytes, err := json.Marshal(schema.tableInfo)
if err != nil {
return errors.Trace(err)
}
var statsBytes []byte
if schema.stats != nil {
statsBytes, err = json.Marshal(schema.stats)
if err != nil {
return errors.Trace(err)
}
}
s := &backuppb.Schema{
Db: dbBytes,
Table: tableBytes,
Crc64Xor: schema.crc64xor,
TotalKvs: schema.totalKvs,
TotalBytes: schema.totalBytes,
Stats: statsBytes,
}
if err := metaWriter.Send(s, op); err != nil {
return errors.Trace(err)
}
updateCh.Inc()
return nil
})
}
if err := errg.Wait(); err != nil {
return errors.Trace(err)
}
log.Info("backup checksum", zap.Duration("take", time.Since(startAll)))
summary.CollectDuration("backup checksum", time.Since(startAll))
return metaWriter.FinishWriteMetas(ctx, op)
}
// Len returns the number of schemas.
func (ss *Schemas) Len() int {
return len(ss.schemas)
}
func calculateChecksum(
ctx context.Context,
table *model.TableInfo,
client kv.Client,
backupTS uint64,
concurrency uint,
) (*tipb.ChecksumResponse, error) {
exe, err := checksum.NewExecutorBuilder(table, backupTS).
SetConcurrency(concurrency).
Build()
if err != nil {
return nil, errors.Trace(err)
}
checksumResp, err := exe.Execute(ctx, client, func() {
// TODO: update progress here.
})
if err != nil {
return nil, errors.Trace(err)
}
return checksumResp, nil
}