示例#1
0
func (p *apPool) applyPluginMeta(ap *availablePlugin) error {
	// Checking if plugin is exclusive
	// (only one instance should be running).
	if ap.meta.Exclusive {
		p.max = 1
	}

	// Set the cache TTL
	cacheTTL := strategy.GlobalCacheExpiration
	// if the plugin exposes a default TTL that is greater the the global default use it
	if ap.meta.CacheTTL != 0 && ap.meta.CacheTTL > strategy.GlobalCacheExpiration {
		cacheTTL = ap.meta.CacheTTL
	}

	// Set the routing and caching strategy
	switch ap.meta.RoutingStrategy {
	case plugin.DefaultRouting:
		p.strategy = strategy.NewLRU(cacheTTL)
	default:
		return ErrBadStrategy
	}

	// set concurrency count
	p.concurrencyCount = ap.meta.ConcurrencyCount

	return nil
}
示例#2
0
func TestRoutingCachingStrategy(t *testing.T) {
	Convey("Given loaded plugins that use sticky routing", t, func() {
		config := NewConfig()
		config.Plugins.All.AddItem("password", ctypes.ConfigValueStr{Value: "testval"})
		c := New(OptSetConfig(config))
		c.Start()
		lpe := newListenToPluginEvent()
		c.eventManager.RegisterHandler("Control.PluginLoaded", lpe)
		_, e := load(c, PluginPath)
		So(e, ShouldBeNil)
		if e != nil {
			t.FailNow()
		}
		metric, err := c.metricCatalog.Get([]string{"intel", "mock", "foo"}, 2)
		So(err, ShouldBeNil)
		So(metric.NamespaceAsString(), ShouldResemble, "/intel/mock/foo")
		So(err, ShouldBeNil)
		<-lpe.done
		Convey("Start the plugins", func() {
			lp, err := c.pluginManager.get("collector:mock:2")
			So(err, ShouldBeNil)
			So(lp, ShouldNotBeNil)
			pool, errp := c.pluginRunner.AvailablePlugins().getOrCreatePool("collector:mock:2")
			So(errp, ShouldBeNil)
			So(pool, ShouldNotBeNil)
			tasks := []string{
				uuid.New(),
				uuid.New(),
				uuid.New(),
				uuid.New(),
				uuid.New(),
			}
			for _, id := range tasks {
				pool.Subscribe(id, strategy.BoundSubscriptionType)
				err = c.pluginRunner.runPlugin(lp.Details)
				So(err, ShouldBeNil)
			}
			// The cache ttl should be 100ms which is what the plugin exposed (no system default was provided)
			ttl, err := pool.CacheTTL(tasks[0])
			So(err, ShouldBeNil)
			So(ttl, ShouldResemble, 100*time.Millisecond)
			So(pool.Strategy(), ShouldHaveSameTypeAs, strategy.NewSticky(ttl))
			So(pool.Count(), ShouldEqual, len(tasks))
			So(pool.SubscriptionCount(), ShouldEqual, len(tasks))
			Convey("Collect metrics", func() {
				taskID := tasks[rand.Intn(len(tasks))]
				for i := 0; i < 10; i++ {
					_, errs := c.CollectMetrics([]core.Metric{metric}, time.Now().Add(time.Second*1), taskID)
					So(errs, ShouldBeEmpty)
				}
				Convey("Check cache stats", func() {
					So(pool.AllCacheHits(), ShouldEqual, 9)
					So(pool.AllCacheMisses(), ShouldEqual, 1)
				})
			})
		})
	})

	Convey("Given loaded plugins that use least-recently-used routing", t, func() {
		c := New()
		c.Start()
		c.Config.Plugins.Collector.Plugins["mock"] = newPluginConfigItem(
			optAddPluginConfigItem("test", ctypes.ConfigValueBool{Value: true}),
			optAddPluginConfigItem("user", ctypes.ConfigValueStr{Value: "jane"}),
			optAddPluginConfigItem("password", ctypes.ConfigValueStr{Value: "doe"}),
		)
		lpe := newListenToPluginEvent()
		c.eventManager.RegisterHandler("Control.PluginLoaded", lpe)
		_, e := load(c, JSONRPCPluginPath)
		So(e, ShouldBeNil)
		if e != nil {
			t.FailNow()
		}
		metric, err := c.metricCatalog.Get([]string{"intel", "mock", "foo"}, 1)
		metric.config = cdata.NewNode()
		So(err, ShouldBeNil)
		So(metric.NamespaceAsString(), ShouldResemble, "/intel/mock/foo")
		So(err, ShouldBeNil)
		<-lpe.done
		Convey("Start the plugins", func() {
			lp, err := c.pluginManager.get("collector:mock:1")
			So(err, ShouldBeNil)
			So(lp, ShouldNotBeNil)
			pool, errp := c.pluginRunner.AvailablePlugins().getOrCreatePool("collector:mock:1")
			time.Sleep(1 * time.Second)
			So(errp, ShouldBeNil)
			So(pool, ShouldNotBeNil)
			tasks := []string{
				uuid.New(),
				uuid.New(),
				uuid.New(),
				uuid.New(),
				uuid.New(),
			}
			for _, id := range tasks {
				pool.Subscribe(id, strategy.BoundSubscriptionType)
				err = c.pluginRunner.runPlugin(lp.Details)
				So(err, ShouldBeNil)
			}
			// The cache ttl should be 100ms which is what the plugin exposed (no system default was provided)
			ttl, err := pool.CacheTTL(tasks[0])
			So(err, ShouldBeNil)
			So(ttl, ShouldResemble, 1100*time.Millisecond)
			So(pool.Strategy(), ShouldHaveSameTypeAs, strategy.NewLRU(ttl))
			So(pool.Count(), ShouldEqual, len(tasks))
			So(pool.SubscriptionCount(), ShouldEqual, len(tasks))
			Convey("Collect metrics", func() {
				taskID := tasks[rand.Intn(len(tasks))]
				for i := 0; i < 10; i++ {
					cr, errs := c.CollectMetrics([]core.Metric{metric}, time.Now().Add(time.Second*1), taskID)
					So(errs, ShouldBeEmpty)
					for i := range cr {
						So(cr[i].Data(), ShouldContainSubstring, "The mock collected data!")
						So(cr[i].Data(), ShouldContainSubstring, "test=true")
					}
				}
				Convey("Check cache stats", func() {
					So(pool.AllCacheHits(), ShouldEqual, 9)
					So(pool.AllCacheMisses(), ShouldEqual, 1)
				})
			})
		})
	})
}