Skip to content

Commit

Permalink
Add ChangeTabletTags and 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 Oct 3, 2024
1 parent 978c59d commit 4d8f9ba
Show file tree
Hide file tree
Showing 35 changed files with 9,081 additions and 5,186 deletions.
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 @@ -18,6 +18,7 @@
- **[Dynamic VReplication Configuration](#dynamic-vreplication-configuration)**
- **[Reference Table Materialization](#reference-table-materialization)**
- **[New VEXPLAIN Modes: TRACE and KEYS](#new-vexplain-modes)**
- **[vtctldclient ChangeTabletTags](#vtctldclient-changetablettags)**

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

Expand Down Expand Up @@ -197,3 +198,22 @@ The KEYS mode for VEXPLAIN offers a concise summary of query structure, highligh
KEYS mode analyzes the query structure without executing it, providing JSON output that includes grouping columns, join columns, filter columns (potential candidates for indexes, primary keys, or sharding keys), and the statement type.

These new VEXPLAIN modes enhance Vitess's query analysis capabilities, allowing for more informed decisions about sharding strategies and query optimization.

### <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 $FLAGS ChangeTabletTags --replace zone1-100 hello=world
- []
+ [hello: "world"]
$ vtctldclient $FLAGS 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

0 comments on commit 4d8f9ba

Please sign in to comment.