Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[topo/helpers] Make CopyShardReplications idempotent #9849

Merged
merged 2 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions go/vt/topo/helpers/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
Expand Down Expand Up @@ -168,8 +169,27 @@ func CopyShardReplications(ctx context.Context, fromTS, toTS *topo.Server) {
continue
}

sriNodes := map[string]struct{}{}
for _, node := range sri.Nodes {
sriNodes[topoproto.TabletAliasString(node.TabletAlias)] = struct{}{}
}

if err := toTS.UpdateShardReplicationFields(ctx, cell, keyspace, shard, func(oldSR *topodatapb.ShardReplication) error {
var nodes []*topodatapb.ShardReplication_Node
for _, oldNode := range oldSR.Nodes {
if _, ok := sriNodes[topoproto.TabletAliasString(oldNode.TabletAlias)]; ok {
continue
}

nodes = append(nodes, oldNode)
}

nodes = append(nodes, sri.ShardReplication.Nodes...)
// Even though ShardReplication currently only has the .Nodes field,
// keeping the proto.Merge call here prevents this copy from
// unintentionally breaking if we add new fields.
proto.Merge(oldSR, sri.ShardReplication)
oldSR.Nodes = nodes
return nil
}); err != nil {
log.Warningf("UpdateShardReplicationFields(%v, %v, %v): %v", cell, keyspace, shard, err)
Expand Down
10 changes: 10 additions & 0 deletions go/vt/topo/helpers/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ func TestBasic(t *testing.T) {
t.Fatalf("unexpected ShardReplication: %v", sr)
}

// check ShardReplications is idempotent
CopyShardReplications(ctx, fromTS, toTS)
sr, err = toTS.GetShardReplication(ctx, "test_cell", "test_keyspace", "0")
if err != nil {
t.Fatalf("toTS.GetShardReplication failed: %v", err)
}
if len(sr.Nodes) != 2 {
t.Fatalf("unexpected ShardReplication after second copy: %v", sr)
}

// check tablet copy
CopyTablets(ctx, fromTS, toTS)
tablets, err := toTS.GetTabletAliasesByCell(ctx, "test_cell")
Expand Down