Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/cmd/keyspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
shards int
clusterSize string
additionalReplicas int
maxRollout int
wait bool
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
63 changes: 62 additions & 1 deletion internal/cmd/keyspace/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
},
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
},
Expand All @@ -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")
}
8 changes: 8 additions & 0 deletions internal/cmd/keyspace/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keyspace

import (
"encoding/json"
"errors"

"github.com/planetscale/cli/internal/cmdutil"
ps "github.com/planetscale/cli/internal/planetscale"
Expand Down Expand Up @@ -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
}
8 changes: 5 additions & 3 deletions internal/cmd/keyspace/update_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.")
Expand Down
1 change: 1 addition & 0 deletions internal/planetscale/keyspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions internal/planetscale/keyspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down