forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_list.go
113 lines (98 loc) · 2.69 KB
/
query_list.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
package tabletserver
import (
"fmt"
"html/template"
"sort"
"sync"
"time"
"github.com/youtube/vitess/go/vt/callinfo"
"golang.org/x/net/context"
)
// QueryDetail is a simple wrapper for Query, Context and a killable conn.
type QueryDetail struct {
ctx context.Context
conn killable
connID int64
start time.Time
}
type killable interface {
Current() string
ID() int64
Kill() error
}
// NewQueryDetail creates a new QueryDetail
func NewQueryDetail(ctx context.Context, conn killable) *QueryDetail {
return &QueryDetail{ctx: ctx, conn: conn, connID: conn.ID(), start: time.Now()}
}
// QueryList holds a thread safe list of QueryDetails
type QueryList struct {
mu sync.Mutex
queryDetails map[int64]*QueryDetail
}
// NewQueryList creates a new QueryList
func NewQueryList() *QueryList {
return &QueryList{queryDetails: make(map[int64]*QueryDetail)}
}
// Add adds a QueryDetail to QueryList
func (ql *QueryList) Add(qd *QueryDetail) {
ql.mu.Lock()
defer ql.mu.Unlock()
ql.queryDetails[qd.connID] = qd
}
// Remove removes a QueryDetail from QueryList
func (ql *QueryList) Remove(qd *QueryDetail) {
ql.mu.Lock()
defer ql.mu.Unlock()
delete(ql.queryDetails, qd.connID)
}
// Terminate updates the query status and kills the connection
func (ql *QueryList) Terminate(connID int64) error {
ql.mu.Lock()
defer ql.mu.Unlock()
qd := ql.queryDetails[connID]
if qd == nil {
return fmt.Errorf("query %v not found", connID)
}
qd.conn.Kill()
return nil
}
// TerminateAll terminates all queries and kills the MySQL connections
func (ql *QueryList) TerminateAll() {
ql.mu.Lock()
defer ql.mu.Unlock()
for _, qd := range ql.queryDetails {
qd.conn.Kill()
}
}
// QueryDetailzRow is used for rendering QueryDetail in a template
type QueryDetailzRow struct {
Query string
ContextHTML template.HTML
Start time.Time
Duration time.Duration
ConnID int64
State string
ShowTerminateLink bool
}
type byStartTime []QueryDetailzRow
func (a byStartTime) Len() int { return len(a) }
func (a byStartTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byStartTime) Less(i, j int) bool { return a[i].Start.Before(a[j].Start) }
// GetQueryzRows returns a list of QueryDetailzRow sorted by start time
func (ql *QueryList) GetQueryzRows() []QueryDetailzRow {
ql.mu.Lock()
rows := []QueryDetailzRow{}
for _, qd := range ql.queryDetails {
row := QueryDetailzRow{
Query: qd.conn.Current(),
ContextHTML: callinfo.HTMLFromContext(qd.ctx),
Start: qd.start,
Duration: time.Now().Sub(qd.start),
ConnID: qd.connID,
}
rows = append(rows, row)
}
ql.mu.Unlock()
sort.Sort(byStartTime(rows))
return rows
}