This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathavailable_plugin.go
671 lines (592 loc) · 18 KB
/
available_plugin.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
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"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/intelsdi-x/gomit"
"github.com/intelsdi-x/snap/control/plugin"
"github.com/intelsdi-x/snap/control/plugin/client"
"github.com/intelsdi-x/snap/control/strategy"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/control_event"
"github.com/intelsdi-x/snap/core/ctypes"
"github.com/intelsdi-x/snap/core/serror"
)
const (
// DefaultClientTimeout - default timeout for RPC method completion
DefaultClientTimeout = time.Second * 10
// DefaultHealthCheckTimeout - default timeout for a health check
DefaultHealthCheckTimeout = time.Second * 10
// DefaultHealthCheckFailureLimit - how any consecutive health check timeouts must occur to trigger a failure
DefaultHealthCheckFailureLimit = 3
)
var (
ErrPoolNotFound = errors.New("plugin pool not found")
ErrBadKey = errors.New("bad key")
ErrMsgInsecurePlugin = "secure framework can't connect to insecure plugin"
ErrMsgInsecureClient = "insecure framework can't connect to secure plugin"
)
// availablePlugin represents a plugin which is
// running and available to respond to requests
type availablePlugin struct {
meta plugin.PluginMeta
key string
pluginType plugin.PluginType
client client.PluginClient
name string
version int
id uint32
hitCount int
lastHitTime time.Time
emitter gomit.Emitter
failedHealthChecks int
healthChan chan error
ePlugin executablePlugin
execPath string
fromPackage bool
pprofPort string
isRemote bool
}
// newAvailablePlugin returns an availablePlugin with information from a
// plugin.Response
func newAvailablePlugin(resp plugin.Response, emitter gomit.Emitter, ep executablePlugin, security client.GRPCSecurity) (*availablePlugin, error) {
if security.TLSEnabled && !resp.Meta.TLSEnabled {
return nil, errors.New(ErrMsgInsecurePlugin + "; plugin_name: " + resp.Meta.Name)
}
if !security.TLSEnabled && resp.Meta.TLSEnabled {
return nil, errors.New(ErrMsgInsecureClient + "; plugin_name: " + resp.Meta.Name)
}
if resp.Type != plugin.CollectorPluginType && resp.Type != plugin.ProcessorPluginType && resp.Type != plugin.PublisherPluginType && resp.Type != plugin.StreamCollectorPluginType {
return nil, strategy.ErrBadType
}
ap := &availablePlugin{
meta: resp.Meta,
name: resp.Meta.Name,
version: resp.Meta.Version,
pluginType: resp.Type,
emitter: emitter,
healthChan: make(chan error, 1),
lastHitTime: time.Now(),
ePlugin: ep,
pprofPort: resp.PprofAddress,
isRemote: false,
}
ap.key = fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", ap.pluginType.String(), ap.name, ap.version)
// Create RPC Client
switch resp.Type {
case plugin.CollectorPluginType:
switch resp.Meta.RPCType {
case plugin.NativeRPC:
log.WithFields(log.Fields{
"_module": "control-aplugin",
"_block": "newAvailablePlugin",
"plugin_name": ap.name,
}).Warning("This plugin is using a deprecated RPC protocol. Find more information here: https://github.com/intelsdi-x/snap/issues/1289 ")
c, e := client.NewCollectorNativeClient(resp.ListenAddress, DefaultClientTimeout, resp.PublicKey, !resp.Meta.Unsecure)
if e != nil {
return nil, errors.New("error while creating client connection: " + e.Error())
}
ap.client = c
case plugin.GRPC:
c, e := client.NewCollectorGrpcClient(resp.ListenAddress, DefaultClientTimeout, security)
if e != nil {
return nil, errors.New("error while creating client connection: " + e.Error())
}
ap.client = c
case plugin.STREAMGRPC:
c, e := client.NewStreamCollectorGrpcClient(
resp.ListenAddress,
DefaultClientTimeout,
security)
if e != nil {
return nil, errors.New("error while creating client connection: " + e.Error())
}
ap.client = c
default:
return nil, errors.New("Invalid RPCTYPE")
}
case plugin.PublisherPluginType:
switch resp.Meta.RPCType {
case plugin.NativeRPC:
c, e := client.NewPublisherNativeClient(resp.ListenAddress, DefaultClientTimeout, resp.PublicKey, !resp.Meta.Unsecure)
if e != nil {
return nil, errors.New("error while creating client connection: " + e.Error())
}
ap.client = c
case plugin.GRPC:
c, e := client.NewPublisherGrpcClient(resp.ListenAddress, DefaultClientTimeout, security)
if e != nil {
return nil, errors.New("error while creating client connection: " + e.Error())
}
ap.client = c
default:
return nil, errors.New("Invalid RPCTYPE")
}
case plugin.ProcessorPluginType:
switch resp.Meta.RPCType {
case plugin.NativeRPC:
c, e := client.NewProcessorNativeClient(resp.ListenAddress, DefaultClientTimeout, resp.PublicKey, !resp.Meta.Unsecure)
if e != nil {
return nil, errors.New("error while creating client connection: " + e.Error())
}
ap.client = c
case plugin.GRPC:
c, e := client.NewProcessorGrpcClient(resp.ListenAddress, DefaultClientTimeout, security)
if e != nil {
return nil, errors.New("error while creating client connection: " + e.Error())
}
ap.client = c
default:
return nil, errors.New("Invalid RPCTYPE")
}
case plugin.StreamCollectorPluginType:
switch resp.Meta.RPCType {
case plugin.STREAMGRPC:
c, e := client.NewStreamCollectorGrpcClient(
resp.ListenAddress,
DefaultClientTimeout,
security)
if e != nil {
return nil, errors.New("error while creating client connection: " + e.Error())
}
ap.client = c
default:
return nil, errors.New("Invalid RPCTYPE")
}
default:
return nil, errors.New("Cannot create a client for a plugin of the type: " + resp.Type.String())
}
return ap, nil
}
func (a *availablePlugin) Port() string {
return a.pprofPort
}
func (a *availablePlugin) ID() uint32 {
return a.id
}
func (a *availablePlugin) SetID(id uint32) {
a.id = id
}
func (a *availablePlugin) Exclusive() bool {
return a.meta.Exclusive
}
func (a *availablePlugin) CacheTTL() time.Duration {
return a.meta.CacheTTL
}
func (a *availablePlugin) RoutingStrategy() plugin.RoutingStrategyType {
return a.meta.RoutingStrategy
}
func (a *availablePlugin) ConcurrencyCount() int {
return a.meta.ConcurrencyCount
}
func (a *availablePlugin) String() string {
return fmt.Sprintf("%s:%s:v%d:id%d", a.TypeName(), a.name, a.version, a.id)
}
func (a *availablePlugin) TypeName() string {
return a.pluginType.String()
}
func (a *availablePlugin) Type() plugin.PluginType {
return a.pluginType
}
func (a *availablePlugin) Name() string {
return a.name
}
func (a *availablePlugin) Version() int {
return a.version
}
func (a *availablePlugin) HitCount() int {
return a.hitCount
}
func (a *availablePlugin) LastHit() time.Time {
return a.lastHitTime
}
func (a *availablePlugin) IsRemote() bool {
return a.isRemote
}
func (a *availablePlugin) SetIsRemote(isRemote bool) {
a.isRemote = isRemote
}
// Stop halts a running availablePlugin
func (a *availablePlugin) Stop(r string) error {
log.WithFields(log.Fields{
"_module": "control-aplugin",
"block": "stop",
"plugin_name": a,
}).Info("stopping available plugin")
if a.IsRemote() {
return a.client.Close()
}
return a.client.Kill(r)
}
// Kill assumes a plugin is not able to hear a Kill RPC call
func (a *availablePlugin) Kill(r string) error {
log.WithFields(log.Fields{
"_module": "control-aplugin",
"block": "kill",
"plugin_name": a,
}).Info("hard killing available plugin")
if a.fromPackage {
log.WithFields(log.Fields{
"_module": "control-aplugin",
"block": "kill",
"plugin_name": a,
"pluginPath": a.execPath,
}).Debug("deleting available plugin package")
os.RemoveAll(filepath.Dir(a.execPath))
}
// If it's a streaming plugin, we need to signal the scheduler that
// this plugin is being killed.
if c, ok := a.client.(client.PluginStreamCollectorClient); ok {
c.Killed()
}
if a.ePlugin != nil {
return a.ePlugin.Kill()
}
return nil
}
// CheckHealth checks the health of a plugin and updates
// a.failedHealthChecks
func (a *availablePlugin) CheckHealth() {
go func() {
a.healthChan <- a.client.Ping()
}()
select {
case err := <-a.healthChan:
if err == nil {
if a.failedHealthChecks > 0 {
// only log on first ok health check
log.WithFields(log.Fields{
"_module": "control-aplugin",
"block": "check-health",
"plugin_name": a,
}).Debug("health is ok")
}
a.failedHealthChecks = 0
} else {
a.healthCheckFailed()
}
case <-time.After(DefaultHealthCheckTimeout):
a.healthCheckFailed()
}
}
// healthCheckFailed increments a.failedHealthChecks and emits a DisabledPluginEvent
// and a HealthCheckFailedEvent
func (a *availablePlugin) healthCheckFailed() {
log.WithFields(log.Fields{
"_module": "control-aplugin",
"block": "check-health",
"plugin_name": a,
}).Warning("heartbeat missed")
a.failedHealthChecks++
if a.failedHealthChecks >= DefaultHealthCheckFailureLimit {
log.WithFields(log.Fields{
"_module": "control-aplugin",
"block": "check-health",
"plugin_name": a,
}).Warning("heartbeat failed")
pde := &control_event.DeadAvailablePluginEvent{
Name: a.name,
Version: a.version,
Type: int(a.pluginType),
Key: a.key,
Id: a.ID(),
String: a.String(),
}
defer a.emitter.Emit(pde)
}
hcfe := &control_event.HealthCheckFailedEvent{
Name: a.name,
Version: a.version,
Type: int(a.pluginType),
}
defer a.emitter.Emit(hcfe)
}
type availablePlugins struct {
// Used to coordinate operations on the table.
*sync.RWMutex
// table holds all the plugin pools.
// The Pools' primary keys are equal to
// {plugin_type}:{plugin_name}:{plugin_version}
table map[string]strategy.Pool
}
func newAvailablePlugins() *availablePlugins {
return &availablePlugins{
RWMutex: &sync.RWMutex{},
table: make(map[string]strategy.Pool),
}
}
func (ap *availablePlugins) insert(pl *availablePlugin) error {
if pl.pluginType != plugin.CollectorPluginType && pl.pluginType != plugin.ProcessorPluginType && pl.pluginType != plugin.PublisherPluginType && pl.pluginType != plugin.StreamCollectorPluginType {
return strategy.ErrBadType
}
ap.Lock()
defer ap.Unlock()
key := fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", pl.TypeName(), pl.name, pl.version)
_, exists := ap.table[key]
if !exists {
p, err := strategy.NewPool(key, pl)
if err != nil {
return serror.New(ErrBadKey, map[string]interface{}{
"key": key,
})
}
ap.table[key] = p
return nil
}
ap.table[key].Insert(pl)
return nil
}
func (ap *availablePlugins) getPool(key string) (strategy.Pool, serror.SnapError) {
ap.RLock()
defer ap.RUnlock()
pool, ok := ap.table[key]
if !ok {
tnv := strings.Split(key, core.Separator)
if len(tnv) != 3 {
return nil, serror.New(ErrBadKey, map[string]interface{}{
"key": key,
})
}
v, err := strconv.Atoi(tnv[2])
if err != nil {
return nil, serror.New(ErrBadKey, map[string]interface{}{
"key": key,
})
}
if v < 1 {
return ap.findLatestPool(tnv[0], tnv[1])
}
// No key found
return nil, serror.New(ErrBadKey, map[string]interface{}{"key": key})
}
return pool, nil
}
func (ap *availablePlugins) collectMetrics(pluginKey string, metricTypes []core.Metric, taskID string) ([]core.Metric, error) {
var results []core.Metric
pool, serr := ap.getPool(pluginKey)
if serr != nil {
return nil, serr
}
if pool == nil {
return nil, serror.New(ErrPoolNotFound, map[string]interface{}{"pool-key": pluginKey})
}
// If the strategy is nil but the pool exists we likely are waiting on the pool to be fully initialized
// because of a plugin load/unload event that is currently being processed. Prevents panic from using nil
// RoutingAndCaching.
if pool.Strategy() == nil {
return nil, errors.New("Plugin strategy not set")
}
metricsToCollect, metricsFromCache := pool.CheckCache(metricTypes, taskID)
if len(metricsToCollect) == 0 {
return metricsFromCache, nil
}
config := metricTypes[0].Config()
cfg := map[string]ctypes.ConfigValue{}
if config != nil {
cfg = config.Table()
}
pool.RLock()
defer pool.RUnlock()
p, serr := pool.SelectAP(taskID, cfg)
if serr != nil {
return nil, serr
}
// cast client to PluginCollectorClient
cli, ok := p.(*availablePlugin).client.(client.PluginCollectorClient)
if !ok {
return nil, serror.New(errors.New("unable to cast client to PluginCollectorClient"))
}
// collect metrics
metrics, err := cli.CollectMetrics(metricsToCollect)
if err != nil {
return nil, serror.New(err)
}
pool.UpdateCache(metrics, taskID)
results = make([]core.Metric, len(metricsFromCache)+len(metrics))
idx := 0
for _, m := range metrics {
results[idx] = m
idx++
}
for _, m := range metricsFromCache {
results[idx] = m
idx++
}
// update plugin stats
p.(*availablePlugin).hitCount++
p.(*availablePlugin).lastHitTime = time.Now()
return results, nil
}
func (ap *availablePlugins) streamMetrics(
pluginKey string,
metricTypes []core.Metric,
taskID string,
maxCollectDuration time.Duration,
maxMetricsBuffer int64) (chan []core.Metric, chan error, error) {
pool, serr := ap.getPool(pluginKey)
if serr != nil {
return nil, nil, serr
}
if pool == nil {
return nil, nil, serror.New(ErrPoolNotFound, map[string]interface{}{"pool-key": pluginKey})
}
if pool.Strategy() == nil {
return nil, nil, errors.New("Plugin strategy not set")
}
config := metricTypes[0].Config()
cfg := map[string]ctypes.ConfigValue{}
if config != nil {
cfg = config.Table()
}
pool.RLock()
defer pool.RUnlock()
p, serr := pool.SelectAP(taskID, cfg)
if serr != nil {
return nil, nil, serr
}
cli, ok := p.(*availablePlugin).client.(client.PluginStreamCollectorClient)
if !ok {
return nil, nil, serror.New(errors.New("Invalid streaming client"))
}
metricChan, errChan, err := cli.StreamMetrics(taskID, metricTypes)
if err != nil {
return nil, nil, serror.New(err)
}
err = cli.UpdateCollectDuration(maxCollectDuration)
if err != nil {
return nil, nil, serror.New(err)
}
err = cli.UpdateMetricsBuffer(maxMetricsBuffer)
if err != nil {
return nil, nil, serror.New(err)
}
return metricChan, errChan, nil
}
func (ap *availablePlugins) publishMetrics(metrics []core.Metric, pluginName string, pluginVersion int, config map[string]ctypes.ConfigValue, taskID string) []error {
key := fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", plugin.PublisherPluginType.String(), pluginName, pluginVersion)
pool, serr := ap.getPool(key)
if serr != nil {
return []error{serr}
}
if pool == nil {
return []error{serror.New(ErrPoolNotFound, map[string]interface{}{"pool-key": key})}
}
pool.RLock()
defer pool.RUnlock()
p, serr := pool.SelectAP(taskID, config)
if serr != nil {
return []error{serr}
}
cli, ok := p.(*availablePlugin).client.(client.PluginPublisherClient)
if !ok {
return []error{errors.New("unable to cast client to PluginPublisherClient")}
}
err := cli.Publish(metrics, config)
if err != nil {
return []error{err}
}
p.(*availablePlugin).hitCount++
p.(*availablePlugin).lastHitTime = time.Now()
return nil
}
func (ap *availablePlugins) processMetrics(metrics []core.Metric, pluginName string, pluginVersion int, config map[string]ctypes.ConfigValue, taskID string) ([]core.Metric, []error) {
var errs []error
key := fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", plugin.ProcessorPluginType.String(), pluginName, pluginVersion)
pool, serr := ap.getPool(key)
if serr != nil {
errs = append(errs, serr)
return nil, errs
}
if pool == nil {
return nil, []error{serror.New(ErrPoolNotFound, map[string]interface{}{"pool-key": key})}
}
pool.RLock()
defer pool.RUnlock()
p, err := pool.SelectAP(taskID, config)
if err != nil {
errs = append(errs, err)
return nil, errs
}
cli, ok := p.(*availablePlugin).client.(client.PluginProcessorClient)
if !ok {
return nil, []error{errors.New("unable to cast client to PluginProcessorClient")}
}
mts, errp := cli.Process(metrics, config)
if errp != nil {
return nil, []error{errp}
}
p.(*availablePlugin).hitCount++
p.(*availablePlugin).lastHitTime = time.Now()
return mts, nil
}
func (ap *availablePlugins) findLatestPool(pType, name string) (strategy.Pool, serror.SnapError) {
// see if there exists a pool at all which matches name version.
var latest strategy.Pool
for key, pool := range ap.table {
tnv := strings.Split(key, core.Separator)
if tnv[0] == pType && tnv[1] == name && pool.Count() > 0 {
latest = pool
break
}
}
if latest != nil {
for key, pool := range ap.table {
tnv := strings.Split(key, core.Separator)
if tnv[0] == pType && tnv[1] == name && pool.Version() > latest.Version() && pool.Count() > 0 {
latest = pool
}
}
return latest, nil
}
return nil, nil
}
func (ap *availablePlugins) getOrCreatePool(key string) (strategy.Pool, error) {
ap.Lock()
defer ap.Unlock()
var err error
pool, ok := ap.table[key]
if ok {
return pool, nil
}
pool, err = strategy.NewPool(key)
if err != nil {
return nil, err
}
ap.table[key] = pool
return pool, nil
}
func (ap *availablePlugins) pools() map[string]strategy.Pool {
ap.RLock()
defer ap.RUnlock()
return ap.table
}
func (ap *availablePlugins) all() []strategy.AvailablePlugin {
var aps = []strategy.AvailablePlugin{}
ap.RLock()
defer ap.RUnlock()
for _, pool := range ap.table {
for _, ap := range pool.Plugins() {
aps = append(aps, ap)
}
}
return aps
}