-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5982 from planetscale/vcursor-refactor
Vcursor refactor
- Loading branch information
Showing
26 changed files
with
857 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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? | ||
} |
Oops, something went wrong.