@@ -26,21 +26,33 @@ import (
26
26
"google.golang.org/protobuf/proto"
27
27
28
28
"vitess.io/vitess/go/sqlescape"
29
+ "vitess.io/vitess/go/vt/logutil"
29
30
"vitess.io/vitess/go/vt/schema"
30
31
"vitess.io/vitess/go/vt/sqlparser"
32
+ "vitess.io/vitess/go/vt/topo"
31
33
"vitess.io/vitess/go/vt/vtctl/schematools"
32
34
"vitess.io/vitess/go/vt/vterrors"
33
35
"vitess.io/vitess/go/vt/vtgate/vindexes"
36
+ "vitess.io/vitess/go/vt/vttablet/tmclient"
34
37
35
38
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
36
39
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
37
40
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
38
41
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
39
42
)
40
43
44
+ // lookup is responsible for performing actions related to lookup vindexes.
45
+ type lookup struct {
46
+ ts * topo.Server
47
+ tmc tmclient.TabletManagerClient
48
+
49
+ logger logutil.Logger
50
+ parser * sqlparser.Parser
51
+ }
52
+
41
53
// prepareCreateLookup performs the preparatory steps for creating a
42
54
// Lookup Vindex.
43
- func (wf * workflowFetcher ) prepareCreateLookup (ctx context.Context , workflow , keyspace string , specs * vschemapb.Keyspace , continueAfterCopyWithOwner bool ) (
55
+ func (l * lookup ) prepareCreateLookup (ctx context.Context , workflow , keyspace string , specs * vschemapb.Keyspace , continueAfterCopyWithOwner bool ) (
44
56
ms * vtctldatapb.MaterializeSettings , sourceVSchema , targetVSchema * vschemapb.Keyspace , cancelFunc func () error , err error ) {
45
57
var (
46
58
// sourceVSchemaTable is the table info present in the vschema.
@@ -54,7 +66,7 @@ func (wf *workflowFetcher) prepareCreateLookup(ctx context.Context, workflow, ke
54
66
)
55
67
56
68
// Validate input vindex.
57
- vindex , vInfo , err := wf .validateAndGetVindex (specs )
69
+ vindex , vInfo , err := l .validateAndGetVindex (specs )
58
70
if err != nil {
59
71
return nil , nil , nil , nil , err
60
72
}
@@ -69,7 +81,7 @@ func (wf *workflowFetcher) prepareCreateLookup(ctx context.Context, workflow, ke
69
81
return nil , nil , nil , nil , err
70
82
}
71
83
72
- sourceVSchema , targetVSchema , err = wf .getTargetAndSourceVSchema (ctx , keyspace , vInfo .targetKeyspace )
84
+ sourceVSchema , targetVSchema , err = l .getTargetAndSourceVSchema (ctx , keyspace , vInfo .targetKeyspace )
73
85
if err != nil {
74
86
return nil , nil , nil , nil , err
75
87
}
@@ -91,7 +103,7 @@ func (wf *workflowFetcher) prepareCreateLookup(ctx context.Context, workflow, ke
91
103
}
92
104
93
105
// Validate against source schema.
94
- sourceShards , err := wf .ts .GetServingShards (ctx , keyspace )
106
+ sourceShards , err := l .ts .GetServingShards (ctx , keyspace )
95
107
if err != nil {
96
108
return nil , nil , nil , nil , err
97
109
}
@@ -102,7 +114,7 @@ func (wf *workflowFetcher) prepareCreateLookup(ctx context.Context, workflow, ke
102
114
}
103
115
104
116
req := & tabletmanagerdatapb.GetSchemaRequest {Tables : []string {vInfo .sourceTableName }}
105
- tableSchema , err := schematools .GetSchema (ctx , wf .ts , wf .tmc , onesource .PrimaryAlias , req )
117
+ tableSchema , err := schematools .GetSchema (ctx , l .ts , l .tmc , onesource .PrimaryAlias , req )
106
118
if err != nil {
107
119
return nil , nil , nil , nil , err
108
120
}
@@ -113,7 +125,7 @@ func (wf *workflowFetcher) prepareCreateLookup(ctx context.Context, workflow, ke
113
125
}
114
126
115
127
// Generate "create table" statement.
116
- createDDL , err = wf .generateCreateDDLStatement (tableSchema , sourceVindexColumns , vInfo , vindex )
128
+ createDDL , err = l .generateCreateDDLStatement (tableSchema , sourceVindexColumns , vInfo , vindex )
117
129
if err != nil {
118
130
return nil , nil , nil , nil , err
119
131
}
@@ -191,7 +203,7 @@ func (wf *workflowFetcher) prepareCreateLookup(ctx context.Context, workflow, ke
191
203
if targetChanged {
192
204
cancelFunc = func () error {
193
205
// Restore the original target vschema.
194
- return wf .ts .SaveVSchema (ctx , vInfo .targetKeyspace , ogTargetVSchema )
206
+ return l .ts .SaveVSchema (ctx , vInfo .targetKeyspace , ogTargetVSchema )
195
207
}
196
208
}
197
209
@@ -230,7 +242,7 @@ type vindexInfo struct {
230
242
}
231
243
232
244
// validateAndGetVindex validates and extracts vindex configuration
233
- func (wf * workflowFetcher ) validateAndGetVindex (specs * vschemapb.Keyspace ) (* vschemapb.Vindex , * vindexInfo , error ) {
245
+ func (l * lookup ) validateAndGetVindex (specs * vschemapb.Keyspace ) (* vschemapb.Vindex , * vindexInfo , error ) {
234
246
if specs == nil {
235
247
return nil , nil , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , "no vindex provided" )
236
248
}
@@ -245,7 +257,7 @@ func (wf *workflowFetcher) validateAndGetVindex(specs *vschemapb.Keyspace) (*vsc
245
257
return nil , nil , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , "vindex %s is not a lookup type" , vindex .Type )
246
258
}
247
259
248
- targetKeyspace , targetTableName , err := wf .parser .ParseTable (vindex .Params ["table" ])
260
+ targetKeyspace , targetTableName , err := l .parser .ParseTable (vindex .Params ["table" ])
249
261
if err != nil || targetKeyspace == "" {
250
262
return nil , nil , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT ,
251
263
"vindex table name (%s) must be in the form <keyspace>.<table>" , vindex .Params ["table" ])
@@ -306,8 +318,8 @@ func (wf *workflowFetcher) validateAndGetVindex(specs *vschemapb.Keyspace) (*vsc
306
318
}, nil
307
319
}
308
320
309
- func (wf * workflowFetcher ) getTargetAndSourceVSchema (ctx context.Context , sourceKeyspace string , targetKeyspace string ) (sourceVSchema * vschemapb.Keyspace , targetVSchema * vschemapb.Keyspace , err error ) {
310
- sourceVSchema , err = wf .ts .GetVSchema (ctx , sourceKeyspace )
321
+ func (l * lookup ) getTargetAndSourceVSchema (ctx context.Context , sourceKeyspace string , targetKeyspace string ) (sourceVSchema * vschemapb.Keyspace , targetVSchema * vschemapb.Keyspace , err error ) {
322
+ sourceVSchema , err = l .ts .GetVSchema (ctx , sourceKeyspace )
311
323
if err != nil {
312
324
return nil , nil , err
313
325
}
@@ -319,7 +331,7 @@ func (wf *workflowFetcher) getTargetAndSourceVSchema(ctx context.Context, source
319
331
if sourceKeyspace == targetKeyspace {
320
332
targetVSchema = sourceVSchema
321
333
} else {
322
- targetVSchema , err = wf .ts .GetVSchema (ctx , targetKeyspace )
334
+ targetVSchema , err = l .ts .GetVSchema (ctx , targetKeyspace )
323
335
if err != nil {
324
336
return nil , nil , err
325
337
}
@@ -367,7 +379,7 @@ func getSourceTable(specs *vschemapb.Keyspace, targetTableName string, fromCols
367
379
return sourceTable , sourceTableName , nil
368
380
}
369
381
370
- func (wf * workflowFetcher ) generateCreateDDLStatement (tableSchema * tabletmanagerdatapb.SchemaDefinition , sourceVindexColumns []string , vInfo * vindexInfo , vindex * vschemapb.Vindex ) (string , error ) {
382
+ func (l * lookup ) generateCreateDDLStatement (tableSchema * tabletmanagerdatapb.SchemaDefinition , sourceVindexColumns []string , vInfo * vindexInfo , vindex * vschemapb.Vindex ) (string , error ) {
371
383
lines := strings .Split (tableSchema .TableDefinitions [0 ].Schema , "\n " )
372
384
if len (lines ) < 3 {
373
385
// Should never happen.
@@ -405,7 +417,7 @@ func (wf *workflowFetcher) generateCreateDDLStatement(tableSchema *tabletmanager
405
417
createDDL := strings .Join (modified , "\n " )
406
418
407
419
// Confirm that our DDL is valid before we create anything.
408
- if _ , err := wf .parser .ParseStrictDDL (createDDL ); err != nil {
420
+ if _ , err := l .parser .ParseStrictDDL (createDDL ); err != nil {
409
421
return "" , vterrors .Errorf (vtrpcpb .Code_INTERNAL , "error: %v; invalid lookup table definition generated: %s" ,
410
422
err , createDDL )
411
423
}
0 commit comments