-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add test for elasticsearch re-connection after network error & allow graceful shutdown #40794
Changes from all commits
c5bb19c
1e02543
59541dc
76d986b
fe2140a
f8411f9
fcab79c
4b41bb6
d76c0d2
6cef800
2e74ea4
198b01b
fc20198
3ca5b9c
5b962d2
459b869
eee35b7
ab39277
5f6edcc
09c7581
8a56f6e
5f04323
f03154b
f3502a3
5e09644
f79c8a6
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 |
---|---|---|
|
@@ -16057,6 +16057,29 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected]/LI | |
limitations under the License. | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/elastic/mock-es | ||
Version: v0.0.0-20240712014503-e5b47ece0015 | ||
Licence type (autodetected): Apache-2.0 | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected]/LICENSE: | ||
|
||
Copyright 2024 Elasticsearch B.V. | ||
|
||
Licensed 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. | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/elastic/tk-btf | ||
Version: v0.1.0 | ||
|
@@ -48374,6 +48397,36 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/mileusna/useragent | ||
Version: v1.3.4 | ||
Licence type (autodetected): MIT | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/github.com/mileusna/[email protected]/LICENSE.md: | ||
|
||
MIT License | ||
|
||
Copyright (c) 2017 Miloš Mileusnić | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/minio/asm2plan9s | ||
Version: v0.0.0-20200509001527-cdd76441f9d8 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
package beater | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"path/filepath" | ||
|
@@ -195,14 +196,16 @@ func (fb *Filebeat) setupPipelineLoaderCallback(b *beat.Beat) error { | |
|
||
overwritePipelines := true | ||
b.OverwritePipelinesCallback = func(esConfig *conf.C) error { | ||
esClient, err := eslegclient.NewConnectedClient(esConfig, "Filebeat") | ||
ctx, cancel := context.WithCancel(context.TODO()) | ||
defer cancel() | ||
esClient, err := eslegclient.NewConnectedClient(ctx, esConfig, "Filebeat") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// When running the subcommand setup, configuration from modules.d directories | ||
// have to be loaded using cfg.Reloader. Otherwise those configurations are skipped. | ||
pipelineLoaderFactory := newPipelineLoaderFactory(b.Config.Output.Config()) | ||
pipelineLoaderFactory := newPipelineLoaderFactory(ctx, b.Config.Output.Config()) | ||
enableAllFilesets, _ := b.BeatConfig.Bool("config.modules.enable_all_filesets", -1) | ||
forceEnableModuleFilesets, _ := b.BeatConfig.Bool("config.modules.force_enable_module_filesets", -1) | ||
filesetOverrides := fileset.FilesetOverrides{ | ||
|
@@ -322,14 +325,6 @@ func (fb *Filebeat) Run(b *beat.Beat) error { | |
outDone := make(chan struct{}) // outDone closes down all active pipeline connections | ||
pipelineConnector := channel.NewOutletFactory(outDone).Create | ||
|
||
// Create a ES connection factory for dynamic modules pipeline loading | ||
var pipelineLoaderFactory fileset.PipelineLoaderFactory | ||
if b.Config.Output.Name() == "elasticsearch" { | ||
pipelineLoaderFactory = newPipelineLoaderFactory(b.Config.Output.Config()) | ||
} else { | ||
logp.Warn(pipelinesWarning) | ||
} | ||
|
||
inputsLogger := logp.NewLogger("input") | ||
v2Inputs := fb.pluginFactory(b.Info, inputsLogger, stateStore) | ||
v2InputLoader, err := v2.NewLoader(inputsLogger, v2Inputs, "type", cfg.DefaultType) | ||
|
@@ -350,8 +345,22 @@ func (fb *Filebeat) Run(b *beat.Beat) error { | |
compat.RunnerFactory(inputsLogger, b.Info, v2InputLoader), | ||
input.NewRunnerFactory(pipelineConnector, registrar, fb.done), | ||
)) | ||
moduleLoader := fileset.NewFactory(inputLoader, b.Info, pipelineLoaderFactory, config.OverwritePipelines) | ||
|
||
// Create a ES connection factory for dynamic modules pipeline loading | ||
var pipelineLoaderFactory fileset.PipelineLoaderFactory | ||
// The pipelineFactory needs a context to control the connections to ES, | ||
// when the pipelineFactory/ESClient are not needed any more the context | ||
// must be cancelled. This pipeline factory will be used by the moduleLoader | ||
// that is run by a crawler, whenever this crawler is stopped we also cancel | ||
// the context. | ||
pipelineFactoryCtx, cancelPipelineFactoryCtx := context.WithCancel(context.Background()) | ||
defer cancelPipelineFactoryCtx() | ||
if b.Config.Output.Name() == "elasticsearch" { | ||
pipelineLoaderFactory = newPipelineLoaderFactory(pipelineFactoryCtx, b.Config.Output.Config()) | ||
} else { | ||
logp.Warn(pipelinesWarning) | ||
} | ||
moduleLoader := fileset.NewFactory(inputLoader, b.Info, pipelineLoaderFactory, config.OverwritePipelines) | ||
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. Also for my understanding just wondering why metricbeat does not use in beaters any eslegclient.NewConnectedClient ? I mean why this pr dooes not affect metricbeat? 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. Honestly, I'm not 100% sure, that's the first time I'm touching this part of the codebase. However on a quick look at Metricbeat's code, it seems it does not load any ingest pipelines and it might not rely on |
||
crawler, err := newCrawler(inputLoader, moduleLoader, config.Inputs, fb.done, *once) | ||
if err != nil { | ||
logp.Err("Could not init crawler: %v", err) | ||
|
@@ -389,6 +398,7 @@ func (fb *Filebeat) Run(b *beat.Beat) error { | |
err = crawler.Start(fb.pipeline, config.ConfigInput, config.ConfigModules) | ||
if err != nil { | ||
crawler.Stop() | ||
cancelPipelineFactoryCtx() | ||
return fmt.Errorf("Failed to start crawler: %w", err) | ||
} | ||
|
||
|
@@ -444,6 +454,7 @@ func (fb *Filebeat) Run(b *beat.Beat) error { | |
modules.Stop() | ||
adiscover.Stop() | ||
crawler.Stop() | ||
cancelPipelineFactoryCtx() | ||
|
||
timeout := fb.config.ShutdownTimeout | ||
// Checks if on shutdown it should wait for all events to be published | ||
|
@@ -487,9 +498,9 @@ func (fb *Filebeat) Stop() { | |
} | ||
|
||
// Create a new pipeline loader (es client) factory | ||
func newPipelineLoaderFactory(esConfig *conf.C) fileset.PipelineLoaderFactory { | ||
func newPipelineLoaderFactory(ctx context.Context, esConfig *conf.C) fileset.PipelineLoaderFactory { | ||
pipelineLoaderFactory := func() (fileset.PipelineLoader, error) { | ||
esClient, err := eslegclient.NewConnectedClient(esConfig, "Filebeat") | ||
esClient, err := eslegclient.NewConnectedClient(ctx, esConfig, "Filebeat") | ||
if err != nil { | ||
return nil, fmt.Errorf("Error creating Elasticsearch client: %w", err) | ||
} | ||
|
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.
Why context is needed here? (Sorry as not so familiar with this part of code) I see that pipelineLoaderFactory is called inside NewFactory. So is not better to pass the context there?
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.
This context controls the lifecycle of the connection used to create the pipeline. It ensures that by the time the callback is done, all connections/requests it created are also shutdown.
The context is passed to
newPipelineLoaderFactory
https://github.com/belimawr/beats/blob/cfd1f1cd26450e2c77d3f73f0656caa76f183ff1/filebeat/beater/filebeat.go#L205 and then passed to
eslegclient.NewConnectedClient
https://github.com/belimawr/beats/blob/cfd1f1cd26450e2c77d3f73f0656caa76f183ff1/filebeat/beater/filebeat.go#L492