Skip to content

Commit

Permalink
movetables mirrortraffic
Browse files Browse the repository at this point in the history
Signed-off-by: Max Englander <[email protected]>
  • Loading branch information
maxenglander committed Jan 3, 2024
1 parent 5100652 commit bee9858
Show file tree
Hide file tree
Showing 18 changed files with 2,514 additions and 554 deletions.
90 changes: 90 additions & 0 deletions go/cmd/vtctldclient/command/vreplication/common/mirrortraffic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright 2023 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 common

import (
"bytes"
"fmt"

"github.com/spf13/cobra"

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

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)

func GetMirrorTrafficCommand(opts *SubCommandsOpts) *cobra.Command {
cmd := &cobra.Command{
Use: "mirrortraffic",
Short: fmt.Sprintf("Mirror traffic for a %s VReplication workflow.", opts.SubCommand),
Example: fmt.Sprintf(`vtctldclient --server localhost:15999 %s --workflow %s --target-keyspace customer mirrortraffic --percent 50.0 --tablet-types "replica,rdonly"`, opts.SubCommand, opts.Workflow),
DisableFlagsInUseLine: true,
Aliases: []string{"MirrorTraffic"},
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Flags().Lookup("tablet-types").Changed {
// We mirror traffic for all tablet types if none are provided.
MirrorTrafficOptions.TabletTypes = []topodatapb.TabletType{
topodatapb.TabletType_PRIMARY,
topodatapb.TabletType_REPLICA,
topodatapb.TabletType_RDONLY,
}
}
return nil
},
RunE: commandMirrorTraffic,
}
return cmd
}

func commandMirrorTraffic(cmd *cobra.Command, args []string) error {
format, err := GetOutputFormat(cmd)
if err != nil {
return err
}

cli.FinishedParsing(cmd)

req := &vtctldatapb.WorkflowMirrorTrafficRequest{
Keyspace: BaseOptions.TargetKeyspace,
Workflow: BaseOptions.Workflow,
TabletTypes: MirrorTrafficOptions.TabletTypes,
Percent: MirrorTrafficOptions.Percent,
}
resp, err := GetClient().WorkflowMirrorTraffic(GetCommandCtx(), req)
if err != nil {
return err
}

var output []byte
if format == "json" {
output, err = cli.MarshalJSONPretty(resp)
if err != nil {
return err
}
} else {
tout := bytes.Buffer{}
tout.WriteString(resp.Summary + "\n\n")
tout.WriteString(fmt.Sprintf("Start State: %s\n", resp.StartState))
tout.WriteString(fmt.Sprintf("Current State: %s\n", resp.CurrentState))
output = tout.Bytes()
}
fmt.Printf("%s\n", output)

return nil
}
5 changes: 5 additions & 0 deletions go/cmd/vtctldclient/command/vreplication/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ func AddCommonCreateFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&CreateOptions.StopAfterCopy, "stop-after-copy", false, "Stop the workflow after it's finished copying the existing rows and before it starts replicating changes.")
}

var MirrorTrafficOptions = struct {
Percent float32
TabletTypes []topodatapb.TabletType
}{}

var SwitchTrafficOptions = struct {
Cells []string
TabletTypes []topodatapb.TabletType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/vtctldclient/command/vreplication/common"
"vitess.io/vitess/go/vt/topo/topoproto"
)

var (
Expand Down Expand Up @@ -59,6 +60,11 @@ func registerCommands(root *cobra.Command) {
base.AddCommand(common.GetStartCommand(opts))
base.AddCommand(common.GetStopCommand(opts))

mirrorTrafficCommand := common.GetMirrorTrafficCommand(opts)
mirrorTrafficCommand.Flags().Float32Var(&common.MirrorTrafficOptions.Percent, "percent", 1.0, "Percentage of traffic to mirror.")
mirrorTrafficCommand.Flags().Var((*topoproto.TabletTypeListFlag)(&common.MirrorTrafficOptions.TabletTypes), "tablet-types", "Source tablet types to mirror traffic from (e.g. PRIMARY,REPLICA,RDONLY).")
base.AddCommand(mirrorTrafficCommand)

switchTrafficCommand := common.GetSwitchTrafficCommand(opts)
common.AddCommonSwitchTrafficFlags(switchTrafficCommand, true)
base.AddCommand(switchTrafficCommand)
Expand Down
Loading

0 comments on commit bee9858

Please sign in to comment.