Skip to content

Commit 73330ca

Browse files
committed
Adjustments for v22
Signed-off-by: Matt Lord <mattalord@gmail.com>
1 parent beaf71b commit 73330ca

2 files changed

Lines changed: 38 additions & 128 deletions

File tree

go/vt/vtctl/workflow/sequences.go

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,21 @@ func (ts *trafficSwitcher) getMaxSequenceValue(ctx context.Context, seq *sequenc
166166
Query: []byte(query.Query),
167167
MaxRows: 1,
168168
})
169-
if terr != nil || len(qr.Rows) != 1 {
169+
if terr != nil {
170170
return vterrors.Errorf(vtrpcpb.Code_INTERNAL,
171171
"failed to get the max used sequence value for target table %s.%s on tablet %s in order to initialize the backing sequence table: %v",
172172
ts.targetKeyspace, seq.usingTableName, topoproto.TabletAliasString(primary.Alias), terr)
173173
}
174-
rawVal := sqltypes.Proto3ToResult(qr).Rows[0][0]
175174
maxID := int64(0)
176-
if !rawVal.IsNull() { // If it's NULL then there are no rows and 0 remains the max
177-
maxID, terr = rawVal.ToInt64()
178-
if terr != nil {
179-
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "failed to get the max used sequence value for target table %s.%s on tablet %s in order to initialize the backing sequence table: %v",
180-
ts.targetKeyspace, seq.usingTableName, topoproto.TabletAliasString(primary.Alias), terr)
175+
// If no rows are returned, max remains 0.
176+
if len(qr.Rows) != 0 {
177+
rawVal := sqltypes.Proto3ToResult(qr).Rows[0][0]
178+
if !rawVal.IsNull() { // If it's NULL then 0 remains the max
179+
maxID, terr = rawVal.ToInt64()
180+
if terr != nil {
181+
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "failed to get the max used sequence value for target table %s.%s on tablet %s in order to initialize the backing sequence table: %v",
182+
ts.targetKeyspace, seq.usingTableName, topoproto.TabletAliasString(primary.Alias), terr)
183+
}
181184
}
182185
}
183186
setMaxSequenceValue(maxID)
@@ -250,27 +253,9 @@ func (ts *trafficSwitcher) getCurrentSequenceValue(ctx context.Context, seq *seq
250253
return currentVal, nil
251254
}
252255

253-
<<<<<<< HEAD
254256
func (ts *trafficSwitcher) updateSequenceValue(ctx context.Context, seq *sequenceMetadata, currentMaxValue int64) error {
255257
if err := seq.escapeValues(); err != nil {
256258
return err
257-
=======
258-
func (ts *trafficSwitcher) updateSequenceValues(ctx context.Context, sequences []*sequenceMetadata, maxValues map[string]int64) error {
259-
sequencesByShard := map[string][]*tabletmanagerdatapb.UpdateSequenceTablesRequest_SequenceMetadata{}
260-
for _, seq := range sequences {
261-
maxValue := maxValues[seq.backingTableName]
262-
sequenceShard, ierr := ts.TopoServer().GetOnlyShard(ctx, seq.backingTableKeyspace)
263-
if ierr != nil || sequenceShard == nil || sequenceShard.PrimaryAlias == nil {
264-
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "failed to get the primary tablet for keyspace %s: %v",
265-
seq.backingTableKeyspace, ierr)
266-
}
267-
tabletAliasStr := topoproto.TabletAliasString(sequenceShard.PrimaryAlias)
268-
sequencesByShard[tabletAliasStr] = append(sequencesByShard[tabletAliasStr], &tabletmanagerdatapb.UpdateSequenceTablesRequest_SequenceMetadata{
269-
BackingTableName: seq.backingTableName,
270-
BackingTableDbName: seq.backingTableDBName,
271-
MaxValue: maxValue,
272-
})
273-
>>>>>>> 81356abde1 (VReplication: Properly Handle Sequence Table Initialization For Empty Tables (#19226))
274259
}
275260
nextVal := currentMaxValue + 1
276261
log.Infof("Updating sequence %s.%s to %d", seq.backingTableDBName, seq.backingTableName, nextVal)
@@ -370,9 +355,6 @@ func (ts *trafficSwitcher) initializeTargetSequences(ctx context.Context, sequen
370355
}
371356
for _, sm := range sequencesByBackingTable {
372357
maxValue := maxValues[sm.backingTableName]
373-
if maxValue == 0 {
374-
continue
375-
}
376358
if err := ts.updateSequenceValue(ctx, sm, maxValue); err != nil {
377359
return err
378360
}

go/vt/vtctl/workflow/sequences_test.go

Lines changed: 28 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ import (
2828
"vitess.io/vitess/go/sqlescape"
2929
"vitess.io/vitess/go/sqltypes"
3030
"vitess.io/vitess/go/vt/mysqlctl/tmutils"
31-
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
3231
"vitess.io/vitess/go/vt/proto/vschema"
33-
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
3432
"vitess.io/vitess/go/vt/sqlparser"
3533
"vitess.io/vitess/go/vt/topo"
3634
"vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication"
35+
36+
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
37+
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
3738
)
3839

3940
func TestInitializeTargetSequences(t *testing.T) {
@@ -89,121 +90,48 @@ func TestInitializeTargetSequences(t *testing.T) {
8990
},
9091
}
9192

92-
<<<<<<< HEAD
93-
env.tmc.expectVRQuery(200, "/select max.*", sqltypes.MakeTestResult(sqltypes.MakeTestFields("maxval", "int64"), "34"))
94-
// Expect the insert query to be executed with 35 as a params, since we provide a maxID of 34 in the last query
95-
env.tmc.expectVRQuery(100, "/insert into.*35.*", &sqltypes.Result{RowsAffected: 1})
96-
=======
9793
type testCase struct {
98-
name string
99-
maxValueRequest *getMaxValueForSequencesRequestResponse
100-
updateSeqTableRequest *tabletmanagerdatapb.UpdateSequenceTablesRequest
94+
name string
95+
selectMaxQuery string
96+
selectMaxResult *sqltypes.Result
97+
insertQuery string
10198
}
10299

103100
testCases := []testCase{
104101
{
105-
name: "initialize sequences",
106-
maxValueRequest: &getMaxValueForSequencesRequestResponse{
107-
req: &tabletmanagerdatapb.GetMaxValueForSequencesRequest{
108-
Sequences: []*tabletmanagerdatapb.GetMaxValueForSequencesRequest_SequenceMetadata{
109-
{
110-
BackingTableName: "my-seq1",
111-
UsingColEscaped: "`my-col`",
112-
UsingTableNameEscaped: fmt.Sprintf("`%s`", tableName),
113-
UsingTableDbNameEscaped: "`vt_targetks`",
114-
},
115-
{
116-
BackingTableName: "my-seq2",
117-
UsingColEscaped: "`my-col-2`",
118-
UsingTableNameEscaped: fmt.Sprintf("`%s`", tableName2),
119-
UsingTableDbNameEscaped: "`vt_targetks`",
120-
},
121-
},
122-
},
123-
res: &tabletmanagerdatapb.GetMaxValueForSequencesResponse{
124-
MaxValuesBySequenceTable: map[string]int64{
125-
"my-seq1": 34,
126-
"my-seq2": 10,
127-
},
128-
},
129-
},
130-
updateSeqTableRequest: &tabletmanagerdatapb.UpdateSequenceTablesRequest{
131-
Sequences: []*tabletmanagerdatapb.UpdateSequenceTablesRequest_SequenceMetadata{
132-
{
133-
BackingTableName: "my-seq1",
134-
BackingTableDbName: "vt_" + sourceKeyspaceName,
135-
MaxValue: 34,
136-
},
137-
{
138-
BackingTableName: "my-seq2",
139-
BackingTableDbName: "vt_" + sourceKeyspaceName,
140-
MaxValue: 10,
141-
},
142-
},
143-
},
102+
name: "initialize sequences",
103+
selectMaxQuery: "/select max.*",
104+
selectMaxResult: sqltypes.MakeTestResult(
105+
sqltypes.MakeTestFields("maxval", "int64"),
106+
"34",
107+
),
108+
insertQuery: "/insert into.*35.*",
144109
},
145110
{
146-
name: "initialize sequences for empty tables",
147-
maxValueRequest: &getMaxValueForSequencesRequestResponse{
148-
req: &tabletmanagerdatapb.GetMaxValueForSequencesRequest{
149-
Sequences: []*tabletmanagerdatapb.GetMaxValueForSequencesRequest_SequenceMetadata{
150-
{
151-
BackingTableName: "my-seq1",
152-
UsingColEscaped: "`my-col`",
153-
UsingTableNameEscaped: fmt.Sprintf("`%s`", tableName),
154-
UsingTableDbNameEscaped: "`vt_targetks`",
155-
},
156-
{
157-
BackingTableName: "my-seq2",
158-
UsingColEscaped: "`my-col-2`",
159-
UsingTableNameEscaped: fmt.Sprintf("`%s`", tableName2),
160-
UsingTableDbNameEscaped: "`vt_targetks`",
161-
},
162-
},
163-
},
164-
res: &tabletmanagerdatapb.GetMaxValueForSequencesResponse{
165-
MaxValuesBySequenceTable: map[string]int64{},
166-
},
167-
},
168-
updateSeqTableRequest: &tabletmanagerdatapb.UpdateSequenceTablesRequest{
169-
Sequences: []*tabletmanagerdatapb.UpdateSequenceTablesRequest_SequenceMetadata{
170-
{
171-
BackingTableName: "my-seq1",
172-
BackingTableDbName: "vt_" + sourceKeyspaceName,
173-
MaxValue: 0,
174-
},
175-
{
176-
BackingTableName: "my-seq2",
177-
BackingTableDbName: "vt_" + sourceKeyspaceName,
178-
MaxValue: 0,
179-
},
180-
},
181-
},
111+
name: "initialize sequences for empty tables",
112+
selectMaxQuery: "/select max.*",
113+
selectMaxResult: sqltypes.MakeTestResult(
114+
sqltypes.MakeTestFields("maxval", "int64"),
115+
),
116+
insertQuery: "/insert into.*1.*",
182117
},
183118
}
184-
>>>>>>> 81356abde1 (VReplication: Properly Handle Sequence Table Initialization For Empty Tables (#19226))
185119

186120
for _, tc := range testCases {
187121
t.Run(tc.name, func(t *testing.T) {
188-
require.NotNil(t, tc.maxValueRequest)
189-
require.NotNil(t, tc.updateSeqTableRequest)
190-
env.tmc.expectGetMaxValueForSequencesRequest(200, tc.maxValueRequest)
191-
env.tmc.expectUpdateSequenceTablesRequest(100, tc.updateSeqTableRequest)
192-
193-
<<<<<<< HEAD
194-
// Expect the queries to be cleared
195-
assert.Emptyf(t, env.tmc.vrQueries[100], "expected no queries to be executed, found: %q", env.tmc.vrQueries[100])
196-
assert.Empty(t, env.tmc.vrQueries[200], "expected no queries to be executed, found: %q", env.tmc.vrQueries[200])
197-
=======
122+
// Expect the select max query to be executed
123+
env.tmc.expectVRQuery(200, tc.selectMaxQuery, tc.selectMaxResult)
124+
// Expect the insert query to be executed
125+
env.tmc.expectVRQuery(100, tc.insertQuery, &sqltypes.Result{RowsAffected: 1})
126+
198127
err = sw.initializeTargetSequences(ctx, sequencesByBackingTable)
199128
assert.NoError(t, err)
200129

201-
// Expect the requests to be cleared.
202-
assert.Emptyf(t, env.tmc.updateSequenceTablesRequests, "expected no remaining UpdateSequenceTables requests")
203-
assert.Emptyf(t, env.tmc.getMaxValueForSequencesRequests, "expected no remaining GetMaxValueForSequences requests")
130+
// Expect the queries to be cleared
131+
assert.Emptyf(t, env.tmc.vrQueries[100], "expected no queries to be executed, found: %q", env.tmc.vrQueries[100])
132+
assert.Empty(t, env.tmc.vrQueries[200], "expected no queries to be executed, found: %q", env.tmc.vrQueries[200])
204133
})
205134
}
206-
>>>>>>> 81356abde1 (VReplication: Properly Handle Sequence Table Initialization For Empty Tables (#19226))
207135
}
208136

209137
func TestGetTargetSequenceMetadata(t *testing.T) {

0 commit comments

Comments
 (0)