vitess.io/vitess@v0.16.2/go/vt/topo/topotests/srv_vschema_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package topotests
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"google.golang.org/protobuf/proto"
    23  
    24  	"vitess.io/vitess/go/vt/topo/memorytopo"
    25  
    26  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    27  	vschemapb "vitess.io/vitess/go/vt/proto/vschema"
    28  )
    29  
    30  func TestRebuildVSchema(t *testing.T) {
    31  	ctx := context.Background()
    32  	emptySrvVSchema := &vschemapb.SrvVSchema{
    33  		RoutingRules:      &vschemapb.RoutingRules{},
    34  		ShardRoutingRules: &vschemapb.ShardRoutingRules{},
    35  	}
    36  
    37  	// Set up topology.
    38  	cells := []string{"cell1", "cell2"}
    39  	ts := memorytopo.NewServer(cells...)
    40  
    41  	// Rebuild with no keyspace / no vschema
    42  	if err := ts.RebuildSrvVSchema(ctx, cells); err != nil {
    43  		t.Errorf("RebuildVSchema failed: %v", err)
    44  	}
    45  	for _, cell := range cells {
    46  		if v, err := ts.GetSrvVSchema(ctx, cell); err != nil || !proto.Equal(v, emptySrvVSchema) {
    47  			t.Errorf("unexpected GetSrvVSchema(%v) result: %v %v", cell, v, err)
    48  		}
    49  	}
    50  
    51  	// create a keyspace, rebuild, should see an empty entry
    52  	emptyKs1SrvVSchema := &vschemapb.SrvVSchema{
    53  		RoutingRules:      &vschemapb.RoutingRules{},
    54  		ShardRoutingRules: &vschemapb.ShardRoutingRules{},
    55  		Keyspaces: map[string]*vschemapb.Keyspace{
    56  			"ks1": {},
    57  		},
    58  	}
    59  	if err := ts.CreateKeyspace(ctx, "ks1", &topodatapb.Keyspace{}); err != nil {
    60  		t.Fatalf("CreateKeyspace(ks1) failed: %v", err)
    61  	}
    62  	if err := ts.RebuildSrvVSchema(ctx, cells); err != nil {
    63  		t.Errorf("RebuildVSchema failed: %v", err)
    64  	}
    65  	for _, cell := range cells {
    66  		if v, err := ts.GetSrvVSchema(ctx, cell); err != nil || !proto.Equal(v, emptyKs1SrvVSchema) {
    67  			t.Errorf("unexpected GetSrvVSchema(%v) result: %v %v", cell, v, err)
    68  		}
    69  	}
    70  
    71  	// save a vschema for the keyspace, rebuild, should see it
    72  	keyspace1 := &vschemapb.Keyspace{
    73  		Sharded: true,
    74  	}
    75  	if err := ts.SaveVSchema(ctx, "ks1", keyspace1); err != nil {
    76  		t.Fatalf("SaveVSchema(ks1) failed: %v", err)
    77  	}
    78  	if err := ts.RebuildSrvVSchema(ctx, cells); err != nil {
    79  		t.Errorf("RebuildVSchema failed: %v", err)
    80  	}
    81  	wanted1 := &vschemapb.SrvVSchema{
    82  		RoutingRules:      &vschemapb.RoutingRules{},
    83  		ShardRoutingRules: &vschemapb.ShardRoutingRules{},
    84  		Keyspaces: map[string]*vschemapb.Keyspace{
    85  			"ks1": keyspace1,
    86  		},
    87  	}
    88  	for _, cell := range cells {
    89  		if v, err := ts.GetSrvVSchema(ctx, cell); err != nil || !proto.Equal(v, wanted1) {
    90  			t.Errorf("unexpected GetSrvVSchema(%v) result: %v %v", cell, v, err)
    91  		}
    92  	}
    93  
    94  	// save a vschema for a new keyspace, rebuild in one cell only
    95  	if err := ts.CreateKeyspace(ctx, "ks2", &topodatapb.Keyspace{}); err != nil {
    96  		t.Fatalf("CreateKeyspace(ks2) failed: %v", err)
    97  	}
    98  	keyspace2 := &vschemapb.Keyspace{
    99  		Sharded: true,
   100  		Vindexes: map[string]*vschemapb.Vindex{
   101  			"name1": {
   102  				Type: "hash",
   103  			},
   104  		},
   105  		Tables: map[string]*vschemapb.Table{
   106  			"table1": {
   107  				ColumnVindexes: []*vschemapb.ColumnVindex{
   108  					{
   109  						Column: "column1",
   110  						Name:   "name1",
   111  					},
   112  				},
   113  			},
   114  		},
   115  	}
   116  	if err := ts.SaveVSchema(ctx, "ks2", keyspace2); err != nil {
   117  		t.Fatalf("SaveVSchema(ks1) failed: %v", err)
   118  	}
   119  	if err := ts.RebuildSrvVSchema(ctx, []string{"cell1"}); err != nil {
   120  		t.Errorf("RebuildVSchema failed: %v", err)
   121  	}
   122  	wanted2 := &vschemapb.SrvVSchema{
   123  		RoutingRules:      &vschemapb.RoutingRules{},
   124  		ShardRoutingRules: &vschemapb.ShardRoutingRules{},
   125  		Keyspaces: map[string]*vschemapb.Keyspace{
   126  			"ks1": keyspace1,
   127  			"ks2": keyspace2,
   128  		},
   129  	}
   130  	if v, err := ts.GetSrvVSchema(ctx, "cell1"); err != nil || !proto.Equal(v, wanted2) {
   131  		t.Errorf("unexpected GetSrvVSchema result: %v %v", v, err)
   132  	}
   133  	if v, err := ts.GetSrvVSchema(ctx, "cell2"); err != nil || !proto.Equal(v, wanted1) {
   134  		t.Errorf("unexpected GetSrvVSchema result: %v %v", v, err)
   135  	}
   136  
   137  	// now rebuild everywhere
   138  	if err := ts.RebuildSrvVSchema(ctx, nil); err != nil {
   139  		t.Errorf("RebuildVSchema failed: %v", err)
   140  	}
   141  	for _, cell := range cells {
   142  		if v, err := ts.GetSrvVSchema(ctx, cell); err != nil || !proto.Equal(v, wanted2) {
   143  			t.Errorf("unexpected GetSrvVSchema(%v) result: %v %v", cell, v, err)
   144  		}
   145  	}
   146  
   147  	rr := &vschemapb.RoutingRules{
   148  		Rules: []*vschemapb.RoutingRule{{
   149  			FromTable: "t1",
   150  			ToTables:  []string{"t2", "t3"},
   151  		}},
   152  	}
   153  
   154  	if err := ts.SaveRoutingRules(ctx, rr); err != nil {
   155  		t.Fatalf("SaveRoutingRules() failed: %v", err)
   156  	}
   157  	if err := ts.RebuildSrvVSchema(ctx, nil); err != nil {
   158  		t.Errorf("RebuildVSchema failed: %v", err)
   159  	}
   160  	wanted3 := &vschemapb.SrvVSchema{
   161  		RoutingRules:      rr,
   162  		ShardRoutingRules: &vschemapb.ShardRoutingRules{},
   163  		Keyspaces: map[string]*vschemapb.Keyspace{
   164  			"ks1": keyspace1,
   165  			"ks2": keyspace2,
   166  		},
   167  	}
   168  	for _, cell := range cells {
   169  		if v, err := ts.GetSrvVSchema(ctx, cell); err != nil || !proto.Equal(v, wanted3) {
   170  			t.Errorf("unexpected GetSrvVSchema(%v) result: %v %v", cell, v, err)
   171  		}
   172  	}
   173  
   174  	wanted4 := wanted1
   175  	wanted4.RoutingRules = rr
   176  
   177  	// Delete a keyspace, checks vschema entry in map goes away.
   178  	if err := ts.SaveVSchema(ctx, "ks2", &vschemapb.Keyspace{}); err != nil {
   179  		t.Fatalf("SaveVSchema(ks1) failed: %v", err)
   180  	}
   181  	if err := ts.DeleteKeyspace(ctx, "ks2"); err != nil {
   182  		t.Fatalf("DeleteKeyspace failed: %v", err)
   183  	}
   184  	if err := ts.RebuildSrvVSchema(ctx, nil); err != nil {
   185  		t.Errorf("RebuildVSchema failed: %v", err)
   186  	}
   187  	for _, cell := range cells {
   188  		if v, err := ts.GetSrvVSchema(ctx, cell); err != nil || !proto.Equal(v, wanted4) {
   189  			t.Errorf("unexpected GetSrvVSchema(%v) result: %v != %v (err = %v)", cell, v, wanted4, err)
   190  		}
   191  	}
   192  }