add support for vtexplain to override the dbname target

This change adds a --dbname option to the vtexplain binary so that
callers can simulate what vtgate will do when the application connects
with a specific database name.

Add a test to show how this can be used to bypass the v3 routing and
send queries directly to a shard.

Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
This commit is contained in:
Michael Demmer 2018-08-16 06:54:49 -07:00
Родитель cd5018743a
Коммит 4659a01410
8 изменённых файлов: 48 добавлений и 3 удалений

Просмотреть файл

@ -14,7 +14,7 @@ select * from user where id in (1,2,3,4,5,6,7,8)
1 ks_sharded/c0-: select * from user where id in (4, 6, 7, 8) limit 10001
----------------------------------------------------------------------
insert into user (id, name) values(2, 'bob')
insert into user (id, name) values (2, 'bob')
1 ks_sharded/c0-: begin
1 ks_sharded/c0-: insert into name_user_map(name, user_id) values ('bob', 2) /* _stream name_user_map (name user_id ) ('Ym9i' 2 ); */ /* vtgate:: keyspace_id:da8a82595aa28154c17717955ffeed8b */

Просмотреть файл

@ -0,0 +1,18 @@
----------------------------------------------------------------------
select * from user where email='null@void.com'
1 ks_sharded/40-80: select * from user where email = 'null@void.com' limit 10001
----------------------------------------------------------------------
select * from user where id in (1,2,3,4,5,6,7,8)
1 ks_sharded/40-80: select * from user where id in (1, 2, 3, 4, 5, 6, 7, 8) limit 10001
----------------------------------------------------------------------
insert into user (id, name) values (2, 'bob')
1 ks_sharded/40-80: begin
1 ks_sharded/40-80: insert into user(id, name) values (2, 'bob')/* vtgate:: filtered_replication_unfriendly */
2 ks_sharded/40-80: commit
----------------------------------------------------------------------

Просмотреть файл

@ -1,3 +1,3 @@
select * from user where email='null@void.com';
select * from user where id in (1,2,3,4,5,6,7,8);
insert into user (id, name) values(2, 'bob');
insert into user (id, name) values (2, 'bob');

Просмотреть файл

@ -0,0 +1,8 @@
/*
* These are the same set of queries from "options-queries.sql" but
* are run with an explicit shard target of ks_sharded/40-80 which
* bypasses the normal v3 routing.
*/
select * from user where email='null@void.com';
select * from user where id in (1,2,3,4,5,6,7,8);
insert into user (id, name) values (2, 'bob');

Просмотреть файл

@ -40,6 +40,7 @@ var (
replicationMode = flag.String("replication-mode", "ROW", "The replication mode to simulate -- must be set to either ROW or STATEMENT")
normalize = flag.Bool("normalize", false, "Whether to enable vtgate normalization")
outputMode = flag.String("output-mode", "text", "Output in human-friendly text or json")
dbName = flag.String("dbname", "", "Optional database target to override normal routing")
// vtexplainFlags lists all the flags that should show in usage
vtexplainFlags = []string{
@ -53,6 +54,7 @@ var (
"sql-file",
"vschema",
"vschema-file",
"dbname",
}
)
@ -153,6 +155,7 @@ func parseAndRun() error {
ReplicationMode: *replicationMode,
NumShards: *numShards,
Normalize: *normalize,
Target: *dbName,
}
log.V(100).Infof("sql %s\n", sql)

Просмотреть файл

@ -63,6 +63,10 @@ type Options struct {
// StrictDDL is used in unit tests only to verify that the schema
// is parsed properly.
StrictDDL bool
// Target is used to override the "database" target in the
// vtgate session to simulate `USE <target>`
Target string
}
// TabletQuery defines a query that was sent to a given tablet and how it was

Просмотреть файл

@ -144,6 +144,16 @@ func TestOptions(t *testing.T) {
testExplain("options", opts, t)
}
func TestTarget(t *testing.T) {
opts := &Options{
ReplicationMode: "ROW",
NumShards: 4,
Normalize: false,
Target: "ks_sharded/40-80",
}
testExplain("target", opts, t)
}
func TestComments(t *testing.T) {
testExplain("comments", defaultTestOpts(), t)

Просмотреть файл

@ -46,7 +46,7 @@ var (
healthCheck *discovery.FakeHealthCheck
vtgateSession = &vtgatepb.Session{
TargetString: "@master",
TargetString: "",
Autocommit: true,
}
)
@ -62,6 +62,8 @@ func initVtgateExecutor(vSchemaStr string, opts *Options) error {
return err
}
vtgateSession.TargetString = opts.Target
streamSize := 10
queryPlanCacheSize := int64(10)
vtgateExecutor = vtgate.NewExecutor(context.Background(), explainTopo, vtexplainCell, "", resolver, opts.Normalize, streamSize, queryPlanCacheSize, false /* legacyAutocommit */)