-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathsessions.go
More file actions
352 lines (303 loc) · 7.27 KB
/
sessions.go
File metadata and controls
352 lines (303 loc) · 7.27 KB
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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package proxy
import (
"sort"
"sync"
"time"
"backend"
"github.com/xelabs/go-mysqlstack/driver"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/xlog"
)
const (
sessionStateInTransaction = "In transaction"
)
// Sessions tuple.
type Sessions struct {
log *xlog.Log
mu sync.RWMutex
// Key is session ID.
sessions map[uint32]*session
}
// NewSessions creates new session.
func NewSessions(log *xlog.Log) *Sessions {
return &Sessions{
log: log,
sessions: make(map[uint32]*session),
}
}
// Add used to add the session to map when session created.
func (ss *Sessions) Add(s *driver.Session) {
ss.mu.Lock()
defer ss.mu.Unlock()
ss.sessions[s.ID()] = newSession(ss.log, s)
}
// Remove used to remove the session from the map when session exit.
func (ss *Sessions) Remove(s *driver.Session) {
ss.mu.Lock()
session, ok := ss.sessions[s.ID()]
if !ok {
ss.mu.Unlock()
return
}
delete(ss.sessions, s.ID())
ss.mu.Unlock()
session.close()
}
// Kill used to kill a live session.
// 1. remove from sessions list.
// 2. close the session from the server side.
// 3. abort the session's txn.
func (ss *Sessions) Kill(id uint32, reason string) {
log := ss.log
ss.mu.Lock()
session, ok := ss.sessions[id]
if !ok {
ss.mu.Unlock()
return
}
delete(ss.sessions, id)
ss.mu.Unlock()
log.Warning("session.id[%v].killed.reason:%s", id, reason)
session.close()
}
// Reaches used to check whether the sessions count reaches(>=) the quota.
func (ss *Sessions) Reaches(quota int) bool {
ss.mu.RLock()
defer ss.mu.RUnlock()
return (len(ss.sessions) >= quota)
}
// getTxnSession used to get current connection session.
func (ss *Sessions) getTxnSession(session *driver.Session) *session {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.sessions[session.ID()]
}
// getSession used to get current connection session.
func (ss *Sessions) getSession(id uint32) *session {
ss.mu.RLock()
defer ss.mu.RUnlock()
return ss.sessions[id]
}
// TxnBinding used to bind txn to the session.
func (ss *Sessions) TxnBinding(s *driver.Session, txn backend.Transaction, node sqlparser.Statement, query string) {
ss.mu.RLock()
session, ok := ss.sessions[s.ID()]
if !ok {
ss.mu.RUnlock()
return
}
ss.mu.RUnlock()
session.mu.Lock()
defer session.mu.Unlock()
q := query
if len(query) > 128 {
q = query[:128]
}
session.query = q
session.node = node
// Bind sid to txn.
txn.SetSessionID(s.ID())
session.transaction = txn
session.timestamp = time.Now().Unix()
}
// TxnUnBinding used to set transaction and node to nil.
func (ss *Sessions) TxnUnBinding(s *driver.Session) {
ss.mu.RLock()
session, ok := ss.sessions[s.ID()]
if !ok {
ss.mu.RUnlock()
return
}
ss.mu.RUnlock()
session.mu.Lock()
defer session.mu.Unlock()
session.node = nil
session.query = ""
session.transaction = nil
session.timestamp = time.Now().Unix()
}
// MultiStmtTxnBinding used to bind txn, node, query to the session
func (ss *Sessions) MultiStmtTxnBinding(s *driver.Session, txn backend.Transaction, node sqlparser.Statement, query string) {
ss.mu.RLock()
session, ok := ss.sessions[s.ID()]
if !ok {
ss.mu.RUnlock()
return
}
ss.mu.RUnlock()
session.mu.Lock()
defer session.mu.Unlock()
q := query
if len(query) > 128 {
q = query[:128]
}
session.query = q
session.node = node
// txn should not be nil when "begin" or "start transaction" is executed, to be set just once during the trans.
if txn != nil {
// Bind sid to txn.
txn.SetSessionID(s.ID())
session.transaction = txn
}
session.timestamp = time.Now().Unix()
}
// MultiStmtTxnUnBinding used to set transaction by isEnd
func (ss *Sessions) MultiStmtTxnUnBinding(s *driver.Session, isEnd bool) {
ss.mu.RLock()
session, ok := ss.sessions[s.ID()]
if !ok {
ss.mu.RUnlock()
return
}
ss.mu.RUnlock()
session.mu.Lock()
defer session.mu.Unlock()
session.node = nil
session.query = ""
// If multiple-statement transaction is end or some errors happen, set transaction to be nil
if isEnd {
session.transaction = nil
}
session.timestamp = time.Now().Unix()
}
// Close used to close all sessions.
func (ss *Sessions) Close() {
i := 0
for {
ss.mu.Lock()
for _, v := range ss.sessions {
v.close()
}
c := len(ss.sessions)
ss.mu.Unlock()
if c > 0 {
ss.log.Warning("session.wait.for.shutdown.live.txn:[%d].wait.seconds:%d", c, i)
time.Sleep(time.Second)
i++
} else {
break
}
}
}
// SessionInfo tuple.
type SessionInfo struct {
ID uint32
User string
Host string
DB string
Command string
Time uint32
State string
Info string
RowsSent uint64
RowsExamined uint64
}
// Sort by id.
type sessionInfos []SessionInfo
// Len impl.
func (q sessionInfos) Len() int { return len(q) }
// Swap impl.
func (q sessionInfos) Swap(i, j int) { q[i], q[j] = q[j], q[i] }
// Less impl.
func (q sessionInfos) Less(i, j int) bool { return q[i].ID < q[j].ID }
// Snapshot returns all session info.
func (ss *Sessions) Snapshot() []SessionInfo {
var infos sessionInfos
now := time.Now().Unix()
ss.mu.Lock()
for _, v := range ss.sessions {
v.mu.Lock()
info := SessionInfo{
ID: v.session.ID(),
User: v.session.User(),
Host: v.session.Addr(),
DB: v.session.Schema(),
Command: "Sleep",
Time: uint32(now - (int64)(v.session.LastQueryTime().Unix())),
}
if v.node != nil {
info.Command = "Query"
info.Info = v.query
}
if v.transaction != nil {
// https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html about state.
info.State = sessionStateInTransaction
}
infos = append(infos, info)
v.mu.Unlock()
}
ss.mu.Unlock()
sort.Sort(infos)
return infos
}
// SnapshotTxn returns all sessions info in transaction.
func (ss *Sessions) SnapshotTxn() []SessionInfo {
var infos sessionInfos
now := time.Now().Unix()
ss.mu.Lock()
for _, v := range ss.sessions {
v.mu.Lock()
if v.transaction == nil {
v.mu.Unlock()
continue
}
info := SessionInfo{
ID: v.session.ID(),
User: v.session.User(),
Host: v.session.Addr(),
DB: v.session.Schema(),
Command: "Sleep",
Time: uint32(now - (int64)(v.session.LastQueryTime().Unix())),
}
if v.node != nil {
info.Command = "Query"
info.Info = v.query
}
info.State = sessionStateInTransaction
infos = append(infos, info)
v.mu.Unlock()
}
ss.mu.Unlock()
sort.Sort(infos)
return infos
}
// Snapshot returns all session info about the user.
func (ss *Sessions) SnapshotUser(user string) []SessionInfo {
var infos sessionInfos
now := time.Now().Unix()
ss.mu.Lock()
for _, v := range ss.sessions {
if v.session.User() != user {
continue
}
v.mu.Lock()
info := SessionInfo{
ID: v.session.ID(),
User: v.session.User(),
Host: v.session.Addr(),
DB: v.session.Schema(),
Command: "Sleep",
Time: uint32(now - (int64)(v.session.LastQueryTime().Unix())),
}
if v.node != nil {
info.Command = "Query"
info.Info = v.query
}
if v.transaction != nil {
// https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html about state.
info.State = sessionStateInTransaction
}
infos = append(infos, info)
v.mu.Unlock()
}
ss.mu.Unlock()
sort.Sort(infos)
return infos
}