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

Module id fail #5261

Merged
merged 24 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6db453b
push changes
mattdurham Sep 17, 2023
a2c2755
Add remove whenever using the component module.
mattdurham Sep 20, 2023
13c99ca
Add additional context and remove dead file.
mattdurham Sep 21, 2023
dfa0b77
Add long changelog comment
mattdurham Sep 21, 2023
01552ce
merge main
mattdurham Sep 21, 2023
f8f9930
fix linter
mattdurham Sep 21, 2023
8ceafb2
Remove mutex check
mattdurham Sep 21, 2023
54b238c
Add changes to support module id removal.
mattdurham Oct 5, 2023
91c0abe
Remove unneeded line
mattdurham Oct 5, 2023
f7caffe
main merge
mattdurham Oct 5, 2023
cb8d986
Fix merge errors.
mattdurham Oct 5, 2023
92cb1c1
Ensure module is checked on first run.
mattdurham Oct 10, 2023
f9e89e0
rename and add comments
mattdurham Oct 10, 2023
6d28509
Add manual removal back in and make test closer to actual usage.
mattdurham Oct 10, 2023
a96a32e
Merge branch 'main' into module_id_fail
mattdurham Oct 10, 2023
6c44b21
move changelog comment to correct location
mattdurham Oct 10, 2023
db23876
A different approach by keying off run instead of build.
mattdurham Oct 11, 2023
a9d509d
Add test for duplicate registration.
mattdurham Oct 11, 2023
e81a2ef
Minor changes
mattdurham Oct 12, 2023
e5588be
Merge branch 'main' into module_id_fail
mattdurham Oct 12, 2023
e69db8c
PR feedback
mattdurham Oct 16, 2023
acaae50
Merge remote-tracking branch 'origin/module_id_fail' into module_id_fail
mattdurham Oct 16, 2023
77777b0
add locks around the reads for tests, its a bit hacky.
mattdurham Oct 16, 2023
9b9f7ad
Merge branch 'main' into module_id_fail
mattdurham Oct 16, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ Main (unreleased)

- Fix `loki.source.file` race condition in cleaning up metrics when stopping to tail files. (@thampiotr)

- Fixed issue where adding a module after initial start, that failed to load then subsequently resolving the issue would cause the module to
permanently fail to load with `id already exists` error. (@mattdurham)

v0.36.1 (2023-09-06)
--------------------

Expand Down
2 changes: 2 additions & 0 deletions component/module/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ func New(o component.Options, args Arguments) (*Component, error) {

c.managedLocalFile, err = c.newManagedLocalComponent(o)
if err != nil {
m.Remove()
return nil, err
}
if err := c.Update(args); err != nil {
m.Remove()
return nil, err
}
return c, nil
Expand Down
1 change: 1 addition & 0 deletions component/module/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func New(o component.Options, args Arguments) (*Component, error) {
}

if err := c.Update(args); err != nil {
m.Remove()
return nil, err
}
return c, nil
Expand Down
2 changes: 2 additions & 0 deletions component/module/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ func New(o component.Options, args Arguments) (*Component, error) {

c.managedRemoteHTTP, err = c.newManagedLocalComponent(o)
if err != nil {
m.Remove()
return nil, err
}
if err := c.Update(args); err != nil {
m.Remove()
return nil, err
}
return c, nil
Expand Down
6 changes: 6 additions & 0 deletions component/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func (c *ModuleComponent) LoadFlowContent(args map[string]any, contentValue stri
return nil
}

// Remove removes the module from the registry. This is normally called after Run but in the case
// where the parent component fails to load and successfully run it must be called manually.
func (c *ModuleComponent) Remove() {
c.mod.Remove()
}

// RunFlowController runs the flow controller that all module components start.
func (c *ModuleComponent) RunFlowController(ctx context.Context) {
c.mod.Run(ctx)
Expand Down
2 changes: 2 additions & 0 deletions component/module/string/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (

// New creates a new module.string component.
func New(o component.Options, args Arguments) (*Component, error) {
var err error
m, err := module.NewModuleComponent(o)
if err != nil {
return nil, err
Expand All @@ -54,6 +55,7 @@ func New(o component.Options, args Arguments) (*Component, error) {
}

if err := c.Update(args); err != nil {
m.Remove()
return nil, err
}
return c, nil
Expand Down
7 changes: 7 additions & 0 deletions component/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type ModuleController interface {
// If id is non-empty, it must be a valid River identifier, matching the
// regex /[A-Za-z_][A-Za-z0-9_]/.
NewModule(id string, export ExportFunc) (Module, error)

// RemoveID is used to remove the id from the cache, this is normally called from Run but if
// the Module is created and then fails during loading then it will leak the id.
RemoveID(id string)
}

// Module is a controller for running components within a Module.
Expand All @@ -50,6 +54,9 @@ type Module interface {
// Run blocks until the provided context is canceled. The ID of a module as defined in
// ModuleController.NewModule will not be released until Run returns.
Run(context.Context)

// Remove is used to manually remove the module from the registry.
Remove()
}

// ExportFunc is used for onExport of the Module
Expand Down
10 changes: 8 additions & 2 deletions pkg/flow/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func (m *moduleController) NewModule(id string, export component.ExportFunc) (co
return mod, nil
}

func (m *moduleController) removeID(id string) {
// RemoveID removes a reference from the module registry and name cache.
func (m *moduleController) RemoveID(id string) {
m.mut.Lock()
defer m.mut.Unlock()

Expand Down Expand Up @@ -129,12 +130,17 @@ func (c *module) LoadConfig(config []byte, args map[string]any) error {
return c.f.LoadFile(ff, args)
}

// Remove removed the ID from the registry. Generally only used if fails to start.
func (c *module) Remove() {
c.o.parent.RemoveID(c.o.ID)
}

// Run starts the Module. No components within the Module
// will be run until Run is called.
//
// Run blocks until the provided context is canceled.
func (c *module) Run(ctx context.Context) {
defer c.o.parent.removeID(c.o.ID)
mattdurham marked this conversation as resolved.
Show resolved Hide resolved
defer c.o.parent.RemoveID(c.o.ID)
c.f.Run(ctx)
}

Expand Down
104 changes: 104 additions & 0 deletions pkg/flow/module_fail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package flow

import (
"context"
"fmt"
"testing"
"time"

"github.com/grafana/agent/component"
mod "github.com/grafana/agent/component/module"
"github.com/stretchr/testify/require"
)

func TestIDRemovalIfFailedToLoad(t *testing.T) {
f := New(testOptions(t))

fullContent := "test.fail.module \"t1\" { content = \"\" }"
fl, err := ReadFile("test", []byte(fullContent))
require.NoError(t, err)
err = f.LoadFile(fl, nil)
require.NoError(t, err)
ctx := context.Background()
ctx, cnc := context.WithTimeout(ctx, 600*time.Second)
defer cnc()
go f.Run(ctx)
require.Eventually(t, func() bool {
t1 := f.loader.Components()[0].Component().(*testFailModule)
return t1 != nil
}, 10*time.Second, 100*time.Millisecond)
t1 := f.loader.Components()[0].Component().(*testFailModule)
badContent :=
`test.fail.module "int" {
content=""
fail=true
}`
err = t1.updateContent(badContent)
require.Error(t, err)
goodContent :=
`test.fail.module "int" {
content=""
fail=false
}`
err = t1.updateContent(goodContent)
require.NoError(t, err)
}

func init() {
mattdurham marked this conversation as resolved.
Show resolved Hide resolved
component.Register(component.Registration{
Name: "test.fail.module",
Args: TestFailArguments{},
Exports: mod.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
m, err := mod.NewModuleComponent(opts)
if err != nil {
return nil, err
}
if args.(TestFailArguments).Fail {
m.Remove()
return nil, fmt.Errorf("module told to fail")
}
err = m.LoadFlowContent(nil, args.(TestFailArguments).Content)
if err != nil {
m.Remove()
return nil, err
}
return &testFailModule{
mc: m,
content: args.(TestFailArguments).Content,
opts: opts,
fail: args.(TestFailArguments).Fail,
ch: make(chan error),
}, nil
},
})
}

type TestFailArguments struct {
Content string `river:"content,attr"`
Fail bool `river:"fail,attr,optional"`
}

type testFailModule struct {
content string
opts component.Options
ch chan error
mc *mod.ModuleComponent
fail bool
}

func (t *testFailModule) Run(ctx context.Context) error {
<-ctx.Done()
return nil
}

func (t *testFailModule) updateContent(content string) error {
t.content = content
err := t.mc.LoadFlowContent(nil, t.content)
return err
}

func (t *testFailModule) Update(_ component.Arguments) error {
return nil
}