Skip to content
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
24 changes: 24 additions & 0 deletions go/vt/sqlparser/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,30 @@ func (d CommentDirectives) GetString(key string, defaultVal string) string {
return stringVal
}

// MultiShardAutocommitDirective returns true if multishard autocommit directive is set to true in query.
func MultiShardAutocommitDirective(stmt Statement) bool {
switch stmt := stmt.(type) {
case *Insert:
directives := ExtractCommentDirectives(stmt.Comments)
if directives.IsSet(DirectiveMultiShardAutocommit) {
return true
}
case *Update:
directives := ExtractCommentDirectives(stmt.Comments)
if directives.IsSet(DirectiveMultiShardAutocommit) {
return true
}
case *Delete:
directives := ExtractCommentDirectives(stmt.Comments)
if directives.IsSet(DirectiveMultiShardAutocommit) {
return true
}
default:
return false
}
return false
}

// SkipQueryPlanCacheDirective returns true if skip query plan cache directive is set to true in query.
func SkipQueryPlanCacheDirective(stmt Statement) bool {
switch stmt := stmt.(type) {
Expand Down
8 changes: 7 additions & 1 deletion go/vt/vtgate/engine/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type Send struct {
// ShardNameNeeded specified that the shard name is added to the bind variables
ShardNameNeeded bool

// MultishardAutocommit specifies that a multishard transaction query can autocommit
MultishardAutocommit bool

noInputs
}

Expand Down Expand Up @@ -108,7 +111,7 @@ func (s *Send) Execute(vcursor VCursor, bindVars map[string]*querypb.BindVariabl

canAutocommit := false
if s.IsDML {
canAutocommit = len(rss) == 1 && vcursor.AutocommitApproval()
canAutocommit = (len(rss) == 1 || s.MultishardAutocommit) && vcursor.AutocommitApproval()
}

rollbackOnError := s.IsDML // for non-dml queries, there's no need to do a rollback
Expand Down Expand Up @@ -180,6 +183,9 @@ func (s *Send) description() PrimitiveDescription {
if s.ShardNameNeeded {
other["ShardNameNeeded"] = true
}
if s.MultishardAutocommit {
other["MultishardAutocommit"] = true
}
return PrimitiveDescription{
OperatorType: "Send",
Keyspace: s.Keyspace,
Expand Down
60 changes: 40 additions & 20 deletions go/vt/vtgate/engine/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ import (

func TestSendTable(t *testing.T) {
type testCase struct {
testName string
sharded bool
shards []string
destination key.Destination
expectedQueryLog []string
expectedError string
isDML bool
singleShardOnly bool
testName string
sharded bool
shards []string
destination key.Destination
expectedQueryLog []string
expectedError string
isDML bool
singleShardOnly bool
multiShardAutocommit bool
}

singleShard := []string{"0"}
Expand All @@ -53,7 +54,8 @@ func TestSendTable(t *testing.T) {
`ResolveDestinations ks [] Destinations:DestinationAllShards()`,
`ExecuteMultiShard ks.0: dummy_query {} false false`,
},
isDML: false,
isDML: false,
multiShardAutocommit: false,
},
{
testName: "sharded with no autocommit",
Expand All @@ -64,7 +66,8 @@ func TestSendTable(t *testing.T) {
`ResolveDestinations ks [] Destinations:DestinationShard(20-)`,
`ExecuteMultiShard ks.DestinationShard(20-): dummy_query {} false false`,
},
isDML: false,
isDML: false,
multiShardAutocommit: false,
},
{
testName: "unsharded",
Expand All @@ -75,7 +78,8 @@ func TestSendTable(t *testing.T) {
`ResolveDestinations ks [] Destinations:DestinationAllShards()`,
`ExecuteMultiShard ks.0: dummy_query {} true true`,
},
isDML: true,
isDML: true,
multiShardAutocommit: false,
},
{
testName: "sharded with single shard destination",
Expand All @@ -86,7 +90,8 @@ func TestSendTable(t *testing.T) {
`ResolveDestinations ks [] Destinations:DestinationShard(20-)`,
`ExecuteMultiShard ks.DestinationShard(20-): dummy_query {} true true`,
},
isDML: true,
isDML: true,
multiShardAutocommit: false,
},
{
testName: "sharded with multi shard destination",
Expand All @@ -97,7 +102,20 @@ func TestSendTable(t *testing.T) {
`ResolveDestinations ks [] Destinations:DestinationAllShards()`,
`ExecuteMultiShard ks.-20: dummy_query {} ks.20-: dummy_query {} true false`,
},
isDML: true,
isDML: true,
multiShardAutocommit: false,
},
{
testName: "sharded with multi shard destination and autocommit",
sharded: true,
shards: twoShards,
destination: key.DestinationAllShards{},
expectedQueryLog: []string{
`ResolveDestinations ks [] Destinations:DestinationAllShards()`,
`ExecuteMultiShard ks.-20: dummy_query {} ks.20-: dummy_query {} true true`,
},
isDML: true,
multiShardAutocommit: true,
},
{
testName: "sharded with multi shard destination",
Expand All @@ -107,9 +125,10 @@ func TestSendTable(t *testing.T) {
expectedQueryLog: []string{
`ResolveDestinations ks [] Destinations:DestinationAllShards()`,
},
expectedError: "Unexpected error, DestinationKeyspaceID mapping to multiple shards: dummy_query, got: DestinationAllShards()",
isDML: true,
singleShardOnly: true,
expectedError: "Unexpected error, DestinationKeyspaceID mapping to multiple shards: dummy_query, got: DestinationAllShards()",
isDML: true,
singleShardOnly: true,
multiShardAutocommit: false,
},
}

Expand All @@ -120,10 +139,11 @@ func TestSendTable(t *testing.T) {
Name: "ks",
Sharded: tc.sharded,
},
Query: "dummy_query",
TargetDestination: tc.destination,
IsDML: tc.isDML,
SingleShardOnly: tc.singleShardOnly,
Query: "dummy_query",
TargetDestination: tc.destination,
IsDML: tc.isDML,
SingleShardOnly: tc.singleShardOnly,
MultishardAutocommit: tc.multiShardAutocommit,
}
vc := &loggingVCursor{shards: tc.shards}
_, err := send.Execute(vc, map[string]*querypb.BindVariable{}, false)
Expand Down
11 changes: 6 additions & 5 deletions go/vt/vtgate/planbuilder/bypass.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ func buildPlanForBypass(stmt sqlparser.Statement, _ *sqlparser.ReservedVars, vsc
return nil, err
}
return &engine.Send{
Keyspace: keyspace,
TargetDestination: vschema.Destination(),
Query: sqlparser.String(stmt),
IsDML: sqlparser.IsDMLStatement(stmt),
SingleShardOnly: false,
Keyspace: keyspace,
TargetDestination: vschema.Destination(),
Query: sqlparser.String(stmt),
IsDML: sqlparser.IsDMLStatement(stmt),
SingleShardOnly: false,
MultishardAutocommit: sqlparser.MultiShardAutocommitDirective(stmt),
}, nil
}
25 changes: 22 additions & 3 deletions go/vt/vtgate/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,40 @@ func TestOne(t *testing.T) {
testFile(t, "onecase.txt", "", vschema, true)
}

func TestBypassPlanningFromFile(t *testing.T) {
func TestBypassPlanningShardTargetFromFile(t *testing.T) {
testOutputTempDir, err := ioutil.TempDir("", "plan_test")
require.NoError(t, err)
defer os.RemoveAll(testOutputTempDir)

vschema := &vschemaWrapper{
v: loadSchema(t, "schema_test.json"),
keyspace: &vindexes.Keyspace{
Name: "main",
Sharded: false,
},
tabletType: topodatapb.TabletType_MASTER,
dest: key.DestinationShard("-80")}

testFile(t, "bypass_shard_cases.txt", testOutputTempDir, vschema, true)
}
func TestBypassPlanningKeyrangeTargetFromFile(t *testing.T) {
testOutputTempDir, err := ioutil.TempDir("", "plan_test")
require.NoError(t, err)
defer os.RemoveAll(testOutputTempDir)

keyRange, _ := key.ParseShardingSpec("-")

vschema := &vschemaWrapper{
v: loadSchema(t, "schema_test.json"),
keyspace: &vindexes.Keyspace{
Name: "main",
Sharded: false,
},
tabletType: topodatapb.TabletType_MASTER,
dest: key.DestinationShard("-80"),
dest: key.DestinationExactKeyRange{KeyRange: keyRange[0]},
}

testFile(t, "bypass_cases.txt", testOutputTempDir, vschema, true)
testFile(t, "bypass_keyrange_cases.txt", testOutputTempDir, vschema, true)
}

func TestWithDefaultKeyspaceFromFile(t *testing.T) {
Expand Down
Loading