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

Make DROP/CREATE DATABASE pluggable #7381

Merged
merged 27 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b7400d4
Start of making CREATE DATABASE pluggable
systay Jan 26, 2021
f85d458
use more restrictive type
systay Jan 26, 2021
768719e
use simpler API for the CREATE DB API
systay Jan 26, 2021
9d200e1
Merge remote-tracking branch 'upstream/master' into create-database
systay Feb 22, 2021
668b66f
make it possible to do both create and drop using a plugin
systay Feb 22, 2021
80b184f
adressed PR review
systay Mar 2, 2021
b324b06
Merge remote-tracking branch 'upstream/master' into create-database
systay Mar 2, 2021
7e776c8
[wip] refactored lots of things
systay Mar 3, 2021
cd26ee2
code refactor
harshit-gangal Mar 3, 2021
02e49ce
e2e test fir dbddl plugin
harshit-gangal Mar 3, 2021
a429c60
[wip] implement the dbddl primitive
systay Mar 3, 2021
90b697d
implemented more of the primitive
systay Mar 3, 2021
a32812c
Merge remote-tracking branch 'upstream/master' into create-database
systay Mar 3, 2021
6657063
fix the primitive on create db and implement drop db
harshit-gangal Mar 4, 2021
c77f7c5
e2e test for drop db
harshit-gangal Mar 4, 2021
73eb606
fix dbdddl plugin unit test
harshit-gangal Mar 4, 2021
c8569f0
dbddl: code refactor
harshit-gangal Mar 4, 2021
295f848
added comments to dbddl
harshit-gangal Mar 4, 2021
10daa0e
added comments directive to set query timeout in db ddl primitive
harshit-gangal Mar 4, 2021
131c5f9
added timeout test
harshit-gangal Mar 5, 2021
9a33429
drop database to wait for vschema to be updated
harshit-gangal Mar 8, 2021
afd6e57
fix plan test
harshit-gangal Mar 9, 2021
6a92f2d
on drop database call, check if keyspace is removed from vschema
harshit-gangal Mar 9, 2021
06da2d7
check resolve destination for drop database to see if keyspace is not…
harshit-gangal Mar 10, 2021
00bf410
Merge branch 'master' of github.com:vitessio/vitess into create-database
harshit-gangal Mar 11, 2021
e762d58
make sure to not return error when things worked
systay Mar 11, 2021
6a7f295
watch vschema in drop database to confirm that the keyspace is no lon…
harshit-gangal Mar 11, 2021
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
189 changes: 189 additions & 0 deletions go/test/endtoend/vtgate/createdb_plugin/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
Copyright 2020 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package unsharded

import (
"context"
"flag"
"fmt"
"os"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/endtoend/cluster"
)

var (
clusterInstance *cluster.LocalProcessCluster
vtParams mysql.ConnParams
keyspaceName = "ks"
cell = "zone1"
hostname = "localhost"
)

func TestMain(m *testing.M) {
defer cluster.PanicHandler(nil)
flag.Parse()

exitCode := func() int {
clusterInstance = cluster.NewCluster(cell, hostname)
defer clusterInstance.Teardown()

// Start topo server
if err := clusterInstance.StartTopo(); err != nil {
return 1
}

// Start keyspace
keyspace := &cluster.Keyspace{
Name: keyspaceName,
}
if err := clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 0, false); err != nil {
return 1
}

// Start vtgate
clusterInstance.VtGateExtraArgs = []string{"-dbddl_plugin", "noop", "-mysql_server_query_timeout", "60s"}
vtgateProcess := clusterInstance.NewVtgateInstance()
vtgateProcess.SysVarSetEnabled = true
if err := vtgateProcess.Setup(); err != nil {
return 1
}

vtParams = mysql.ConnParams{
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
}
return m.Run()
}()
os.Exit(exitCode)
}

func TestDBDDLPlugin(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

createAndDrop := func(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
qr := exec(t, conn, `create database aaa`)
require.EqualValues(t, 1, qr.RowsAffected)
}()
time.Sleep(300 * time.Millisecond)
start(t, "aaa")

// wait until the create database query has returned
wg.Wait()

exec(t, conn, `use aaa`)
exec(t, conn, `create table t (id bigint primary key)`)
exec(t, conn, `insert into t(id) values (1),(2),(3),(4),(5)`)
assertMatches(t, conn, "select count(*) from t", `[[INT64(5)]]`)

wg.Add(1)
go func() {
defer wg.Done()
_ = exec(t, conn, `drop database aaa`)
}()
time.Sleep(300 * time.Millisecond)
shutdown(t, "aaa")

// wait until the drop database query has returned
wg.Wait()

_, err = conn.ExecuteFetch(`select count(*) from t`, 1000, true)
require.Error(t, err)
}
t.Run("first try", func(t *testing.T) {
createAndDrop(t)
})
if !t.Failed() {
t.Run("second try", func(t *testing.T) {
createAndDrop(t)
})
}
}

func start(t *testing.T, ksName string) {
keyspace := &cluster.Keyspace{
Name: ksName,
}
require.NoError(t,
clusterInstance.StartUnshardedKeyspace(*keyspace, 0, false),
"new database creation failed")
}

func shutdown(t *testing.T, ksName string) {
for _, ks := range clusterInstance.Keyspaces {
if ks.Name != ksName {
continue
}
for _, shard := range ks.Shards {
for _, tablet := range shard.Vttablets {
if tablet.MysqlctlProcess.TabletUID > 0 {
_, err := tablet.MysqlctlProcess.StopProcess()
assert.NoError(t, err)
}
if tablet.MysqlctldProcess.TabletUID > 0 {
err := tablet.MysqlctldProcess.Stop()
assert.NoError(t, err)
}
_ = tablet.VttabletProcess.TearDown()
}
}
}

require.NoError(t,
clusterInstance.VtctlclientProcess.ExecuteCommand("DeleteKeyspace", "-recursive", ksName))

require.NoError(t,
clusterInstance.VtctlclientProcess.ExecuteCommand("RebuildVSchemaGraph"))
}

func exec(t *testing.T, conn *mysql.Conn, query string) *sqltypes.Result {
t.Helper()
qr, err := conn.ExecuteFetch(query, 1000, true)
require.NoError(t, err)
return qr
}

func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) {
t.Helper()
qr := exec(t, conn, query)
got := fmt.Sprintf("%v", qr.Rows)
diff := cmp.Diff(expected, got)
if diff != "" {
t.Errorf("Query: %s (-want +got):\n%s", query, diff)
}
}
13 changes: 7 additions & 6 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package sqlparser

import (
"fmt"
"strings"

"vitess.io/vitess/go/sqltypes"
Expand Down Expand Up @@ -343,6 +342,7 @@ type (

// DropDatabase represents a DROP database statement.
DropDatabase struct {
Comments Comments
DBName string
IfExists bool
}
Expand All @@ -359,6 +359,7 @@ type (

// CreateDatabase represents a CREATE database statement.
CreateDatabase struct {
Comments Comments
DBName string
IfNotExists bool
CreateOptions []CollateAndCharset
Expand Down Expand Up @@ -2039,9 +2040,9 @@ func (node *SetTransaction) Format(buf *TrackedBuffer) {
func (node *DropDatabase) Format(buf *TrackedBuffer) {
exists := ""
if node.IfExists {
exists = " if exists"
exists = "if exists "
}
buf.WriteString(fmt.Sprintf("%s database%s %v", DropStr, exists, node.DBName))
buf.astPrintf(node, "%s database %v%s%s", DropStr, node.Comments, exists, node.DBName)
}

// Format formats the node.
Expand Down Expand Up @@ -3204,11 +3205,11 @@ func (node *SelectInto) Format(buf *TrackedBuffer) {

// Format formats the node.
func (node *CreateDatabase) Format(buf *TrackedBuffer) {
buf.WriteString("create database")
buf.astPrintf(node, "create database %v", node.Comments)
if node.IfNotExists {
buf.WriteString(" if not exists")
buf.WriteString("if not exists ")
}
buf.astPrintf(node, " %s", node.DBName)
buf.astPrintf(node, "%s", node.DBName)
if node.CreateOptions != nil {
for _, createOption := range node.CreateOptions {
if createOption.IsDefault {
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1764,12 +1764,12 @@ var (
}, {
input: "rollback",
}, {
input: "create database test_db",
input: "create database /* simple */ test_db",
}, {
input: "create schema test_db",
output: "create database test_db",
}, {
input: "create database if not exists test_db",
input: "create database /* simple */ if not exists test_db",
}, {
input: "create schema if not exists test_db",
output: "create database if not exists test_db",
Expand All @@ -1785,12 +1785,12 @@ var (
input: "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mysql` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;",
output: "create database if not exists mysql default character set utf8mb4 collate utf8mb4_0900_ai_ci",
}, {
input: "drop database test_db",
input: "drop database /* simple */ test_db",
}, {
input: "drop schema test_db",
output: "drop database test_db",
}, {
input: "drop database if exists test_db",
input: "drop database /* simple */ if exists test_db",
}, {
input: "delete a.*, b.* from tbl_a a, tbl_b b where a.id = b.id and b.name = 'test'",
output: "delete a, b from tbl_a as a, tbl_b as b where a.id = b.id and b.`name` = 'test'",
Expand Down
6 changes: 6 additions & 0 deletions go/vt/sqlparser/rewriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading