Skip to content

Commit

Permalink
Also generate default new UUID in VtctldServer VDiffCreate
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Oct 18, 2024
1 parent e881b9f commit 6c50c1d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
11 changes: 11 additions & 0 deletions go/vt/vtctl/workflow/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"text/template"
"time"

"github.com/google/uuid"
"golang.org/x/exp/maps"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
Expand Down Expand Up @@ -1837,6 +1838,16 @@ func (s *Server) VDiffCreate(ctx context.Context, req *vtctldatapb.VDiffCreateRe
span.Annotate("auto_start", req.GetAutoStart())
}

var err error
req.Uuid = strings.TrimSpace(req.Uuid)
if req.Uuid == "" { // Generate a UUID
req.Uuid = uuid.New().String()
} else { // Validate UUID if provided
if err = uuid.Validate(req.Uuid); err != nil {
return nil, vterrors.Wrapf(err, "invalid UUID provided: %s", req.Uuid)
}
}

tabletTypesStr := discovery.BuildTabletTypesString(req.TabletTypes, req.TabletSelectionPreference)

// This is a pointer so there's no ZeroValue in the message
Expand Down
34 changes: 28 additions & 6 deletions go/vt/vtctl/workflow/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"vitess.io/vitess/go/test/utils"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/memorytopo"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/topotools"
"vitess.io/vitess/go/vt/vtenv"
Expand Down Expand Up @@ -182,9 +181,17 @@ func TestCheckReshardingJournalExistsOnTablet(t *testing.T) {
// to ensure that it behaves as expected given a specific request.
func TestVDiffCreate(t *testing.T) {
ctx := context.Background()
ts := memorytopo.NewServer(ctx, "cell")
tmc := &fakeTMC{}
s := NewServer(vtenv.NewTestEnv(), ts, tmc)
workflowName := "wf1"
sourceKeyspace := &testKeyspace{
KeyspaceName: "source",
ShardNames: []string{"0"},
}
targetKeyspace := &testKeyspace{
KeyspaceName: "target",
ShardNames: []string{"-80", "80-"},
}
env := newTestEnv(t, ctx, defaultCellName, sourceKeyspace, targetKeyspace)
defer env.close()

tests := []struct {
name string
Expand All @@ -197,17 +204,32 @@ func TestVDiffCreate(t *testing.T) {
// We did not provide any keyspace or shard.
wantErr: "FindAllShardsInKeyspace() invalid keyspace name: UnescapeID err: invalid input identifier ''",
},
{
name: "generated UUID",
req: &vtctldatapb.VDiffCreateRequest{
TargetKeyspace: targetKeyspace.KeyspaceName,
Workflow: workflowName,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := s.VDiffCreate(ctx, tt.req)
if tt.wantErr == "" {
env.tmc.expectVRQueryResultOnKeyspaceTablets(targetKeyspace.KeyspaceName, &queryResult{
query: "select vrepl_id, table_name, lastpk from _vt.copy_state where vrepl_id in (1) and id in (select max(id) from _vt.copy_state where vrepl_id in (1) group by vrepl_id, table_name)",
result: &querypb.QueryResult{},
})
}
got, err := env.ws.VDiffCreate(ctx, tt.req)
if tt.wantErr != "" {
require.EqualError(t, err, tt.wantErr)
return
}
require.NoError(t, err)
require.NotNil(t, got)
require.NotEmpty(t, got.UUID)
// Ensure that we always use a valid UUID.
err = uuid.Validate(got.UUID)
require.NoError(t, err)
})
}
}
Expand Down

0 comments on commit 6c50c1d

Please sign in to comment.