Skip to content

Commit

Permalink
test: add testing for shard scoped foreign keys
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
GuptaManan100 committed Mar 26, 2024
1 parent 90c0057 commit fabd90b
Show file tree
Hide file tree
Showing 5 changed files with 491 additions and 77 deletions.
33 changes: 12 additions & 21 deletions go/test/endtoend/vtgate/foreignkey/fk_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,20 @@ func (fz *fuzzer) generateDeleteDMLQuery() string {
}

// start starts running the fuzzer.
func (fz *fuzzer) start(t *testing.T, sharded bool) {
func (fz *fuzzer) start(t *testing.T, keyspace string) {
// We mark the fuzzer thread to be running now.
fz.shouldStop.Store(false)
fz.wg.Add(fz.concurrency)
for i := 0; i < fz.concurrency; i++ {
fuzzerThreadId := i
go func() {
fz.runFuzzerThread(t, sharded, fuzzerThreadId)
fz.runFuzzerThread(t, keyspace, fuzzerThreadId)
}()
}
}

// runFuzzerThread is used to run a thread of the fuzzer.
func (fz *fuzzer) runFuzzerThread(t *testing.T, sharded bool, fuzzerThreadId int) {
func (fz *fuzzer) runFuzzerThread(t *testing.T, keyspace string, fuzzerThreadId int) {
// Whenever we finish running this thread, we should mark the thread has stopped.
defer func() {
fz.wg.Done()
Expand All @@ -293,16 +293,9 @@ func (fz *fuzzer) runFuzzerThread(t *testing.T, sharded bool, fuzzerThreadId int
defer mysqlDb.Close()
}
// Set the correct keyspace to use from VtGates.
if sharded {
_ = utils.Exec(t, mcmp.VtConn, "use `ks`")
if vitessDb != nil {
_, _ = vitessDb.Exec("use `ks`")
}
} else {
_ = utils.Exec(t, mcmp.VtConn, "use `uks`")
if vitessDb != nil {
_, _ = vitessDb.Exec("use `uks`")
}
_ = utils.Exec(t, mcmp.VtConn, fmt.Sprintf("use `%v`", keyspace))
if vitessDb != nil {
_, _ = vitessDb.Exec(fmt.Sprintf("use `%v`", keyspace))
}
if fz.queryFormat == OlapSQLQueries {
_ = utils.Exec(t, mcmp.VtConn, "set workload = olap")
Expand Down Expand Up @@ -715,21 +708,19 @@ func TestFkFuzzTest(t *testing.T) {
valFalse := false
for _, fkState := range []*bool{nil, &valTrue, &valFalse} {
for _, tt := range testcases {
for _, testSharded := range []bool{false, true} {
for _, keyspace := range []string{unshardedKs, shardedKs, shardScopedKs} {
for _, queryFormat := range []QueryFormat{OlapSQLQueries, SQLQueries, PreparedStatmentQueries, PreparedStatementPacket} {
if fkState != nil && (queryFormat != SQLQueries || tt.concurrency != 1) {
continue
}
t.Run(getTestName(tt.name, testSharded)+fmt.Sprintf(" FkState - %v QueryFormat - %v", sqlparser.FkChecksStateString(fkState), queryFormat), func(t *testing.T) {
t.Run(getTestName(tt.name, keyspace)+fmt.Sprintf(" FkState - %v QueryFormat - %v", sqlparser.FkChecksStateString(fkState), queryFormat), func(t *testing.T) {
mcmp, closer := start(t)
defer closer()
// Set the correct keyspace to use from VtGates.
if testSharded {
if keyspace == shardedKs {
t.Skip("Skip test since we don't have sharded foreign key support yet")
_ = utils.Exec(t, mcmp.VtConn, "use `ks`")
} else {
_ = utils.Exec(t, mcmp.VtConn, "use `uks`")
}
// Set the correct keyspace to use from VtGates.
_ = utils.Exec(t, mcmp.VtConn, fmt.Sprintf("use `%v`", keyspace))

// Ensure that the Vitess database is originally empty
ensureDatabaseState(t, mcmp.VtConn, true)
Expand All @@ -739,7 +730,7 @@ func TestFkFuzzTest(t *testing.T) {
fz := newFuzzer(tt.concurrency, tt.maxValForId, tt.maxValForCol, tt.insertShare, tt.deleteShare, tt.updateShare, queryFormat, fkState)

// Start the fuzzer.
fz.start(t, testSharded)
fz.start(t, keyspace)

// Wait for the timeForTesting so that the threads continue to run.
totalTime := time.After(tt.timeForTesting)
Expand Down
66 changes: 34 additions & 32 deletions go/test/endtoend/vtgate/foreignkey/fk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,17 +859,15 @@ func TestFkScenarios(t *testing.T) {
}

for _, tt := range testcases {
for _, testSharded := range []bool{false, true} {
t.Run(getTestName(tt.name, testSharded), func(t *testing.T) {
for _, keyspace := range []string{unshardedKs, shardedKs, shardScopedKs} {
t.Run(getTestName(tt.name, keyspace), func(t *testing.T) {
mcmp, closer := start(t)
defer closer()
// Set the correct keyspace to use from VtGates.
if testSharded {
if keyspace == shardedKs {
t.Skip("Skip test since we don't have sharded foreign key support yet")
_ = utils.Exec(t, mcmp.VtConn, "use `ks`")
} else {
_ = utils.Exec(t, mcmp.VtConn, "use `uks`")
}
// Set the correct keyspace to use from VtGates.
_ = utils.Exec(t, mcmp.VtConn, fmt.Sprintf("use `%v`", keyspace))

// Insert all the data required for running the test.
for _, query := range tt.dataQueries {
Expand All @@ -892,17 +890,15 @@ func TestFkScenarios(t *testing.T) {
}
}

for _, testSharded := range []bool{false, true} {
t.Run(getTestName("Transactions with intermediate failure", testSharded), func(t *testing.T) {
for _, keyspace := range []string{unshardedKs, shardedKs, shardScopedKs} {
t.Run(getTestName("Transactions with intermediate failure", keyspace), func(t *testing.T) {
mcmp, closer := start(t)
defer closer()
// Set the correct keyspace to use from VtGates.
if testSharded {
if keyspace == shardedKs {
t.Skip("Skip test since we don't have sharded foreign key support yet")
_ = utils.Exec(t, mcmp.VtConn, "use `ks`")
} else {
_ = utils.Exec(t, mcmp.VtConn, "use `uks`")
}
// Set the correct keyspace to use from VtGates.
_ = utils.Exec(t, mcmp.VtConn, fmt.Sprintf("use `%v`", keyspace))

// Insert some rows
mcmp.Exec("INSERT INTO fk_t10(id, col) VALUES (1, 7), (2, 9), (3, 5)")
Expand Down Expand Up @@ -1114,28 +1110,34 @@ func TestFkQueries(t *testing.T) {
},
}

for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
mcmp, closer := start(t)
defer closer()
_ = utils.Exec(t, mcmp.VtConn, "use `uks`")
for _, tt := range testcases {
for _, keyspace := range []string{unshardedKs, shardedKs, shardScopedKs} {
t.Run(getTestName(tt.name, keyspace), func(t *testing.T) {
mcmp, closer := start(t)
defer closer()
if keyspace == shardedKs {
t.Skip("Skip test since we don't have sharded foreign key support yet")
}
// Set the correct keyspace to use from VtGates.
_ = utils.Exec(t, mcmp.VtConn, fmt.Sprintf("use `%v`", keyspace))

// Ensure that the Vitess database is originally empty
ensureDatabaseState(t, mcmp.VtConn, true)
ensureDatabaseState(t, mcmp.MySQLConn, true)
// Ensure that the Vitess database is originally empty
ensureDatabaseState(t, mcmp.VtConn, true)
ensureDatabaseState(t, mcmp.MySQLConn, true)

for _, query := range testcase.queries {
_, _ = mcmp.ExecAllowAndCompareError(query)
if t.Failed() {
break
for _, query := range tt.queries {
_, _ = mcmp.ExecAllowAndCompareError(query)
if t.Failed() {
break
}
}
}

// ensure Vitess database has some data. This ensures not all the commands failed.
ensureDatabaseState(t, mcmp.VtConn, false)
// Verify the consistency of the data.
verifyDataIsCorrect(t, mcmp, 1)
})
// ensure Vitess database has some data. This ensures not all the commands failed.
ensureDatabaseState(t, mcmp.VtConn, false)
// Verify the consistency of the data.
verifyDataIsCorrect(t, mcmp, 1)
})
}
}
}

Expand Down
49 changes: 31 additions & 18 deletions go/test/endtoend/vtgate/foreignkey/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
mysqlParams mysql.ConnParams
vtgateGrpcAddress string
shardedKs = "ks"
shardScopedKs = "sks"
unshardedKs = "uks"
unshardedUnmanagedKs = "unmanaged_uks"
Cell = "test"
Expand All @@ -47,6 +48,9 @@ var (
//go:embed sharded_vschema.json
shardedVSchema string

//go:embed shard_scoped_vschema.json
shardScopedVSchema string

//go:embed unsharded_vschema.json
unshardedVSchema string

Expand Down Expand Up @@ -119,6 +123,18 @@ func TestMain(m *testing.M) {
return 1
}

// Start shard-scoped keyspace
ssKs := &cluster.Keyspace{
Name: shardScopedKs,
SchemaSQL: schemaSQL,
VSchema: shardScopedVSchema,
}

err = clusterInstance.StartKeyspace(*ssKs, []string{"-80", "80-"}, 1, false)
if err != nil {
return 1
}

uKs := &cluster.Keyspace{
Name: unshardedKs,
SchemaSQL: schemaSQL,
Expand Down Expand Up @@ -196,28 +212,25 @@ func startBenchmark(b *testing.B) {
}

func clearOutAllData(t testing.TB, vtConn *mysql.Conn, mysqlConn *mysql.Conn) {
_ = utils.Exec(t, vtConn, "use `ks/-80`")
tables := []string{"t4", "t3", "t2", "t1", "multicol_tbl2", "multicol_tbl1"}
tables = append(tables, fkTables...)
for _, table := range tables {
_, _ = utils.ExecAllowError(t, vtConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
_, _ = utils.ExecAllowError(t, mysqlConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
}
_ = utils.Exec(t, vtConn, "use `ks/80-`")
for _, table := range tables {
_, _ = utils.ExecAllowError(t, vtConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
_, _ = utils.ExecAllowError(t, mysqlConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
keyspaces := []string{`ks/-80`, `ks/80-`, `sks/-80`, `sks/80-`}
for _, keyspace := range keyspaces {
_ = utils.Exec(t, vtConn, fmt.Sprintf("use `%v`", keyspace))
for _, table := range tables {
_, _ = utils.ExecAllowError(t, vtConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
_, _ = utils.ExecAllowError(t, mysqlConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
}
}
_ = utils.Exec(t, vtConn, "use `uks`")

tables = []string{"u_t1", "u_t2", "u_t3"}
tables = append(tables, fkTables...)
for _, table := range tables {
_, _ = utils.ExecAllowError(t, vtConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
_, _ = utils.ExecAllowError(t, mysqlConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
}
_ = utils.Exec(t, vtConn, "use `unmanaged_uks`")
for _, table := range tables {
_, _ = utils.ExecAllowError(t, vtConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
_, _ = utils.ExecAllowError(t, mysqlConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
keyspaces = []string{`uks`, `unmanaged_uks`}
for _, keyspace := range keyspaces {
_ = utils.Exec(t, vtConn, fmt.Sprintf("use `%v`", keyspace))
for _, table := range tables {
_, _ = utils.ExecAllowError(t, vtConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
_, _ = utils.ExecAllowError(t, mysqlConn, "delete /*+ SET_VAR(foreign_key_checks=OFF) */ from "+table)
}
}
}
Loading

0 comments on commit fabd90b

Please sign in to comment.