Skip to content

Commit

Permalink
Merge pull request #9173 from ajm188/trafficswitcher-more-refactors
Browse files Browse the repository at this point in the history
Move `doCellsHaveRdonlyTablets` to topotools
  • Loading branch information
deepthi authored Nov 11, 2021
2 parents 635fed1 + dc50ff2 commit c88156d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
40 changes: 38 additions & 2 deletions go/vt/topotools/tablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ package topotools
// This file contains utility functions for tablets

import (
"context"
"errors"
"fmt"

"context"

"google.golang.org/protobuf/proto"

"vitess.io/vitess/go/vt/hook"
Expand Down Expand Up @@ -103,6 +102,43 @@ func CheckOwnership(oldTablet, newTablet *topodatapb.Tablet) error {
return nil
}

// DoCellsHaveRdonlyTablets returns true if any of the cells has at least one
// tablet with type RDONLY. If the slice of cells to search over is empty, it
// checks all cells in the topo.
func DoCellsHaveRdonlyTablets(ctx context.Context, ts *topo.Server, cells []string) (bool, error) {
areAnyRdonly := func(tablets []*topo.TabletInfo) bool {
for _, tablet := range tablets {
if tablet.Type == topodatapb.TabletType_RDONLY {
return true
}
}

return false
}

if len(cells) == 0 {
tablets, err := GetAllTabletsAcrossCells(ctx, ts)
if err != nil {
return false, err
}

return areAnyRdonly(tablets), nil
}

for _, cell := range cells {
tablets, err := GetAllTablets(ctx, ts, cell)
if err != nil {
return false, err
}

if areAnyRdonly(tablets) {
return true, nil
}
}

return false, nil
}

// IsPrimaryTablet is a helper function to determine whether the current tablet
// is a primary before we allow its tablet record to be deleted. The canonical
// way to determine the only true primary in a shard is to list all the tablets
Expand Down
35 changes: 1 addition & 34 deletions go/vt/wrangler/traffic_switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,39 +262,6 @@ func (wr *Wrangler) getWorkflowState(ctx context.Context, targetKeyspace, workfl
return ts, state, nil
}

func (wr *Wrangler) doCellsHaveRdonlyTablets(ctx context.Context, cells []string) (bool, error) {
areAnyRdonly := func(tablets []*topo.TabletInfo) bool {
for _, tablet := range tablets {
if tablet.Type == topodatapb.TabletType_RDONLY {
return true
}
}
return false
}

if len(cells) == 0 {
tablets, err := topotools.GetAllTabletsAcrossCells(ctx, wr.ts)
if err != nil {
return false, err
}
if areAnyRdonly(tablets) {
return true, nil
}

} else {
for _, cell := range cells {
tablets, err := topotools.GetAllTablets(ctx, wr.ts, cell)
if err != nil {
return false, err
}
if areAnyRdonly(tablets) {
return true, nil
}
}
}
return false, nil
}

// SwitchReads is a generic way of switching read traffic for a resharding workflow.
func (wr *Wrangler) SwitchReads(ctx context.Context, targetKeyspace, workflowName string, servedTypes []topodatapb.TabletType,
cells []string, direction workflow.TrafficSwitchDirection, dryRun bool) (*[]string, error) {
Expand Down Expand Up @@ -334,7 +301,7 @@ func (wr *Wrangler) SwitchReads(ctx context.Context, targetKeyspace, workflowNam
// incorrectly report that not all reads have been switched. User currently is forced to switch non-existent rdonly tablets
if switchReplicas && !switchRdonly {
var err error
rdonlyTabletsExist, err := wr.doCellsHaveRdonlyTablets(ctx, cells)
rdonlyTabletsExist, err := topotools.DoCellsHaveRdonlyTablets(ctx, wr.ts, cells)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c88156d

Please sign in to comment.