forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_list.go
129 lines (110 loc) · 3.35 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright 2017 Google 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 agreedto 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 tabletserver
import (
"fmt"
"html/template"
"sort"
"sync"
"time"
"golang.org/x/net/context"
"vitess.io/vitess/go/vt/callinfo"
)
// 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(message string, startTime time.Duration) 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("QueryList.Terminate()", time.Since(qd.start))
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("QueryList.TerminateAll()", time.Since(qd.start))
}
}
// 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
}