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

[release-18.0] Direct PR. Fix regression where reference tables with a different name on sharded keyspaces were not routed correctly. #15788

Merged
Merged
2 changes: 1 addition & 1 deletion go/test/endtoend/vreplication/helper_test.go
Expand Up @@ -463,7 +463,7 @@ func validateDryRunResults(t *testing.T, output string, want []string) {
w = strings.TrimSpace(w[1:])
result := strings.HasPrefix(g, w)
match = result
//t.Logf("Partial match |%v|%v|%v\n", w, g, match)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should be removed or uncommented.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove in the next vrep. PR.

// t.Logf("Partial match |%v|%v|%v\n", w, g, match)
rohit-nayak-ps marked this conversation as resolved.
Show resolved Hide resolved
} else {
match = g == w
}
Expand Down
152 changes: 152 additions & 0 deletions go/test/endtoend/vreplication/reference_test.go
@@ -0,0 +1,152 @@
package vreplication

import (
"testing"

"github.com/stretchr/testify/require"
)

const (
uksSchema = `
create table product (id int, mfg_id int, cat_id int, name varchar(128), primary key(id));
create table cat (id int, name varchar(128), primary key(id));
create table mfg (id int, name varchar(128), primary key(id));
`
sksSchema = `
create table product (id int, mfg_id int, cat_id int, name varchar(128), primary key(id));
create table cat (id int, name varchar(128), primary key(id));
create table mfg2 (id int, name varchar(128), primary key(id));
`
uksVSchema = `
{
"sharded": false,
"tables": {
"product": {},
"cat": {},
"mfg": {}
}
}`

sksVSchema = `
{
"sharded": true,
"tables": {
"product": {
"column_vindexes": [
{
"column": "id",
"name": "hash"
}
]
},
"cat": {
"type": "reference",
"source": "uks.cat"
},
"mfg2": {
"type": "reference",
"source": "uks.mfg"
}
},
"vindexes": {
"hash": {
"type": "hash"
}
}
}`
materializeCatSpec = `
{
"workflow": "wfCat",
"source_keyspace": "uks",
"target_keyspace": "sks",
"table_settings": [ {"target_table": "cat", "source_expression": "select id, name from cat" }]
}`
materializeMfgSpec = `
{
"workflow": "wfMfg",
"source_keyspace": "uks",
"target_keyspace": "sks",
"table_settings": [ {"target_table": "mfg2", "source_expression": "select id, name from mfg" }]
}`
initializeTables = `
use uks;
insert into product values (1, 1, 1, 'p1');
insert into product values (2, 2, 2, 'p2');
insert into product values (3, 3, 3, 'p3');
insert into cat values (1, 'c1');
insert into cat values (2, 'c2');
insert into cat values (3, 'c3');
insert into mfg values (1, 'm1');
insert into mfg values (2, 'm2');
insert into mfg values (3, 'm3');
insert into mfg values (4, 'm4');
`
)

func TestReferenceTableMaterializationAndRouting(t *testing.T) {
var err error
defaultCellName := "zone1"
allCells := []string{defaultCellName}
allCellNames = defaultCellName
vc = NewVitessCluster(t, "TestReferenceTableMaterializationAndRouting", allCells, mainClusterConfig)
defer vc.TearDown(t)
defaultReplicas = 0 // because of CI resource constraints we can only run this test with primary tablets
defer func() { defaultReplicas = 1 }()
uks := "uks"
sks := "sks"

defaultCell := vc.Cells[defaultCellName]
vc.AddKeyspace(t, []*Cell{defaultCell}, uks, "0", uksVSchema, uksSchema, defaultReplicas, defaultRdonly, 100, nil)
vc.AddKeyspace(t, []*Cell{defaultCell}, sks, "-80,80-", sksVSchema, sksSchema, defaultReplicas, defaultRdonly, 200, nil)
vtgateConn := getConnection(t, vc.ClusterConfig.hostname, vc.ClusterConfig.vtgateMySQLPort)

verifyClusterHealth(t, vc)
_, _, err = vtgateConn.ExecuteFetchMulti(initializeTables, 0, false)
require.NoError(t, err)
vtgateConn.Close()

materialize(t, materializeCatSpec, false)
materialize(t, materializeMfgSpec, false)

tabDash80 := vc.getPrimaryTablet(t, sks, "-80")
tab80Dash := vc.getPrimaryTablet(t, sks, "80-")
catchup(t, tabDash80, "wfCat", "Materialize Category")
catchup(t, tab80Dash, "wfCat", "Materialize Category")
catchup(t, tabDash80, "wfMfg", "Materialize Manufacturer")
catchup(t, tab80Dash, "wfMfg", "Materialize Manufacturer")

vtgateConn = getConnection(t, vc.ClusterConfig.hostname, vc.ClusterConfig.vtgateMySQLPort)
defer vtgateConn.Close()
waitForRowCount(t, vtgateConn, sks, "cat", 3)
waitForRowCount(t, vtgateConn, sks, "mfg2", 4)

execRefQuery(t, "insert into mfg values (5, 'm5')")
execRefQuery(t, "insert into mfg2 values (6, 'm6')")
execRefQuery(t, "insert into uks.mfg values (7, 'm7')")
execRefQuery(t, "insert into sks.mfg2 values (8, 'm8')")
waitForRowCount(t, vtgateConn, uks, "mfg", 8)

execRefQuery(t, "update mfg set name = concat(name, '-updated') where id = 1")
execRefQuery(t, "update mfg2 set name = concat(name, '-updated') where id = 2")
execRefQuery(t, "update uks.mfg set name = concat(name, '-updated') where id = 3")
execRefQuery(t, "update sks.mfg2 set name = concat(name, '-updated') where id = 4")

waitForRowCount(t, vtgateConn, uks, "mfg", 8)
qr := execVtgateQuery(t, vtgateConn, "uks", "select count(*) from uks.mfg where name like '%updated%'")
require.NotNil(t, qr)
require.Equal(t, "4", qr.Rows[0][0].ToString())

execRefQuery(t, "delete from mfg where id = 5")
execRefQuery(t, "delete from mfg2 where id = 6")
execRefQuery(t, "delete from uks.mfg where id = 7")
execRefQuery(t, "delete from sks.mfg2 where id = 8")
waitForRowCount(t, vtgateConn, uks, "mfg", 4)

}

func execRefQuery(t *testing.T, query string) {
vtgateConn := getConnection(t, vc.ClusterConfig.hostname, vc.ClusterConfig.vtgateMySQLPort)
defer vtgateConn.Close()
_, err := vtgateConn.ExecuteFetch(query, 0, false)
require.NoError(t, err)
}
18 changes: 17 additions & 1 deletion go/vt/vtgate/planbuilder/dml_planner.go
Expand Up @@ -20,6 +20,7 @@ import (
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)

func rewriteRoutedTables(stmt sqlparser.Statement, vschema plancontext.VSchema) error {
Expand All @@ -42,7 +43,22 @@ func rewriteRoutedTables(stmt sqlparser.Statement, vschema plancontext.VSchema)
return false, vterrors.VT09014()
}

if vschemaTable.Name.String() != tableName.Name.String() {
if vschemaTable.Type == vindexes.TypeReference && vschemaTable.Source != nil {
// This is a reference table. We need to rewrite the table name and keyspace to that of the source table.
vschemaTable, vindexTbl, _, _, _, err = vschema.FindTableOrVindex(vschemaTable.Source.TableName)
if err != nil {
return false, err
}
if vindexTbl != nil {
// vindex cannot be present in a dml statement.
return false, vterrors.VT09014()
}
Comment on lines +52 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this will happen? do you have a test case for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the nil vindexTbl check? The same check is made above in the same function. So I assumed it was required. I can remove it, but is there any problem adding the extra check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging as is for now: assuming an additional check is safe. Can remove later on if required.

tableName.Qualifier = vschemaTable.GetTableName().Qualifier
tableName.Name = sqlparser.NewIdentifierCS(vschemaTable.Name.String())
newAliasTbl := sqlparser.NewAliasedTableExpr(tableName, "")
aliasTbl.Expr = newAliasTbl.Expr
aliasTbl.As = newAliasTbl.As
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alias should be either an existing one or the reference table name. Should not be source table name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to add some tests around it in dml_cases.json

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you have created a new test file for it. So, those can be added there.
DML with alias.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the test cases into reference_cases.json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alias should be either an existing one or the reference table name. Should not be source table name.

So I should only change the qualifier and retain the existing alias' table name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harshit-gangal, updated the code as we discussed and added more test cases for queries where aliases are specified.

} else if vschemaTable.Name.String() != tableName.Name.String() {
name := tableName.Name
if aliasTbl.As.IsEmpty() {
// if the user hasn't specified an alias, we'll insert one here so the old table name still works
Expand Down
14 changes: 14 additions & 0 deletions go/vt/vtgate/planbuilder/plan_test.go
Expand Up @@ -732,3 +732,17 @@ func benchmarkPlanner(b *testing.B, version plancontext.PlannerVersion, testCase
}
}
}

func TestReferenceRerouting(t *testing.T) {
reset := oprewriters.EnableDebugPrinting()
defer reset()

lv := loadSchema(t, "vschemas/schema.json", true)
setFks(t, lv)
vschema := &vschemawrapper.VSchemaWrapper{
V: lv,
TestBuilder: TestBuilder,
}

testFile(t, "reference_table_cases.json", "", vschema, false)
}
rohit-nayak-ps marked this conversation as resolved.
Show resolved Hide resolved