forked from mattrobenolt/ps-http-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
539 lines (468 loc) · 14.4 KB
/
main.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
"unicode"
"connectrpc.com/connect"
gonanoid "github.com/matoous/go-nanoid/v2"
"github.com/planetscale/log"
"github.com/planetscale/psdb/auth"
psdbv1alpha1 "github.com/planetscale/psdb/types/psdb/v1alpha1"
"github.com/planetscale/psdb/types/psdb/v1alpha1/psdbv1alpha1connect"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqlescape"
"vitess.io/vitess/go/sqltypes"
"github.com/mattrobenolt/ps-http-sim/internal/session"
"github.com/mattrobenolt/ps-http-sim/internal/vitess"
)
var (
connPool = map[mysqlConnKey]*timedConn{}
connMu sync.RWMutex
)
type mysqlConnKey struct {
username, pass, dbname, session string
}
type timedConn struct {
*mysql.Conn
key mysqlConnKey
mu sync.Mutex
timer *time.Timer
}
var (
commandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagListenAddr = commandLine.String("listen-addr", "127.0.0.1", "HTTP server address")
flagListenPort = commandLine.Uint("listen-port", 8080, "HTTP server port")
flagMySQLAddr = commandLine.String("mysql-addr", "127.0.0.1", "MySQL address")
flagMySQLPort = commandLine.Uint("mysql-port", 3306, "MySQL port")
flagMySQLNoPass = commandLine.Bool("mysql-no-pass", false, "Don't use password for MySQL connection")
flagMySQLIdleTimeout = commandLine.Duration("mysql-idle-timeout", 10*time.Second, "MySQL connection idle timeout")
flagMySQLMaxRows = commandLine.Uint("mysql-max-rows", 1000, "Max rows for a single query result set")
flagMySQLDbname = commandLine.String("mysql-dbname", "mysql", "MySQL database to connect to")
flagMySQLListenPort = commandLine.Uint("mysql-listen-port", 0, "Run a TCP proxy back to the underlying MySQL server")
)
var errSessionInUse = errors.New("session already in use")
// getConn gets or dials a connection from the connection pool
// connections are maintained unique for credential combos and session id
// since this isn't meant to truly represent reality, it's possible you
// can do things with connections locally by munging session ids or auth
// that aren't allowed on PlanetScale. This is meant to just mimic the public API.
func getConn(ctx context.Context, uname, pass, dbname, session string) (*timedConn, error) {
key := mysqlConnKey{uname, pass, dbname, session}
// check first if there's already a connection
connMu.RLock()
if conn, ok := connPool[key]; ok {
defer connMu.RUnlock()
if conn.mu.TryLock() {
// if we already have a timer on this connection, stop it
// and remove it, because this connection is checked out
// we don't want to start the timer until it's returned back
// to the pool, therefore considering it "idle" after it's
// unused.
if conn.timer != nil {
conn.timer.Stop()
conn.timer = nil
}
return conn, nil
} else {
return nil, errSessionInUse
}
}
connMu.RUnlock()
// if not, dial for a new connection
// without a lock, so parallel dials can happen
rawConn, err := dial(ctx, uname, pass, dbname)
if err != nil {
return nil, err
}
// lock to write to map
connMu.Lock()
connPool[key] = &timedConn{Conn: rawConn, key: key}
connMu.Unlock()
// since it was parallel, the last one would have won and been written
// so re-read back so we use the conn that was actually stored in the pool
return getConn(ctx, uname, pass, dbname, session)
}
func returnConn(conn *timedConn) {
// set idle timeout when the connection is returned to the pool
conn.timer = time.AfterFunc(*flagMySQLIdleTimeout, func() {
closeIdleConn(conn.key)
})
conn.mu.Unlock()
}
func closeIdleConn(key mysqlConnKey) {
logger.Debug("closing idle connection",
log.String("username", key.username),
log.String("session_id", key.session),
)
closeConn(key)
}
func closeConn(key mysqlConnKey) {
connMu.Lock()
if conn, ok := connPool[key]; ok {
conn.Close()
if conn.timer != nil {
conn.timer.Stop()
}
delete(connPool, key)
}
connMu.Unlock()
}
// dial connects to the underlying MySQL server, and switches to the underlying
// database automatically.
func dial(ctx context.Context, uname, pass, dbname string) (*mysql.Conn, error) {
if *flagMySQLNoPass {
pass = ""
}
conn, err := mysql.Connect(ctx, &mysql.ConnParams{
Host: *flagMySQLAddr,
Port: int(*flagMySQLPort),
Uname: uname,
Pass: pass,
})
if err != nil {
return nil, err
}
if dbname != "" {
if _, err := conn.ExecuteFetch("USE "+sqlescape.EscapeID(dbname), 1, false); err != nil {
conn.Close()
return nil, err
}
}
return conn, nil
}
func runTCPProxy() error {
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *flagListenAddr, *flagMySQLListenPort))
if err != nil {
return err
}
mysqlAddr := fmt.Sprintf("%s:%d", *flagMySQLAddr, *flagMySQLPort)
go func() {
for {
clientConn, err := l.Accept()
if err != nil {
logger.Error("failed to accept TCP connection", log.Error(err))
}
start := time.Now()
ll := logger.With(
log.String("client_addr", clientConn.RemoteAddr().String()),
)
ll.Info("new MySQL connection")
go func() {
defer func() {
clientConn.Close()
ll.Info("client disconnected",
log.Duration("duration", time.Since(start)),
)
}()
upstreamConn, err := net.Dial("tcp", mysqlAddr)
if err != nil {
logger.Error("failed to connect", log.Error(err))
return
}
defer upstreamConn.Close()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer func() {
upstreamConn.Close()
wg.Done()
}()
if _, err := io.Copy(upstreamConn, clientConn); err != nil {
ll.Warn(err.Error(), log.String("side", "server"))
}
}()
go func() {
defer func() {
clientConn.Close()
wg.Done()
}()
if _, err := io.Copy(clientConn, upstreamConn); err != nil {
// client disconnected, so just debug level
ll.Debug(err.Error(), log.String("side", "client"))
}
}()
wg.Wait()
}()
}
}()
return nil
}
func isQueryFiltered(query string) bool {
const minLength = 4
if len(query) < minLength {
return false
}
query = strings.TrimSpace(query)
if len(query) < minLength {
return false
}
// we want to explicitly filter out `SET @@boost_cached_queries = true` for now
if strings.EqualFold(query[:minLength], "set ") {
query = strings.TrimLeftFunc(query[minLength:], unicode.IsSpace)
return strings.HasPrefix(query, "@@boost_cached_queries")
}
// prevent any special `USE @replica` since that isn't going to work locally
if strings.EqualFold(query[:minLength], "use ") {
query = strings.TrimLeftFunc(query[minLength:], unicode.IsSpace)
return strings.ContainsRune(query, '@')
}
return false
}
func init() {
// Vitess doesn't play nicely, so replace the entire default flagset
flag.CommandLine = commandLine
flag.Parse()
}
var logger *log.Logger
func main() {
cfg := log.NewPlanetScaleConfig("pretty", log.DebugLevel)
logger, _ = cfg.Build()
defer logger.Sync()
mux := http.NewServeMux()
mux.Handle(psdbv1alpha1connect.NewDatabaseHandler(server{}))
if *flagMySQLListenPort > 0 {
logger.Info("running MySQL proxy",
log.String("addr", *flagListenAddr),
log.Uint("port", *flagMySQLListenPort),
)
if err := runTCPProxy(); err != nil {
logger.Panic("failed to start MySQL proxy", log.Error(err))
}
}
logger.Info("running HTTP server",
log.String("addr", *flagListenAddr),
log.Uint("port", *flagListenPort),
)
panic(http.ListenAndServe(
fmt.Sprintf("%s:%d", *flagListenAddr, *flagListenPort),
h2c.NewHandler(mux, &http2.Server{}),
))
}
type server struct{}
func (server) CreateSession(
ctx context.Context,
req *connect.Request[psdbv1alpha1.CreateSessionRequest],
) (*connect.Response[psdbv1alpha1.CreateSessionResponse], error) {
ll := logger.With(
log.String("method", "CreateSession"),
log.String("content_type", req.Header().Get("Content-Type")),
)
creds, err := auth.ParseWithSecret(req.Header().Get("Authorization"))
if err != nil || creds.Type() != auth.BasicAuthType {
ll.Error("unauthenticated", log.Error(err))
return nil, connect.NewError(connect.CodeUnauthenticated, err)
}
ll = ll.With(
log.String("user", creds.Username()),
)
sess := session.New(*flagMySQLDbname)
sessionID := session.UUID(sess)
dbname := session.DBName(sess)
conn, err := getConn(ctx, creds.Username(), string(creds.SecretBytes()), dbname, sessionID)
if err != nil {
if strings.Contains(err.Error(), "Access denied for user") {
ll.Error("unauthenticated", log.Error(err))
return nil, connect.NewError(connect.CodeUnauthenticated, err)
} else if err == errSessionInUse {
ll.Warn(err.Error())
return nil, connect.NewError(
connect.CodePermissionDenied,
fmt.Errorf("%s: %s", err.Error(), sessionID),
)
}
ll.Error("failed to connect", log.Error(err))
return nil, err
}
defer returnConn(conn)
ll.Info("ok")
return connect.NewResponse(&psdbv1alpha1.CreateSessionResponse{
Branch: gonanoid.Must(), // there is no branch, so fake it
User: &psdbv1alpha1.User{
Username: creds.Username(),
Psid: "planetscale-1",
},
Session: sess,
}), nil
}
func (server) Execute(
ctx context.Context,
req *connect.Request[psdbv1alpha1.ExecuteRequest],
) (*connect.Response[psdbv1alpha1.ExecuteResponse], error) {
ll := logger.With(
log.String("method", "Execute"),
log.String("content_type", req.Header().Get("Content-Type")),
)
creds, err := auth.ParseWithSecret(req.Header().Get("Authorization"))
if err != nil || creds.Type() != auth.BasicAuthType {
ll.Error("unauthenticated", log.Error(err))
return nil, connect.NewError(connect.CodeUnauthenticated, err)
}
ll = ll.With(
log.String("user", creds.Username()),
)
msg := req.Msg
query := msg.Query
sess := msg.Session
clientSession := sess != nil
// if there is no session, let's generate a new one
if !clientSession {
sess = session.New(*flagMySQLDbname)
}
sessionID := session.UUID(sess)
dbname := session.DBName(sess)
ll = ll.With(
log.String("query", query),
log.String("session_id", sessionID),
log.Bool("client_session", clientSession),
)
conn, err := getConn(ctx, creds.Username(), string(creds.SecretBytes()), dbname, sessionID)
if err != nil {
if strings.Contains(err.Error(), "Access denied for user") {
ll.Error("unauthenticated", log.Error(err))
return nil, connect.NewError(connect.CodeUnauthenticated, err)
} else if err == errSessionInUse {
ll.Warn(err.Error())
return nil, connect.NewError(
connect.CodePermissionDenied,
fmt.Errorf("%s: %s", err.Error(), sessionID),
)
}
ll.Error("failed to connect", log.Error(err))
return nil, err
}
defer returnConn(conn)
start := time.Now()
var qr *sqltypes.Result
if isQueryFiltered(query) {
qr = &sqltypes.Result{}
err = nil
ll.Info("ignored")
} else {
// This is a gross simplificiation, but is likely sufficient
qr, err = conn.ExecuteFetch(query, int(*flagMySQLMaxRows), true)
ll.Info("ok")
}
timing := time.Since(start)
session.Update(qr, sess)
return connect.NewResponse(&psdbv1alpha1.ExecuteResponse{
Session: sess,
Result: vitess.ResultToProto(qr),
Error: vitess.ToVTRPC(err),
Timing: timing.Seconds(),
}), nil
}
func (server) StreamExecute(
ctx context.Context,
req *connect.Request[psdbv1alpha1.ExecuteRequest],
stream *connect.ServerStream[psdbv1alpha1.ExecuteResponse],
) error {
ll := logger.With(
log.String("method", "StreamExecute"),
log.String("content_type", req.Header().Get("Content-Type")),
)
creds, err := auth.ParseWithSecret(req.Header().Get("Authorization"))
if err != nil || creds.Type() != auth.BasicAuthType {
ll.Error("unauthenticated", log.Error(err))
return connect.NewError(connect.CodeUnauthenticated, err)
}
ll = ll.With(
log.String("user", creds.Username()),
)
msg := req.Msg
query := msg.Query
sess := msg.Session
clientSession := sess != nil
// if there is no session, let's generate a new one
if !clientSession {
sess = session.New(*flagMySQLDbname)
}
sessionID := session.UUID(sess)
dbname := session.DBName(sess)
ll = ll.With(
log.String("query", query),
log.String("session_id", sessionID),
log.Bool("client_session", clientSession),
)
conn, err := getConn(ctx, creds.Username(), string(creds.SecretBytes()), dbname, sessionID)
if err != nil {
if strings.Contains(err.Error(), "Access denied for user") {
ll.Error("unauthenticated", log.Error(err))
return connect.NewError(connect.CodeUnauthenticated, err)
} else if err == errSessionInUse {
ll.Warn(err.Error())
return connect.NewError(
connect.CodePermissionDenied,
fmt.Errorf("%s: %s", err.Error(), sessionID),
)
}
ll.Error("failed to connect", log.Error(err))
return err
}
defer returnConn(conn)
// fake a streaming response by just returning 2 messages of the same payload
// far from reality, but a simple way to exercise the protocol.
qr, err := conn.ExecuteFetch(query, int(*flagMySQLMaxRows), true)
session.Update(qr, sess)
ll.Info("send msg")
if err := stream.Send(&psdbv1alpha1.ExecuteResponse{
Session: sess,
Result: vitess.ResultToProto(qr),
Error: vitess.ToVTRPC(err),
}); err != nil {
ll.Error("send failed", log.Error(err))
return err
}
ll.Info("send msg")
if err := stream.Send(&psdbv1alpha1.ExecuteResponse{
Session: sess,
Result: vitess.ResultToProto(qr),
Error: vitess.ToVTRPC(err),
}); err != nil {
ll.Error("send failed", log.Error(err))
return err
}
return nil
}
func (server) CloseSession(
ctx context.Context,
req *connect.Request[psdbv1alpha1.CloseSessionRequest],
) (*connect.Response[psdbv1alpha1.CloseSessionResponse], error) {
ll := logger.With(
log.String("method", "CloseSession"),
log.String("content_type", req.Header().Get("Content-Type")),
)
creds, err := auth.ParseWithSecret(req.Header().Get("Authorization"))
if err != nil || creds.Type() != auth.BasicAuthType {
ll.Error("unauthenticated", log.Error(err))
return nil, connect.NewError(connect.CodeUnauthenticated, err)
}
sess := req.Msg.Session
if sess != nil {
closeConn(mysqlConnKey{
username: creds.Username(),
pass: string(creds.SecretBytes()),
dbname: session.DBName(sess),
session: session.UUID(sess),
})
}
return connect.NewResponse(&psdbv1alpha1.CloseSessionResponse{
Session: session.Reset(sess),
}), nil
}
func (server) Prepare(
ctx context.Context,
req *connect.Request[psdbv1alpha1.PrepareRequest],
) (*connect.Response[psdbv1alpha1.PrepareResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("not implemented"))
}