-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix leak caused by input runners created when checking their configuration #23722
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
Changes from 8 commits
eba6805
18d5e08
4afe023
bd6d8b5
df2bcfe
7f2131e
92f11a0
526df51
904ec10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,7 +18,10 @@ | |||||
| package fileset | ||||||
|
|
||||||
| import ( | ||||||
| "fmt" | ||||||
|
|
||||||
| "github.com/gofrs/uuid" | ||||||
| "github.com/mitchellh/hashstructure" | ||||||
|
|
||||||
| "github.com/elastic/beats/v7/libbeat/beat" | ||||||
| "github.com/elastic/beats/v7/libbeat/cfgfile" | ||||||
|
|
@@ -27,9 +30,6 @@ import ( | |||||
| "github.com/elastic/beats/v7/libbeat/logp" | ||||||
| "github.com/elastic/beats/v7/libbeat/monitoring" | ||||||
| "github.com/elastic/beats/v7/libbeat/outputs/elasticsearch" | ||||||
| pubpipeline "github.com/elastic/beats/v7/libbeat/publisher/pipeline" | ||||||
|
|
||||||
| "github.com/mitchellh/hashstructure" | ||||||
| ) | ||||||
|
|
||||||
| var ( | ||||||
|
|
@@ -77,15 +77,9 @@ func NewFactory( | |||||
|
|
||||||
| // Create creates a module based on a config | ||||||
| func (f *Factory) Create(p beat.PipelineConnector, c *common.Config) (cfgfile.Runner, error) { | ||||||
| // Start a registry of one module: | ||||||
| m, err := NewModuleRegistry([]*common.Config{c}, f.beatInfo, false) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| pConfigs, err := m.GetInputConfigs() | ||||||
| m, pConfigs, err := f.createRegistry(c) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| return nil, fmt.Errorf("create module registry for filesets: %w", err) | ||||||
| } | ||||||
|
|
||||||
| // Hash module ID | ||||||
|
|
@@ -116,8 +110,36 @@ func (f *Factory) Create(p beat.PipelineConnector, c *common.Config) (cfgfile.Ru | |||||
| } | ||||||
|
|
||||||
| func (f *Factory) CheckConfig(c *common.Config) error { | ||||||
| _, err := f.Create(pubpipeline.NewNilPipeline(), c) | ||||||
| return err | ||||||
| _, pConfigs, err := f.createRegistry(c) | ||||||
| if err != nil { | ||||||
| return fmt.Errorf("create module registry for filesets: %w", err) | ||||||
|
||||||
| return fmt.Errorf("create module registry for filesets: %w", err) | |
| return fmt.Errorf("could not create module registry for filesets: %w", err) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| // +build !integration | ||
|
|
||
| package container | ||
|
|
||
| import ( | ||
| "os" | ||
| "path" | ||
| "testing" | ||
|
|
||
| "github.com/elastic/beats/v7/filebeat/input/inputtest" | ||
| "github.com/elastic/beats/v7/libbeat/common" | ||
| ) | ||
|
|
||
| func TestNewInputDone(t *testing.T) { | ||
| config := common.MapStr{ | ||
| "paths": path.Join(os.TempDir(), "logs", "*.log"), | ||
| } | ||
| inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| // +build !integration | ||
|
|
||
| package docker | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/elastic/beats/v7/filebeat/input/inputtest" | ||
| "github.com/elastic/beats/v7/libbeat/common" | ||
| ) | ||
|
|
||
| func TestNewInputDone(t *testing.T) { | ||
| config := common.MapStr{ | ||
| "containers.ids": "fad130edd3d2", | ||
| } | ||
| inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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 inputtest | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/elastic/beats/v7/filebeat/channel" | ||
| "github.com/elastic/beats/v7/filebeat/input" | ||
| "github.com/elastic/beats/v7/libbeat/beat" | ||
| "github.com/elastic/beats/v7/libbeat/common" | ||
| "github.com/elastic/beats/v7/libbeat/tests/resources" | ||
| ) | ||
|
|
||
| // Outlet is an empty outlet for testing. | ||
| type Outlet struct{} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have some "support" in order to create more extensive tests. e.g. e.g. the callback can be used to 'record' events in unit tests in order to validate them afterwards.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, but we wouldn't be using these features here by now, wdyt about adding them when there is a use case? |
||
|
|
||
| func (o Outlet) OnEvent(event beat.Event) bool { return true } | ||
| func (o Outlet) Close() error { return nil } | ||
| func (o Outlet) Done() <-chan struct{} { return nil } | ||
|
|
||
| // Connector is a connector to a test empty outlet. | ||
| var Connector = channel.ConnectorFunc( | ||
| func(_ *common.Config, _ beat.ClientConfig) (channel.Outleter, error) { | ||
| return Outlet{}, nil | ||
| }, | ||
| ) | ||
|
|
||
| // AssertNotStartedInputCanBeDone checks that the context of an input can be | ||
| // done before starting the input, and it doesn't leak goroutines. This is | ||
| // important to confirm that leaks don't happen with CheckConfig. | ||
| func AssertNotStartedInputCanBeDone(t *testing.T, factory input.Factory, configMap *common.MapStr) { | ||
| goroutines := resources.NewGoroutinesChecker() | ||
| defer goroutines.Check(t) | ||
|
|
||
| config, err := common.NewConfigFrom(configMap) | ||
| require.NoError(t, err) | ||
|
|
||
| context := input.Context{ | ||
| Done: make(chan struct{}), | ||
| } | ||
|
|
||
| _, err = factory(config, Connector, context) | ||
| assert.NoError(t, err) | ||
|
|
||
| close(context.Done) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| // +build !integration | ||
|
|
||
| package kafka | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/elastic/beats/v7/filebeat/input/inputtest" | ||
| "github.com/elastic/beats/v7/libbeat/common" | ||
| ) | ||
|
|
||
| func TestNewInputDone(t *testing.T) { | ||
| config := common.MapStr{ | ||
| "hosts": "localhost:9092", | ||
| "topics": "messages", | ||
| "group_id": "filebeat", | ||
| } | ||
| inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| // +build !integration | ||
|
|
||
| package redis | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/elastic/beats/v7/filebeat/input/inputtest" | ||
| "github.com/elastic/beats/v7/libbeat/common" | ||
| ) | ||
|
|
||
| func TestNewInputDone(t *testing.T) { | ||
| config := common.MapStr{ | ||
| "hosts": "localhost:3679", | ||
| } | ||
| inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on changing the wording that something did not work out
Disclaimer: I'm no native speaker
I'd prefer if our logs do not use
didn't,couldn'tor similar. Instead put some emphasis on thenotby usingcould not,can not,must not. If something "Failed", let's usefailed toThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼 edited to use the full form of negation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, adding
could not.I have been changing my opinion on these kind of messages over time. Lately I am wondering if, when wrapping, it is not enough to mention what was being done. The wrapped error
erralready has the root cause, and the caller knows how it affects upper layers or the user, the context provided in places like here give an orientation of what was being done.Thinking in a made up example, these are the kind of messages logged when one or the other strategy are used:
Failed to read configuration: could not read configuration from file /somefile: could not open file: permission denied.Failed to read configuration: read configuration from file /somefile: open file: permission denied.Both messages provide the same information: what failed, what was it trying to do and what was the root cause of the issue. In the first version the
could nots don't provide additional information, and when many errors are chained they can look too redundant. Second option looks enough, more compact and easier to read.In the code, the message looks more complete when mentioning that something didn't went as expected, but this information is actually redundant with the fact that an error is being wrapped and returned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, part of the problem is we do not capture stack traces (or locations) on errors + generic error types normally do not carry additional 'context' (and we do not define our own error types when we should). Plus there is no generic support to add diagnostic context to add extra information to an error value (I'm using sderr like
sderr.With("file", ...).Wrap(err, "read configuration")if I need this). Aaannnd we do not spend too much time thinking how the final log message might look like :)If you look at the error from the stdlib, it includes the operation and the cause like
open file: permission denied. In order to create user-readable errors one might use an encoding like<operation>: <optional context>: <optional message>: <cause>, where<cause>should follow a similar pattern.Plus our logs are sometimes redundant to our error message. Following the aforementioned schema + reduce redundancy in the log (we do not need to mention that "reading configuration" failed), the log message would become:
Error: read configuration: filename /somefile: open file: permission denied.Beside the
Errorand thepermission deniedthere is no mention offailing,can not, or similar. The message more or less resembles that call stack (at the cost of having "readable" english).But our logs/errors normally follow the schema
Error doing X: failed to <message similar to X>: could not <message>: <cause>. Although it does not read nice, we start each sentence with "failed" and might want to keep that.Some related readings: