Skip to content

Commit

Permalink
Add ChangeTabletTags/ChangeTags RPCs
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Sep 27, 2024
1 parent 670192d commit 03a26d1
Show file tree
Hide file tree
Showing 30 changed files with 8,713 additions and 5,171 deletions.
23 changes: 22 additions & 1 deletion go/cmd/vtctldclient/cli/tablets.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package cli

import (
"vitess.io/vitess/go/vt/topo/topoproto"
"fmt"
"strings"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/topo/topoproto"
)

// TabletAliasesFromPosArgs takes a list of positional (non-flag) arguments and
Expand All @@ -38,3 +40,22 @@ func TabletAliasesFromPosArgs(args []string) ([]*topodatapb.TabletAlias, error)

return aliases, nil
}

// TabletTagsFromPosArgs takes a list of positional (non-flag) arguements and
// converts them to a map of tablet tags.
func TabletTagsFromPosArgs(args []string) (map[string]string, error) {
if len(args) == 0 {
return nil, fmt.Errorf("no tablet tags specified")
}

tags := make(map[string]string, len(args))
for _, kvPair := range args {
if !strings.Contains(kvPair, "=") {
return nil, fmt.Errorf("invalid tablet tag %q specified. tablet tags must be specified in key=value format", kvPair)
}
fields := strings.SplitN(kvPair, ":", 2)
tags[fields[0]] = fields[1]
}

return tags, nil
}
48 changes: 48 additions & 0 deletions go/cmd/vtctldclient/command/tablets.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ import (
)

var (
// ChangeTabletTags makes a ChangeTabletTags gRPC call to a vtctld.
ChangeTabletTags = &cobra.Command{
Use: "ChangeTabletTags <alias> <tablet-tag> [ <tablet-tag> ... ]",
Short: "Changes the tablet tags for the specified tablet, if possible.",
Long: `Changes the tablet tags for the specified tablet, if possible.
Tags must be specified as key=value pairs.`,
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(2),
RunE: commandChangeTabletTags,
}
// ChangeTabletType makes a ChangeTabletType gRPC call to a vtctld.
ChangeTabletType = &cobra.Command{
Use: "ChangeTabletType [--dry-run] <alias> <tablet-type>",
Expand Down Expand Up @@ -212,6 +223,40 @@ Note that, in the SleepTablet implementation, the value should be positively-sig
}
)

var changeTabletTagsOptions = struct {
Replace bool
}{}

func commandChangeTabletTags(cmd *cobra.Command, args []string) error {
allArgs := cmd.Flags().Args()

alias, err := topoproto.ParseTabletAlias(allArgs[0])
if err != nil {
return err
}

tags, err := cli.TabletTagsFromPosArgs(allArgs[1:])
if err != nil {
return err
}

cli.FinishedParsing(cmd)

resp, err := client.ChangeTabletTags(commandCtx, &vtctldatapb.ChangeTabletTagsRequest{
TabletAlias: alias,
Tags: tags,
Replace: changeTabletTagsOptions.Replace,
})
if err != nil {
return err
}

fmt.Printf("- %v\n", resp.BeforeTags)
fmt.Printf("+ %v\n", resp.AfterTags)

return nil
}

var changeTabletTypeOptions = struct {
DryRun bool
}{}
Expand Down Expand Up @@ -629,6 +674,9 @@ func commandStopReplication(cmd *cobra.Command, args []string) error {
}

func init() {
ChangeTabletTags.Flags().BoolVarP(&changeTabletTagsOptions.Replace, "replace", "r", false, "Replace all tablet tags with the tags provided. By default tags are merged/updated.")
Root.AddCommand(ChangeTabletTags)

ChangeTabletType.Flags().BoolVarP(&changeTabletTypeOptions.DryRun, "dry-run", "d", false, "Shows the proposed change without actually executing it.")
Root.AddCommand(ChangeTabletType)

Expand Down
1 change: 1 addition & 0 deletions go/flags/endtoend/vtctldclient.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Available Commands:
ApplyVSchema Applies the VTGate routing schema to the provided keyspace. Shows the result after application.
Backup Uses the BackupStorage service on the given tablet to create and store a new backup.
BackupShard Finds the most up-to-date REPLICA, RDONLY, or SPARE tablet in the given shard and uses the BackupStorage service on that tablet to create and store a new backup.
ChangeTabletTags Changes the tablet tags for the specified tablet, if possible.
ChangeTabletType Changes the db type for the specified tablet, if possible.
CheckThrottler Issue a throttler check on the given tablet.
CreateKeyspace Creates the specified keyspace in the topology.
Expand Down
Loading

0 comments on commit 03a26d1

Please sign in to comment.