Skip to content
Merged
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
116 changes: 116 additions & 0 deletions go/cmd/vtctldclient/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ import (
"github.com/spf13/cobra"

"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)

Expand Down Expand Up @@ -64,6 +67,16 @@ var (
Args: cobra.NoArgs,
RunE: commandGetKeyspaces,
}
getTabletCmd = &cobra.Command{
Use: "GetTablet alias",
Args: cobra.ExactArgs(1),
RunE: commandGetTablet,
}
getTabletsCmd = &cobra.Command{
Use: "GetTablets [--cell $c1, ...] [--keyspace $ks [--shard $shard]]",
Args: cobra.NoArgs,
RunE: commandGetTablets,
}
initShardPrimaryCmd = &cobra.Command{
Use: "InitShardPrimary",
Args: cobra.ExactArgs(2),
Expand Down Expand Up @@ -166,6 +179,100 @@ func commandGetKeyspaces(cmd *cobra.Command, args []string) error {
return nil
}

func commandGetTablet(cmd *cobra.Command, args []string) error {
aliasStr := cmd.Flags().Arg(0)
alias, err := topoproto.ParseTabletAlias(aliasStr)
if err != nil {
return err
}

resp, err := client.GetTablet(commandCtx, &vtctldatapb.GetTabletRequest{TabletAlias: alias})
if err != nil {
return err
}

data, err := MarshalJSON(resp.Tablet)
if err != nil {
return err
}

fmt.Printf("%s\n", data)

return nil
}

var getTabletsArgs = struct {
Cells []string
Keyspace string
Shard string

Format string
}{}

func commandGetTablets(cmd *cobra.Command, args []string) error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't/don't test the CLI output in commands.go, right? Not a leading question, just curious!

format := strings.ToLower(getTabletsArgs.Format)

switch format {
case "awk", "json":
default:
return fmt.Errorf("invalid output format, got %s", getTabletsArgs.Format)
}

if getTabletsArgs.Keyspace == "" && getTabletsArgs.Shard != "" {
return fmt.Errorf("--shard (= %s) cannot be passed without also passing --keyspace", getTabletsArgs.Shard)
}

resp, err := client.GetTablets(commandCtx, &vtctldatapb.GetTabletsRequest{
Cells: getTabletsArgs.Cells,
Keyspace: getTabletsArgs.Keyspace,
Shard: getTabletsArgs.Shard,
})
if err != nil {
return err
}

switch format {
case "awk":
lineFn := func(t *topodatapb.Tablet) string {
ti := topo.TabletInfo{
Tablet: t,
}

keyspace := t.Keyspace
if keyspace == "" {
keyspace = "<null>"
}

shard := t.Shard
if shard == "" {
shard = "<null>"
}

mtst := "<null>"
// special case for old primary that hasn't been updated in the topo
// yet.
if t.MasterTermStartTime != nil && t.MasterTermStartTime.Seconds > 0 {
mtst = logutil.ProtoToTime(t.MasterTermStartTime).Format(time.RFC3339)
}

return fmt.Sprintf("%v %v %v %v %v %v %v %v", topoproto.TabletAliasString(t.Alias), keyspace, shard, topoproto.TabletTypeLString(t.Type), ti.Addr(), ti.MysqlAddr(), fmtMapAwkable(t.Tags), mtst)
}

for _, t := range resp.Tablets {
fmt.Println(lineFn(t))
}
case "json":
data, err := MarshalJSON(resp.Tablets)
if err != nil {
return err
}

fmt.Printf("%s\n", data)
}

return nil
}

var initShardPrimaryArgs = struct {
WaitReplicasTimeout time.Duration
Force bool
Expand Down Expand Up @@ -199,12 +306,21 @@ func commandInitShardPrimary(cmd *cobra.Command, args []string) error {

func init() {
rootCmd.AddCommand(findAllShardsInKeyspaceCmd)

rootCmd.AddCommand(getCellInfoNamesCmd)
rootCmd.AddCommand(getCellInfoCmd)
rootCmd.AddCommand(getCellsAliasesCmd)

rootCmd.AddCommand(getKeyspaceCmd)
rootCmd.AddCommand(getKeyspacesCmd)

rootCmd.AddCommand(getTabletCmd)
getTabletsCmd.Flags().StringSliceVarP(&getTabletsArgs.Cells, "cell", "c", nil, "TODO")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these left as TODOs on purpose?

getTabletsCmd.Flags().StringVarP(&getTabletsArgs.Keyspace, "keyspace", "k", "", "TODO")
getTabletsCmd.Flags().StringVarP(&getTabletsArgs.Shard, "shard", "s", "", "TODO")
getTabletsCmd.Flags().StringVar(&getTabletsArgs.Format, "format", "awk", "Output format to use; valid choices are (json, awk)")
rootCmd.AddCommand(getTabletsCmd)

initShardPrimaryCmd.Flags().DurationVar(&initShardPrimaryArgs.WaitReplicasTimeout, "wait-replicas-timeout", 30*time.Second, "time to wait for replicas to catch up in reparenting")
initShardPrimaryCmd.Flags().BoolVar(&initShardPrimaryArgs.Force, "force", false, "will force the reparent even if the provided tablet is not a master or the shard master")
rootCmd.AddCommand(initShardPrimaryCmd)
Expand Down
38 changes: 38 additions & 0 deletions go/cmd/vtctldclient/formats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2021 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"sort"
"strings"
)

func fmtMapAwkable(m map[string]string) string {
pairs := make([]string, len(m))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed opportunity to call these awkWords tbh

i := 0

for k, v := range m {
pairs[i] = fmt.Sprintf("%v: %q", k, v)

i++
}

sort.Strings(pairs)

return "[" + strings.Join(pairs, " ") + "]"
}
Loading