-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Lord <[email protected]>
- Loading branch information
Showing
19 changed files
with
3,642 additions
and
2,370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
go/cmd/vtctldclient/command/vreplication/materialize/create.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
Copyright 2023 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 materialize | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"vitess.io/vitess/go/cmd/vtctldclient/cli" | ||
"vitess.io/vitess/go/cmd/vtctldclient/command/vreplication/common" | ||
"vitess.io/vitess/go/vt/sqlparser" | ||
"vitess.io/vitess/go/vt/topo/topoproto" | ||
|
||
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata" | ||
) | ||
|
||
var ( | ||
createOptions = struct { | ||
SourceKeyspace string | ||
TableSettings tableSettings | ||
}{} | ||
|
||
// create makes a MaterializeCreate gRPC call to a vtctld. | ||
create = &cobra.Command{ | ||
Use: "create", | ||
Short: "Create and run a Materialize VReplication workflow.", | ||
Example: `vtctldclient --server localhost:15999 materialize --workflow product_sales --target-keyspace customer create --source-keyspace commerce --table-settings 'target-table=sales_by_sku,"source-query=select sku, count(*), sum(price) from corder group by order_id"' --cells zone1 --cells zone2 --tablet-types replica`, | ||
SilenceUsage: true, | ||
DisableFlagsInUseLine: true, | ||
Aliases: []string{"Create"}, | ||
Args: cobra.NoArgs, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
if err := common.ParseAndValidateCreateOptions(cmd); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
RunE: commandCreate, | ||
} | ||
) | ||
|
||
func commandCreate(cmd *cobra.Command, args []string) error { | ||
format, err := common.GetOutputFormat(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
tsp := common.GetTabletSelectionPreference(cmd) | ||
cli.FinishedParsing(cmd) | ||
|
||
ms := &vtctldatapb.MaterializeSettings{ | ||
Workflow: common.BaseOptions.Workflow, | ||
TargetKeyspace: common.BaseOptions.TargetKeyspace, | ||
SourceKeyspace: createOptions.SourceKeyspace, | ||
TableSettings: createOptions.TableSettings.val, | ||
Cell: strings.Join(common.CreateOptions.Cells, ","), | ||
TabletTypes: topoproto.MakeStringTypeCSV(common.CreateOptions.TabletTypes), | ||
TabletSelectionPreference: tsp, | ||
} | ||
|
||
req := &vtctldatapb.MaterializeCreateRequest{ | ||
Settings: ms, | ||
} | ||
|
||
_, err = common.GetClient().MaterializeCreate(common.GetCommandCtx(), req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if format == "json" { | ||
resp := struct { | ||
Action string | ||
Status string | ||
}{ | ||
Action: "create", | ||
Status: "success", | ||
} | ||
jsonText, _ := cli.MarshalJSONPretty(resp) | ||
fmt.Println(string(jsonText)) | ||
} else { | ||
fmt.Printf("Materialization workflow %s successfully created in the %s keyspace. Use show to view the status.\n", | ||
common.BaseOptions.Workflow, common.BaseOptions.TargetKeyspace) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// tableSettings is a wrapper around a slice of TableMaterializeSettings | ||
// proto messages that implements the pflag.Value interface. | ||
type tableSettings struct { | ||
val []*vtctldatapb.TableMaterializeSettings | ||
} | ||
|
||
func (ts *tableSettings) String() string { | ||
tsj, _ := json.Marshal(ts.val) | ||
return string(tsj) | ||
} | ||
|
||
func (ts *tableSettings) Set(v string) error { | ||
ts.val = make([]*vtctldatapb.TableMaterializeSettings, 0) | ||
err := json.Unmarshal([]byte(v), &ts.val) | ||
if err != nil { | ||
return fmt.Errorf("table-settings is not valid JSON") | ||
} | ||
if len(ts.val) == 0 { | ||
return fmt.Errorf("empty table-settings") | ||
} | ||
|
||
// You cannot have multiple source expressions against the | ||
// same table. | ||
seenSourceTables := make(map[string]bool) | ||
for _, tms := range ts.val { | ||
if tms.TargetTable == "" || tms.SourceExpression == "" { | ||
return fmt.Errorf("missing target_table or source_expression") | ||
} | ||
// Validate that the query is valid. | ||
stmt, err := sqlparser.Parse(tms.SourceExpression) | ||
if err != nil { | ||
return fmt.Errorf("invalid source_expression: %q", tms.SourceExpression) | ||
} | ||
// Validate that each source-expression uses a different table. | ||
// If any of them query the same table the materialize workflow | ||
// will fail. | ||
err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { | ||
switch node := node.(type) { | ||
case sqlparser.TableName: | ||
if !node.Name.IsEmpty() { | ||
if seenSourceTables[node.Name.String()] { | ||
return false, fmt.Errorf("multiple source_expression queries use the same table: %q", node.Name.String()) | ||
} | ||
seenSourceTables[node.Name.String()] = true | ||
} | ||
} | ||
return true, nil | ||
}, stmt) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (ts *tableSettings) Type() string { | ||
return "JSON" | ||
} |
61 changes: 61 additions & 0 deletions
61
go/cmd/vtctldclient/command/vreplication/materialize/materialize.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2023 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 materialize | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"vitess.io/vitess/go/cmd/vtctldclient/command/vreplication/common" | ||
) | ||
|
||
var ( | ||
// materialize is the base command for all actions related to Materialize. | ||
materialize = &cobra.Command{ | ||
Use: "Materialize --workflow <workflow> --target-keyspace <keyspace> [command] [command-flags]", | ||
Short: "Perform commands related to materializing query results from the source keyspace into tables in the target keyspace.", | ||
DisableFlagsInUseLine: true, | ||
Aliases: []string{"materialize"}, | ||
Args: cobra.ExactArgs(1), | ||
} | ||
) | ||
|
||
func registerCommands(root *cobra.Command) { | ||
common.AddCommonFlags(materialize) | ||
root.AddCommand(materialize) | ||
|
||
common.AddCommonCreateFlags(create) | ||
create.Flags().StringVar(&createOptions.SourceKeyspace, "source-keyspace", "", "Keyspace where the tables are being moved from.") | ||
create.MarkFlagRequired("source-keyspace") | ||
create.Flags().Var(&createOptions.TableSettings, "table-settings", "A JSON array where each value must contain two key/value pairs. The first key is 'target_table' and it is the name of the table in the target-keyspace to store the results in. The second key is 'source_expression' and its value is the select to run against the source table. An optional k/v pair can be specified for 'create_ddl' which provides the DDL to create the target table if it does not exist.") | ||
create.MarkFlagRequired("table-settings") | ||
root.AddCommand(create) | ||
|
||
opts := &common.SubCommandsOpts{ | ||
SubCommand: "Materialize", | ||
Workflow: "product_sales", | ||
} | ||
materialize.AddCommand(common.GetShowCommand(opts)) | ||
|
||
materialize.AddCommand(common.GetStartCommand(opts)) | ||
materialize.AddCommand(common.GetStopCommand(opts)) | ||
|
||
materialize.AddCommand(common.GetCancelCommand(opts)) | ||
} | ||
|
||
func init() { | ||
common.RegisterCommandHandler("Materialize", registerCommands) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.