This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gateway.go
531 lines (459 loc) · 13 KB
/
gateway.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package grpcsql
import (
"database/sql/driver"
"fmt"
"io"
"sync"
"time"
"github.com/CanonicalLtd/go-grpc-sql/internal/protocol"
"github.com/CanonicalLtd/go-sqlite3"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
// Gateway mapping gRPC requests to SQL queries.
type Gateway struct {
driver driver.Driver // Underlying SQL driver.
conn *gatewayConn
}
// NewGateway creates a new gRPC gateway executing requests against the given
// SQL driver.
func NewGateway(drv driver.Driver) *Gateway {
return &Gateway{
driver: drv,
conn: &gatewayConn{driver: drv},
}
}
// Conn creates a new database connection using the underlying driver, and
// start accepting requests for it.
func (s *Gateway) Conn(stream protocol.SQL_ConnServer) error {
defer s.conn.rollback()
for {
request, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return errors.Wrapf(err, "failed to receive request")
}
response, err := s.conn.handle(request)
if err != nil {
// TODO: add support for more driver-specific errors.
switch err := err.(type) {
case sqlite3.Error:
response = protocol.NewResponseSQLiteError(
err.Code, err.ExtendedCode, err.Error())
case sqlite3.ErrNo:
response = protocol.NewResponseSQLiteError(
err, 0, err.Error())
default:
return errors.Wrapf(err, "failed to handle %s request", request.Code)
}
}
if err := stream.Send(response); err != nil {
return errors.Wrapf(err, "failed to send response")
}
}
}
// Track a single driver connection
type gatewayConn struct {
driver driver.Driver
driverConn driver.Conn
stmts map[int64]driver.Stmt
txs map[int64]driver.Tx
rows map[int64]driver.Rows
serial int64
mu sync.Mutex
refcount int
txCh chan struct{}
}
// Handle a single gRPC request for this connection.
func (c *gatewayConn) handle(request *protocol.Request) (*protocol.Response, error) {
var message proto.Message
switch request.Code {
case protocol.RequestCode_OPEN:
message = &protocol.RequestOpen{}
case protocol.RequestCode_PREPARE:
message = &protocol.RequestPrepare{}
case protocol.RequestCode_EXEC:
message = &protocol.RequestExec{}
case protocol.RequestCode_QUERY:
message = &protocol.RequestQuery{}
case protocol.RequestCode_NEXT:
message = &protocol.RequestNext{}
case protocol.RequestCode_COLUMN_TYPE_SCAN_TYPE:
message = &protocol.RequestColumnTypeScanType{}
case protocol.RequestCode_COLUMN_TYPE_DATABASE_TYPE_NAME:
message = &protocol.RequestColumnTypeDatabaseTypeName{}
case protocol.RequestCode_ROWS_CLOSE:
message = &protocol.RequestRowsClose{}
case protocol.RequestCode_STMT_CLOSE:
message = &protocol.RequestStmtClose{}
case protocol.RequestCode_BEGIN:
message = &protocol.RequestBegin{}
case protocol.RequestCode_COMMIT:
message = &protocol.RequestCommit{}
case protocol.RequestCode_ROLLBACK:
message = &protocol.RequestRollback{}
case protocol.RequestCode_CLOSE:
message = &protocol.RequestClose{}
case protocol.RequestCode_CONN_EXEC:
message = &protocol.RequestConnExec{}
default:
return nil, fmt.Errorf("invalid request code %d", request.Code)
}
if err := proto.Unmarshal(request.Data, message); err != nil {
return nil, errors.Wrapf(err, "request parse error")
}
// The very first request must be an OPEN one.
if c.driverConn == nil && request.Code != protocol.RequestCode_OPEN {
return nil, fmt.Errorf("expected OPEN request, got %s", request.Code)
}
switch r := message.(type) {
case *protocol.RequestOpen:
return c.handleOpen(r)
case *protocol.RequestPrepare:
return c.handlePrepare(r)
case *protocol.RequestExec:
return c.handleExec(r)
case *protocol.RequestQuery:
return c.handleQuery(r)
case *protocol.RequestNext:
return c.handleNext(r)
case *protocol.RequestColumnTypeScanType:
return c.handleColumnTypeScanType(r)
case *protocol.RequestColumnTypeDatabaseTypeName:
return c.handleColumnTypeDatabaseTypeName(r)
case *protocol.RequestRowsClose:
return c.handleRowsClose(r)
case *protocol.RequestStmtClose:
return c.handleStmtClose(r)
case *protocol.RequestBegin:
return c.handleBegin(r)
case *protocol.RequestCommit:
return c.handleCommit(r)
case *protocol.RequestRollback:
return c.handleRollback(r)
case *protocol.RequestClose:
return c.handleClose(r)
case *protocol.RequestConnExec:
return c.handleConnExec(r)
default:
panic("unhandled request payload type")
}
}
// Handle a request of type OPEN.
func (c *gatewayConn) handleOpen(request *protocol.RequestOpen) (*protocol.Response, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.refcount > 0 {
c.refcount++
return protocol.NewResponseOpen(), nil
}
driverConn, err := c.driver.Open(request.Name)
if err != nil {
return nil, fmt.Errorf("could not open driver connection: %v", err)
}
c.driverConn = driverConn
c.stmts = make(map[int64]driver.Stmt)
c.txs = make(map[int64]driver.Tx)
c.rows = make(map[int64]driver.Rows)
c.refcount++
response := protocol.NewResponseOpen()
return response, nil
}
func (c *gatewayConn) abort() {
for id, rows := range c.rows {
rows.Close()
delete(c.rows, id)
}
for id, stmt := range c.stmts {
stmt.Close()
delete(c.stmts, id)
}
}
// Handle a request of type PREPARE.
func (c *gatewayConn) handlePrepare(request *protocol.RequestPrepare) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverStmt, err := c.driverConn.Prepare(request.Query)
if err != nil {
return nil, err
}
c.serial++
c.stmts[c.serial] = driverStmt
return protocol.NewResponsePrepare(c.serial, driverStmt.NumInput()), nil
}
// Handle a request of type EXEC.
func (c *gatewayConn) handleExec(request *protocol.RequestExec) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverStmt, ok := c.stmts[request.Id]
if !ok {
return nil, fmt.Errorf("no prepared statement with ID %d", request.Id)
}
args, err := protocol.ToDriverValues(request.Args)
if err != nil {
return nil, err
}
result, err := driverStmt.Exec(args)
if err != nil {
return nil, err
}
lastInsertID, err := result.LastInsertId()
if err != nil {
return nil, err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return nil, err
}
response := protocol.NewResponseExec(lastInsertID, rowsAffected)
return response, nil
}
// Handle a request of type QUERY.
func (c *gatewayConn) handleQuery(request *protocol.RequestQuery) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverStmt, ok := c.stmts[request.Id]
if !ok {
return nil, fmt.Errorf("no prepared statement with ID %d", request.Id)
}
args, err := protocol.ToDriverValues(request.Args)
if err != nil {
return nil, err
}
driverRows, err := driverStmt.Query(args)
if err != nil {
return nil, err
}
c.serial++
c.rows[c.serial] = driverRows
return protocol.NewResponseQuery(c.serial, driverRows.Columns()), nil
}
// Handle a request of type NEXT.
func (c *gatewayConn) handleNext(request *protocol.RequestNext) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverRows, ok := c.rows[request.Id]
if !ok {
return nil, fmt.Errorf("no rows with ID %d", request.Id)
}
dest := make([]driver.Value, int(request.Len))
err := driverRows.Next(dest)
if err == io.EOF {
return protocol.NewResponseNext(true, nil), nil
}
if err != nil {
return nil, err
}
values, err := protocol.FromDriverValues(dest)
if err != nil {
return nil, err
}
return protocol.NewResponseNext(false, values), nil
}
// Handle a request of type COLUMN_TYPE_SCAN_TYPE.
func (c *gatewayConn) handleColumnTypeScanType(request *protocol.RequestColumnTypeScanType) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverRows, ok := c.rows[request.Id]
if !ok {
return nil, fmt.Errorf("no rows with ID %d", request.Id)
}
code := protocol.ValueCode_BYTES
typeScanner, ok := driverRows.(driver.RowsColumnTypeScanType)
if ok {
typ := typeScanner.ColumnTypeScanType(int(request.Column))
code = protocol.ToValueCode(typ)
}
return protocol.NewResponseColumnTypeScanType(code), nil
}
// Handle a request of type COLUMN_TYPE_DATABASE_TYPE_NAME.
func (c *gatewayConn) handleColumnTypeDatabaseTypeName(request *protocol.RequestColumnTypeDatabaseTypeName) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverRows, ok := c.rows[request.Id]
if !ok {
return nil, fmt.Errorf("no rows with ID %d", request.Id)
}
name := ""
nameScanner, ok := driverRows.(driver.RowsColumnTypeDatabaseTypeName)
if ok {
name = nameScanner.ColumnTypeDatabaseTypeName(int(request.Column))
}
return protocol.NewResponseColumnTypeDatabaseTypeName(name), nil
}
// Handle a request of type ROWS_CLOSE.
func (c *gatewayConn) handleRowsClose(request *protocol.RequestRowsClose) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverRows, ok := c.rows[request.Id]
if !ok {
return nil, fmt.Errorf("no rows with ID %d", request.Id)
}
delete(c.rows, request.Id)
if err := driverRows.Close(); err != nil {
return nil, err
}
response := protocol.NewResponseRowsClose()
return response, nil
}
// Handle a request of type STMT_CLOSE.
func (c *gatewayConn) handleStmtClose(request *protocol.RequestStmtClose) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverStmt, ok := c.stmts[request.Id]
if !ok {
return nil, fmt.Errorf("no prepared statement with ID %d", request.Id)
}
delete(c.stmts, request.Id)
if err := driverStmt.Close(); err != nil {
return nil, err
}
response := protocol.NewResponseStmtClose()
return response, nil
}
// Handle a request of type BEGIN.
func (c *gatewayConn) handleBegin(request *protocol.RequestBegin) (*protocol.Response, error) {
c.mu.Lock()
if len(c.txs) != 0 {
c.mu.Unlock()
return nil, fmt.Errorf("transaction in progress")
}
driverTx, err := c.driverConn.Begin()
if err != nil {
c.mu.Unlock()
return nil, err
}
c.serial++
c.txs[c.serial] = driverTx
c.txCh = make(chan struct{})
// Kill the transaction after a fixed timeout.
go func(tx driver.Tx, serial int64, ch chan struct{}) {
select {
case <-ch:
case <-time.After(5 * time.Second):
c.rollback()
}
}(driverTx, c.serial, c.txCh)
return protocol.NewResponseBegin(c.serial), nil
}
// Handle a request of type COMMIT.
func (c *gatewayConn) handleCommit(request *protocol.RequestCommit) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverTx, ok := c.txs[request.Id]
if !ok {
return nil, fmt.Errorf("no transaction with ID %d", request.Id)
}
delete(c.txs, request.Id)
defer func() {
c.abort()
close(c.txCh)
c.mu.Unlock()
}()
if err := driverTx.Commit(); err != nil {
return nil, err
}
response := protocol.NewResponseCommit()
return response, nil
}
// Handle a request of type ROLLBACK.
func (c *gatewayConn) handleRollback(request *protocol.RequestRollback) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
driverTx, ok := c.txs[request.Id]
if !ok {
c.abort()
return nil, fmt.Errorf("no transaction with ID %d", request.Id)
}
delete(c.txs, request.Id)
defer func() {
c.abort()
close(c.txCh)
c.mu.Unlock()
}()
if err := driverTx.Rollback(); err != nil {
return nil, err
}
response := protocol.NewResponseRollback()
return response, nil
}
func (c *gatewayConn) rollback() {
if len(c.txs) > 1 {
panic("multiple transactions detected")
}
if len(c.txs) == 0 {
// nothing to do
return
}
for id, tx := range c.txs {
c.abort()
delete(c.txs, id)
tx.Rollback()
close(c.txCh)
c.mu.Unlock()
}
}
// Handle a request of type CLOSE.
func (c *gatewayConn) handleClose(request *protocol.RequestClose) (*protocol.Response, error) {
c.mu.Lock()
defer c.mu.Unlock()
c.refcount--
if c.refcount == 0 {
c.rollback()
conn := c.driverConn
c.driverConn = nil
c.txs = nil
c.stmts = nil
c.rows = nil
if err := conn.Close(); err != nil {
return nil, err
}
}
response := protocol.NewResponseClose()
return response, nil
}
// Handle a request of type CONN_EXEC.
func (c *gatewayConn) handleConnExec(request *protocol.RequestConnExec) (*protocol.Response, error) {
if len(c.txs) != 1 {
return nil, fmt.Errorf("not in a transaction")
}
args, err := protocol.ToDriverValues(request.Args)
if err != nil {
c.abort()
return nil, err
}
execer, ok := c.driverConn.(driver.Execer)
if !ok {
c.abort()
return nil, fmt.Errorf("backend driver does not implement driver.Execer")
}
result, err := execer.Exec(request.Query, args)
if err != nil {
c.abort()
return nil, err
}
lastInsertID, err := result.LastInsertId()
if err != nil {
c.abort()
return nil, err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
c.abort()
return nil, err
}
response := protocol.NewResponseExec(lastInsertID, rowsAffected)
return response, nil
}