|
| 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