/
queue.go
239 lines (191 loc) · 6.72 KB
/
queue.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
/*
Copyright 2014 Outbrain 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 discovery manages a queue of discovery requests: an ordered
queue with no duplicates.
push() operation never blocks while pop() blocks on an empty queue.
*/
package dtstruct
import (
"github.com/montanaflynn/stats"
"sync"
"time"
"gitee.com/opengauss/ham4db/go/core/log"
)
// TODO need more refactor for this
// discoveryQueueMap contains the discovery queue which can then be accessed via an API call for monitoring.
// Currently this is accessed by ContinuousDiscovery() but also from http api calls.
// I may need to protect this better?
var discoveryQueueMap map[string]*Queue
var dcLock sync.Mutex
func init() {
discoveryQueueMap = make(map[string]*Queue)
}
// Queue contains information for managing discovery requests
type Queue struct {
sync.Mutex
name string
done chan struct{}
queue chan InstanceKey
queuedKeys map[InstanceKey]time.Time
consumedKeys map[InstanceKey]time.Time
metrics []MetricQueue
maxMetricSize int
timeToWarn time.Duration // second
}
// start to monitor queue sizes until we are told to stop, hard-coded at every second to do the periodic expiry
func (q *Queue) start() {
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
q.collectMetric()
case <-q.done:
return
}
}
}
// stop monitoring the queue
func (q *Queue) stop() {
q.done <- struct{}{}
}
// collectMetric do a check of the entries in the queue, both those active and queued
func (q *Queue) collectMetric() {
q.Lock()
defer q.Unlock()
// append new queue metric
q.metrics = append(q.metrics, MetricQueue{Queued: len(q.queuedKeys), Active: len(q.consumedKeys)})
// remove old entries if we get too big
if len(q.metrics) > q.maxMetricSize {
q.metrics = q.metrics[len(q.metrics)-q.maxMetricSize:]
}
}
// QueueLen returns the length of the queue (channel size + queued size)
func (q *Queue) QueueLen() int {
q.Lock()
defer q.Unlock()
return len(q.queue) + len(q.queuedKeys)
}
// Push enqueues a key if it is not on a queue and is not being
// processed; silently returns otherwise.
func (q *Queue) Push(key InstanceKey) {
q.Lock()
defer q.Unlock()
// is it enqueued already?
if _, found := q.queuedKeys[key]; found {
return
}
// is it being processed now?
if _, found := q.consumedKeys[key]; found {
return
}
q.queuedKeys[key] = time.Now()
q.queue <- key // TODO maybe block
}
// Consume fetches a key to process; blocks if queue is empty.
// Release must be called once after Consume.
func (q *Queue) Consume() InstanceKey {
q.Lock()
queue := q.queue
q.Unlock()
key := <-queue
q.Lock()
defer q.Unlock()
// alarm if have been waiting for too long
timeOnQueue := time.Since(q.queuedKeys[key])
if timeOnQueue > time.Duration(q.timeToWarn)*time.Second {
log.Warning("key %v spent %.4fs waiting on a discoveryQueueMap", key, timeOnQueue.Seconds())
}
q.consumedKeys[key] = q.queuedKeys[key]
delete(q.queuedKeys, key)
return key
}
// Release removes a key from a list of being processed keys
// which allows that key to be pushed into the queue again.
func (q *Queue) Release(key InstanceKey) {
q.Lock()
defer q.Unlock()
delete(q.consumedKeys, key)
}
// DiscoveryQueueMetrics returns some raw queue metric based on the
// period (last N entries) requested.
func (q *Queue) DiscoveryQueueMetrics(period int) []MetricQueue {
q.Lock()
defer q.Unlock()
// adjust period in case we ask for something that's too long
if period > len(q.metrics) {
log.Debugf("DiscoveryQueueMetrics: wanted: %d, adjusting period to %d", period, len(q.metrics))
period = len(q.metrics)
}
a := q.metrics[len(q.metrics)-period:]
log.Debugf("DiscoveryQueueMetrics: returning values: %+v", a)
return a
}
// AggregatedDiscoveryQueueMetrics Returns some aggregate statistics
// based on the period (last N entries) requested. We store up to
// config.Config.DiscoveryQueueMaxStatisticsSize values and collect once
// a second so we expect period to be a smaller value.
func (q *Queue) AggregatedDiscoveryQueueMetrics(period int) *MetricAggregatedQueue {
wanted := q.DiscoveryQueueMetrics(period)
var activeEntries, queuedEntries []int
// fill vars
for i := range wanted {
activeEntries = append(activeEntries, wanted[i].Active)
queuedEntries = append(queuedEntries, wanted[i].Queued)
}
metricAgg := MetricAggregate{}
a := &MetricAggregatedQueue{
ActiveMinEntries: metricAgg.Min(intSliceToFloat64Slice(activeEntries)),
ActiveMeanEntries: metricAgg.Mean(intSliceToFloat64Slice(activeEntries)),
ActiveMedianEntries: metricAgg.Median(intSliceToFloat64Slice(activeEntries)),
ActiveP95Entries: metricAgg.Percentile(intSliceToFloat64Slice(activeEntries), 95),
ActiveMaxEntries: metricAgg.Max(intSliceToFloat64Slice(activeEntries)),
QueuedMinEntries: metricAgg.Min(intSliceToFloat64Slice(queuedEntries)),
QueuedMeanEntries: metricAgg.Mean(intSliceToFloat64Slice(queuedEntries)),
QueuedMedianEntries: metricAgg.Median(intSliceToFloat64Slice(queuedEntries)),
QueuedP95Entries: metricAgg.Percentile(intSliceToFloat64Slice(queuedEntries), 95),
QueuedMaxEntries: metricAgg.Max(intSliceToFloat64Slice(queuedEntries)),
}
log.Debugf("AggregatedDiscoveryQueueMetrics: returning values: %+v", a)
return a
}
// we pull out values in ints so convert to float64 for metric calculations
func intSliceToFloat64Slice(someInts []int) stats.Float64Data {
var slice stats.Float64Data
for _, v := range someInts {
slice = append(slice, float64(v))
}
return slice
}
// CreateOrReturnQueue allows for creation of a new discovery queue or
// returning a pointer to an existing one given the name.
func CreateOrReturnQueue(name string, capacity uint, maxMetricSize int, timeToWarn uint) *Queue {
dcLock.Lock()
defer dcLock.Unlock()
// check if queue with name is exist
if q, found := discoveryQueueMap[name]; found {
return q
}
// create new one and put to discovery queue map
q := &Queue{
name: name,
queuedKeys: make(map[InstanceKey]time.Time),
consumedKeys: make(map[InstanceKey]time.Time),
queue: make(chan InstanceKey, capacity),
maxMetricSize: maxMetricSize,
timeToWarn: time.Duration(timeToWarn) * time.Second,
}
discoveryQueueMap[name] = q
go q.start()
return q
}