diff --git a/internal/cmd/keyspace/create.go b/internal/cmd/keyspace/create.go index 255eddef..68f891a6 100644 --- a/internal/cmd/keyspace/create.go +++ b/internal/cmd/keyspace/create.go @@ -20,6 +20,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { shards int clusterSize string additionalReplicas int + maxRollout int wait bool } @@ -31,6 +32,13 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { ctx := cmd.Context() database, branch, keyspace := args[0], args[1], args[2] + if cmd.Flags().Changed("max-rollout") { + if err := validateMaxRollout(flags.maxRollout); err != nil { + return err + } + createReq.MaxRollout = &flags.maxRollout + } + createReq.Organization = ch.Config.Organization createReq.Database = database createReq.Branch = branch @@ -75,6 +83,10 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { end() } + if createReq.MaxRollout != nil && (k.MaxRollout == nil || *k.MaxRollout != *createReq.MaxRollout) { + fmt.Fprintf(cmd.ErrOrStderr(), "Warning: --max-rollout was not applied; set it with 'pscale keyspace update-settings %s %s %s --org %s --max-rollout %d'.\n", database, branch, keyspace, ch.Config.Organization, *createReq.MaxRollout) + } + if ch.Printer.Format() == printer.Human { ch.Printer.Printf("Keyspace %s was successfully created.\n", printer.BoldBlue(k.Name)) return nil @@ -86,6 +98,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { cmd.Flags().IntVar(&flags.shards, "shards", 1, "Number of shards in the keyspace") cmd.Flags().StringVar(&flags.clusterSize, "cluster-size", "", "cluster size for the keyspace. Use `pscale size cluster list` to get a list of valid sizes.") cmd.Flags().IntVar(&flags.additionalReplicas, "additional-replicas", 0, "number of additional replicas per shard. By default, each production cluster includes 2 replicas.") + cmd.Flags().IntVar(&flags.maxRollout, "max-rollout", 0, "Maximum number of shards to roll out changes to concurrently (1-32). Defaults to a value based on the shard count.") cmd.Flags().BoolVar(&flags.wait, "wait", false, "Wait until the keyspace is ready") cmd.MarkFlagRequired("cluster-size") diff --git a/internal/cmd/keyspace/create_test.go b/internal/cmd/keyspace/create_test.go index 494fb029..84d0e095 100644 --- a/internal/cmd/keyspace/create_test.go +++ b/internal/cmd/keyspace/create_test.go @@ -27,12 +27,14 @@ func TestKeyspace_CreateCmd(t *testing.T) { branch := "main" keyspace := "sharded" + maxRollout := 8 ks := &ps.Keyspace{ ID: "wantid", Name: keyspace, ClusterSize: "PS-20", Replicas: 3, Shards: 2, + MaxRollout: &maxRollout, } svc := &mock.KeyspacesService{ @@ -44,6 +46,8 @@ func TestKeyspace_CreateCmd(t *testing.T) { c.Assert(req.ClusterSize, qt.Equals, "PS-20") c.Assert(req.ExtraReplicas, qt.Equals, 1) c.Assert(req.Shards, qt.Equals, 2) + c.Assert(req.MaxRollout, qt.Not(qt.IsNil)) + c.Assert(*req.MaxRollout, qt.Equals, 8) return ks, nil }, @@ -62,10 +66,13 @@ func TestKeyspace_CreateCmd(t *testing.T) { } cmd := CreateCmd(ch) - cmd.SetArgs([]string{db, branch, keyspace, "--cluster-size", "PS-20", "--shards", "2", "--additional-replicas", "1"}) + cmd.SetArgs([]string{db, branch, keyspace, "--cluster-size", "PS-20", "--shards", "2", "--additional-replicas", "1", "--max-rollout", "8"}) + var stderr bytes.Buffer + cmd.SetErr(&stderr) err := cmd.Execute() c.Assert(err, qt.IsNil) c.Assert(svc.CreateFnInvoked, qt.IsTrue) + c.Assert(stderr.String(), qt.Equals, "") c.Assert(buf.String(), qt.JSONEquals, ks) } @@ -149,6 +156,7 @@ func TestKeyspace_CreateCmdOnlyClusterSize(t *testing.T) { c.Assert(req.ClusterSize, qt.Equals, "PS-10") c.Assert(req.ExtraReplicas, qt.Equals, 0) c.Assert(req.Shards, qt.Equals, 1) + c.Assert(req.MaxRollout, qt.IsNil) return ks, nil }, @@ -173,3 +181,56 @@ func TestKeyspace_CreateCmdOnlyClusterSize(t *testing.T) { c.Assert(svc.CreateFnInvoked, qt.IsTrue) c.Assert(buf.String(), qt.JSONEquals, ks) } + +func TestKeyspace_CreateCmdRejectsOutOfRangeMaxRollout(t *testing.T) { + c := qt.New(t) + + format := printer.JSON + p := printer.NewPrinter(&format) + + for _, arg := range []string{"--max-rollout=0", "--max-rollout=33"} { + svc := &mock.KeyspacesService{} + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := CreateCmd(ch) + cmd.SetArgs([]string{"planetscale", "main", "sharded", "--cluster-size", "PS-20", arg}) + err := cmd.Execute() + c.Assert(err, qt.ErrorMatches, ".*max-rollout must be between 1 and 32") + c.Assert(svc.CreateFnInvoked, qt.IsFalse) + } +} + +func TestKeyspace_CreateCmdWarnsWhenMaxRolloutNotApplied(t *testing.T) { + c := qt.New(t) + + format := printer.JSON + p := printer.NewPrinter(&format) + p.SetResourceOutput(&bytes.Buffer{}) + + svc := &mock.KeyspacesService{ + CreateFn: func(_ context.Context, req *ps.CreateKeyspaceRequest) (*ps.Keyspace, error) { + return &ps.Keyspace{Name: req.Name}, nil + }, + } + ch := &cmdutil.Helper{ + Printer: p, + Config: &config.Config{Organization: "planetscale"}, + Client: func() (*ps.Client, error) { + return &ps.Client{Keyspaces: svc}, nil + }, + } + + cmd := CreateCmd(ch) + cmd.SetArgs([]string{"planetscale", "main", "sharded", "--cluster-size", "PS-20", "--max-rollout", "8"}) + var stderr bytes.Buffer + cmd.SetErr(&stderr) + c.Assert(cmd.Execute(), qt.IsNil) + c.Assert(stderr.String(), qt.Contains, "--max-rollout was not applied") + c.Assert(stderr.String(), qt.Contains, "update-settings planetscale main sharded --org planetscale --max-rollout 8") +} diff --git a/internal/cmd/keyspace/keyspace.go b/internal/cmd/keyspace/keyspace.go index 7ca665af..126c4d4c 100644 --- a/internal/cmd/keyspace/keyspace.go +++ b/internal/cmd/keyspace/keyspace.go @@ -2,6 +2,7 @@ package keyspace import ( "encoding/json" + "errors" "github.com/planetscale/cli/internal/cmdutil" ps "github.com/planetscale/cli/internal/planetscale" @@ -121,3 +122,10 @@ func (k *Keyspace) MarshalJSON() ([]byte, error) { func (k *Keyspace) MarshalCSVValue() interface{} { return []*Keyspace{k} } + +func validateMaxRollout(n int) error { + if n < 1 || n > 32 { + return errors.New("--max-rollout must be between 1 and 32") + } + return nil +} diff --git a/internal/cmd/keyspace/update_settings.go b/internal/cmd/keyspace/update_settings.go index 855be348..7c55ae41 100644 --- a/internal/cmd/keyspace/update_settings.go +++ b/internal/cmd/keyspace/update_settings.go @@ -81,8 +81,10 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { return errors.New("--throttler-threshold must be greater than or equal to 0") } - if maxRolloutChanged && (flags.maxRollout < 1 || flags.maxRollout > 32) { - return errors.New("--max-rollout must be between 1 and 32") + if maxRolloutChanged { + if err := validateMaxRollout(flags.maxRollout); err != nil { + return err + } } if strategyChanged && !slices.Contains(diskScalingStrategies, flags.diskScalingStrategy) { @@ -212,7 +214,7 @@ func UpdateSettingsCmd(ch *cmdutil.Helper) *cobra.Command { cmd.Flags().BoolVar(&flags.vreplicationFlags.VPlayerBatching, "vreplication-batch-replication-events", false, "When enabled, sends fewer queries to MySQL to improve performance.") cmd.Flags().BoolVar(&flags.throttlerEnabled, "throttler-enabled", true, "Pause schema migrations and VReplication workflows when replication lag rises above the threshold.") cmd.Flags().Float64Var(&flags.throttlerThreshold, "throttler-threshold", 5, "Replication lag in seconds above which migrations and workflows are paused.") - cmd.Flags().IntVar(&flags.maxRollout, "max-rollout", 1, "Maximum number of shards to roll out changes to concurrently (1-32).") + cmd.Flags().IntVar(&flags.maxRollout, "max-rollout", 0, "Maximum number of shards to roll out changes to concurrently (1-32).") cmd.Flags().StringVar(&flags.diskScalingStrategy, "disk-scaling-strategy", "grow", fmt.Sprintf("The disk scaling strategy (%s). 'grow' lets dedicated disks grow automatically up to --max-storage; 'disable' turns autoscaling off; 'shrink' recreates disks at --storage and then disables autoscaling.", strings.Join(diskScalingStrategies, ", "))) cmd.Flags().StringVar(&flags.maxStorage, "max-storage", "", "The maximum size that dedicated disks may autoscale to, in bytes or with a unit such as 200GiB or 1TiB. Must be at least 12 GiB and no smaller than the current disk size. Cannot be raised above the organization default (4 TiB, or 16 TiB for managed tenancy). Read-only while PlanetScale staff have raised it above that default.") cmd.Flags().StringVar(&flags.storage, "storage", "", "The disk size to recreate disks at, in bytes or with a unit such as 200GiB or 1TiB. Must be a multiple of 1 GiB. Only accepted when the disk scaling strategy is shrink, either passed with --disk-scaling-strategy or already set on the keyspace.") diff --git a/internal/planetscale/keyspaces.go b/internal/planetscale/keyspaces.go index d85d1877..772e12a5 100644 --- a/internal/planetscale/keyspaces.go +++ b/internal/planetscale/keyspaces.go @@ -80,6 +80,7 @@ type CreateKeyspaceRequest struct { ClusterSize string `json:"cluster_size"` ExtraReplicas int `json:"extra_replicas"` Shards int `json:"shards"` + MaxRollout *int `json:"max_rollout,omitempty"` } type ExternalDatasource struct { diff --git a/internal/planetscale/keyspaces_test.go b/internal/planetscale/keyspaces_test.go index 048b3117..ca620a7b 100644 --- a/internal/planetscale/keyspaces_test.go +++ b/internal/planetscale/keyspaces_test.go @@ -172,6 +172,9 @@ func TestKeyspaces_Create(t *testing.T) { _, err := w.Write([]byte(out)) c.Assert(err, qt.IsNil) c.Assert(r.Method, qt.Equals, http.MethodPost) + raw, err := io.ReadAll(r.Body) + c.Assert(err, qt.IsNil) + c.Assert(strings.TrimSpace(string(raw)), qt.Equals, `{"name":"qux","cluster_size":"small","extra_replicas":3,"shards":2}`) })) client, err := NewClient(WithBaseURL(ts.URL))