-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[vtctld] migrate tablet getters #7311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rohit-nayak-ps
merged 5 commits into
vitessio:master
from
tinyspeck:am_vtctld_tablet_getters
Jan 18, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
| ) | ||
|
|
||
|
|
@@ -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), | ||
|
|
@@ -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 { | ||
| 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 | ||
|
|
@@ -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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missed opportunity to call these |
||
| i := 0 | ||
|
|
||
| for k, v := range m { | ||
| pairs[i] = fmt.Sprintf("%v: %q", k, v) | ||
|
|
||
| i++ | ||
| } | ||
|
|
||
| sort.Strings(pairs) | ||
|
|
||
| return "[" + strings.Join(pairs, " ") + "]" | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!