-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
354 lines (302 loc) · 9.34 KB
/
handler.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
package psdbproxy
import (
"context"
"errors"
"log/slog"
"sync"
"time"
"connectrpc.com/connect"
"github.com/planetscale/psdb/core/client"
psdbpb "github.com/planetscale/psdb/types/psdb/v1alpha1"
"github.com/planetscale/psdb/types/psdb/v1alpha1/psdbv1alpha1connect"
querypb "github.com/planetscale/vitess-types/gen/vitess/query/v16"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/mysql/replication"
"vitess.io/vitess/go/mysql/sqlerror"
"vitess.io/vitess/go/sqltypes"
vitessquerypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/vtenv"
"vitess.io/vitess/go/vt/vterrors"
)
var errNotImplemented = errors.New("not implemented")
const mysqlVersion = "8.0.34-psdbproxy"
func (s *Server) handler() (*handler, error) {
env, err := vtenv.New(vtenv.Options{
MySQLServerVersion: mysqlVersion,
})
if err != nil {
return nil, err
}
return &handler{
logger: s.Logger,
client: client.New(
s.UpstreamAddr,
psdbv1alpha1connect.NewDatabaseClient,
s.Authorization,
),
connections: map[*mysql.Conn]*clientData{},
env: env,
}, nil
}
type handler struct {
mysql.UnimplementedHandler
logger *slog.Logger
client psdbv1alpha1connect.DatabaseClient
connectionsMu sync.RWMutex
connections map[*mysql.Conn]*clientData
env *vtenv.Environment
}
func (h *handler) testCredentials(timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
_, err := h.client.CreateSession(
ctx, connect.NewRequest(&psdbpb.CreateSessionRequest{}),
)
return err
}
func (h *handler) NewConnection(c *mysql.Conn) {
data := &clientData{
start: time.Now(),
remoteAddr: c.RemoteAddr().String(),
}
h.connectionsMu.Lock()
h.connections[c] = data
h.connectionsMu.Unlock()
h.logger.LogAttrs(
context.Background(),
slog.LevelDebug,
"new connection",
slog.String("addr", data.remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
)
}
func (h *handler) ConnectionClosed(c *mysql.Conn) {
h.connectionsMu.Lock()
start := h.connections[c].start
remoteAddr := h.connections[c].remoteAddr
delete(h.connections, c)
h.connectionsMu.Unlock()
h.logger.LogAttrs(
context.Background(),
slog.LevelDebug,
"connection closed",
slog.String("addr", remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
slog.Duration("duration", time.Since(start)),
)
}
type clientData struct {
start time.Time
remoteAddr string
Session *psdbpb.Session
}
func (d *clientData) IsOLAP() bool {
if d.Session == nil {
return false
}
return d.Session.GetVitessSession().Options.Workload == querypb.ExecuteOptions_OLAP
}
var emptyBindVars = make(map[string]*querypb.BindVariable)
func (h *handler) clientData(c *mysql.Conn) *clientData {
h.connectionsMu.RLock()
defer h.connectionsMu.RUnlock()
return h.connections[c]
}
func (h *handler) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error {
data := h.clientData(c)
defer h.logger.LogAttrs(
context.Background(),
slog.LevelDebug,
"execute",
slog.String("addr", data.remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
slog.String("query", query),
slog.Bool("olap", data.IsOLAP()),
)
if data.IsOLAP() {
return h.streamExecute(c, data, query, emptyBindVars, callback)
}
resp, err := h.client.Execute(context.Background(), connect.NewRequest(&psdbpb.ExecuteRequest{
Session: data.Session,
Query: query,
BindVariables: emptyBindVars,
}))
if resp != nil && resp.Msg != nil {
bindSession(c, data, resp.Msg.GetSession())
}
if err != nil {
return sqlerror.NewSQLErrorFromError(err)
}
if resp.Msg.Error != nil {
return sqlerror.NewSQLErrorFromError(vterrors.FromVTRPC(
castRPCError(resp.Msg.Error)),
)
}
return callback(sqltypes.Proto3ToResult(
castQueryResult(resp.Msg.GetResult())),
)
}
func (h *handler) ComPrepare(c *mysql.Conn, query string, bindVars map[string]*vitessquerypb.BindVariable) ([]*vitessquerypb.Field, error) {
data := h.clientData(c)
defer h.logger.LogAttrs(
context.Background(),
slog.LevelDebug,
"prepare",
slog.String("addr", data.remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
slog.String("query", query),
)
resp, err := h.client.Prepare(context.Background(), connect.NewRequest(&psdbpb.PrepareRequest{
Session: data.Session,
Query: query,
BindVariables: castBindVars(bindVars),
}))
if resp != nil && resp.Msg != nil {
bindSession(c, data, resp.Msg.GetSession())
}
if err != nil {
return nil, sqlerror.NewSQLErrorFromError(err)
}
if resp.Msg.Error != nil {
return nil, sqlerror.NewSQLErrorFromError(vterrors.FromVTRPC(
castRPCError(resp.Msg.Error)),
)
}
return castFields(resp.Msg.GetFields()), nil
}
func (h *handler) ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, callback func(*sqltypes.Result) error) error {
data := h.clientData(c)
defer h.logger.LogAttrs(
context.Background(),
slog.LevelDebug,
"stmt_execute",
slog.String("addr", data.remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
slog.String("query", prepare.PrepareStmt),
slog.Bool("olap", data.IsOLAP()),
)
if data.IsOLAP() {
return h.streamExecute(c, data, prepare.PrepareStmt, castBindVars(prepare.BindVars), callback)
}
resp, err := h.client.Execute(context.Background(), connect.NewRequest(&psdbpb.ExecuteRequest{
Session: data.Session,
Query: prepare.PrepareStmt,
BindVariables: castBindVars(prepare.BindVars),
}))
if resp != nil && resp.Msg != nil {
bindSession(c, data, resp.Msg.GetSession())
}
if err != nil {
return sqlerror.NewSQLErrorFromError(err)
}
if resp.Msg.Error != nil {
return sqlerror.NewSQLErrorFromError(vterrors.FromVTRPC(
castRPCError(resp.Msg.Error)),
)
}
return callback(sqltypes.Proto3ToResult(
castQueryResult(resp.Msg.GetResult())),
)
}
func (h *handler) ComRegisterReplica(c *mysql.Conn, replicaHost string, replicaPort uint16, replicaUser string, replicaPassword string) error {
return errNotImplemented
}
func (h *handler) ComBinlogDump(c *mysql.Conn, logFile string, binlogPos uint32) error {
return errNotImplemented
}
func (h *handler) ComBinlogDumpGTID(c *mysql.Conn, logFile string, logPos uint64, gtidSet replication.GTIDSet) error {
return errNotImplemented
}
func (h *handler) WarningCount(c *mysql.Conn) uint16 {
session := h.clientData(c).Session
if session == nil {
return 0
}
return uint16(len(session.GetVitessSession().GetWarnings()))
}
func (h *handler) streamExecute(c *mysql.Conn, data *clientData, query string, bindVars map[string]*querypb.BindVariable, callback func(*sqltypes.Result) error) error {
stream, err := h.client.StreamExecute(context.Background(), connect.NewRequest(&psdbpb.ExecuteRequest{
Session: data.Session,
Query: query,
BindVariables: bindVars,
}))
if err != nil {
return sqlerror.NewSQLErrorFromError(err)
}
var fields []*querypb.Field
var resp *psdbpb.ExecuteResponse
for stream.Receive() {
resp = stream.Msg()
// NOTE: Some results do not have any Result. This is most likely
// the case when a Session is returned. While Vitess currently (as of v18)
// is implemented such that the last streaming response
// contains a Session, but not Result, I do not want to assume
// this is always the case, so this is implemented to handle
// both existing or none existing, or either existing to cover
// our bases.
// Some results may contain a Session, if so
// we need to bind it to the mysql.Conn like normal
if resp.Session != nil {
bindSession(c, data, resp.Session)
}
// If we have ane error, we just return the error
if resp.Error != nil {
return sqlerror.NewSQLErrorFromError(vterrors.FromVTRPC(
castRPCError(resp.Error)),
)
}
// Lastly if there are results, we return them to the mysql client.
// messages without results get ignored at this point since they
// likely only contained session data.
if resp.Result != nil {
if fields == nil {
fields = resp.Result.GetFields()
}
if err := callback(sqltypes.CustomProto3ToResult(
castFields(fields), castQueryResult(resp.GetResult())),
); err != nil {
return err
}
}
// For each iteration, stream.Msg() is reused to the same struct,
// We can do resp.Reset(). This nulls out everything, but doesn't let
// anything me reused. This is nuclear, but it would get the job done.
// Doing this means `resp.Result` gets nulled, and needs to be GC'd for each page.
// hitting each field explicitly with a `Reset()` allows us to reuse memory
// safely and efficiently between.
// See: https://pkg.go.dev/google.golang.org/protobuf/proto#Reset
// So this maintains the actual structs, but zeroed out.
if resp.Session != nil {
resp.Session.Reset()
}
if resp.Result != nil {
resp.Result.Reset()
}
if resp.Error != nil {
resp.Error.Reset()
}
}
if err := stream.Err(); err != nil {
return sqlerror.NewSQLErrorFromError(err)
}
return nil
}
func (h *handler) Env() *vtenv.Environment {
return h.env
}
func bindSession(c *mysql.Conn, data *clientData, session *psdbpb.Session) {
if session == nil || session.VitessSession == nil {
return
}
if session.VitessSession.InTransaction {
c.StatusFlags |= mysql.ServerStatusInTrans
} else {
c.StatusFlags &= mysql.NoServerStatusInTrans
}
if session.VitessSession.Autocommit {
c.StatusFlags |= mysql.ServerStatusAutocommit
} else {
c.StatusFlags &= mysql.NoServerStatusAutocommit
}
data.Session = session
}