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

Fix panic in schema tracker in presence of keyspace routing rules #16383

Merged
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
8 changes: 4 additions & 4 deletions go/vt/vtgate/vschema_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ func (vm *VSchemaManager) updateTableInfo(vschema *vindexes.VSchema, ks *vindexe
// 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 @@ -240,7 +240,7 @@ func (vm *VSchemaManager) updateTableInfo(vschema *vindexes.VSchema, ks *vindexe
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
Expand Up @@ -336,6 +336,49 @@ func TestVSchemaUpdate(t *testing.T) {
}
}

// TestKeyspaceRoutingRules tests that the vschema manager doens't panic in the presence of keyspace routing rules.
func TestKeyspaceRoutingRules(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},
},
"ks2": {
Tables: map[string]*vindexes.Table{},
Keyspace: &vindexes.Keyspace{Name: "ks2", Sharded: true},
},
},
KeyspaceRoutingRules: map[string]string{
"ks": "ks2",
},
}
// Ensure that updating the vschema manager from the vschema doesn't cause a panic.
vm.updateFromSchema(vs)
}

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