Skip to content

Commit 0ea2138

Browse files
authored
Merge pull request #1261 from planetscale/nick/throttler-vtctld-commands
Add throttler subcommands to branch vtctld
2 parents 479943c + 6973c9a commit 0ea2138

6 files changed

Lines changed: 410 additions & 3 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/mattn/go-shellwords v1.0.12
2626
github.com/mitchellh/go-homedir v1.1.0
2727
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
28-
github.com/planetscale/planetscale-go v0.164.0
28+
github.com/planetscale/planetscale-go v0.165.0
2929
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4
3030
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7
3131
github.com/spf13/cobra v1.10.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjL
176176
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
177177
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e h1:MZ8D+Z3m2vvqGZLvoQfpaGg/j1fNDr4j03s3PRz4rVY=
178178
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e/go.mod h1:hwAsSPQdvPa3WcfKfzTXxtEq/HlqwLjQasfO6QbGo4Q=
179-
github.com/planetscale/planetscale-go v0.164.0 h1:k/Jw1robfLhcGS3f4FtqxQXEDuuddj0bnTE/jbAo4/o=
180-
github.com/planetscale/planetscale-go v0.164.0/go.mod h1:paQCI5SgquuoewvMQM7R+r1XJO868bdP6/ihGidYRM0=
179+
github.com/planetscale/planetscale-go v0.165.0 h1:huSIreetdhlXwzSweuI1rB6hlAyYEWvZULjPmkCwTdg=
180+
github.com/planetscale/planetscale-go v0.165.0/go.mod h1:paQCI5SgquuoewvMQM7R+r1XJO868bdP6/ihGidYRM0=
181181
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4 h1:Xv5pj20Rhfty1Tv0OVcidg4ez4PvGrpKvb6rvUwQgDs=
182182
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4/go.mod h1:M52h5IWxAcbdQ1hSZrLAGQC4ZXslxEsK/Wh9nu3wdWs=
183183
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7 h1:aRd6vdE1fyuSI4RVj7oCr8lFmgqXvpnPUmN85VbZCp8=
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package vtctld
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/planetscale/cli/internal/cmdutil"
7+
"github.com/planetscale/cli/internal/printer"
8+
ps "github.com/planetscale/planetscale-go/planetscale"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
// ThrottlerCmd groups the tablet throttler subcommands. The throttler controls
13+
// how aggressively Vitess applies background work (such as Online DDL and
14+
// VReplication) based on replication lag and other metrics.
15+
func ThrottlerCmd(ch *cmdutil.Helper) *cobra.Command {
16+
cmd := &cobra.Command{
17+
Use: "throttler <command>",
18+
Short: "Inspect and configure the tablet throttler",
19+
}
20+
21+
cmd.AddCommand(ThrottlerStatusCmd(ch))
22+
cmd.AddCommand(ThrottlerCheckCmd(ch))
23+
cmd.AddCommand(ThrottlerUpdateConfigCmd(ch))
24+
25+
return cmd
26+
}
27+
28+
// ThrottlerStatusCmd reads the live throttler status from a single tablet.
29+
func ThrottlerStatusCmd(ch *cmdutil.Helper) *cobra.Command {
30+
var flags struct {
31+
tabletAlias string
32+
}
33+
34+
cmd := &cobra.Command{
35+
Use: "status <database> <branch>",
36+
Short: "Get the throttler status for a single tablet",
37+
Long: "Get the throttler status for a single tablet, identified by its alias. " +
38+
"Discover tablet aliases with `pscale branch vtctld list-tablets`.",
39+
Args: cmdutil.RequiredArgs("database", "branch"),
40+
RunE: func(cmd *cobra.Command, args []string) error {
41+
ctx := cmd.Context()
42+
database, branch := args[0], args[1]
43+
44+
client, err := ch.Client()
45+
if err != nil {
46+
return err
47+
}
48+
49+
end := ch.Printer.PrintProgress(
50+
fmt.Sprintf("Fetching throttler status for tablet %s on %s…",
51+
printer.BoldBlue(flags.tabletAlias), progressTarget(ch.Config.Organization, database, branch)))
52+
defer end()
53+
54+
data, err := client.Vtctld.GetThrottlerStatus(ctx, &ps.VtctldGetThrottlerStatusRequest{
55+
Organization: ch.Config.Organization,
56+
Database: database,
57+
Branch: branch,
58+
TabletAlias: flags.tabletAlias,
59+
})
60+
if err != nil {
61+
return cmdutil.HandleError(err)
62+
}
63+
64+
end()
65+
return ch.Printer.PrettyPrintJSON(data)
66+
},
67+
}
68+
69+
cmd.Flags().StringVar(&flags.tabletAlias, "tablet-alias", "", "Alias of the tablet to probe (e.g. \"zone1-0000000100\")")
70+
cmd.MarkFlagRequired("tablet-alias") // nolint:errcheck
71+
72+
return cmd
73+
}
74+
75+
// ThrottlerCheckCmd issues a throttler check against a single tablet.
76+
func ThrottlerCheckCmd(ch *cmdutil.Helper) *cobra.Command {
77+
var flags struct {
78+
tabletAlias string
79+
appName string
80+
scope string
81+
skipRequestHeartbeats bool
82+
okIfNotExists bool
83+
}
84+
85+
cmd := &cobra.Command{
86+
Use: "check <database> <branch>",
87+
Short: "Issue a throttler check against a single tablet",
88+
Long: "Issue a throttler check against a single tablet, identified by its alias. " +
89+
"Discover tablet aliases with `pscale branch vtctld list-tablets`.",
90+
Args: cmdutil.RequiredArgs("database", "branch"),
91+
RunE: func(cmd *cobra.Command, args []string) error {
92+
ctx := cmd.Context()
93+
database, branch := args[0], args[1]
94+
95+
client, err := ch.Client()
96+
if err != nil {
97+
return err
98+
}
99+
100+
end := ch.Printer.PrintProgress(
101+
fmt.Sprintf("Checking throttler for tablet %s on %s…",
102+
printer.BoldBlue(flags.tabletAlias), progressTarget(ch.Config.Organization, database, branch)))
103+
defer end()
104+
105+
req := &ps.VtctldCheckThrottlerRequest{
106+
Organization: ch.Config.Organization,
107+
Database: database,
108+
Branch: branch,
109+
TabletAlias: flags.tabletAlias,
110+
AppName: flags.appName,
111+
Scope: flags.scope,
112+
}
113+
if cmd.Flags().Changed("skip-request-heartbeats") {
114+
req.SkipRequestHeartbeats = &flags.skipRequestHeartbeats
115+
}
116+
if cmd.Flags().Changed("ok-if-not-exists") {
117+
req.OkIfNotExists = &flags.okIfNotExists
118+
}
119+
120+
data, err := client.Vtctld.CheckThrottler(ctx, req)
121+
if err != nil {
122+
return cmdutil.HandleError(err)
123+
}
124+
125+
end()
126+
return ch.Printer.PrettyPrintJSON(data)
127+
},
128+
}
129+
130+
cmd.Flags().StringVar(&flags.tabletAlias, "tablet-alias", "", "Alias of the tablet to check (e.g. \"zone1-0000000100\")")
131+
cmd.Flags().StringVar(&flags.appName, "app-name", "", "App to issue the check on behalf of (e.g. \"online-ddl\"). Defaults to the throttler's default app.")
132+
cmd.Flags().StringVar(&flags.scope, "scope", "", "Scope of the check, either \"shard\" or \"self\". Defaults to the throttler's default scope.")
133+
cmd.Flags().BoolVar(&flags.skipRequestHeartbeats, "skip-request-heartbeats", false, "Do not renew the throttler's heartbeat lease while serving this check")
134+
cmd.Flags().BoolVar(&flags.okIfNotExists, "ok-if-not-exists", false, "Return OK even if the requested metric does not exist")
135+
cmd.MarkFlagRequired("tablet-alias") // nolint:errcheck
136+
137+
return cmd
138+
}
139+
140+
// ThrottlerUpdateConfigCmd updates the tablet throttler configuration for a
141+
// keyspace.
142+
func ThrottlerUpdateConfigCmd(ch *cmdutil.Helper) *cobra.Command {
143+
var flags struct {
144+
keyspace string
145+
enabled bool
146+
threshold float64
147+
}
148+
149+
cmd := &cobra.Command{
150+
Use: "update-config <database> <branch>",
151+
Short: "Update the throttler configuration for a keyspace",
152+
Long: "Update the tablet throttler configuration for a keyspace. The throttler is " +
153+
"enabled or disabled with --enabled; this flag is required because there is no " +
154+
"separate \"leave unchanged\" state.",
155+
Args: cmdutil.RequiredArgs("database", "branch"),
156+
RunE: func(cmd *cobra.Command, args []string) error {
157+
ctx := cmd.Context()
158+
database, branch := args[0], args[1]
159+
160+
client, err := ch.Client()
161+
if err != nil {
162+
return err
163+
}
164+
165+
end := ch.Printer.PrintProgress(
166+
fmt.Sprintf("Updating throttler config for keyspace %s on %s…",
167+
printer.BoldBlue(flags.keyspace), progressTarget(ch.Config.Organization, database, branch)))
168+
defer end()
169+
170+
req := &ps.VtctldUpdateThrottlerConfigRequest{
171+
Organization: ch.Config.Organization,
172+
Database: database,
173+
Branch: branch,
174+
Keyspace: flags.keyspace,
175+
Enabled: flags.enabled,
176+
}
177+
if cmd.Flags().Changed("threshold") {
178+
req.Threshold = &flags.threshold
179+
}
180+
181+
data, err := client.Vtctld.UpdateThrottlerConfig(ctx, req)
182+
if err != nil {
183+
return cmdutil.HandleError(err)
184+
}
185+
186+
end()
187+
return ch.Printer.PrettyPrintJSON(data)
188+
},
189+
}
190+
191+
cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace whose throttler config to update")
192+
cmd.Flags().BoolVar(&flags.enabled, "enabled", false, "Enable (true) or disable (false) the throttler for the keyspace")
193+
cmd.Flags().Float64Var(&flags.threshold, "threshold", 0, "Replication lag threshold in seconds for the default check (defaults to 5.0 server-side when omitted)")
194+
cmd.MarkFlagRequired("keyspace") // nolint:errcheck
195+
cmd.MarkFlagRequired("enabled") // nolint:errcheck
196+
197+
return cmd
198+
}

0 commit comments

Comments
 (0)