forked from couchbase/cbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_supervisor.go
192 lines (163 loc) · 4.52 KB
/
query_supervisor.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
// Copyright (c) 2018 Couchbase, 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 cbft
import (
"context"
"fmt"
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/blevesearch/bleve/search/query"
"github.com/couchbase/cbgt/rest"
)
type QuerySupervisorContext struct {
Query query.Query
Cancel context.CancelFunc
Size int
From int
Timeout int64
addedAt time.Time
}
type QuerySupervisor struct {
m sync.RWMutex
queryMap map[uint64]*QuerySupervisorContext
id uint64
added uint64
removed uint64
}
var querySupervisor *QuerySupervisor
func init() {
querySupervisor = &QuerySupervisor{
queryMap: make(map[uint64]*QuerySupervisorContext),
}
}
func (qs *QuerySupervisor) AddEntry(qsc *QuerySupervisorContext) uint64 {
id := atomic.AddUint64(&qs.id, 1)
qs.m.Lock()
if qsc == nil {
qsc = &QuerySupervisorContext{}
}
qsc.addedAt = time.Now()
qs.queryMap[id] = qsc
qs.m.Unlock()
atomic.AddUint64(&qs.added, 1)
return id
}
func (qs *QuerySupervisor) DeleteEntry(id uint64) {
qs.m.Lock()
if _, exists := qs.queryMap[id]; exists {
delete(qs.queryMap, id)
atomic.AddUint64(&qs.removed, 1)
}
qs.m.Unlock()
}
func (qs *QuerySupervisor) Count() uint64 {
removed := atomic.LoadUint64(&qs.removed)
added := atomic.LoadUint64(&qs.added)
return (added - removed)
}
type RunningQueryDetails struct {
Query query.Query `json:"query"`
Size int `json:"size"`
From int `json:"from"`
Timeout int64 `json:"timeout"`
ExecutionTime string `json:"execution_time"`
}
func (qs *QuerySupervisor) ListLongerThan(longerThan time.Duration) map[uint64]*RunningQueryDetails {
qs.m.RLock()
queryMap := map[uint64]*RunningQueryDetails{}
for key, val := range qs.queryMap {
timeSince := time.Since(val.addedAt)
if timeSince > longerThan {
queryMap[key] = &RunningQueryDetails{
Query: val.Query,
Size: val.Size,
From: val.From,
Timeout: val.Timeout,
ExecutionTime: fmt.Sprintf("%s", timeSince),
}
}
}
qs.m.RUnlock()
return queryMap
}
func (qs *QuerySupervisor) ExecutionTime(id uint64) (time.Duration, bool) {
qs.m.RLock()
defer qs.m.RUnlock()
if val, exists := qs.queryMap[id]; exists {
return time.Since(val.addedAt), true
}
return 0, false
}
func (qs *QuerySupervisor) KillQuery(id uint64) bool {
qs.m.Lock()
defer qs.m.Unlock()
if val, exists := qs.queryMap[id]; exists {
val.Cancel()
return true
}
return false
}
type QuerySupervisorDetails struct{}
func NewQuerySupervisorDetails() *QuerySupervisorDetails {
return &QuerySupervisorDetails{}
}
func (qss *QuerySupervisorDetails) ServeHTTP(
w http.ResponseWriter, req *http.Request) {
queryCount := querySupervisor.Count()
queryMap := querySupervisor.ListLongerThan(0)
rv := struct {
Status string `json:"status"`
ActiveQueryCount uint64 `json:"activeQueryCount"`
ActiveQueryMap map[uint64]*RunningQueryDetails `json:"activeQueryMap"`
}{
Status: "ok",
ActiveQueryCount: queryCount,
ActiveQueryMap: queryMap,
}
rest.MustEncode(w, rv)
}
type QueryKiller struct{}
func NewQueryKiller() *QueryKiller {
return &QueryKiller{}
}
func (qk *QueryKiller) ServeHTTP(
w http.ResponseWriter, req *http.Request) {
queryID := rest.RequestVariableLookup(req, "queryID")
if queryID == "" {
rest.PropagateError(w, nil, "query killer: query ID not provided",
http.StatusBadRequest)
return
}
qid, err := strconv.ParseUint(queryID, 10, 64)
if err != nil {
rest.PropagateError(w, nil,
fmt.Sprintf("query killer: query ID '%v' not a uint64", queryID),
http.StatusBadRequest)
return
}
if !querySupervisor.KillQuery(qid) {
rest.PropagateError(w, nil,
fmt.Sprintf("query killer: query ID '%v' not found", qid),
http.StatusBadRequest)
return
}
rv := struct {
Status string `json:"status"`
Msg string `json:"msg"`
}{
Status: "ok",
Msg: fmt.Sprintf("query with ID '%v' was canceled!", queryID),
}
rest.MustEncode(w, rv)
}