From 0a2393f4e0f21f093cf5027c176d52df80fb96cf Mon Sep 17 00:00:00 2001 From: Liu Haikuo Date: Thu, 21 Mar 2019 18:00:50 +0800 Subject: [PATCH 1/2] Add builtinFuncs to ExecutionState.Funcs as desired to reduce memory usage. --- tick/stateful/eval_function_node.go | 2 +- tick/stateful/execution_state.go | 2 +- tick/stateful/functions.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/tick/stateful/eval_function_node.go b/tick/stateful/eval_function_node.go index c702c8447..2705e9f53 100644 --- a/tick/stateful/eval_function_node.go +++ b/tick/stateful/eval_function_node.go @@ -259,7 +259,7 @@ func eval(n NodeEvaluator, scope *Scope, executionState ExecutionState) (interfa } func lookupFunc(name string, funcs Funcs, scope ReadOnlyScope) Func { - f := funcs[name] + f := funcs.Get(name) if f != nil { return f } diff --git a/tick/stateful/execution_state.go b/tick/stateful/execution_state.go index 02360816b..c6baf66fc 100644 --- a/tick/stateful/execution_state.go +++ b/tick/stateful/execution_state.go @@ -8,7 +8,7 @@ type ExecutionState struct { func CreateExecutionState() ExecutionState { return ExecutionState{ - Funcs: NewFunctions(), + Funcs: NewEmptyFuncs(), } } diff --git a/tick/stateful/functions.go b/tick/stateful/functions.go index c9ea01fc4..1cf1d18bc 100644 --- a/tick/stateful/functions.go +++ b/tick/stateful/functions.go @@ -132,6 +132,29 @@ func FuncDomains(f Func) Domains { // Lookup for functions type Funcs map[string]Func +func (funcs Funcs) Get(name string) Func { + if f := funcs[name]; f != nil { + return f + } + if f := statelessFuncs[name]; f != nil { + funcs[name] = f + return f + } + switch name { + case "sigma": + funcs[name] = &sigma{} + return funcs[name] + case "count": + funcs[name] = &count{} + return funcs[name] + case "spread": + funcs[name] = &spread{min: math.Inf(+1), max: math.Inf(-1)} + return funcs[name] + default: + return nil + } +} + var statelessFuncs Funcs var builtinFuncs Funcs @@ -252,6 +275,11 @@ func NewFunctions() Funcs { return funcs } +// Return empty Funcs +func NewEmptyFuncs() Funcs { + return make(Funcs, 0) +} + type math1Func func(float64) float64 type math1 struct { name string From 0c2dfd4a43a5786e7a3dcdd9838f775af37951cb Mon Sep 17 00:00:00 2001 From: xiaobeiyang <48786770+xiaobeiyang@users.noreply.github.com> Date: Thu, 21 Mar 2019 18:12:19 +0800 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef2465ba4..706af9aa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Features - [#2154](https://github.com/influxdata/kapacitor/pull/2154): Add ability to skip ssl verification with an alert post node. Thanks @itsHabib! +- [#2177](https://github.com/influxdata/kapacitor/issues/2177): NewFunctions func occupies many memory when state_count has large working_cardinality ### Bugfixes