/
context.go
214 lines (184 loc) · 5.86 KB
/
context.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
// Copyright 2020-2021 Dolthub, Inc.
//
// 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 server
import (
"context"
"sync"
"time"
"github.com/dolthub/vitess/go/mysql"
"github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/grant_tables"
)
// SessionBuilder creates sessions given a MySQL connection and a server address.
type SessionBuilder func(ctx context.Context, conn *mysql.Conn, addr string) (sql.Session, error)
// DoneFunc is a function that must be executed when the session is used and
// it can be disposed.
type DoneFunc func()
// DefaultSessionBuilder is a SessionBuilder that returns a base session.
func DefaultSessionBuilder(ctx context.Context, c *mysql.Conn, addr string) (sql.Session, error) {
host := ""
user := ""
mysqlConnectionUser, ok := c.UserData.(grant_tables.MysqlConnectionUser)
if ok {
host = mysqlConnectionUser.Host
user = mysqlConnectionUser.User
}
client := sql.Client{Address: host, User: user, Capabilities: c.Capabilities}
return sql.NewBaseSessionWithClientServer(addr, client, c.ConnectionID), nil
}
type managedSession struct {
session sql.Session
conn *mysql.Conn
}
// SessionManager is in charge of creating new sessions for the given
// connections and keep track of which sessions are in each connection, so
// they can be cancelled if the connection is closed.
type SessionManager struct {
addr string
tracer opentracing.Tracer
hasDBFunc func(name string) bool
memory *sql.MemoryManager
processlist sql.ProcessList
mu *sync.Mutex
builder SessionBuilder
sessions map[uint32]*managedSession
pid uint64
}
// NewSessionManager creates a SessionManager with the given SessionBuilder.
func NewSessionManager(
builder SessionBuilder,
tracer opentracing.Tracer,
hasDBFunc func(name string) bool,
memory *sql.MemoryManager,
processlist sql.ProcessList,
addr string,
) *SessionManager {
return &SessionManager{
addr: addr,
tracer: tracer,
hasDBFunc: hasDBFunc,
memory: memory,
processlist: processlist,
mu: new(sync.Mutex),
builder: builder,
sessions: make(map[uint32]*managedSession),
}
}
func (s *SessionManager) nextPid() uint64 {
s.mu.Lock()
defer s.mu.Unlock()
s.pid++
return s.pid
}
// NewSession creates a Session for the given connection and saves it to the session pool.
func (s *SessionManager) NewSession(ctx context.Context, conn *mysql.Conn) error {
s.mu.Lock()
defer s.mu.Unlock()
session, err := s.builder(ctx, conn, s.addr)
if err != nil {
return err
}
s.sessions[conn.ConnectionID] = &managedSession{session, conn}
logger := s.sessions[conn.ConnectionID].session.GetLogger()
if logger == nil {
log := logrus.StandardLogger()
logger = logrus.NewEntry(log)
}
s.sessions[conn.ConnectionID].session.SetLogger(
logger.WithField(sqle.ConnectionIdLogField, conn.ConnectionID).
WithField(sqle.ConnectTimeLogKey, time.Now()),
)
return err
}
func (s *SessionManager) SetDB(conn *mysql.Conn, db string) error {
sess, err := s.getOrCreateSession(context.Background(), conn)
if err != nil {
return err
}
if db != "" && !s.hasDBFunc(db) {
return sql.ErrDatabaseNotFound.New(db)
}
sess.SetCurrentDatabase(db)
return nil
}
func (s *SessionManager) session(conn *mysql.Conn) sql.Session {
s.mu.Lock()
defer s.mu.Unlock()
return s.sessions[conn.ConnectionID].session
}
// NewContext creates a new context for the session at the given conn.
func (s *SessionManager) NewContext(conn *mysql.Conn) (*sql.Context, error) {
return s.NewContextWithQuery(conn, "")
}
func (s *SessionManager) getOrCreateSession(ctx context.Context, conn *mysql.Conn) (sql.Session, error) {
s.mu.Lock()
sess, ok := s.sessions[conn.ConnectionID]
// Release this lock immediately. If we call NewSession below, we
// cannot hold the lock. We will relock if we need to.
s.mu.Unlock()
if !ok {
err := s.NewSession(ctx, conn)
if err != nil {
return nil, err
}
s.mu.Lock()
sess = s.sessions[conn.ConnectionID]
s.mu.Unlock()
}
return sess.session, nil
}
// NewContextWithQuery creates a new context for the session at the given conn.
func (s *SessionManager) NewContextWithQuery(conn *mysql.Conn, query string) (*sql.Context, error) {
ctx := context.Background()
sess, err := s.getOrCreateSession(ctx, conn)
if err != nil {
return nil, err
}
context := sql.NewContext(
ctx,
sql.WithSession(sess),
sql.WithTracer(s.tracer),
sql.WithPid(s.nextPid()),
sql.WithQuery(query),
sql.WithMemoryManager(s.memory),
sql.WithProcessList(s.processlist),
sql.WithRootSpan(s.tracer.StartSpan("query")),
sql.WithServices(sql.Services{
KillConnection: s.killConnection,
LoadInfile: conn.LoadInfile,
}),
)
return context, nil
}
// Exposed through sql.Services.KillConnection. At the time that this is
// called, any outstanding process has been killed through ProcessList.Kill()
// as well.
func (s *SessionManager) killConnection(connID uint32) error {
s.mu.Lock()
defer s.mu.Unlock()
if entry, ok := s.sessions[connID]; ok {
delete(s.sessions, connID)
entry.conn.Close()
}
return nil
}
// Remove the session assosiated with |conn| from the session manager.
func (s *SessionManager) CloseConn(conn *mysql.Conn) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.sessions, conn.ConnectionID)
}