forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtgate.go
241 lines (219 loc) · 7.66 KB
/
vtgate.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package vtgate provides query routing rpc services
// for vttablets.
package vtgate
import (
"time"
log "github.com/golang/glog"
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/vt/vtgate/proto"
)
var RpcVTGate *VTGate
// VTGate is the rpc interface to vtgate. Only one instance
// can be created.
type VTGate struct {
resolver *Resolver
}
// registration mechanism
type RegisterVTGate func(*VTGate)
var RegisterVTGates []RegisterVTGate
func Init(serv SrvTopoServer, cell string, retryDelay time.Duration, retryCount int, timeout time.Duration) {
if RpcVTGate != nil {
log.Fatalf("VTGate already initialized")
}
RpcVTGate = &VTGate{
resolver: NewResolver(serv, cell, retryDelay, retryCount, timeout),
}
for _, f := range RegisterVTGates {
f(RpcVTGate)
}
}
// ExecuteShard executes a non-streaming query on the specified shards.
func (vtg *VTGate) ExecuteShard(context interface{}, query *proto.QueryShard, reply *proto.QueryResult) error {
qr, err := vtg.resolver.Execute(
context,
query.Sql,
query.BindVariables,
query.Keyspace,
query.TabletType,
query.Session,
func(keyspace string) ([]string, error) {
return query.Shards, nil
},
)
if err == nil {
proto.PopulateQueryResult(qr, reply)
} else {
reply.Error = err.Error()
log.Errorf("ExecuteShard: %v, query: %+v", err, query)
}
reply.Session = query.Session
return nil
}
// ExecuteKeyspaceIds executes a non-streaming query based on the specified keyspace ids.
func (vtg *VTGate) ExecuteKeyspaceIds(context interface{}, query *proto.KeyspaceIdQuery, reply *proto.QueryResult) error {
qr, err := vtg.resolver.ExecuteKeyspaceIds(context, query)
if err == nil {
proto.PopulateQueryResult(qr, reply)
} else {
reply.Error = err.Error()
log.Errorf("ExecuteKeyspaceIds: %v, query: %+v", err, query)
}
reply.Session = query.Session
return nil
}
// ExecuteKeyRanges executes a non-streaming query based on the specified keyranges.
func (vtg *VTGate) ExecuteKeyRanges(context interface{}, query *proto.KeyRangeQuery, reply *proto.QueryResult) error {
qr, err := vtg.resolver.ExecuteKeyRanges(context, query)
if err == nil {
proto.PopulateQueryResult(qr, reply)
} else {
reply.Error = err.Error()
log.Errorf("ExecuteKeyRange: %v, query: %+v", err, query)
}
reply.Session = query.Session
return nil
}
// ExecuteEntityIds excutes a non-streaming query based on given KeyspaceId map.
func (vtg *VTGate) ExecuteEntityIds(context interface{}, query *proto.EntityIdsQuery, reply *proto.QueryResult) error {
qr, err := vtg.resolver.ExecuteEntityIds(context, query)
if err == nil {
proto.PopulateQueryResult(qr, reply)
} else {
reply.Error = err.Error()
log.Errorf("ExecuteEntityIds: %v, query: %+v", err, query)
}
reply.Session = query.Session
return nil
}
// ExecuteBatchShard executes a group of queries on the specified shards.
func (vtg *VTGate) ExecuteBatchShard(context interface{}, batchQuery *proto.BatchQueryShard, reply *proto.QueryResultList) error {
qrs, err := vtg.resolver.ExecuteBatch(
context,
batchQuery.Queries,
batchQuery.Keyspace,
batchQuery.TabletType,
batchQuery.Session,
func(keyspace string) ([]string, error) {
return batchQuery.Shards, nil
},
)
if err == nil {
reply.List = qrs.List
} else {
reply.Error = err.Error()
log.Errorf("ExecuteBatchShard: %v, queries: %+v", err, batchQuery)
}
reply.Session = batchQuery.Session
return nil
}
// ExecuteBatchKeyspaceIds executes a group of queries based on the specified keyspace ids.
func (vtg *VTGate) ExecuteBatchKeyspaceIds(context interface{}, query *proto.KeyspaceIdBatchQuery, reply *proto.QueryResultList) error {
qrs, err := vtg.resolver.ExecuteBatchKeyspaceIds(
context,
query)
if err == nil {
reply.List = qrs.List
} else {
reply.Error = err.Error()
log.Errorf("ExecuteBatchKeyspaceIds: %v, query: %+v", err, query)
}
reply.Session = query.Session
return nil
}
// StreamExecuteKeyspaceIds executes a streaming query on the specified KeyspaceIds.
// The KeyspaceIds are resolved to shards using the serving graph.
// This function currently temporarily enforces the restriction of executing on
// one shard since it cannot merge-sort the results to guarantee ordering of
// response which is needed for checkpointing.
// The api supports supplying multiple KeyspaceIds to make it future proof.
func (vtg *VTGate) StreamExecuteKeyspaceIds(context interface{}, query *proto.KeyspaceIdQuery, sendReply func(*proto.QueryResult) error) error {
err := vtg.resolver.StreamExecuteKeyspaceIds(
context,
query,
func(mreply *mproto.QueryResult) error {
reply := new(proto.QueryResult)
proto.PopulateQueryResult(mreply, reply)
// Note we don't populate reply.Session here,
// as it may change incrementaly as responses are sent.
return sendReply(reply)
})
if err != nil {
log.Errorf("StreamExecuteKeyspaceIds: %v, query: %+v", err, query)
}
// now we can send the final Sessoin info.
if query.Session != nil {
sendReply(&proto.QueryResult{Session: query.Session})
}
return err
}
// StreamExecuteKeyRanges executes a streaming query on the specified KeyRanges.
// The KeyRanges are resolved to shards using the serving graph.
// This function currently temporarily enforces the restriction of executing on
// one shard since it cannot merge-sort the results to guarantee ordering of
// response which is needed for checkpointing.
// The api supports supplying multiple keyranges to make it future proof.
func (vtg *VTGate) StreamExecuteKeyRanges(context interface{}, query *proto.KeyRangeQuery, sendReply func(*proto.QueryResult) error) error {
err := vtg.resolver.StreamExecuteKeyRanges(
context,
query,
func(mreply *mproto.QueryResult) error {
reply := new(proto.QueryResult)
proto.PopulateQueryResult(mreply, reply)
// Note we don't populate reply.Session here,
// as it may change incrementaly as responses are sent.
return sendReply(reply)
})
if err != nil {
log.Errorf("StreamExecuteKeyRange: %v, query: %+v", err, query)
}
// now we can send the final Session info.
if query.Session != nil {
sendReply(&proto.QueryResult{Session: query.Session})
}
return err
}
// StreamExecuteShard executes a streaming query on the specified shards.
func (vtg *VTGate) StreamExecuteShard(context interface{}, query *proto.QueryShard, sendReply func(*proto.QueryResult) error) error {
err := vtg.resolver.StreamExecute(
context,
query.Sql,
query.BindVariables,
query.Keyspace,
query.TabletType,
query.Session,
func(keyspace string) ([]string, error) {
return query.Shards, nil
},
func(mreply *mproto.QueryResult) error {
reply := new(proto.QueryResult)
proto.PopulateQueryResult(mreply, reply)
// Note we don't populate reply.Session here,
// as it may change incrementaly as responses
// are sent.
return sendReply(reply)
})
if err != nil {
log.Errorf("StreamExecuteShard: %v, query: %+v", err, query)
}
// now we can send the final Session info.
if query.Session != nil {
sendReply(&proto.QueryResult{Session: query.Session})
}
return err
}
// Begin begins a transaction. It has to be concluded by a Commit or Rollback.
func (vtg *VTGate) Begin(context interface{}, outSession *proto.Session) error {
outSession.InTransaction = true
return nil
}
// Commit commits a transaction.
func (vtg *VTGate) Commit(context interface{}, inSession *proto.Session) error {
return vtg.resolver.Commit(context, inSession)
}
// Rollback rolls back a transaction.
func (vtg *VTGate) Rollback(context interface{}, inSession *proto.Session) error {
return vtg.resolver.Rollback(context, inSession)
}