-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
send.go
202 lines (167 loc) · 5.93 KB
/
send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
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 engine
import (
"context"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/srvtopo"
"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.
TargetDestination key.Destination
// Query specifies the query to be executed.
Query string
// IsDML specifies how to deal with autocommit behaviour
IsDML bool
// SingleShardOnly specifies that the query must be send to only single shard
SingleShardOnly bool
// 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
}
// ShardName as key for setting shard name in bind variables map
const ShardName = "__vt_shard"
// NeedsTransaction implements the Primitive interface
func (s *Send) NeedsTransaction() bool {
return s.IsDML
}
// RouteType implements Primitive interface
func (s *Send) RouteType() string {
if s.IsDML {
return "SendDML"
}
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 ""
}
// TryExecute implements Primitive interface
func (s *Send) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
ctx, cancelFunc := addQueryTimeout(ctx, vcursor, 0)
defer cancelFunc()
rss, _, err := vcursor.ResolveDestinations(ctx, s.Keyspace.Name, nil, []key.Destination{s.TargetDestination})
if err != nil {
return nil, err
}
if !s.Keyspace.Sharded && len(rss) != 1 {
return nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "Keyspace does not have exactly one shard: %v", rss)
}
if s.SingleShardOnly && len(rss) != 1 {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "Unexpected error, DestinationKeyspaceID mapping to multiple shards: %s, got: %v", s.Query, s.TargetDestination)
}
queries := make([]*querypb.BoundQuery, len(rss))
for i, rs := range rss {
bv := bindVars
if s.ShardNameNeeded {
bv = copyBindVars(bindVars)
bv[ShardName] = sqltypes.StringBindVariable(rs.Target.Shard)
}
queries[i] = &querypb.BoundQuery{
Sql: s.Query,
BindVariables: bv,
}
}
rollbackOnError := s.IsDML // for non-dml queries, there's no need to do a rollback
result, errs := vcursor.ExecuteMultiShard(ctx, s, rss, queries, rollbackOnError, s.canAutoCommit(vcursor, rss))
err = vterrors.Aggregate(errs)
if err != nil {
return nil, err
}
return result, nil
}
func (s *Send) canAutoCommit(vcursor VCursor, rss []*srvtopo.ResolvedShard) bool {
if s.IsDML {
return (len(rss) == 1 || s.MultishardAutocommit) && vcursor.AutocommitApproval()
}
return false
}
func copyBindVars(in map[string]*querypb.BindVariable) map[string]*querypb.BindVariable {
out := make(map[string]*querypb.BindVariable, len(in)+1)
for k, v := range in {
out[k] = v
}
return out
}
// TryStreamExecute implements Primitive interface
func (s *Send) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
rss, _, err := vcursor.ResolveDestinations(ctx, s.Keyspace.Name, nil, []key.Destination{s.TargetDestination})
if err != nil {
return err
}
if !s.Keyspace.Sharded && len(rss) != 1 {
return vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "Keyspace does not have exactly one shard: %v", rss)
}
if s.SingleShardOnly && len(rss) != 1 {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "Unexpected error, DestinationKeyspaceID mapping to multiple shards: %s, got: %v", s.Query, s.TargetDestination)
}
multiBindVars := make([]map[string]*querypb.BindVariable, len(rss))
for i, rs := range rss {
bv := bindVars
if s.ShardNameNeeded {
bv = copyBindVars(bindVars)
bv[ShardName] = sqltypes.StringBindVariable(rs.Target.Shard)
}
multiBindVars[i] = bv
}
errors := vcursor.StreamExecuteMulti(ctx, s, s.Query, rss, multiBindVars, s.IsDML, s.canAutoCommit(vcursor, rss), callback)
return vterrors.Aggregate(errors)
}
// GetFields implements Primitive interface
func (s *Send) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
qr, err := vcursor.ExecutePrimitive(ctx, s, bindVars, false)
if err != nil {
return nil, err
}
qr.Rows = nil
return qr, nil
}
func (s *Send) description() PrimitiveDescription {
other := map[string]any{
"Query": s.Query,
"Table": s.GetTableName(),
}
if s.IsDML {
other["IsDML"] = true
}
if s.SingleShardOnly {
other["SingleShardOnly"] = true
}
if s.ShardNameNeeded {
other["ShardNameNeeded"] = true
}
if s.MultishardAutocommit {
other["MultishardAutocommit"] = true
}
return PrimitiveDescription{
OperatorType: "Send",
Keyspace: s.Keyspace,
TargetDestination: s.TargetDestination,
Other: other,
}
}