|
| 1 | +/* |
| 2 | +Copyright 2025 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package vdiff |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "vitess.io/vitess/go/sqltypes" |
| 26 | + "vitess.io/vitess/go/stats" |
| 27 | + "vitess.io/vitess/go/vt/binlog/binlogplayer" |
| 28 | + |
| 29 | + querypb "vitess.io/vitess/go/vt/proto/query" |
| 30 | + tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata" |
| 31 | +) |
| 32 | + |
| 33 | +func TestUpdateTableProgress(t *testing.T) { |
| 34 | + dr := &DiffReport{ |
| 35 | + ProcessedRows: 1e9, |
| 36 | + } |
| 37 | + queryTemplate := `update _vt.vdiff_table set rows_compared = 1000000000, lastpk = '%s', report = '{"TableName":"","ProcessedRows":1000000000,"MatchingRows":0,"MismatchedRows":0,"ExtraRowsSource":0,"ExtraRowsTarget":0}' where vdiff_id = 1 and table_name = 'test'` |
| 38 | + |
| 39 | + testCases := []struct { |
| 40 | + name string |
| 41 | + fields []*querypb.Field |
| 42 | + pkCols []int |
| 43 | + sourcePkCols []int |
| 44 | + lastRow []sqltypes.Value |
| 45 | + expectedLastPK string |
| 46 | + wantErr bool |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "identical PKs", |
| 50 | + fields: []*querypb.Field{ |
| 51 | + { |
| 52 | + Name: "a", Type: sqltypes.Int64, |
| 53 | + }, |
| 54 | + { |
| 55 | + Name: "b", Type: sqltypes.Int64, |
| 56 | + }, |
| 57 | + }, |
| 58 | + pkCols: []int{0, 1}, |
| 59 | + sourcePkCols: []int{0, 1}, |
| 60 | + lastRow: []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewInt64(2)}, |
| 61 | + expectedLastPK: `target:{fields:{name:"a" type:INT64} fields:{name:"b" type:INT64} rows:{lengths:1 lengths:1 values:"12"}}`, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "more PK cols on target", |
| 65 | + fields: []*querypb.Field{ |
| 66 | + { |
| 67 | + Name: "a", Type: sqltypes.Int64, |
| 68 | + }, |
| 69 | + { |
| 70 | + Name: "b", Type: sqltypes.Int64, |
| 71 | + }, |
| 72 | + }, |
| 73 | + pkCols: []int{0, 1}, |
| 74 | + sourcePkCols: []int{0}, |
| 75 | + lastRow: []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewInt64(2)}, |
| 76 | + expectedLastPK: `target:{fields:{name:"a" type:INT64} fields:{name:"b" type:INT64} rows:{lengths:1 lengths:1 values:"12"}} source:{fields:{name:"a" type:INT64} rows:{lengths:1 values:"1"}}`, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "more PK cols on source", |
| 80 | + fields: []*querypb.Field{ |
| 81 | + { |
| 82 | + Name: "a", Type: sqltypes.Int64, |
| 83 | + }, |
| 84 | + { |
| 85 | + Name: "b", Type: sqltypes.Int64, |
| 86 | + }, |
| 87 | + }, |
| 88 | + pkCols: []int{0}, |
| 89 | + sourcePkCols: []int{0, 1}, |
| 90 | + lastRow: []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewInt64(2)}, |
| 91 | + expectedLastPK: `target:{fields:{name:"a" type:INT64} rows:{lengths:1 values:"1"}} source:{fields:{name:"a" type:INT64} fields:{name:"b" type:INT64} rows:{lengths:1 lengths:1 values:"12"}}`, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + for _, tc := range testCases { |
| 96 | + t.Run(tc.name, func(t *testing.T) { |
| 97 | + dbc := binlogplayer.NewMockDBClient(t) |
| 98 | + dbc.ExpectRequest(fmt.Sprintf(queryTemplate, tc.expectedLastPK), &sqltypes.Result{}, nil) |
| 99 | + td := &tableDiffer{ |
| 100 | + wd: &workflowDiffer{ |
| 101 | + ct: &controller{ |
| 102 | + id: 1, |
| 103 | + TableDiffRowCounts: stats.NewCountersWithSingleLabel("", "", "Rows"), |
| 104 | + }, |
| 105 | + opts: &tabletmanagerdatapb.VDiffOptions{ |
| 106 | + CoreOptions: &tabletmanagerdatapb.VDiffCoreOptions{}, |
| 107 | + }, |
| 108 | + }, |
| 109 | + table: &tabletmanagerdatapb.TableDefinition{ |
| 110 | + Name: "test", |
| 111 | + }, |
| 112 | + tablePlan: &tablePlan{ |
| 113 | + pkCols: tc.pkCols, |
| 114 | + sourcePkCols: tc.sourcePkCols, |
| 115 | + table: &tabletmanagerdatapb.TableDefinition{ |
| 116 | + Fields: tc.fields, |
| 117 | + }, |
| 118 | + }, |
| 119 | + } |
| 120 | + if err := td.updateTableProgress(dbc, dr, tc.lastRow); (err != nil) != tc.wantErr { |
| 121 | + require.FailNow(t, "tableDiffer.updateTableProgress() error = %v, wantErr %v", |
| 122 | + err, tc.wantErr) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
0 commit comments