Skip to content

Commit

Permalink
Add tests for various switch/reverse traffics for reads/writes and va…
Browse files Browse the repository at this point in the history
…lidate

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
  • Loading branch information
rohit-nayak-ps committed Oct 18, 2024
1 parent 979e289 commit 68545a2
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 10 deletions.
21 changes: 19 additions & 2 deletions go/test/endtoend/vreplication/resharding_workflows_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,23 @@ func validateReadsRoute(t *testing.T, tabletTypes string, tablet *cluster.Vttabl
}
}

func validateRoutingRule(t *testing.T, table, tabletType, from, to string) bool {
rr := getRoutingRules(t)
for _, r := range rr.GetRules() {
s := fmt.Sprintf("%s.%s", from, table)
if tabletType != "" && tabletType != "primary" {
s = fmt.Sprintf("%s@%s", s, tabletType)
}
if r.FromTable == s {
toTable := r.ToTables[0]
if toTable == to {
return true
}
}
}
return false
}

func validateReadsRouteToSource(t *testing.T, tabletTypes string) {
if sourceReplicaTab != nil {
validateReadsRoute(t, tabletTypes, sourceReplicaTab)
Expand Down Expand Up @@ -899,7 +916,7 @@ func setupMinimalCluster(t *testing.T) *VitessCluster {

zone1 := vc.Cells["zone1"]

vc.AddKeyspace(t, []*Cell{zone1}, "product", "0", initialProductVSchema, initialProductSchema, 0, 0, 100, nil)
vc.AddKeyspace(t, []*Cell{zone1}, "product", "0", initialProductVSchema, initialProductSchema, defaultReplicas, defaultRdonly, 100, nil)

verifyClusterHealth(t, vc)
insertInitialData(t)
Expand All @@ -912,7 +929,7 @@ func setupMinimalCluster(t *testing.T) *VitessCluster {
func setupMinimalCustomerKeyspace(t *testing.T) map[string]*cluster.VttabletProcess {
tablets := make(map[string]*cluster.VttabletProcess)
if _, err := vc.AddKeyspace(t, []*Cell{vc.Cells["zone1"]}, "customer", "-80,80-",
customerVSchema, customerSchema, 0, 0, 200, nil); err != nil {
customerVSchema, customerSchema, defaultReplicas, defaultRdonly, 200, nil); err != nil {
t.Fatal(err)
}
defaultCell := vc.Cells[vc.CellNames[0]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,19 @@ func TestVtctldclientCLI(t *testing.T) {
targetKeyspaceName := "customer"
var mt iMoveTables
workflowName := "wf1"

sourceReplicaTab = vc.Cells["zone1"].Keyspaces[sourceKeyspaceName].Shards["0"].Tablets["zone1-101"].Vttablet
require.NotNil(t, sourceReplicaTab)
sourceTab = vc.Cells["zone1"].Keyspaces[sourceKeyspaceName].Shards["0"].Tablets["zone1-100"].Vttablet
require.NotNil(t, sourceTab)

targetTabs := setupMinimalCustomerKeyspace(t)
targetTab1 = targetTabs["-80"]
require.NotNil(t, targetTab1)
targetTab2 = targetTabs["80-"]
require.NotNil(t, targetTab2)
targetReplicaTab1 = vc.Cells["zone1"].Keyspaces[targetKeyspaceName].Shards["-80"].Tablets["zone1-201"].Vttablet
require.NotNil(t, targetReplicaTab1)

t.Run("RoutingRulesApply", func(t *testing.T) {
testRoutingRulesApplyCommands(t)
Expand Down Expand Up @@ -163,7 +175,71 @@ func testMoveTablesFlags2(t *testing.T, mt *iMoveTables, sourceKeyspace, targetK
for _, tab := range targetTabs {
catchup(t, tab, workflowName, "MoveTables")
}

(*mt).SwitchReads()
validateReadsRouteToTarget(t, "replica")
validateRoutingRule(t, "customer", "replica", sourceKs, targetKs)
validateRoutingRule(t, "customer", "", targetKs, sourceKs)

(*mt).ReverseReads()
validateReadsRouteToSource(t, "replica")
validateRoutingRule(t, "customer", "replica", targetKs, sourceKs)
validateRoutingRule(t, "customer", "", targetKs, sourceKs)

(*mt).SwitchReadsAndWrites()
validateReadsRouteToTarget(t, "replica")
validateRoutingRule(t, "customer", "replica", sourceKs, targetKs)
validateWritesRouteToTarget(t)
validateRoutingRule(t, "customer", "", sourceKs, targetKs)

(*mt).ReverseReadsAndWrites()
validateReadsRouteToSource(t, "replica")
validateRoutingRule(t, "customer", "replica", targetKs, sourceKs)
validateWritesRouteToSource(t)
validateRoutingRule(t, "customer", "", targetKs, sourceKs)

(*mt).SwitchReadsAndWrites()
validateReadsRouteToTarget(t, "replica")
validateRoutingRule(t, "customer", "replica", sourceKs, targetKs)
validateWritesRouteToTarget(t)
validateRoutingRule(t, "customer", "", sourceKs, targetKs)

(*mt).ReverseReads()
validateReadsRouteToSource(t, "replica")
validateRoutingRule(t, "customer", "replica", targetKs, sourceKs)
validateWritesRouteToTarget(t)
validateRoutingRule(t, "customer", "", sourceKs, targetKs)

(*mt).ReverseWrites()
validateReadsRouteToSource(t, "replica")
validateRoutingRule(t, "customer", "replica", targetKs, sourceKs)
validateWritesRouteToSource(t)
validateRoutingRule(t, "customer", "", targetKs, sourceKs)

(*mt).SwitchReadsAndWrites()
validateReadsRouteToTarget(t, "replica")
validateRoutingRule(t, "customer", "replica", sourceKs, targetKs)
validateWritesRouteToTarget(t)
validateRoutingRule(t, "customer", "", sourceKs, targetKs)

(*mt).ReverseWrites()
validateReadsRouteToTarget(t, "replica")
validateRoutingRule(t, "customer", "replica", sourceKs, targetKs)
validateWritesRouteToSource(t)
validateRoutingRule(t, "customer", "", targetKs, sourceKs)

(*mt).ReverseReads()
validateReadsRouteToSource(t, "replica")
validateRoutingRule(t, "customer", "replica", targetKs, sourceKs)
validateWritesRouteToSource(t)
validateRoutingRule(t, "customer", "", targetKs, sourceKs)

(*mt).SwitchReadsAndWrites()
validateReadsRouteToTarget(t, "replica")
validateRoutingRule(t, "customer", "replica", sourceKs, targetKs)
validateWritesRouteToTarget(t)
validateRoutingRule(t, "customer", "", sourceKs, targetKs)

(*mt).Complete()
confirmRoutingRulesExist(t)
// Confirm that --keep-data was honored.
Expand Down
71 changes: 63 additions & 8 deletions go/test/endtoend/vreplication/wrappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type iWorkflow interface {
SwitchReads()
SwitchWrites()
SwitchReadsAndWrites()
ReverseReads()
ReverseWrites()
ReverseReadsAndWrites()
Cancel()
Complete()
Expand Down Expand Up @@ -156,13 +158,26 @@ func (vmt *VtctlMoveTables) exec(action string) {
require.NoError(vmt.vc.t, err)
}
func (vmt *VtctlMoveTables) SwitchReads() {
// TODO implement me
panic("implement me")
err := tstWorkflowExecVtctl(vmt.vc.t, "", vmt.workflowName, vmt.sourceKeyspace, vmt.targetKeyspace,
vmt.tables, workflowActionSwitchTraffic, "replica,rdonly", "", "", defaultWorkflowExecOptions)
require.NoError(vmt.vc.t, err)
}

func (vmt *VtctlMoveTables) SwitchWrites() {
// TODO implement me
panic("implement me")
err := tstWorkflowExecVtctl(vmt.vc.t, "", vmt.workflowName, vmt.sourceKeyspace, vmt.targetKeyspace,
vmt.tables, workflowActionSwitchTraffic, "primary", "", "", defaultWorkflowExecOptions)
require.NoError(vmt.vc.t, err)
}
func (vmt *VtctlMoveTables) ReverseReads() {
err := tstWorkflowExecVtctl(vmt.vc.t, "", vmt.workflowName, vmt.sourceKeyspace, vmt.targetKeyspace,
vmt.tables, workflowActionReverseTraffic, "replica,rdonly", "", "", defaultWorkflowExecOptions)
require.NoError(vmt.vc.t, err)
}

func (vmt *VtctlMoveTables) ReverseWrites() {
err := tstWorkflowExecVtctl(vmt.vc.t, "", vmt.workflowName, vmt.sourceKeyspace, vmt.targetKeyspace,
vmt.tables, workflowActionReverseTraffic, "primary", "", "", defaultWorkflowExecOptions)
require.NoError(vmt.vc.t, err)
}

func (vmt *VtctlMoveTables) Cancel() {
Expand Down Expand Up @@ -259,6 +274,18 @@ func (v VtctldMoveTables) SwitchWrites() {
v.exec(args...)
}

func (v VtctldMoveTables) ReverseReads() {
args := []string{"ReverseTraffic", "--tablet-types=rdonly,replica"}
args = append(args, v.switchFlags...)
v.exec(args...)
}

func (v VtctldMoveTables) ReverseWrites() {
args := []string{"ReverseTraffic", "--tablet-types=primary"}
args = append(args, v.switchFlags...)
v.exec(args...)
}

func (v VtctldMoveTables) Cancel() {
v.exec("Cancel")
}
Expand Down Expand Up @@ -323,6 +350,16 @@ type VtctlReshard struct {
*reshardWorkflow
}

func (vrs *VtctlReshard) ReverseReads() {
//TODO implement me
panic("implement me")
}

func (vrs *VtctlReshard) ReverseWrites() {
//TODO implement me
panic("implement me")
}

func (vrs *VtctlReshard) Flavor() string {
return "vtctl"
}
Expand Down Expand Up @@ -449,13 +486,31 @@ func (v VtctldReshard) Show() {
}

func (v VtctldReshard) SwitchReads() {
// TODO implement me
panic("implement me")
args := []string{"SwitchTraffic"}
args = append(args, v.switchFlags...)
args = append(args, "--tablet-types=rdonly,replica")
v.exec(args...)
}

func (v VtctldReshard) SwitchWrites() {
// TODO implement me
panic("implement me")
args := []string{"SwitchTraffic"}
args = append(args, v.switchFlags...)
args = append(args, "--tablet-types=primary")
v.exec(args...)
}

func (v VtctldReshard) ReverseReads() {
args := []string{"ReverseTraffic"}
args = append(args, v.switchFlags...)
args = append(args, "--tablet-types=rdonly,replica")
v.exec(args...)
}

func (v VtctldReshard) ReverseWrites() {
args := []string{"ReverseTraffic"}
args = append(args, v.switchFlags...)
args = append(args, "--tablet-types=primary")
v.exec(args...)
}

func (v VtctldReshard) Cancel() {
Expand Down

0 comments on commit 68545a2

Please sign in to comment.