/
workflow_differ.go
322 lines (293 loc) · 10.4 KB
/
workflow_differ.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
Copyright 2022 The Vitess Authors.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vdiff
import (
"context"
"fmt"
"reflect"
"strings"
"google.golang.org/protobuf/encoding/prototext"
"vitess.io/vitess/go/vt/binlog/binlogplayer"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/log"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
querypb "vitess.io/vitess/go/vt/proto/query"
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtctl/schematools"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication"
)
// workflowDiffer has metadata and state for the vdiff of a single workflow on this tablet
// only one vdiff can be running for a workflow at any time.
type workflowDiffer struct {
ct *controller
tableDiffers map[string]*tableDiffer // key is table name
opts *tabletmanagerdatapb.VDiffOptions
}
func newWorkflowDiffer(ct *controller, opts *tabletmanagerdatapb.VDiffOptions) (*workflowDiffer, error) {
wd := &workflowDiffer{
ct: ct,
opts: opts,
tableDiffers: make(map[string]*tableDiffer, 1),
}
return wd, nil
}
// If the only difference is the order in which the rows were returned
// by MySQL on each side then we'll have the same number of extras on
// both sides. If that's the case, then let's see if the extra rows on
// both sides are actually different.
func (wd *workflowDiffer) reconcileExtraRows(dr *DiffReport, maxExtraRowsToCompare int64) {
if (dr.ExtraRowsSource == dr.ExtraRowsTarget) && (dr.ExtraRowsSource <= maxExtraRowsToCompare) {
for i := range dr.ExtraRowsSourceDiffs {
foundMatch := false
for j := range dr.ExtraRowsTargetDiffs {
if reflect.DeepEqual(dr.ExtraRowsSourceDiffs[i], dr.ExtraRowsTargetDiffs[j]) {
dr.ExtraRowsSourceDiffs = append(dr.ExtraRowsSourceDiffs[:i], dr.ExtraRowsSourceDiffs[i+1:]...)
dr.ExtraRowsSource--
dr.ExtraRowsTargetDiffs = append(dr.ExtraRowsTargetDiffs[:j], dr.ExtraRowsTargetDiffs[j+1:]...)
dr.ExtraRowsTarget--
dr.ProcessedRows--
dr.MatchingRows++
foundMatch = true
break
}
}
// If we didn't find a match then the tables are in fact different and we can short circuit the second pass
if !foundMatch {
break
}
}
}
// We can now trim the extra rows diffs on both sides to the maxVDiffReportSampleRows value
if len(dr.ExtraRowsSourceDiffs) > maxVDiffReportSampleRows {
dr.ExtraRowsSourceDiffs = dr.ExtraRowsSourceDiffs[:maxVDiffReportSampleRows-1]
}
if len(dr.ExtraRowsTargetDiffs) > maxVDiffReportSampleRows {
dr.ExtraRowsTargetDiffs = dr.ExtraRowsTargetDiffs[:maxVDiffReportSampleRows-1]
}
}
func (wd *workflowDiffer) diffTable(ctx context.Context, dbClient binlogplayer.DBClient, td *tableDiffer) error {
select {
case <-ctx.Done():
return vterrors.Errorf(vtrpcpb.Code_CANCELED, "context has expired")
default:
}
log.Infof("Starting differ on table %s for vdiff %s", td.table.Name, wd.ct.uuid)
if err := td.updateTableState(ctx, dbClient, StartedState); err != nil {
return err
}
if err := td.initialize(ctx); err != nil {
return err
}
log.Infof("Table initialization done on table %s for vdiff %s", td.table.Name, wd.ct.uuid)
dr, err := td.diff(ctx, &wd.opts.CoreOptions.MaxRows, wd.opts.ReportOptions.DebugQuery, false, wd.opts.CoreOptions.MaxExtraRowsToCompare)
if err != nil {
log.Errorf("Encountered an error diffing table %s for vdiff %s: %v", td.table.Name, wd.ct.uuid, err)
return err
}
log.Infof("Table diff done on table %s for vdiff %s with report: %+v", td.table.Name, wd.ct.uuid, dr)
if dr.ExtraRowsSource > 0 || dr.ExtraRowsTarget > 0 {
wd.reconcileExtraRows(dr, wd.opts.CoreOptions.MaxExtraRowsToCompare)
}
if dr.MismatchedRows > 0 || dr.ExtraRowsTarget > 0 || dr.ExtraRowsSource > 0 {
if err := updateTableMismatch(dbClient, wd.ct.id, td.table.Name); err != nil {
return err
}
}
log.Infof("Completed reconciliation on table %s for vdiff %s with updated report: %+v", td.table.Name, wd.ct.uuid, dr)
if err := td.updateTableStateAndReport(ctx, dbClient, CompletedState, dr); err != nil {
return err
}
return nil
}
func (wd *workflowDiffer) diff(ctx context.Context) error {
dbClient := wd.ct.dbClientFactory()
if err := dbClient.Connect(); err != nil {
return err
}
defer dbClient.Close()
select {
case <-ctx.Done():
return vterrors.Errorf(vtrpcpb.Code_CANCELED, "context has expired")
default:
}
filter := wd.ct.filter
req := &tabletmanagerdatapb.GetSchemaRequest{}
schm, err := schematools.GetSchema(ctx, wd.ct.ts, wd.ct.tmc, wd.ct.vde.thisTablet.Alias, req)
if err != nil {
return vterrors.Wrap(err, "GetSchema")
}
if err = wd.buildPlan(dbClient, filter, schm); err != nil {
return vterrors.Wrap(err, "buildPlan")
}
if err := wd.initVDiffTables(dbClient); err != nil {
return err
}
for _, td := range wd.tableDiffers {
select {
case <-ctx.Done():
return vterrors.Errorf(vtrpcpb.Code_CANCELED, "context has expired")
default:
}
query := fmt.Sprintf(sqlGetVDiffTable, wd.ct.id, encodeString(td.table.Name))
qr, err := dbClient.ExecuteFetch(query, 1)
if err != nil {
return err
}
if len(qr.Rows) == 0 {
return fmt.Errorf("no vdiff table found for %s on tablet %v",
td.table.Name, wd.ct.vde.thisTablet.Alias)
}
log.Infof("Starting diff of table %s for vdiff %s", td.table.Name, wd.ct.uuid)
if err := wd.diffTable(ctx, dbClient, td); err != nil {
if err := td.updateTableState(ctx, dbClient, ErrorState); err != nil {
return err
}
insertVDiffLog(ctx, dbClient, wd.ct.id, fmt.Sprintf("Table %s Error: %s", td.table.Name, err))
return err
}
if err := td.updateTableState(ctx, dbClient, CompletedState); err != nil {
return err
}
log.Infof("Completed diff of table %s for vdiff %s", td.table.Name, wd.ct.uuid)
}
if err := wd.markIfCompleted(ctx, dbClient); err != nil {
return err
}
return nil
}
func (wd *workflowDiffer) markIfCompleted(ctx context.Context, dbClient binlogplayer.DBClient) error {
query := fmt.Sprintf(sqlGetIncompleteTables, wd.ct.id)
qr, err := dbClient.ExecuteFetch(query, -1)
if err != nil {
return err
}
// Double check to be sure all of the individual table diffs completed without error
// before marking the vdiff as completed.
if len(qr.Rows) == 0 {
if err := wd.ct.updateState(dbClient, CompletedState, nil); err != nil {
return err
}
}
return nil
}
func (wd *workflowDiffer) buildPlan(dbClient binlogplayer.DBClient, filter *binlogdatapb.Filter, schm *tabletmanagerdatapb.SchemaDefinition) error {
var specifiedTables []string
optTables := strings.TrimSpace(wd.opts.CoreOptions.Tables)
if optTables != "" {
specifiedTables = strings.Split(optTables, ",")
}
for _, table := range schm.TableDefinitions {
// if user specified tables explicitly only use those, otherwise diff all tables in workflow
if len(specifiedTables) != 0 && !stringListContains(specifiedTables, table.Name) {
continue
}
if schema.IsInternalOperationTableName(table.Name) {
continue
}
rule, err := vreplication.MatchTable(table.Name, filter)
if err != nil {
return err
}
if rule == nil || rule.Filter == "exclude" {
continue
}
sourceQuery := rule.Filter
switch {
case rule.Filter == "":
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("select * from %v", sqlparser.NewIdentifierCS(table.Name))
sourceQuery = buf.String()
case key.IsKeyRange(rule.Filter):
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("select * from %v where in_keyrange(%v)", sqlparser.NewIdentifierCS(table.Name), sqlparser.NewStrLiteral(rule.Filter))
sourceQuery = buf.String()
}
td := newTableDiffer(wd, table, sourceQuery)
lastpkpb, err := wd.getTableLastPK(dbClient, table.Name)
if err != nil {
return err
}
td.lastPK = lastpkpb
wd.tableDiffers[table.Name] = td
if _, err := td.buildTablePlan(); err != nil {
return err
}
}
if len(wd.tableDiffers) == 0 {
return fmt.Errorf("no tables found to diff, %s:%s, on tablet %v",
optTables, specifiedTables, wd.ct.vde.thisTablet.Alias)
}
return nil
}
// getTableLastPK gets the lastPK protobuf message for a given vdiff table.
func (wd *workflowDiffer) getTableLastPK(dbClient binlogplayer.DBClient, tableName string) (*querypb.QueryResult, error) {
query := fmt.Sprintf(sqlGetVDiffTable, wd.ct.id, encodeString(tableName))
qr, err := dbClient.ExecuteFetch(query, 1)
if err != nil {
return nil, err
}
if len(qr.Rows) == 1 {
var lastpk []byte
if lastpk, err = qr.Named().Row().ToBytes("lastpk"); err != nil {
return nil, err
}
if len(lastpk) != 0 {
var lastpkpb querypb.QueryResult
if err := prototext.Unmarshal(lastpk, &lastpkpb); err != nil {
return nil, err
}
return &lastpkpb, nil
}
}
return nil, nil
}
func (wd *workflowDiffer) initVDiffTables(dbClient binlogplayer.DBClient) error {
tableIn := strings.Builder{}
n := 0
for tableName := range wd.tableDiffers {
tableIn.WriteString(encodeString(tableName))
if n++; n < len(wd.tableDiffers) {
tableIn.WriteByte(',')
}
}
query := fmt.Sprintf(sqlGetAllTableRows, encodeString(wd.ct.vde.dbName), tableIn.String())
qr, err := dbClient.ExecuteFetch(query, -1)
if err != nil {
return err
}
for _, row := range qr.Named().Rows {
tableName, _ := row.ToString("table_name")
tableRows, _ := row.ToInt64("table_rows")
query := fmt.Sprintf(sqlGetVDiffTable, wd.ct.id, encodeString(tableName))
qr, err := dbClient.ExecuteFetch(query, -1)
if err != nil {
return err
}
if len(qr.Rows) == 0 {
query = fmt.Sprintf(sqlNewVDiffTable, wd.ct.id, encodeString(tableName), tableRows)
} else if len(qr.Rows) == 1 {
query = fmt.Sprintf(sqlUpdateTableRows, tableRows, wd.ct.id, encodeString(tableName))
} else {
return fmt.Errorf("invalid state found for vdiff table %s for vdiff_id %d on tablet %s",
tableName, wd.ct.id, wd.ct.vde.thisTablet.Alias)
}
if _, err := dbClient.ExecuteFetch(query, 1); err != nil {
return err
}
}
return nil
}