Skip to content

Commit

Permalink
vschema: routing rules proto changes
Browse files Browse the repository at this point in the history
This is the first part of the changes to implement vitessio#4790.
This part implements all the management functionality for
routing rules.

Signed-off-by: Sugu Sougoumarane <[email protected]>
  • Loading branch information
sougou committed Apr 23, 2019
1 parent c585c8f commit 0879118
Show file tree
Hide file tree
Showing 21 changed files with 571 additions and 139 deletions.
5 changes: 4 additions & 1 deletion go/cmd/topo2topo/topo2topo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
doShards = flag.Bool("do-shards", false, "copies the shard information")
doShardReplications = flag.Bool("do-shard-replications", false, "copies the shard replication information")
doTablets = flag.Bool("do-tablets", false, "copies the tablet information")
doRoutingRules = flag.Bool("do-routing-rules", false, "copies the routing rules")
)

func main() {
Expand Down Expand Up @@ -87,7 +88,9 @@ func copyTopos(ctx context.Context, fromTS, toTS *topo.Server) {
if *doTablets {
helpers.CopyTablets(ctx, fromTS, toTS)
}

if *doRoutingRules {
helpers.CopyRoutingRules(ctx, fromTS, toTS)
}
}

func compareTopos(ctx context.Context, fromTS, toTS *topo.Server) {
Expand Down
198 changes: 151 additions & 47 deletions go/vt/proto/vschema/vschema.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions go/vt/topo/helpers/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package helpers
import (
"reflect"

"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/topo"
Expand Down Expand Up @@ -179,3 +180,19 @@ func CompareShardReplications(ctx context.Context, fromTS, toTS *topo.Server) er
}
return nil
}

// CompareRoutingRules will compare the routing rules in the destination topo.
func CompareRoutingRules(ctx context.Context, fromTS, toTS *topo.Server) error {
rrFrom, err := fromTS.GetRoutingRules(ctx)
if err != nil {
return vterrors.Wrapf(err, "GetKeyspace(from)")
}
rrTo, err := toTS.GetRoutingRules(ctx)
if err != nil {
return vterrors.Wrapf(err, "GetKeyspace(to)")
}
if !proto.Equal(rrFrom, rrTo) {
return vterrors.Errorf(vtrpc.Code_FAILED_PRECONDITION, "routing rules: %v does not match %v", rrFrom, rrTo)
}
return nil
}
7 changes: 7 additions & 0 deletions go/vt/topo/helpers/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ func TestBasicCompare(t *testing.T) {
if err != nil {
t.Fatalf("Compare tablets failed: %v", err)
}

err = CompareRoutingRules(ctx, fromTS, toTS)
if err == nil {
t.Fatalf("Compare routing rules is not failing when topos are not in sync")
}

CopyRoutingRules(ctx, fromTS, toTS)
}
11 changes: 11 additions & 0 deletions go/vt/topo/helpers/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,14 @@ func CopyShardReplications(ctx context.Context, fromTS, toTS *topo.Server) {
}
}
}

// CopyRoutingRules will create the routing rules in the destination topo.
func CopyRoutingRules(ctx context.Context, fromTS, toTS *topo.Server) {
rr, err := fromTS.GetRoutingRules(ctx)
if err != nil {
log.Fatalf("GetRoutingRules: %v", err)
}
if err := toTS.SaveRoutingRules(ctx, rr); err != nil {
log.Errorf("SaveRoutingRules(%v): %v", rr, err)
}
}
15 changes: 13 additions & 2 deletions go/vt/topo/helpers/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"vitess.io/vitess/go/vt/topo/topoproto"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
)

func createSetup(ctx context.Context, t *testing.T) (*topo.Server, *topo.Server) {
Expand Down Expand Up @@ -86,6 +87,16 @@ func createSetup(ctx context.Context, t *testing.T) (*topo.Server, *topo.Server)
t.Fatalf("cannot create slave tablet: %v", err)
}

rr := &vschemapb.RoutingRules{
Rules: []*vschemapb.RoutingRule{{
FromTable: "t1",
ToTables: []string{"t2", "t3"},
}},
}
if err := fromTS.SaveRoutingRules(ctx, rr); err != nil {
t.Fatalf("cannot save routing rules: %v", err)
}

return fromTS, toTS
}

Expand Down Expand Up @@ -116,12 +127,12 @@ func TestBasic(t *testing.T) {
CopyShards(ctx, fromTS, toTS)

// check ShardReplication copy
sr, err := fromTS.GetShardReplication(ctx, "test_cell", "test_keyspace", "0")
_, err = fromTS.GetShardReplication(ctx, "test_cell", "test_keyspace", "0")
if err != nil {
t.Fatalf("fromTS.GetShardReplication failed: %v", err)
}
CopyShardReplications(ctx, fromTS, toTS)
sr, err = toTS.GetShardReplication(ctx, "test_cell", "test_keyspace", "0")
sr, err := toTS.GetShardReplication(ctx, "test_cell", "test_keyspace", "0")
if err != nil {
t.Fatalf("toTS.GetShardReplication failed: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/topo/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var (
// Deprecated
// LockTimeout is the command line flag that introduces a shorter
// timeout for locking topology structures.
deprecatedLockTimeout = flag.Duration("lock_timeout", defaultLockTimeout, "deprecated: timeout for acquiring topology locks, use remote_operation_timeout")
_ = flag.Duration("lock_timeout", defaultLockTimeout, "deprecated: timeout for acquiring topology locks, use remote_operation_timeout")

// RemoteOperationTimeout is used for operations where we have to
// call out to another process.
Expand Down
Loading

0 comments on commit 0879118

Please sign in to comment.