Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge scope hierarchy for evaluation #2151

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion internal/runtime/internal/controller/value_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (vc *valueCache) BuildContext() *vm.Scope {
vc.mut.RLock()
defer vc.mut.RUnlock()

scope := vm.NewScopeWithParent(vc.scope, make(map[string]interface{}))
scope := vm.NewScope(make(map[string]interface{}))

// First, partition components by Alloy block name.
var componentsByBlockName = make(map[string][]ComponentID)
Expand Down Expand Up @@ -199,6 +199,11 @@ func (vc *valueCache) BuildContext() *vm.Scope {
}
}

if vc.scope != nil {
// Merges the current scope with the one built from the components at this level.
scope.Variables = deepMergeMaps(vc.scope.Variables, scope.Variables)
}

return scope
}

Expand Down Expand Up @@ -234,3 +239,26 @@ func (vc *valueCache) buildValue(from []ComponentID, offset int) interface{} {
}
return attrs
}

// deepMergeMaps merges two maps. It uses the values of map2 in case of conflict.
wildum marked this conversation as resolved.
Show resolved Hide resolved
func deepMergeMaps(map1, map2 map[string]any) map[string]any {
merged := make(map[string]any, len(map1)+len(map2))

for key, value := range map1 {
merged[key] = value
}

for key, value := range map2 {
if existingValue, exists := merged[key]; exists {
if map1Value, ok1 := existingValue.(map[string]any); ok1 {
if map2Value, ok2 := value.(map[string]any); ok2 {
merged[key] = deepMergeMaps(map1Value, map2Value)
continue
}
}
}
merged[key] = value
}

return merged
}
119 changes: 119 additions & 0 deletions internal/runtime/internal/controller/value_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"testing"

"github.com/grafana/alloy/syntax/vm"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -174,3 +175,121 @@ func TestModuleArgumentCache(t *testing.T) {
})
}
}

func TestBuildContextWithScope(t *testing.T) {
vc := newValueCache()
scope := vm.NewScope(
map[string]any{
"test": map[string]any{
"scope": barArgs{Number: 13},
},
},
)
vc.SetScope(scope)
vc.CacheExports(ComponentID{"foo", "bar"}, barArgs{Number: 12})
res := vc.BuildContext()

expected := map[string]any{
"test": map[string]any{
"scope": barArgs{
Number: 13,
},
},
"foo": map[string]any{
"bar": barArgs{
Number: 12,
},
},
}
require.Equal(t, expected, res.Variables)
}

func TestBuildContextWithScopeConflict(t *testing.T) {
vc := newValueCache()
scope := vm.NewScope(
map[string]any{
"test": map[string]any{
"scope": barArgs{Number: 13},
},
},
)
vc.SetScope(scope)
vc.CacheExports(ComponentID{"test", "bar"}, barArgs{Number: 12})
res := vc.BuildContext()

expected := map[string]any{
"test": map[string]any{
"scope": barArgs{
Number: 13,
},
"bar": barArgs{
Number: 12,
},
},
}
require.Equal(t, expected, res.Variables)
}

func TestBuildContextWithScopeOverride(t *testing.T) {
vc := newValueCache()
scope := vm.NewScope(
map[string]any{
"test": map[string]any{
"scope": barArgs{Number: 13},
},
},
)
vc.SetScope(scope)
vc.CacheExports(ComponentID{"test", "scope"}, barArgs{Number: 12})
res := vc.BuildContext()

expected := map[string]any{
"test": map[string]any{
"scope": barArgs{
Number: 12,
},
},
}
require.Equal(t, expected, res.Variables)

originalScope := vm.NewScope(
map[string]any{
"test": map[string]any{
"scope": barArgs{Number: 13},
},
},
)
require.Equal(t, vc.scope, originalScope) // ensure that the original scope is not modified after building the context
}

func TestScopeMergeMoreNesting(t *testing.T) {
vc := newValueCache()
scope := vm.NewScope(
map[string]any{
"test": map[string]any{
"cp1": map[string]any{
"scope": barArgs{Number: 13},
},
"cp2": barArgs{Number: 12},
},
},
)
vc.SetScope(scope)
vc.CacheExports(ComponentID{"test", "cp1", "foo"}, barArgs{Number: 12})
vc.CacheExports(ComponentID{"test", "cp1", "bar", "fizz"}, barArgs{Number: 2})
res := vc.BuildContext()

expected := map[string]any{
"test": map[string]any{
"cp1": map[string]any{
"scope": barArgs{Number: 13},
"foo": barArgs{Number: 12},
"bar": map[string]any{
"fizz": barArgs{Number: 2},
},
},
"cp2": barArgs{Number: 12},
},
}
require.Equal(t, expected, res.Variables)
}
20 changes: 3 additions & 17 deletions syntax/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func New(node ast.Node) *Evaluator {
//
// Each call to Evaluate may provide a different scope with new values for
// available variables. If a variable used by the Evaluator's node isn't
// defined in scope or any of the parent scopes, Evaluate will return an error.
// defined in scope, Evaluate will return an error.
func (vm *Evaluator) Evaluate(scope *Scope, v interface{}) (err error) {
// Track a map that allows us to associate values with ast.Nodes so we can
// return decorated error messages.
Expand Down Expand Up @@ -455,11 +455,6 @@ func (vm *Evaluator) evaluateExpr(scope *Scope, assoc map[value.Value]ast.Node,

// A Scope exposes a set of variables available to use during evaluation.
type Scope struct {
// Parent optionally points to a parent Scope containing more variable.
// Variables defined in children scopes take precedence over variables of the
// same name found in parent scopes.
Parent *Scope

// Variables holds the list of available variable names that can be used when
// evaluating a node.
//
Expand All @@ -475,22 +470,13 @@ func NewScope(variables map[string]interface{}) *Scope {
}
}

func NewScopeWithParent(parent *Scope, variables map[string]interface{}) *Scope {
return &Scope{
Parent: parent,
Variables: variables,
}
}

// Lookup looks up a named identifier from the scope, all of the scope's
// parents, and the stdlib.
// Lookup looks up a named identifier from the scope and the stdlib.
func (s *Scope) Lookup(name string) (interface{}, bool) {
// Traverse the scope first, then fall back to stdlib.
for s != nil {
if s != nil {
wildum marked this conversation as resolved.
Show resolved Hide resolved
if val, ok := s.Variables[name]; ok {
return val, true
}
s = s.Parent
}
if ident, ok := stdlib.Identifiers[name]; ok {
return ident, true
Expand Down
Loading