This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathprocesslist.go
329 lines (273 loc) · 7.5 KB
/
processlist.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
package sql
import (
"context"
"fmt"
"sync"
"time"
"github.com/sirupsen/logrus"
"gopkg.in/src-d/go-errors.v1"
)
// Progress between done items and total items.
type Progress struct {
Name string
Done int64
Total int64
}
func (p Progress) totalString() string {
var total = "?"
if p.Total > 0 {
total = fmt.Sprint(p.Total)
}
return total
}
// TableProgress keeps track of a table progress, and for each of its partitions
type TableProgress struct {
Progress
PartitionsProgress map[string]PartitionProgress
}
func NewTableProgress(name string, total int64) TableProgress {
return TableProgress{
Progress: Progress{
Name: name,
Total: total,
},
PartitionsProgress: make(map[string]PartitionProgress),
}
}
func (p TableProgress) String() string {
return fmt.Sprintf("%s (%d/%s partitions)", p.Name, p.Done, p.totalString())
}
// PartitionProgress keeps track of a partition progress
type PartitionProgress struct {
Progress
}
func (p PartitionProgress) String() string {
return fmt.Sprintf("%s (%d/%s rows)", p.Name, p.Done, p.totalString())
}
// ProcessType is the type of process.
type ProcessType byte
const (
// QueryProcess is a query process.
QueryProcess ProcessType = iota
// CreateIndexProcess is a process to create an index.
CreateIndexProcess
)
func (p ProcessType) String() string {
switch p {
case QueryProcess:
return "query"
case CreateIndexProcess:
return "create_index"
default:
return "invalid"
}
}
// Process represents a process in the SQL server.
type Process struct {
Pid uint64
Connection uint32
User string
Type ProcessType
Query string
Progress map[string]TableProgress
StartedAt time.Time
Kill context.CancelFunc
}
// Done needs to be called when this process has finished.
func (p *Process) Done() { p.Kill() }
// Seconds returns the number of seconds this process has been running.
func (p *Process) Seconds() uint64 {
return uint64(time.Since(p.StartedAt) / time.Second)
}
// ProcessList is a structure that keeps track of all the processes and their
// status.
type ProcessList struct {
mu sync.RWMutex
procs map[uint64]*Process
}
// NewProcessList creates a new process list.
func NewProcessList() *ProcessList {
return &ProcessList{
procs: make(map[uint64]*Process),
}
}
// ErrPidAlreadyUsed is returned when the pid is already registered.
var ErrPidAlreadyUsed = errors.NewKind("pid %d is already in use")
// AddProcess adds a new process to the list given a process type and a query.
// Steps is a map between the name of the items that need to be completed and
// the total amount in these items. -1 means unknown.
// It returns a new context that should be passed around from now on. That
// context will be cancelled if the process is killed.
func (pl *ProcessList) AddProcess(
ctx *Context,
typ ProcessType,
query string,
) (*Context, error) {
pl.mu.Lock()
defer pl.mu.Unlock()
if _, ok := pl.procs[ctx.Pid()]; ok {
return nil, ErrPidAlreadyUsed.New(ctx.Pid())
}
newCtx, cancel := context.WithCancel(ctx)
ctx = ctx.WithContext(newCtx)
pl.procs[ctx.Pid()] = &Process{
Pid: ctx.Pid(),
Connection: ctx.ID(),
Type: typ,
Query: query,
Progress: make(map[string]TableProgress),
User: ctx.Session.Client().User,
StartedAt: time.Now(),
Kill: cancel,
}
return ctx, nil
}
// UpdateTableProgress updates the progress of the table with the given name for the
// process with the given pid.
func (pl *ProcessList) UpdateTableProgress(pid uint64, name string, delta int64) {
pl.mu.Lock()
defer pl.mu.Unlock()
p, ok := pl.procs[pid]
if !ok {
return
}
progress, ok := p.Progress[name]
if !ok {
progress = NewTableProgress(name, -1)
}
progress.Done += delta
p.Progress[name] = progress
}
// UpdatePartitionProgress updates the progress of the table partition with the
// given name for the process with the given pid.
func (pl *ProcessList) UpdatePartitionProgress(pid uint64, tableName, partitionName string, delta int64) {
pl.mu.Lock()
defer pl.mu.Unlock()
p, ok := pl.procs[pid]
if !ok {
return
}
tablePg, ok := p.Progress[tableName]
if !ok {
return
}
partitionPg, ok := tablePg.PartitionsProgress[partitionName]
if !ok {
partitionPg = PartitionProgress{Progress: Progress{Name: partitionName, Total: -1}}
}
partitionPg.Done += delta
tablePg.PartitionsProgress[partitionName] = partitionPg
}
// AddTableProgress adds a new item to track progress from to the process with
// the given pid. If the pid does not exist, it will do nothing.
func (pl *ProcessList) AddTableProgress(pid uint64, name string, total int64) {
pl.mu.Lock()
defer pl.mu.Unlock()
p, ok := pl.procs[pid]
if !ok {
return
}
if pg, ok := p.Progress[name]; ok {
pg.Total = total
p.Progress[name] = pg
} else {
p.Progress[name] = NewTableProgress(name, total)
}
}
// AddPartitionProgress adds a new item to track progress from to the process with
// the given pid. If the pid or the table does not exist, it will do nothing.
func (pl *ProcessList) AddPartitionProgress(pid uint64, tableName, partitionName string, total int64) {
pl.mu.Lock()
defer pl.mu.Unlock()
p, ok := pl.procs[pid]
if !ok {
return
}
tablePg, ok := p.Progress[tableName]
if !ok {
return
}
if pg, ok := tablePg.PartitionsProgress[partitionName]; ok {
pg.Total = total
tablePg.PartitionsProgress[partitionName] = pg
} else {
tablePg.PartitionsProgress[partitionName] =
PartitionProgress{Progress: Progress{Name: partitionName, Total: total}}
}
}
// RemoveTableProgress removes an existing item tracking progress from the
// process with the given pid, if it exists.
func (pl *ProcessList) RemoveTableProgress(pid uint64, name string) {
pl.mu.Lock()
defer pl.mu.Unlock()
p, ok := pl.procs[pid]
if !ok {
return
}
delete(p.Progress, name)
}
// RemovePartitionProgress removes an existing item tracking progress from the
// process with the given pid, if it exists.
func (pl *ProcessList) RemovePartitionProgress(pid uint64, tableName, partitionName string) {
pl.mu.Lock()
defer pl.mu.Unlock()
p, ok := pl.procs[pid]
if !ok {
return
}
tablePg, ok := p.Progress[tableName]
if !ok {
return
}
delete(tablePg.PartitionsProgress, partitionName)
}
// Kill terminates all queries for a given connection id.
func (pl *ProcessList) Kill(connID uint32) {
pl.mu.Lock()
defer pl.mu.Unlock()
for pid, proc := range pl.procs {
if proc.Connection == connID {
logrus.Infof("kill query: pid %d", pid)
proc.Done()
delete(pl.procs, pid)
}
}
}
// KillOnlyQueries kills all queries, but not index creation queries, for a
// given connection id.
func (pl *ProcessList) KillOnlyQueries(connID uint32) {
pl.mu.Lock()
defer pl.mu.Unlock()
for pid, proc := range pl.procs {
if proc.Connection == connID && proc.Type == QueryProcess {
logrus.Infof("kill query: pid %d", pid)
proc.Done()
delete(pl.procs, pid)
}
}
}
// Done removes the finished process with the given pid from the process list.
// If the process does not exist, it will do nothing.
func (pl *ProcessList) Done(pid uint64) {
pl.mu.Lock()
defer pl.mu.Unlock()
if proc, ok := pl.procs[pid]; ok {
proc.Done()
}
delete(pl.procs, pid)
}
// Processes returns the list of current running processes.
func (pl *ProcessList) Processes() []Process {
pl.mu.RLock()
defer pl.mu.RUnlock()
var result = make([]Process, 0, len(pl.procs))
for _, proc := range pl.procs {
p := *proc
var progress = make(map[string]TableProgress, len(p.Progress))
for n, p := range p.Progress {
progress[n] = p
}
result = append(result, p)
}
return result
}