-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Vcursor refactor #5982
Vcursor refactor #5982
Changes from all commits
220a931
8c343a0
fe9975a
3add0bd
59173b4
cc6c455
289ef81
f1010d5
8219745
d93107b
c4658bf
5a9f4da
c6e02cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
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 utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
// MustMatchFn is used to create a common diff function for a test file | ||
// Usage in *_test.go file: | ||
// | ||
// Top declaration: | ||
// | ||
// var mustMatch = testutils.MustMatchFn( | ||
// []interface{}{ // types with unexported fields | ||
// type1{}, | ||
// type2{}, | ||
// ... | ||
// typeN{}, | ||
// }, | ||
// []string{ // ignored fields | ||
// ".id", // id numbers are unstable | ||
// ".createAt", // created dates might not be interesting to compare | ||
// }, | ||
// ) | ||
// | ||
// In Test*() function: | ||
// | ||
// mustMatch(t, want, got, "something doesn't match") | ||
func MustMatchFn(allowUnexportedTypes []interface{}, ignoredFields []string, extraOpts ...cmp.Option) func(t *testing.T, want, got interface{}, errMsg string) { | ||
diffOpts := append([]cmp.Option{ | ||
cmp.AllowUnexported(allowUnexportedTypes...), | ||
cmpIgnoreFields(ignoredFields...), | ||
}, extraOpts...) | ||
// Diffs want/got and fails with errMsg on any failure. | ||
return func(t *testing.T, want, got interface{}, errMsg string) { | ||
t.Helper() | ||
diff := cmp.Diff(want, got, diffOpts...) | ||
if diff != "" { | ||
t.Fatalf("%s: (-want +got)\n%v", errMsg, diff) | ||
} | ||
} | ||
} | ||
|
||
// MustMatch is a convenience version of MustMatchFn with no overrides. | ||
// Usage in Test*() function: | ||
// | ||
// testutils.MustMatch(t, want, got, "something doesn't match") | ||
var MustMatch = MustMatchFn(nil, nil) | ||
|
||
// Skips fields of pathNames for cmp.Diff. | ||
// Similar to standard cmpopts.IgnoreFields, but allows unexported fields. | ||
func cmpIgnoreFields(pathNames ...string) cmp.Option { | ||
skipFields := make(map[string]bool, len(pathNames)) | ||
for _, name := range pathNames { | ||
skipFields[name] = true | ||
} | ||
|
||
return cmp.FilterPath(func(path cmp.Path) bool { | ||
for _, ps := range path { | ||
if skipFields[ps.String()] { | ||
return true | ||
} | ||
} | ||
return false | ||
}, cmp.Ignore()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package engine | ||
|
||
import ( | ||
"vitess.io/vitess/go/sqltypes" | ||
"vitess.io/vitess/go/vt/key" | ||
"vitess.io/vitess/go/vt/proto/query" | ||
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" | ||
"vitess.io/vitess/go/vt/vterrors" | ||
"vitess.io/vitess/go/vt/vtgate/vindexes" | ||
|
||
querypb "vitess.io/vitess/go/vt/proto/query" | ||
) | ||
|
||
var _ Primitive = (*Send)(nil) | ||
|
||
// Send is an operator to send query to the specific keyspace, tabletType and destination | ||
type Send struct { | ||
// Keyspace specifies the keyspace to send the query to. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also see that |
||
Keyspace *vindexes.Keyspace | ||
|
||
// TargetDestination specifies an explicit target destination to send the query to. | ||
// This bypases the core of the v3 engine. | ||
TargetDestination key.Destination | ||
|
||
// Query specifies the query to be executed. | ||
Query string | ||
|
||
// NoAutoCommit specifies if we need to check autocommit behaviour | ||
NoAutoCommit bool | ||
|
||
noInputs | ||
} | ||
|
||
// RouteType implements Primitive interface | ||
func (s *Send) RouteType() string { | ||
if s.NoAutoCommit { | ||
return "SendNoAutoCommit" | ||
} | ||
|
||
return "Send" | ||
} | ||
|
||
// GetKeyspaceName implements Primitive interface | ||
func (s *Send) GetKeyspaceName() string { | ||
return s.Keyspace.Name | ||
} | ||
|
||
// GetTableName implements Primitive interface | ||
func (s *Send) GetTableName() string { | ||
return "" | ||
} | ||
|
||
// Execute implements Primitive interface | ||
func (s *Send) Execute(vcursor VCursor, bindVars map[string]*query.BindVariable, _ bool) (*sqltypes.Result, error) { | ||
rss, _, err := vcursor.ResolveDestinations(s.Keyspace.Name, nil, []key.Destination{s.TargetDestination}) | ||
if err != nil { | ||
return nil, vterrors.Wrap(err, "sendExecute") | ||
} | ||
|
||
if !s.Keyspace.Sharded && len(rss) != 1 { | ||
return nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "Keyspace does not have exactly one shard: %v", rss) | ||
} | ||
|
||
queries := make([]*querypb.BoundQuery, len(rss)) | ||
for i := range rss { | ||
queries[i] = &querypb.BoundQuery{ | ||
Sql: s.Query, | ||
BindVariables: bindVars, | ||
} | ||
} | ||
|
||
canAutocommit := false | ||
if !s.NoAutoCommit { | ||
canAutocommit = len(rss) == 1 && vcursor.AutocommitApproval() | ||
} | ||
|
||
rollbackOnError := !s.NoAutoCommit // for non-dml queries, there's no need to do a rollback | ||
result, errs := vcursor.ExecuteMultiShard(rss, queries, rollbackOnError, canAutocommit) | ||
err = vterrors.Aggregate(errs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return result, nil | ||
} | ||
|
||
// StreamExecute implements Primitive interface | ||
func (s *Send) StreamExecute(vcursor VCursor, bindVars map[string]*query.BindVariable, wantields bool, callback func(*sqltypes.Result) error) error { | ||
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "not reachable") // TODO: systay - this should work | ||
} | ||
|
||
// GetFields implements Primitive interface | ||
func (s *Send) GetFields(vcursor VCursor, bindVars map[string]*query.BindVariable) (*sqltypes.Result, error) { | ||
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "not reachable") // TODO: systay - @sugu, is this correct? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct for now. We have to watch out if/when we allow these primitives to become composable. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend a custom JSON marshaller that emits an additional fake
"Opcode": "Send"
, which will make the output plan more readable (look at engine/limit.go).