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 5 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
20 changes: 20 additions & 0 deletions changelog/21.0/21.0.0/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- **[New VEXPLAIN Modes: TRACE and KEYS](#new-vexplain-modes)**
- **[Errant GTID Detection on VTTablets](#errant-gtid-vttablet)**
- **[Automatically Replace MySQL auto_increment Clauses with Vitess Sequences](#auto-replace-mysql-autoinc-with-seq)**
- **[vtctldclient ChangeTabletTags](#vtctldclient-changetablettags)**

## <a id="major-changes"/>Major Changes</a>

Expand Down Expand Up @@ -218,3 +219,22 @@ work automatically during the [`MoveTables`](https://vitess.io/docs/reference/vr
[`--remove-sharded-auto-increment` boolean flag](https://vitess.io/docs/20.0/reference/programs/vtctldclient/vtctldclient_movetables/vtctldclient_movetables_create/) and you should begin using the new
[`--sharded-auto-increment-handling` flag](https://vitess.io/docs/21.0/reference/programs/vtctldclient/vtctldclient_movetables/vtctldclient_movetables_create/) instead. Please see the new
[`MoveTables` Auto Increment Handling](https://vitess.io/docs/21.0/reference/vreplication/movetables/#auto-increment-handling) documentation for additional details.

Copy link
Contributor

Choose a reason for hiding this comment

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

We generally keep the summary short and expect users to look at the vitess.io docs for the details.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rohit-nayak-ps makes sense, updated!

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

The `vtctldclient` command `ChangeTabletTags` was added to allow the tags of a tablet to be changed dynamically.

This command allows one or many tablet tags to be defined using key=value format. The provided tags are merged with existing tags by default. The optional flag `--replace` causes the existing tags to be replaced with the provided tags. To support this, the VTCtld RPC `ChangeTabletTags` and the VTTablet RPC `ChangeTags` were added.

Previous to this release the only way to define tablet tags was the `--init_tags` flag of `vttablet`, which requires a restart for a change to take effect.

Example:
```bash
$ vtctldclient --server :15999 ChangeTabletTags --replace zone1-100 hello=world
- []
+ [hello: "world"]
$ vtctldclient --server :15999 GetTablet zone1-100 | jq .tags
{
"hello": "world"
}
```
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