Skip to content

Commit

Permalink
[release-19.0] Fix panic in schema tracker in presence of keyspace ro…
Browse files Browse the repository at this point in the history
…uting rules (#16383) (#16406)

Signed-off-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
GuptaManan100 authored Jul 17, 2024
1 parent fc48181 commit 711730d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
8 changes: 4 additions & 4 deletions go/vt/vtgate/vschema_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ func (vm *VSchemaManager) updateFromSchema(vschema *vindexes.VSchema) {
// Now that we have ensured that all the tables are created, we can start populating the foreign keys
// in the tables.
for tblName, tblInfo := range m {
rTbl, err := vschema.FindRoutedTable(ksName, tblName, topodatapb.TabletType_PRIMARY)
if err != nil {
log.Errorf("error finding routed table %s: %v", tblName, err)
rTbl := ks.Tables[tblName]
if rTbl == nil {
log.Errorf("unable to find table %s in %s", tblName, ksName)
continue
}
for _, fkDef := range tblInfo.ForeignKeys {
Expand All @@ -223,7 +223,7 @@ func (vm *VSchemaManager) updateFromSchema(vschema *vindexes.VSchema) {
continue
}
parentTbl, err := vschema.FindRoutedTable(ksName, fkDef.ReferenceDefinition.ReferencedTable.Name.String(), topodatapb.TabletType_PRIMARY)
if err != nil {
if err != nil || parentTbl == nil {
log.Errorf("error finding parent table %s: %v", fkDef.ReferenceDefinition.ReferencedTable.Name.String(), err)
continue
}
Expand Down
43 changes: 43 additions & 0 deletions go/vt/vtgate/vschema_manager_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vtgate

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -336,6 +337,48 @@ func TestVSchemaUpdate(t *testing.T) {
}
}

// TestRoutingRules tests that the vschema manager uses the correct tables despite the routing rules.
func TestRoutingRules(t *testing.T) {
cols1 := []vindexes.Column{{
Name: sqlparser.NewIdentifierCI("id"),
Type: querypb.Type_INT64,
}}
// Create a vschema manager with a fake vschema that returns a table with a column and a primary key.
vm := &VSchemaManager{}
vm.schema = &fakeSchema{t: map[string]*vindexes.TableInfo{
"t1": {
Columns: cols1,
Indexes: []*sqlparser.IndexDefinition{
{
Info: &sqlparser.IndexInfo{Type: sqlparser.IndexTypePrimary},
Columns: []*sqlparser.IndexColumn{
{
Column: sqlparser.NewIdentifierCI("id"),
},
},
},
},
},
}}
// Define a vschema that has a keyspace routing rule.
vs := &vindexes.VSchema{
Keyspaces: map[string]*vindexes.KeyspaceSchema{
"ks": {
Tables: map[string]*vindexes.Table{},
Keyspace: &vindexes.Keyspace{Name: "ks", Sharded: true},
},
},
RoutingRules: map[string]*vindexes.RoutingRule{
"ks.t1": {
Error: fmt.Errorf("error in routing rules"),
},
},
}
// Ensure that updating the vschema manager from the vschema doesn't cause a panic.
vm.updateFromSchema(vs)
require.Len(t, vs.Keyspaces["ks"].Tables["t1"].PrimaryKey, 1)
}

func TestRebuildVSchema(t *testing.T) {
cols1 := []vindexes.Column{{
Name: sqlparser.NewIdentifierCI("id"),
Expand Down

0 comments on commit 711730d

Please sign in to comment.