-
Notifications
You must be signed in to change notification settings - Fork 264
/
protocol.go
576 lines (443 loc) · 12.6 KB
/
protocol.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright 2021 Matrix Origin
//
// 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 frontend
import (
"context"
"fmt"
"math"
"sync"
"sync/atomic"
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/vm/process"
"github.com/fagongzi/goetty/v2"
"github.com/matrixorigin/matrixone/pkg/logutil"
)
// Response Categories
const (
// OkResponse OK message
OkResponse = iota
// ErrorResponse Error message
ErrorResponse
// EoFResponse EOF message
EoFResponse
// ResultResponse result message
ResultResponse
// LocalInfileRequest local infile message
LocalInfileRequest
)
type Request struct {
//the command type from the client
cmd CommandType
// sequence num
seq uint8
//the data from the client
data interface{}
}
func (req *Request) GetData() interface{} {
return req.data
}
func (req *Request) SetData(data interface{}) {
req.data = data
}
func (req *Request) GetCmd() CommandType {
return req.cmd
}
func (req *Request) SetCmd(cmd CommandType) {
req.cmd = cmd
}
type Response struct {
//the category of the response
category int
//the status of executing the peer request
status uint16
//the command type which generates the response
cmd int
//the data of the response
data interface{}
/*
ok response
*/
affectedRows, lastInsertId uint64
warnings uint16
}
func NewResponse(category int, affectedRows, lastInsertId uint64, warnings, status uint16, cmd int, d interface{}) *Response {
return &Response{
category: category,
affectedRows: affectedRows,
lastInsertId: lastInsertId,
warnings: warnings,
status: status,
cmd: cmd,
data: d,
}
}
func NewGeneralErrorResponse(cmd CommandType, status uint16, err error) *Response {
return NewResponse(ErrorResponse, 0, 0, 0, status, int(cmd), err)
}
func NewGeneralOkResponse(cmd CommandType, status uint16) *Response {
return NewResponse(OkResponse, 0, 0, 0, status, int(cmd), nil)
}
func NewOkResponse(affectedRows, lastInsertId uint64, warnings, status uint16, cmd int, d interface{}) *Response {
return NewResponse(OkResponse, affectedRows, lastInsertId, warnings, status, cmd, d)
}
func (resp *Response) GetData() interface{} {
return resp.data
}
func (resp *Response) SetData(data interface{}) {
resp.data = data
}
func (resp *Response) GetStatus() uint16 {
return resp.status
}
func (resp *Response) SetStatus(status uint16) {
resp.status = status
}
func (resp *Response) GetCategory() int {
return resp.category
}
func (resp *Response) SetCategory(category int) {
resp.category = category
}
type Protocol interface {
IsEstablished() bool
SetEstablished()
// GetRequest gets Request from Packet
GetRequest(payload []byte) *Request
// SendResponse sends a response to the client for the application request
SendResponse(context.Context, *Response) error
// ConnectionID the identity of the client
ConnectionID() uint32
// Peer gets the address [Host:Port,Host:Port] of the client and the server
Peer() string
GetDatabaseName() string
SetDatabaseName(string)
GetUserName() string
SetUserName(string)
GetSequenceId() uint8
SetSequenceID(value uint8)
GetDebugString() string
GetTcpConnection() goetty.IOSession
GetCapability() uint32
SetCapability(uint32)
GetConnectAttrs() map[string]string
IsTlsEstablished() bool
SetTlsEstablished()
HandleHandshake(ctx context.Context, payload []byte) (bool, error)
Authenticate(ctx context.Context) error
SendPrepareResponse(ctx context.Context, stmt *PrepareStmt) error
Quit()
incDebugCount(int)
resetDebugCount() []uint64
UpdateCtx(context.Context)
}
type ProtocolImpl struct {
m sync.Mutex
io IOPackage
tcpConn goetty.IOSession
quit atomic.Bool
//random bytes
salt []byte
//the id of the connection
connectionID uint32
// whether the handshake succeeded
established atomic.Bool
// whether the tls handshake succeeded
tlsEstablished atomic.Bool
//The sequence-id is incremented with each packet and may wrap around.
//It starts at 0 and is reset to 0 when a new command begins in the Command Phase.
sequenceId atomic.Uint32
//for debug
debugCount [16]uint64
ctx context.Context
}
func (pi *ProtocolImpl) UpdateCtx(ctx context.Context) {
pi.ctx = ctx
}
func (pi *ProtocolImpl) incDebugCount(i int) {
if i >= 0 && i < len(pi.debugCount) {
atomic.AddUint64(&pi.debugCount[i], 1)
}
}
func (pi *ProtocolImpl) resetDebugCount() []uint64 {
ret := make([]uint64, len(pi.debugCount))
for i := 0; i < len(pi.debugCount); i++ {
ret[i] = atomic.LoadUint64(&pi.debugCount[i])
}
return ret
}
func (pi *ProtocolImpl) setQuit(b bool) bool {
return pi.quit.Swap(b)
}
func (pi *ProtocolImpl) GetSequenceId() uint8 {
return uint8(pi.sequenceId.Load())
}
func (pi *ProtocolImpl) getDebugStringUnsafe() string {
if pi.tcpConn != nil {
return fmt.Sprintf("connectionId %d|%s", pi.connectionID, pi.tcpConn.RemoteAddress())
}
return ""
}
func (pi *ProtocolImpl) GetDebugString() string {
pi.m.Lock()
defer pi.m.Unlock()
return pi.getDebugStringUnsafe()
}
func (pi *ProtocolImpl) GetSalt() []byte {
pi.m.Lock()
defer pi.m.Unlock()
return pi.salt
}
// SetSalt updates the salt value. This happens with proxy mode enabled.
func (pi *ProtocolImpl) SetSalt(s []byte) {
pi.m.Lock()
defer pi.m.Unlock()
pi.salt = s
}
func (pi *ProtocolImpl) IsEstablished() bool {
return pi.established.Load()
}
func (pi *ProtocolImpl) SetEstablished() {
logDebugf(pi.GetDebugString(), "SWITCH ESTABLISHED to true")
pi.established.Store(true)
}
func (pi *ProtocolImpl) IsTlsEstablished() bool {
return pi.tlsEstablished.Load()
}
func (pi *ProtocolImpl) SetTlsEstablished() {
logutil.Debugf("SWITCH TLS_ESTABLISHED to true")
pi.tlsEstablished.Store(true)
}
func (pi *ProtocolImpl) ConnectionID() uint32 {
return pi.connectionID
}
// Quit kill tcpConn still connected.
// before calling NewMysqlClientProtocol, tcpConn.Connected() must be true
// please check goetty/application.go::doStart() and goetty/application.go::NewIOSession(...) for details
func (pi *ProtocolImpl) Quit() {
//if it was quit, do nothing
if pi.setQuit(true) {
return
}
if pi.tcpConn != nil {
if err := pi.tcpConn.Disconnect(); err != nil {
return
}
}
//release salt
if pi.salt != nil {
pi.salt = nil
}
}
func (pi *ProtocolImpl) GetTcpConnection() goetty.IOSession {
return pi.tcpConn
}
func (pi *ProtocolImpl) Peer() string {
tcp := pi.GetTcpConnection()
if tcp == nil {
return ""
}
return tcp.RemoteAddress()
}
func (mp *MysqlProtocolImpl) GetRequest(payload []byte) *Request {
req := &Request{
cmd: CommandType(payload[0]),
data: payload[1:],
}
return req
}
func (mp *MysqlProtocolImpl) SendResponse(ctx context.Context, resp *Response) error {
//move here to prohibit potential recursive lock
var attachAbort string
mp.m.Lock()
defer mp.m.Unlock()
switch resp.category {
case OkResponse:
s, ok := resp.data.(string)
if !ok {
return mp.sendOKPacket(resp.affectedRows, resp.lastInsertId, uint16(resp.status), resp.warnings, "")
}
return mp.sendOKPacket(resp.affectedRows, resp.lastInsertId, uint16(resp.status), resp.warnings, s)
case EoFResponse:
return mp.sendEOFPacket(0, uint16(resp.status))
case ErrorResponse:
err := resp.data.(error)
if err == nil {
return mp.sendOKPacket(0, 0, uint16(resp.status), 0, "")
}
switch myerr := err.(type) {
case *moerr.Error:
var code uint16
if myerr.MySQLCode() != moerr.ER_UNKNOWN_ERROR {
code = myerr.MySQLCode()
} else {
code = myerr.ErrorCode()
}
errMsg := myerr.Error()
if attachAbort != "" {
errMsg = fmt.Sprintf("%s\n%s", myerr.Error(), attachAbort)
}
return mp.sendErrPacket(code, myerr.SqlState(), errMsg)
}
errMsg := ""
if attachAbort != "" {
errMsg = fmt.Sprintf("%s\n%s", err, attachAbort)
} else {
errMsg = fmt.Sprintf("%v", err)
}
return mp.sendErrPacket(moerr.ER_UNKNOWN_ERROR, DefaultMySQLState, errMsg)
case ResultResponse:
mer := resp.data.(*MysqlExecutionResult)
if mer == nil {
return mp.sendOKPacket(0, 0, uint16(resp.status), 0, "")
}
if mer.Mrs() == nil {
return mp.sendOKPacket(mer.AffectedRows(), mer.InsertID(), uint16(resp.status), mer.Warnings(), "")
}
return mp.sendResultSet(ctx, mer.Mrs(), resp.cmd, mer.Warnings(), uint16(resp.status))
case LocalInfileRequest:
s, _ := resp.data.(string)
return mp.sendLocalInfileRequest(s)
default:
return moerr.NewInternalError(ctx, "unsupported response:%d ", resp.category)
}
}
func (mp *MysqlProtocolImpl) DisableAutoFlush() {
mp.disableAutoFlush = true
}
func (mp *MysqlProtocolImpl) EnableAutoFlush() {
mp.disableAutoFlush = false
}
func (mp *MysqlProtocolImpl) Flush() error {
return nil
}
var _ MysqlProtocol = &FakeProtocol{}
const (
fakeConnectionID uint32 = math.MaxUint32
)
// FakeProtocol works for the background transaction that does not use the network protocol.
type FakeProtocol struct {
username string
database string
ioses goetty.IOSession
}
func (fp *FakeProtocol) UpdateCtx(ctx context.Context) {
}
func (fp *FakeProtocol) GetCapability() uint32 {
return DefaultCapability
}
func (fp *FakeProtocol) SetCapability(uint32) {
}
func (fp *FakeProtocol) IsTlsEstablished() bool {
return true
}
func (fp *FakeProtocol) SetTlsEstablished() {
}
func (fp *FakeProtocol) HandleHandshake(ctx context.Context, payload []byte) (bool, error) {
return false, nil
}
func (fp *FakeProtocol) Authenticate(ctx context.Context) error {
return nil
}
func (fp *FakeProtocol) GetTcpConnection() goetty.IOSession {
return fp.ioses
}
func (fp *FakeProtocol) GetDebugString() string {
return "fake protocol"
}
func (fp *FakeProtocol) GetSequenceId() uint8 {
return 0
}
func (fp *FakeProtocol) SetSequenceID(value uint8) {
}
func (fp *FakeProtocol) GetConnectAttrs() map[string]string {
return nil
}
func (fp *FakeProtocol) SendPrepareResponse(ctx context.Context, stmt *PrepareStmt) error {
return nil
}
func (fp *FakeProtocol) ParseSendLongData(ctx context.Context, proc *process.Process, stmt *PrepareStmt, data []byte, pos int) error {
return nil
}
func (fp *FakeProtocol) ParseExecuteData(ctx context.Context, proc *process.Process, stmt *PrepareStmt, data []byte, pos int) error {
return nil
}
func (fp *FakeProtocol) SendResultSetTextBatchRow(mrs *MysqlResultSet, cnt uint64) error {
return nil
}
func (fp *FakeProtocol) SendResultSetTextBatchRowSpeedup(mrs *MysqlResultSet, cnt uint64) error {
return nil
}
func (fp *FakeProtocol) SendColumnDefinitionPacket(ctx context.Context, column Column, cmd int) error {
return nil
}
func (fp *FakeProtocol) SendColumnCountPacket(count uint64) error {
return nil
}
func (fp *FakeProtocol) SendEOFPacketIf(warnings uint16, status uint16) error {
return nil
}
func (fp *FakeProtocol) sendOKPacket(affectedRows uint64, lastInsertId uint64, status uint16, warnings uint16, message string) error {
return nil
}
func (fp *FakeProtocol) sendEOFOrOkPacket(warnings uint16, status uint16) error {
return nil
}
func (fp *FakeProtocol) ResetStatistics() {}
func (fp *FakeProtocol) GetStats() string {
return ""
}
func (fp *FakeProtocol) CalculateOutTrafficBytes(reset bool) (int64, int64) { return 0, 0 }
func (fp *FakeProtocol) IsEstablished() bool {
return true
}
func (fp *FakeProtocol) SetEstablished() {}
func (fp *FakeProtocol) GetRequest(payload []byte) *Request {
return nil
}
func (fp *FakeProtocol) SendResponse(ctx context.Context, resp *Response) error {
return nil
}
func (fp *FakeProtocol) ConnectionID() uint32 {
return fakeConnectionID
}
func (fp *FakeProtocol) Peer() string {
return "0.0.0.0:0"
}
func (fp *FakeProtocol) GetDatabaseName() string {
return fp.database
}
func (fp *FakeProtocol) SetDatabaseName(s string) {
fp.database = s
}
func (fp *FakeProtocol) GetUserName() string {
return fp.username
}
func (fp *FakeProtocol) SetUserName(s string) {
fp.username = s
}
func (fp *FakeProtocol) Quit() {}
func (fp *FakeProtocol) sendLocalInfileRequest(filename string) error {
return nil
}
func (fp *FakeProtocol) incDebugCount(int) {}
func (fp *FakeProtocol) resetDebugCount() []uint64 {
return nil
}
func (fp *FakeProtocol) DisableAutoFlush() {
}
func (fp *FakeProtocol) EnableAutoFlush() {
}
func (fp *FakeProtocol) Flush() error {
return nil
}