-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
187 lines (146 loc) · 4.56 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
/*
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 discovery
import (
"sync"
"time"
"github.com/github/orchestrator/go/config"
"github.com/github/orchestrator/go/inst"
"github.com/openark/golib/log"
)
// QueueMetric contains the queue's active and queued sizes
type QueueMetric struct {
Active int
Queued int
}
// Queue contains information for managing discovery requests
type Queue struct {
sync.Mutex
name string
done chan struct{}
queue chan inst.InstanceKey
queuedKeys map[inst.InstanceKey]time.Time
consumedKeys map[inst.InstanceKey]time.Time
metrics []QueueMetric
}
// DiscoveryQueue 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 discoveryQueue map[string](*Queue)
var dcLock sync.Mutex
func init() {
discoveryQueue = make(map[string](*Queue))
}
// StopMonitoring stops monitoring all the queues
func StopMonitoring() {
for _, q := range discoveryQueue {
q.stopMonitoring()
}
}
// CreateOrReturnQueue allows for creation of a new discovery queue or
// returning a pointer to an existing one given the name.
func CreateOrReturnQueue(name string) *Queue {
dcLock.Lock()
defer dcLock.Unlock()
if q, found := discoveryQueue[name]; found {
return q
}
q := &Queue{
name: name,
queuedKeys: make(map[inst.InstanceKey]time.Time),
consumedKeys: make(map[inst.InstanceKey]time.Time),
queue: make(chan inst.InstanceKey, config.Config.DiscoveryQueueCapacity),
}
go q.startMonitoring()
discoveryQueue[name] = q
return q
}
// monitoring queue sizes until we are told to stop
func (q *Queue) startMonitoring() {
log.Debugf("Queue.startMonitoring(%s)", q.name)
ticker := time.NewTicker(time.Second) // hard-coded at every second
for {
select {
case <-ticker.C: // do the periodic expiry
q.collectStatistics()
case <-q.done:
return
}
}
}
// Stop monitoring the queue
func (q *Queue) stopMonitoring() {
q.done <- struct{}{}
}
// do a check of the entries in the queue, both those active and queued
func (q *Queue) collectStatistics() {
q.Lock()
defer q.Unlock()
q.metrics = append(q.metrics, QueueMetric{Queued: len(q.queuedKeys), Active: len(q.consumedKeys)})
// remove old entries if we get too big
if len(q.metrics) > config.Config.DiscoveryQueueMaxStatisticsSize {
q.metrics = q.metrics[len(q.metrics)-config.Config.DiscoveryQueueMaxStatisticsSize:]
}
}
// 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 inst.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
}
// Consume fetches a key to process; blocks if queue is empty.
// Release must be called once after Consume.
func (q *Queue) Consume() inst.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(config.Config.InstancePollSeconds)*time.Second {
log.Warningf("key %v spent %.4fs waiting on a discoveryQueue", 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 inst.InstanceKey) {
q.Lock()
defer q.Unlock()
delete(q.consumedKeys, key)
}