forked from openark/orchestrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
292 lines (246 loc) · 7.72 KB
/
collection.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
/*
Copyright 2017 Simon J Mudd
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 collection holds routines for collecting "high frequency"
metrics and handling their auto-expiry based on a configured retention
time. This becomes more interesting as the number of MySQL servers
monitored by orchestrator increases.
Most monitoring systems look at different metrics over a period
like 1, 10, 30 or 60 seconds but even at second resolution orchestrator
may have polled a number of servers.
It can be helpful to collect the raw values, and then allow external
monitoring to pull via an http api call either pre-cooked aggregate
data or the raw data for custom analysis over the period requested.
This is expected to be used for the following types of metric:
* discovery metrics (time to poll a MySQL server and collect status)
* queue metrics (statistics within the discovery queue itself)
* query metrics (statistics on the number of queries made to the
backend MySQL database)
Orchestrator code can just add a new metric without worrying about
removing it later, and other code which serves API requests can
pull out the data when needed for the requested time period.
For current metrics two api urls have been provided: one provides
the raw data and the other one provides a single set of aggregate
data which is suitable for easy collection by monitoring systems.
Expiry is triggered by default if the collection is created via
CreateOrReturnCollection() and uses an expiry period of
DiscoveryCollectionRetentionSeconds. It can also be enabled by
calling StartAutoExpiration() after setting the required expire
period with SetExpirePeriod().
This will trigger periodic calls (every second) to ensure the removal
of metrics which have passed the time specified. Not enabling expiry
will mean data is collected but never freed which will make
orchestrator run out of memory eventually.
Current code uses DiscoveryCollectionRetentionSeconds as the
time to keep metric data.
*/
package collection
import (
"errors"
"sync"
"time"
// "github.com/openark/golib/log"
"github.com/github/orchestrator/go/config"
)
// Metric is an interface containing a metric
type Metric interface {
When() time.Time // when the metric was taken
}
// Collection contains a collection of Metrics
type Collection struct {
sync.Mutex // for locking the structure
monitoring bool // am I monitoring the queue size?
collection []Metric
done chan struct{} // to indicate that we are finishing expiry processing
expirePeriod time.Duration // time to keep the collection information for
}
// hard-coded at every second
const defaultExpireTickerPeriod = time.Second
// backendMetricCollection contains the last N backend "channelled"
// metrics which can then be accessed via an API call for monitoring.
var (
namedCollection map[string](*Collection)
namedCollectionLock sync.Mutex
)
func init() {
namedCollection = make(map[string](*Collection))
}
// StopMonitoring stops monitoring all the collections
func StopMonitoring() {
for _, q := range namedCollection {
q.StopAutoExpiration()
}
}
// CreateOrReturnCollection allows for creation of a new collection or
// returning a pointer to an existing one given the name. This allows access
// to the data structure from the api interface (http/api.go) and also when writing (inst).
func CreateOrReturnCollection(name string) *Collection {
namedCollectionLock.Lock()
defer namedCollectionLock.Unlock()
if q, found := namedCollection[name]; found {
return q
}
qmc := &Collection{
collection: nil,
done: make(chan struct{}),
// WARNING: use a different configuration name
expirePeriod: time.Duration(config.Config.DiscoveryCollectionRetentionSeconds) * time.Second,
}
go qmc.StartAutoExpiration()
namedCollection[name] = qmc
return qmc
}
// SetExpirePeriod determines after how long the collected data should be removed
func (c *Collection) SetExpirePeriod(duration time.Duration) {
c.Lock()
defer c.Unlock()
c.expirePeriod = duration
}
// ExpirePeriod returns the currently configured expiration period
func (c *Collection) ExpirePeriod() time.Duration {
c.Lock()
defer c.Unlock()
return c.expirePeriod
}
// StopAutoExpiration prepares to stop by terminating the auto-expiration process
func (c *Collection) StopAutoExpiration() {
if c == nil {
return
}
c.Lock()
if !c.monitoring {
c.Unlock()
return
}
c.monitoring = false
c.Unlock()
// no locking here deliberately
c.done <- struct{}{}
}
// StartAutoExpiration initiates the auto expiry procedure which
// periodically checks for metrics in the collection which need to
// be expired according to bc.ExpirePeriod.
func (c *Collection) StartAutoExpiration() {
if c == nil {
return
}
c.Lock()
if c.monitoring {
c.Unlock()
return
}
c.monitoring = true
c.Unlock()
// log.Infof("StartAutoExpiration: %p with expirePeriod: %v", c, c.expirePeriod)
ticker := time.NewTicker(defaultExpireTickerPeriod)
for {
select {
case <-ticker.C: // do the periodic expiry
c.removeBefore(time.Now().Add(-c.expirePeriod))
case <-c.done: // stop the ticker and return
ticker.Stop()
return
}
}
}
// Metrics returns a slice containing all the metric values
func (c *Collection) Metrics() []Metric {
if c == nil {
return nil
}
c.Lock()
defer c.Unlock()
if len(c.collection) == 0 {
return nil // nothing to return
}
return c.collection
}
// Since returns the Metrics on or after the given time. We assume
// the metrics are stored in ascending time.
// Iterate backwards until we reach the first value before the given time
// or the end of the array.
func (c *Collection) Since(t time.Time) ([]Metric, error) {
if c == nil {
return nil, errors.New("Collection.Since: c == nil")
}
c.Lock()
defer c.Unlock()
if len(c.collection) == 0 {
return nil, nil // nothing to return
}
last := len(c.collection)
first := last - 1
done := false
for !done {
if c.collection[first].When().After(t) || c.collection[first].When().Equal(t) {
if first == 0 {
break // as can't go lower
}
first--
} else {
if first != last {
first++ // go back one (except if we're already at the end)
}
break
}
}
return c.collection[first:last], nil
}
// removeBefore is called by StartAutoExpiration and removes collection values
// before the given time.
func (c *Collection) removeBefore(t time.Time) error {
if c == nil {
return errors.New("Collection.removeBefore: c == nil")
}
c.Lock()
defer c.Unlock()
cLen := len(c.collection)
if cLen == 0 {
return nil // we have a collection but no data
}
// remove old data here.
first := 0
done := false
for !done {
if c.collection[first].When().Before(t) {
first++
if first == cLen {
break
}
} else {
first--
break
}
}
// get the interval we need.
if first == len(c.collection) {
c.collection = nil // remove all entries
} else if first != -1 {
c.collection = c.collection[first:]
}
return nil // no errors
}
// Append a new Metric to the existing collection
func (c *Collection) Append(m Metric) error {
if c == nil {
return errors.New("Collection.Append: c == nil")
}
c.Lock()
defer c.Unlock()
// we don't want to add nil metrics
if c == nil {
return errors.New("Collection.Append: c == nil")
}
c.collection = append(c.collection, m)
return nil
}