Skip to content
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

Add ChangeTabletTags RPC to vtctl, ChangeTags RPC to vttablet #16857

Merged
merged 7 commits into from
Oct 8, 2024
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
7 changes: 6 additions & 1 deletion changelog/21.0/21.0.0/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
- **[Errant GTID Detection on VTTablets](#errant-gtid-vttablet)**
- **[Automatically Replace MySQL auto_increment Clauses with Vitess Sequences](#auto-replace-mysql-autoinc-with-seq)**
- **[Experimental MySQL 8.4 support](#experimental-mysql-84)**
- **[Curreny Errant GTIDs Count Metric](#errant-gtid-metric)**
- **[Current Errant GTIDs Count Metric](#errant-gtid-metric)**
- **[vtctldclient ChangeTabletTags](#vtctldclient-changetablettags)**


## <a id="major-changes"/>Major Changes</a>
Expand Down Expand Up @@ -229,3 +230,7 @@ We have added experimental support for MySQL 8.4. It passes the Vitess test suit
### <a id="errant-gtid-metric"/>Current Errant GTIDs Count Metric
A new metric called `CurrentErrantGTIDCount` has been added to the `VTOrc` component.
This metric shows the current count of the errant GTIDs in the tablets.

### <a id="vtctldclient-changetablettags"/>`vtctldclient ChangeTabletTags` command

The `vtctldclient` command `ChangeTabletTags` was added to allow the tags of a tablet to be changed dynamically.
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/cli/tablets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2024 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 cli

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTabletTagsFromPosArgs(t *testing.T) {
t.Parallel()

{
tags, err := TabletTagsFromPosArgs([]string{"fail"})
assert.Error(t, err)
assert.Nil(t, tags)
}
{
tags, err := TabletTagsFromPosArgs([]string{"hello=world"})
assert.NoError(t, err)
assert.Equal(t, map[string]string{
"hello": "world",
}, tags)
}
{
tags, err := TabletTagsFromPosArgs([]string{"hello=world", "test=123"})
assert.NoError(t, err)
assert.Equal(t, map[string]string{
"hello": "world",
"test": "123",
}, tags)
}
}
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", cli.MarshalMapAWK(resp.BeforeTags))
fmt.Printf("+ %v\n", cli.MarshalMapAWK(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
Loading