Skip to content

Commit

Permalink
cr: remove ApplyMirrorRules vtctldclient command
Browse files Browse the repository at this point in the history
Signed-off-by: Max Englander <[email protected]>
  • Loading branch information
maxenglander committed Jun 24, 2024
1 parent 71b9a00 commit 446445c
Show file tree
Hide file tree
Showing 14 changed files with 697 additions and 2,080 deletions.
24 changes: 1 addition & 23 deletions changelog/21.0/21.0.0/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,7 @@ The following metrics that were deprecated in the previous release, have now bee

Traffic mirroring is intended to help reduce some of the uncertainty inherent to `MoveTables SwitchTraffic`. When traffic mirroring is enabled, VTGate will mirror a percentage of traffic from one keyspace to another.

Mirror rules may be enabled through `vtctldclient` in two ways:

* With `ApplyMirrorRules`.
* With `MoveTables MirrorTraffic`, which uses `ApplyMirrorRules` under the hood.

Example with `ApplyMirrorRules`:

```bash
$ vtctldclient --server :15999 ApplyMirrorRules --rules "$(cat <<EOF
{
"rules": [
{
"from_table": "commerce.corders",
"to_table": "customer.corders",
"percent": 5.0
}
]
}
EOF
)"
```

Example with `MoveTables MirrorTraffic`:
Mirror rules may be enabled through `vtctldclient` with `MoveTables MirrorTraffic`. For example:

```bash
$ vtctldclient --server :15999 MoveTables --target-keyspace customer --workflow commerce2customer MirrorTraffic --percent 5.0
Expand Down
113 changes: 7 additions & 106 deletions go/cmd/vtctldclient/command/mirror_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,114 +17,22 @@ limitations under the License.
package command

import (
"errors"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/vtctldclient/cli"
"vitess.io/vitess/go/json2"

vschemapb "vitess.io/vitess/go/vt/proto/vschema"
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)

var (
// ApplyMirrorRules makes an ApplyMirrorRules gRPC call to a vtctld.
ApplyMirrorRules = &cobra.Command{
Use: "ApplyMirrorRules {--rules RULES | --rules-file RULES_FILE} [--cells=c1,c2,...] [--skip-rebuild] [--dry-run]",
Short: "Applies the VSchema mirror rules.",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
RunE: commandApplyMirrorRules,
}
// GetMirrorRules makes a GetMirrorRules gRPC call to a vtctld.
GetMirrorRules = &cobra.Command{
Use: "GetMirrorRules",
Short: "Displays the VSchema mirror rules.",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
RunE: commandGetMirrorRules,
}
)

var applyMirrorRulesOptions = struct {
Rules string
RulesFilePath string
Cells []string
SkipRebuild bool
DryRun bool
}{}

func commandApplyMirrorRules(cmd *cobra.Command, args []string) error {
if applyMirrorRulesOptions.Rules != "" && applyMirrorRulesOptions.RulesFilePath != "" {
return fmt.Errorf("cannot pass both --rules (=%s) and --rules-file (=%s)", applyMirrorRulesOptions.Rules, applyMirrorRulesOptions.RulesFilePath)
}

if applyMirrorRulesOptions.Rules == "" && applyMirrorRulesOptions.RulesFilePath == "" {
return errors.New("must pass exactly one of --rules or --rules-file")
}

cli.FinishedParsing(cmd)

var rulesBytes []byte
if applyMirrorRulesOptions.RulesFilePath != "" {
data, err := os.ReadFile(applyMirrorRulesOptions.RulesFilePath)
if err != nil {
return err
}

rulesBytes = data
} else {
rulesBytes = []byte(applyMirrorRulesOptions.Rules)
}

mr := &vschemapb.MirrorRules{}
if err := json2.Unmarshal(rulesBytes, &mr); err != nil {
return err
}

// Round-trip so when we display the result it's readable.
data, err := cli.MarshalJSON(mr)
if err != nil {
return err
}

if applyMirrorRulesOptions.DryRun {
fmt.Printf("[DRY RUN] Would have saved new MirrorRules object:\n%s\n", data)

if applyMirrorRulesOptions.SkipRebuild {
fmt.Println("[DRY RUN] Would not have rebuilt VSchema graph, would have required operator to run RebuildVSchemaGraph for changes to take effect")
} else {
fmt.Print("[DRY RUN] Would have rebuilt the VSchema graph")
if len(applyMirrorRulesOptions.Cells) == 0 {
fmt.Print(" in all cells\n")
} else {
fmt.Printf(" in the following cells: %s.\n", strings.Join(applyMirrorRulesOptions.Cells, ", "))
}
}

return nil
}

_, err = client.ApplyMirrorRules(commandCtx, &vtctldatapb.ApplyMirrorRulesRequest{
MirrorRules: mr,
SkipRebuild: applyMirrorRulesOptions.SkipRebuild,
RebuildCells: applyMirrorRulesOptions.Cells,
})
if err != nil {
return err
}

fmt.Printf("New MirrorRules object:\n%s\nIf this is not what you expected, check the input data (as JSON parsing will skip unexpected fields).\n", data)

if applyMirrorRulesOptions.SkipRebuild {
fmt.Println("Skipping rebuild of VSchema graph, will need to run RebuildVSchemaGraph for changes to take effect.")
}

return nil
// GetMirrorRules makes a GetMirrorRules gRPC call to a vtctld.
var GetMirrorRules = &cobra.Command{
Use: "GetMirrorRules",
Short: "Displays the VSchema mirror rules.",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
RunE: commandGetMirrorRules,
}

func commandGetMirrorRules(cmd *cobra.Command, args []string) error {
Expand All @@ -146,12 +54,5 @@ func commandGetMirrorRules(cmd *cobra.Command, args []string) error {
}

func init() {
ApplyMirrorRules.Flags().StringVarP(&applyMirrorRulesOptions.Rules, "rules", "r", "", "Mirror rules, specified as a string.")
ApplyMirrorRules.Flags().StringVarP(&applyMirrorRulesOptions.RulesFilePath, "rules-file", "f", "", "Path to a file containing mirror rules specified as JSON.")
ApplyMirrorRules.Flags().StringSliceVarP(&applyMirrorRulesOptions.Cells, "cells", "c", nil, "Limit the VSchema graph rebuilding to the specified cells. Ignored if --skip-rebuild is specified.")
ApplyMirrorRules.Flags().BoolVar(&applyMirrorRulesOptions.SkipRebuild, "skip-rebuild", false, "Skip rebuilding the SrvVSchema objects.")
ApplyMirrorRules.Flags().BoolVarP(&applyMirrorRulesOptions.DryRun, "dry-run", "d", false, "Load the specified mirror rules as a validation step, but do not actually apply the rules to the topo.")
Root.AddCommand(ApplyMirrorRules)

Root.AddCommand(GetMirrorRules)
}
1 change: 0 additions & 1 deletion go/flags/endtoend/vtctldclient.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Available Commands:
AddCellInfo Registers a local topology service in a new cell by creating the CellInfo.
AddCellsAlias Defines a group of cells that can be referenced by a single name (the alias).
ApplyKeyspaceRoutingRules Applies the provided keyspace routing rules.
ApplyMirrorRules Applies the VSchema mirror rules.
ApplyRoutingRules Applies the VSchema routing rules.
ApplySchema Applies the schema change to the specified keyspace on every primary, running in parallel on all shards. The changes are then propagated to replicas via replication.
ApplyShardRoutingRules Applies the provided shard routing rules.
Expand Down
Loading

0 comments on commit 446445c

Please sign in to comment.