forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription_group.go
559 lines (500 loc) · 16.7 KB
/
subscription_group.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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 control
import (
"errors"
"fmt"
"sync"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/cdata"
"github.com/intelsdi-x/snap/core/control_event"
"github.com/intelsdi-x/snap/core/serror"
log "github.com/Sirupsen/logrus"
)
var (
// ErrSubscriptionGroupAlreadyExists - error message when the subscription
// group already exists
ErrSubscriptionGroupAlreadyExists = core.ErrSubscriptionGroupAlreadyExists
// ErrSubscriptionGroupDoesNotExist - error message when the subscription
// group does not exist
ErrSubscriptionGroupDoesNotExist = core.ErrSubscriptionGroupDoesNotExist
ErrConfigRequiredForMetric = errors.New("config required")
)
// ManagesSubscriptionGroups is the interface implemented by an object that can
// manage subscription groups.
type ManagesSubscriptionGroups interface {
Process() (errs []serror.SnapError)
Add(id string, requested []core.RequestedMetric,
configTree *cdata.ConfigDataTree,
plugins []core.SubscribedPlugin) []serror.SnapError
Get(id string) ([]core.Metric, []serror.SnapError, error)
Remove(id string) []serror.SnapError
ValidateDeps(requested []core.RequestedMetric,
plugins []core.SubscribedPlugin,
configTree *cdata.ConfigDataTree) (serrs []serror.SnapError)
validateMetric(
metric core.Metric) (serrs []serror.SnapError)
}
type subscriptionGroup struct {
*pluginControl
// requested metrics - never updated
requestedMetrics []core.RequestedMetric
// requested plugins - contains only processors and publishers;
// never updated
requestedPlugins []core.SubscribedPlugin
// config from request - never updated
configTree *cdata.ConfigDataTree
// resulting metrics - updated after plugin load/unload events
metrics []core.Metric
// resulting plugins - updated after plugin load/unload events
plugins []core.SubscribedPlugin
// errors generated the last time the subscription was processed
// subscription groups are processed when the subscription group is added
// and when plugins are loaded/unloaded
errors []serror.SnapError
}
type subscriptionMap map[string]*subscriptionGroup
type subscriptionGroups struct {
subscriptionMap
*sync.Mutex
*pluginControl
}
func newSubscriptionGroups(control *pluginControl) *subscriptionGroups {
return &subscriptionGroups{
make(map[string]*subscriptionGroup),
&sync.Mutex{},
control,
}
}
// Add adds a subscription group provided a subscription group id, requested
// metrics, config tree and plugins. The requested metrics are mapped to
// collector plugins which are then combined with the provided (processor and
// publisher) plugins. The provided config map is used to construct the
// []core.Metric which will be used during collect calls made against the
// subscription group.
// Returns an array of errors ([]serror.SnapError).
// `ErrSubscriptionGroupAlreadyExists` is returned if the subscription already
// exists. Also, if there are errors mapping the requested metrics to plugins
// those are returned.
func (s subscriptionGroups) Add(id string, requested []core.RequestedMetric,
configTree *cdata.ConfigDataTree,
plugins []core.SubscribedPlugin) []serror.SnapError {
s.Lock()
defer s.Unlock()
errs := s.add(id, requested, configTree, plugins)
return errs
}
func (s subscriptionGroups) add(id string, requested []core.RequestedMetric,
configTree *cdata.ConfigDataTree,
plugins []core.SubscribedPlugin) []serror.SnapError {
if _, ok := s.subscriptionMap[id]; ok {
return []serror.SnapError{serror.New(ErrSubscriptionGroupAlreadyExists)}
}
subscriptionGroup := &subscriptionGroup{
requestedMetrics: requested,
requestedPlugins: plugins,
configTree: configTree,
pluginControl: s.pluginControl,
}
errs := subscriptionGroup.process(id)
s.subscriptionMap[id] = subscriptionGroup
return errs
}
// Remove removes a subscription group given a subscription group ID.
func (s subscriptionGroups) Remove(id string) []serror.SnapError {
s.Lock()
defer s.Unlock()
return s.remove(id)
}
func (s subscriptionGroups) remove(id string) []serror.SnapError {
subscriptionGroup, ok := s.subscriptionMap[id]
if !ok {
return []serror.SnapError{serror.New(ErrSubscriptionGroupDoesNotExist)}
}
serrs := subscriptionGroup.unsubscribePlugins(id, s.subscriptionMap[id].plugins)
delete(s.subscriptionMap, id)
return serrs
}
// Get returns the metrics (core.Metric) and an array of serror.SnapError when
// provided a subscription ID. The array of serror.SnapError returned was
// produced the last time `process` was run which is important since
// unloading/loading a plugin may produce errors when the requested metrics
// are looked up in the metric catalog. Those errors will be provided back to
// the caller of the subscription group on the next `CollectMetrics`.
// Returns `ErrSubscriptionGroupDoesNotExist` when the subscription group
// does not exist.
func (s subscriptionGroups) Get(id string) ([]core.Metric, []serror.SnapError, error) {
s.Lock()
defer s.Unlock()
return s.get(id)
}
func (s subscriptionGroups) get(id string) ([]core.Metric, []serror.SnapError, error) {
if _, ok := s.subscriptionMap[id]; !ok {
return nil, nil, ErrSubscriptionGroupDoesNotExist
}
sg := s.subscriptionMap[id]
return sg.metrics, sg.errors, nil
}
// Process compares the new set of plugins with the previous set of plugins
// for the given subscription group subscribing to plugins that were added
// and unsubscribing to those that were removed since the last time the
// subscription group was processed.
// Returns an array of errors ([]serror.SnapError) which can occur when
// mapping requested metrics to collector plugins and getting a core.Plugin
// from a core.Requested.Plugin.
// When processing a subscription group the resulting metrics
// (subscriptionGroup.metrics) for all subscription groups are updated based
// on the requested metrics (subscriptionGroup.requestedMetrics). Similarly
// the required plugins (subscriptionGroup.plugins) are also updated.
func (s *subscriptionGroups) Process() (errs []serror.SnapError) {
s.Lock()
defer s.Unlock()
for id, group := range s.subscriptionMap {
if serrs := group.process(id); serrs != nil {
errs = append(errs, serrs...)
}
}
return errs
}
func (s *subscriptionGroups) ValidateDeps(requested []core.RequestedMetric,
plugins []core.SubscribedPlugin,
configTree *cdata.ConfigDataTree) (serrs []serror.SnapError) {
// resolve requested metrics and map to collectors
metrics, collectors, errs := s.getMetricsAndCollectors(requested, configTree)
if errs != nil {
serrs = append(serrs, errs...)
}
// validateMetrics
for _, m := range metrics {
config := configTree.Get(m.Namespace().Strings())
// in case there is not config tree doesn't have configuration for current ns
// initialize config node, so it does not panic later on
if config == nil {
config = cdata.NewNode()
}
mt := &metric{
namespace: m.Namespace(),
version: m.Version(),
config: config,
}
errs := s.validateMetric(mt)
if len(errs) > 0 {
serrs = append(serrs, errs...)
}
}
// add collectors to plugins (process and publishers)
for _, collector := range collectors {
plugins = append(plugins, collector)
}
// validate plugins
for _, plg := range plugins {
typ, err := core.ToPluginType(plg.TypeName())
if err != nil {
return []serror.SnapError{serror.New(err)}
}
plg.Config().ReverseMerge(
s.Config.Plugins.getPluginConfigDataNode(
typ, plg.Name(), plg.Version()))
errs := s.validatePluginSubscription(plg)
if len(errs) > 0 {
serrs = append(serrs, errs...)
return serrs
}
}
return
}
func (p *subscriptionGroups) validatePluginSubscription(pl core.SubscribedPlugin) []serror.SnapError {
var serrs = []serror.SnapError{}
controlLogger.WithFields(log.Fields{
"_block": "validate-plugin-subscription",
"plugin": fmt.Sprintf("%s:%d", pl.Name(), pl.Version()),
}).Info(fmt.Sprintf("validating dependencies for plugin %s:%d", pl.Name(), pl.Version()))
lp, err := p.pluginManager.get(fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", pl.TypeName(), pl.Name(), pl.Version()))
if err != nil {
se := serror.New(fmt.Errorf("Plugin not found: type(%s) name(%s) version(%d)", pl.TypeName(), pl.Name(), pl.Version()))
se.SetFields(map[string]interface{}{
"name": pl.Name(),
"version": pl.Version(),
"type": pl.TypeName(),
})
serrs = append(serrs, se)
return serrs
}
if lp.ConfigPolicy != nil {
ncd := lp.ConfigPolicy.Get([]string{""})
_, errs := ncd.Process(pl.Config().Table())
if errs != nil && errs.HasErrors() {
for _, e := range errs.Errors() {
se := serror.New(e)
se.SetFields(map[string]interface{}{"name": pl.Name(), "version": pl.Version()})
serrs = append(serrs, se)
}
}
}
return serrs
}
func (s *subscriptionGroups) validateMetric(
metric core.Metric) (serrs []serror.SnapError) {
m, err := s.metricCatalog.Get(metric.Namespace(),
metric.Version())
if err != nil {
serrs = append(serrs, serror.New(err, map[string]interface{}{
"name": metric.Namespace().String(),
"version": metric.Version(),
}))
return serrs
}
// No metric found return error.
if m == nil {
serrs = append(
serrs, serror.New(
fmt.Errorf("no metric found cannot subscribe: (%s) version(%d)",
metric.Namespace(), metric.Version())))
return serrs
}
m.config = metric.Config()
typ, serr := core.ToPluginType(m.Plugin.TypeName())
if serr != nil {
return []serror.SnapError{serror.New(err)}
}
// merge global plugin config
if m.config != nil {
m.config.ReverseMerge(
s.Config.Plugins.getPluginConfigDataNode(typ,
m.Plugin.Name(), m.Plugin.Version()))
} else {
m.config = s.Config.Plugins.getPluginConfigDataNode(typ,
m.Plugin.Name(), m.Plugin.Version())
}
// When a metric is added to the MetricCatalog, the policy of rules defined by the plugin is added to the metric's policy.
// If no rules are defined for a metric, we set the metric's policy to an empty ConfigPolicyNode.
// Checking m.policy for nil will not work, we need to check if rules are nil.
if m.policy.HasRules() {
if m.Config() == nil {
fields := log.Fields{
"metric": m.Namespace(),
"version": m.Version(),
"plugin": m.Plugin.Name(),
}
serrs = append(serrs, serror.New(ErrConfigRequiredForMetric, fields))
return serrs
}
ncdTable, errs := m.policy.Process(m.Config().Table())
if errs != nil && errs.HasErrors() {
for _, e := range errs.Errors() {
serrs = append(serrs, serror.New(e))
}
return serrs
}
m.config = cdata.FromTable(*ncdTable)
}
return serrs
}
func (s *subscriptionGroup) process(id string) (serrs []serror.SnapError) {
// gathers collectors based on requested metrics
metrics, plugins, serrs := s.getMetricsAndCollectors(
s.requestedMetrics, s.configTree)
controlLogger.WithFields(log.Fields{
"collectors": fmt.Sprintf("%+v", plugins),
"metrics": fmt.Sprintf("%+v", s.requestedMetrics),
}).Debug("gathered collectors")
//add processors and publishers to collectors just gathered
for _, plugin := range s.requestedPlugins {
if plugin.TypeName() != core.CollectorPluginType.String() {
plugins = append(plugins, plugin)
}
}
// calculates those plugins that need to be subscribed and unsubscribed to
subs, unsubs := comparePlugins(plugins, s.plugins)
controlLogger.WithFields(log.Fields{
"subs": fmt.Sprintf("%+v", subs),
"unsubs": fmt.Sprintf("%+v", unsubs),
}).Debug("subscriptions")
if len(subs) > 0 {
if errs := s.subscribePlugins(id, subs); errs != nil {
serrs = append(serrs, errs...)
}
}
if len(unsubs) > 0 {
if errs := s.unsubscribePlugins(id, unsubs); errs != nil {
serrs = append(serrs, errs...)
}
}
//updating view
s.metrics = metrics
s.plugins = plugins
s.errors = serrs
return serrs
}
func (s *subscriptionGroup) subscribePlugins(id string,
plugins []core.SubscribedPlugin) (serrs []serror.SnapError) {
for _, sub := range plugins {
controlLogger.WithFields(log.Fields{
"name": sub.Name(),
"type": sub.TypeName(),
"version": sub.Version(),
"_block": "subscriptionGroup.subscribePlugins",
}).Debug("plugin subscription")
if sub.Version() < 1 {
latest, err := s.pluginManager.get(fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", sub.TypeName(),
sub.Name(), sub.Version()))
if err != nil {
serrs = append(serrs, serror.New(err))
return serrs
}
pool, err := s.pluginRunner.AvailablePlugins().getOrCreatePool(latest.Key())
if err != nil {
serrs = append(serrs, serror.New(err))
return serrs
}
pool.Subscribe(id)
if pool.Eligible() {
err = s.verifyPlugin(latest)
if err != nil {
serrs = append(serrs, serror.New(err))
return serrs
}
err = s.pluginRunner.runPlugin(latest.Details)
if err != nil {
serrs = append(serrs, serror.New(err))
return serrs
}
}
} else {
pool, err := s.pluginRunner.AvailablePlugins().getOrCreatePool(fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d",
sub.TypeName(), sub.Name(), sub.Version()))
if err != nil {
serrs = append(serrs, serror.New(err))
return serrs
}
pool.Subscribe(id)
if pool.Eligible() {
pl, err := s.pluginManager.get(fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d",
sub.TypeName(), sub.Name(), sub.Version()))
if err != nil {
serrs = append(serrs, serror.New(err))
return serrs
}
err = s.verifyPlugin(pl)
if err != nil {
serrs = append(serrs, serror.New(err))
return serrs
}
err = s.pluginRunner.runPlugin(pl.Details)
if err != nil {
serrs = append(serrs, serror.New(err))
return serrs
}
}
}
serr := s.sendPluginSubscriptionEvent(id, sub)
if serr != nil {
serrs = append(serrs, serr)
}
}
return
}
func (p *subscriptionGroup) unsubscribePlugins(id string,
plugins []core.SubscribedPlugin) (serrs []serror.SnapError) {
for _, plugin := range plugins {
controlLogger.WithFields(log.Fields{
"name": plugin.Name(),
"type": plugin.TypeName(),
"version": plugin.Version(),
"_block": "subscriptionGroup.unsubscribePlugins",
}).Debug("plugin unsubscription")
pool, err := p.pluginRunner.AvailablePlugins().getPool(
fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", plugin.TypeName(),
plugin.Name(), plugin.Version()))
if err != nil {
serrs = append(serrs, err)
return serrs
}
if pool != nil {
pool.Unsubscribe(id)
}
serr := p.sendPluginUnsubscriptionEvent(id, plugin)
if serr != nil {
serrs = append(serrs, serr)
}
}
return
}
func (p *subscriptionGroup) sendPluginSubscriptionEvent(taskID string,
pl core.Plugin) serror.SnapError {
pt, err := core.ToPluginType(pl.TypeName())
if err != nil {
return serror.New(err)
}
e := &control_event.PluginSubscriptionEvent{
TaskId: taskID,
PluginType: int(pt),
PluginName: pl.Name(),
PluginVersion: pl.Version(),
}
if _, err := p.eventManager.Emit(e); err != nil {
return serror.New(err)
}
return nil
}
func (p *subscriptionGroup) sendPluginUnsubscriptionEvent(taskID string,
pl core.Plugin) serror.SnapError {
pt, err := core.ToPluginType(pl.TypeName())
if err != nil {
return serror.New(err)
}
e := &control_event.PluginUnsubscriptionEvent{
TaskId: taskID,
PluginType: int(pt),
PluginName: pl.Name(),
PluginVersion: pl.Version(),
}
if _, err := p.eventManager.Emit(e); err != nil {
return serror.New(err)
}
return nil
}
// comparePlugins compares the new state of plugins with the previous state.
// It returns an array of plugins that need to be subscribed and an array of
// plugins that need to be unsubscribed.
func comparePlugins(newPlugins,
oldPlugins []core.SubscribedPlugin) (adds,
removes []core.SubscribedPlugin) {
newMap := make(map[string]int)
oldMap := make(map[string]int)
for _, n := range newPlugins {
newMap[key(n)]++
}
for _, o := range oldPlugins {
oldMap[key(o)]++
}
for _, n := range newPlugins {
if oldMap[key(n)] > 0 {
oldMap[key(n)]--
continue
}
adds = append(adds, n)
}
for _, o := range oldPlugins {
if newMap[key(o)] > 0 {
newMap[key(o)]--
continue
}
removes = append(removes, o)
}
return
}
func key(p core.SubscribedPlugin) string {
return fmt.Sprintf("%v"+core.Separator+"%v"+core.Separator+"%v", p.TypeName(), p.Name(), p.Version())
}