-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for vtgate traffic mirroring (#15945)
Signed-off-by: Max Englander <[email protected]> Signed-off-by: Max Englander <[email protected]> Co-authored-by: Deepthi Sigireddi <[email protected]>
- Loading branch information
1 parent
0f4b544
commit 2c468b2
Showing
45 changed files
with
6,106 additions
and
751 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
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 command | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"vitess.io/vitess/go/cmd/vtctldclient/cli" | ||
|
||
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata" | ||
) | ||
|
||
// 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 { | ||
cli.FinishedParsing(cmd) | ||
|
||
resp, err := client.GetMirrorRules(commandCtx, &vtctldatapb.GetMirrorRulesRequest{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := cli.MarshalJSON(resp.MirrorRules) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("%s\n", data) | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
Root.AddCommand(GetMirrorRules) | ||
} |
89 changes: 89 additions & 0 deletions
89
go/cmd/vtctldclient/command/vreplication/common/mirrortraffic.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
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 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 MoveTables workflow.", opts.SubCommand), | ||
Example: fmt.Sprintf(`vtctldclient --server localhost:15999 %s --workflow %s --target-keyspace customer mirrortraffic --percent 5.0`, opts.SubCommand, opts.Workflow), | ||
DisableFlagsInUseLine: true, | ||
Aliases: []string{"MirrorTraffic"}, | ||
Args: cobra.NoArgs, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
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, | ||
} | ||
} | ||
}, | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
go/test/endtoend/vreplication/movetables_mirrortraffic_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
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 vreplication | ||
|
||
import ( | ||
"testing" | ||
|
||
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" | ||
topodatapb "vitess.io/vitess/go/vt/proto/topodata" | ||
) | ||
|
||
func testMoveTablesMirrorTraffic(t *testing.T, flavor workflowFlavor) { | ||
setSidecarDBName("_vt") | ||
vc = setupMinimalCluster(t) | ||
defer vc.TearDown() | ||
|
||
sourceKeyspace := "product" | ||
targetKeyspace := "customer" | ||
workflowName := "wf1" | ||
tables := []string{"customer", "loadtest", "customer2"} | ||
|
||
_ = setupMinimalCustomerKeyspace(t) | ||
|
||
mtwf := &moveTablesWorkflow{ | ||
workflowInfo: &workflowInfo{ | ||
vc: vc, | ||
workflowName: workflowName, | ||
targetKeyspace: targetKeyspace, | ||
}, | ||
sourceKeyspace: sourceKeyspace, | ||
tables: "customer,loadtest,customer2", | ||
mirrorFlags: []string{"--percent", "25"}, | ||
} | ||
mt := newMoveTables(vc, mtwf, flavor) | ||
|
||
// Mirror rules do not exist by default. | ||
mt.Create() | ||
confirmNoMirrorRules(t) | ||
|
||
waitForWorkflowState(t, vc, ksWorkflow, binlogdatapb.VReplicationWorkflowState_Running.String()) | ||
|
||
// Mirror rules can be created after a MoveTables workflow is created. | ||
mt.MirrorTraffic() | ||
confirmMirrorRulesExist(t) | ||
expectMirrorRules(t, sourceKeyspace, targetKeyspace, tables, []topodatapb.TabletType{ | ||
topodatapb.TabletType_PRIMARY, | ||
topodatapb.TabletType_REPLICA, | ||
topodatapb.TabletType_RDONLY, | ||
}, 25) | ||
|
||
// Mirror rules can be adjusted after mirror rules are in place. | ||
mtwf.mirrorFlags[1] = "50" | ||
mt.MirrorTraffic() | ||
confirmMirrorRulesExist(t) | ||
expectMirrorRules(t, sourceKeyspace, targetKeyspace, tables, []topodatapb.TabletType{ | ||
topodatapb.TabletType_PRIMARY, | ||
topodatapb.TabletType_REPLICA, | ||
topodatapb.TabletType_RDONLY, | ||
}, 50) | ||
|
||
// Mirror rules can be adjusted multiple times after mirror rules are in | ||
// place. | ||
mtwf.mirrorFlags[1] = "75" | ||
mt.MirrorTraffic() | ||
confirmMirrorRulesExist(t) | ||
expectMirrorRules(t, sourceKeyspace, targetKeyspace, tables, []topodatapb.TabletType{ | ||
topodatapb.TabletType_PRIMARY, | ||
topodatapb.TabletType_REPLICA, | ||
topodatapb.TabletType_RDONLY, | ||
}, 75) | ||
|
||
lg := newLoadGenerator(t, vc) | ||
go func() { | ||
lg.start() | ||
}() | ||
lg.waitForCount(1000) | ||
|
||
mt.SwitchReads() | ||
confirmMirrorRulesExist(t) | ||
|
||
// Mirror rules can be adjusted for writes after reads have been switched. | ||
mtwf.mirrorFlags[1] = "100" | ||
mtwf.mirrorFlags = append(mtwf.mirrorFlags, "--tablet-types", "primary") | ||
mt.MirrorTraffic() | ||
confirmMirrorRulesExist(t) | ||
expectMirrorRules(t, sourceKeyspace, targetKeyspace, tables, []topodatapb.TabletType{ | ||
topodatapb.TabletType_PRIMARY, | ||
}, 100) | ||
|
||
// Mirror rules are removed after writes are switched. | ||
mt.SwitchWrites() | ||
confirmNoMirrorRules(t) | ||
} | ||
|
||
func TestMoveTablesMirrorTraffic(t *testing.T) { | ||
currentWorkflowType = binlogdatapb.VReplicationWorkflowType_MoveTables | ||
t.Run(workflowFlavorNames[workflowFlavorVtctld], func(t *testing.T) { | ||
testMoveTablesMirrorTraffic(t, workflowFlavorVtctld) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.