-
Notifications
You must be signed in to change notification settings - Fork 60
Add vtctld set-shard-tablet-control command #1268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
maxenglander
wants to merge
4
commits into
maxeng/issues-gh-1753/get-shard
Choose a base branch
from
maxeng/issues-gh-1753/set-shard-tablet-control
base: maxeng/issues-gh-1753/get-shard
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
| package vtctld | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| ps "github.com/planetscale/planetscale-go/planetscale" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // SetShardTabletControlCmd updates shard tablet controls via vtctld. | ||
| func SetShardTabletControlCmd(ch *cmdutil.Helper) *cobra.Command { | ||
| var flags struct { | ||
| keyspace string | ||
| shard string | ||
| tabletType string | ||
| cells []string | ||
| deniedTables []string | ||
| remove bool | ||
| disableQueryService bool | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "set-shard-tablet-control <database> <branch>", | ||
| Short: "Update shard tablet controls for a branch", | ||
| Long: "Update live shard tablet controls from the cluster via vtctld, " + | ||
| "including denied tables used during MoveTables cleanup.", | ||
| Args: cmdutil.RequiredArgs("database", "branch"), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
| database, branch := args[0], args[1] | ||
|
|
||
| if flags.keyspace == "" { | ||
| return fmt.Errorf("keyspace is required") | ||
| } | ||
| if flags.shard == "" { | ||
| return fmt.Errorf("shard is required") | ||
| } | ||
| if flags.tabletType == "" { | ||
| return fmt.Errorf("tablet-type is required") | ||
| } | ||
|
|
||
| removeSet := cmd.Flags().Changed("remove") | ||
| disableQueryServiceSet := cmd.Flags().Changed("disable-query-service") | ||
|
|
||
| client, err := ch.Client() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| end := ch.Printer.PrintProgress( | ||
| fmt.Sprintf("Updating tablet controls for %s/%s on %s…", | ||
| flags.keyspace, flags.shard, | ||
| progressTarget(ch.Config.Organization, database, branch))) | ||
| defer end() | ||
|
|
||
| req := &ps.VtctldSetShardTabletControlRequest{ | ||
| Organization: ch.Config.Organization, | ||
| Database: database, | ||
| Branch: branch, | ||
| Keyspace: flags.keyspace, | ||
| Shard: flags.shard, | ||
| TabletType: flags.tabletType, | ||
| Cells: flags.cells, | ||
| DeniedTables: flags.deniedTables, | ||
| } | ||
| if removeSet { | ||
| req.Remove = &flags.remove | ||
| } | ||
| if disableQueryServiceSet { | ||
| req.DisableQueryService = &flags.disableQueryService | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| data, err := client.Vtctld.SetShardTabletControl(ctx, req) | ||
| if err != nil { | ||
| return cmdutil.HandleError(err) | ||
| } | ||
|
|
||
| end() | ||
| return ch.Printer.PrettyPrintJSON(data) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace name") | ||
| cmd.Flags().StringVar(&flags.shard, "shard", "", "Shard name (e.g. \"-\" for unsharded)") | ||
| cmd.Flags().StringVar(&flags.tabletType, "tablet-type", "", "Tablet type (e.g. rdonly, replica, primary)") | ||
| cmd.Flags().StringSliceVar(&flags.cells, "cells", nil, "Cells to update (comma-separated)") | ||
| cmd.Flags().StringSliceVar(&flags.deniedTables, "denied-tables", nil, "Tables to add to or remove from the denylist (comma-separated)") | ||
| cmd.Flags().BoolVar(&flags.remove, "remove", false, "Remove tablet controls (or specific denied tables when combined with --denied-tables)") | ||
| cmd.Flags().BoolVar(&flags.disableQueryService, "disable-query-service", false, "Disable query service on the provided tablets") | ||
| cmd.MarkFlagRequired("keyspace") | ||
| cmd.MarkFlagRequired("shard") | ||
| cmd.MarkFlagRequired("tablet-type") | ||
|
|
||
| return cmd | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
internal/cmd/branch/vtctld/set_shard_tablet_control_test.go
This file contains hidden or 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,66 @@ | ||
| package vtctld | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| qt "github.com/frankban/quicktest" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| "github.com/planetscale/cli/internal/config" | ||
| "github.com/planetscale/cli/internal/mock" | ||
| "github.com/planetscale/cli/internal/printer" | ||
| ps "github.com/planetscale/planetscale-go/planetscale" | ||
| ) | ||
|
|
||
| func TestSetShardTabletControl(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| org := "my-org" | ||
| db := "my-db" | ||
| branch := "my-branch" | ||
|
|
||
| svc := &mock.VtctldService{ | ||
| SetShardTabletControlFn: func(ctx context.Context, req *ps.VtctldSetShardTabletControlRequest) (json.RawMessage, error) { | ||
| c.Assert(req.Organization, qt.Equals, org) | ||
| c.Assert(req.Database, qt.Equals, db) | ||
| c.Assert(req.Branch, qt.Equals, branch) | ||
| c.Assert(req.Keyspace, qt.Equals, "commerce") | ||
| c.Assert(req.Shard, qt.Equals, "-") | ||
| c.Assert(req.TabletType, qt.Equals, "rdonly") | ||
| c.Assert(req.DeniedTables, qt.DeepEquals, []string{"customers"}) | ||
| c.Assert(req.Remove, qt.Not(qt.IsNil)) | ||
| c.Assert(*req.Remove, qt.Equals, true) | ||
| return json.RawMessage(`{}`), nil | ||
| }, | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| format := printer.JSON | ||
| p := printer.NewPrinter(&format) | ||
| p.SetResourceOutput(&buf) | ||
|
|
||
| ch := &cmdutil.Helper{ | ||
| Printer: p, | ||
| Config: &config.Config{Organization: org}, | ||
| Client: func() (*ps.Client, error) { | ||
| return &ps.Client{ | ||
| Vtctld: svc, | ||
| }, nil | ||
| }, | ||
| } | ||
|
|
||
| cmd := SetShardTabletControlCmd(ch) | ||
| cmd.SetArgs([]string{db, branch}) | ||
| cmd.Flags().Set("keyspace", "commerce") | ||
| cmd.Flags().Set("shard", "-") | ||
| cmd.Flags().Set("tablet-type", "rdonly") | ||
| cmd.Flags().Set("denied-tables", "customers") | ||
| cmd.Flags().Set("remove", "true") | ||
|
|
||
| err := cmd.Execute() | ||
| c.Assert(err, qt.IsNil) | ||
| c.Assert(svc.SetShardTabletControlFnInvoked, qt.IsTrue) | ||
| } |
This file contains hidden or 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,67 @@ | ||
| package vtctld | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| ps "github.com/planetscale/planetscale-go/planetscale" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // GetShardCmd reads a shard record from the cluster via vtctld. | ||
| func GetShardCmd(ch *cmdutil.Helper) *cobra.Command { | ||
| var flags struct { | ||
| keyspace string | ||
| shard string | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "get-shard <database> <branch>", | ||
| Short: "Get a shard record for a branch", | ||
| Long: "Get a live shard record from the cluster via vtctld, including tablet controls and denied tables.", | ||
| Args: cmdutil.RequiredArgs("database", "branch"), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
| database, branch := args[0], args[1] | ||
|
|
||
| if flags.keyspace == "" { | ||
| return fmt.Errorf("keyspace is required") | ||
| } | ||
| if flags.shard == "" { | ||
| return fmt.Errorf("shard is required") | ||
| } | ||
|
|
||
| client, err := ch.Client() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| end := ch.Printer.PrintProgress( | ||
| fmt.Sprintf("Fetching shard %s/%s for %s\u2026", | ||
| flags.keyspace, flags.shard, | ||
| progressTarget(ch.Config.Organization, database, branch))) | ||
| defer end() | ||
|
|
||
| data, err := client.Vtctld.GetShard(ctx, &ps.VtctldGetShardRequest{ | ||
| Organization: ch.Config.Organization, | ||
| Database: database, | ||
| Branch: branch, | ||
| Keyspace: flags.keyspace, | ||
| Shard: flags.shard, | ||
| }) | ||
| if err != nil { | ||
| return cmdutil.HandleError(err) | ||
| } | ||
|
|
||
| end() | ||
| return ch.Printer.PrettyPrintJSON(data) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace name") | ||
| cmd.Flags().StringVar(&flags.shard, "shard", "", "Shard name (e.g. \"-\" for unsharded)") | ||
| cmd.MarkFlagRequired("keyspace") | ||
| cmd.MarkFlagRequired("shard") | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or 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,56 @@ | ||
| package vtctld | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| qt "github.com/frankban/quicktest" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| "github.com/planetscale/cli/internal/config" | ||
| "github.com/planetscale/cli/internal/mock" | ||
| "github.com/planetscale/cli/internal/printer" | ||
| ps "github.com/planetscale/planetscale-go/planetscale" | ||
| ) | ||
|
|
||
| func TestGetShard(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| org := "my-org" | ||
| db := "my-db" | ||
| branch := "my-branch" | ||
|
|
||
| svc := &mock.VtctldService{ | ||
| GetShardFn: func(ctx context.Context, req *ps.VtctldGetShardRequest) (json.RawMessage, error) { | ||
| c.Assert(req.Organization, qt.Equals, org) | ||
| c.Assert(req.Database, qt.Equals, db) | ||
| c.Assert(req.Branch, qt.Equals, branch) | ||
| c.Assert(req.Keyspace, qt.Equals, "commerce") | ||
| c.Assert(req.Shard, qt.Equals, "-") | ||
| return json.RawMessage(`{"keyspace":"commerce","name":"-"}`), nil | ||
| }, | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| format := printer.JSON | ||
| p := printer.NewPrinter(&format) | ||
| p.SetResourceOutput(&buf) | ||
|
|
||
| ch := &cmdutil.Helper{ | ||
| Printer: p, | ||
| Config: &config.Config{Organization: org}, | ||
| Client: func() (*ps.Client, error) { | ||
| return &ps.Client{ | ||
| Vtctld: svc, | ||
| }, nil | ||
| }, | ||
| } | ||
|
|
||
| cmd := GetShardCmd(ch) | ||
| cmd.SetArgs([]string{db, branch, "--keyspace", "commerce", "--shard", "-"}) | ||
| err := cmd.Execute() | ||
| c.Assert(err, qt.IsNil) | ||
| c.Assert(svc.GetShardFnInvoked, qt.IsTrue) | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing mutation flag validation
Medium Severity
The command reads
removeanddisable-query-serviceviaFlags().Changedbut never requires at least one of--remove,--denied-tables, or--disable-query-servicebefore calling the API. The PR describes that check and manual “CLI validation,” yet no matchingfmt.Errorfexists in this repo (unlikedeployrequest edit).Reviewed by Cursor Bugbot for commit 27c1ed7. Configure here.