-
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
[libbeat] Fix / clarify the module reload logic #16440
Changes from all commits
276c6cb
b987226
3a64cae
9982772
7cd61b0
beadaf3
5066edc
a14ecc5
c1ca81b
11cdbf5
3840fcc
181f081
eb763e8
fdf2c03
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 |
---|---|---|
|
@@ -45,6 +45,10 @@ var ( | |
|
||
debugf = logp.MakeDebug("cfgfile") | ||
|
||
// configScans measures how many times the config dir was scanned for | ||
// changes, configReloads measures how many times there were changes that | ||
// triggered an actual reload. | ||
configScans = monitoring.NewInt(nil, "libbeat.config.scans") | ||
configReloads = monitoring.NewInt(nil, "libbeat.config.reloads") | ||
moduleStarts = monitoring.NewInt(nil, "libbeat.config.module.starts") | ||
moduleStops = monitoring.NewInt(nil, "libbeat.config.module.stops") | ||
|
@@ -185,7 +189,11 @@ func (rl *Reloader) Run(runnerFactory RunnerFactory) { | |
rl.config.Reload.Period = 0 | ||
} | ||
|
||
overwriteUpdate := true | ||
// If forceReload is set, the configuration should be reloaded | ||
// even if there are no changes. It is set on the first iteration, | ||
// and whenever an attempted reload fails. It is unset whenever | ||
// a reload succeeds. | ||
forceReload := true | ||
|
||
for { | ||
select { | ||
|
@@ -195,7 +203,7 @@ func (rl *Reloader) Run(runnerFactory RunnerFactory) { | |
|
||
case <-time.After(rl.config.Reload.Period): | ||
debugf("Scan for new config files") | ||
configReloads.Add(1) | ||
configScans.Add(1) | ||
|
||
files, updated, err := gw.Scan() | ||
if err != nil { | ||
|
@@ -204,21 +212,22 @@ func (rl *Reloader) Run(runnerFactory RunnerFactory) { | |
logp.Err("Error fetching new config files: %v", err) | ||
} | ||
|
||
// no file changes | ||
if !updated && !overwriteUpdate { | ||
overwriteUpdate = false | ||
// if there are no changes, skip this reload unless forceReload is set. | ||
if !updated && !forceReload { | ||
continue | ||
} | ||
configReloads.Add(1) | ||
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. Wondering if the config reload counter should only increase when it was successful? And have an additional error counter for debugging? 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. I'm hesitant, because an error counter here wouldn't be at the right granularity -- we get an error here even if almost everything reloads successfully and there's one invalid file in the search path, and it would be misleading to report that as though the reload didn't happen. The related monitor vars |
||
|
||
// Load all config objects | ||
configs, _ := rl.loadConfigs(files) | ||
|
||
debugf("Number of module configs found: %v", len(configs)) | ||
|
||
if err := list.Reload(configs); err != nil { | ||
// Make sure the next run also updates because some runners were not properly loaded | ||
overwriteUpdate = true | ||
} | ||
err = list.Reload(configs) | ||
// Force reload on the next iteration if and only if this one failed. | ||
// (Any errors are already logged by list.Reload, so we don't need to | ||
// propagate the details further.) | ||
forceReload = err != nil | ||
} | ||
|
||
// Path loading is enabled but not reloading. Loads files only once and then stops. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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 cfgfile | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
func TestReloader(t *testing.T) { | ||
// Create random temp directory | ||
dir, err := ioutil.TempDir("", "libbeat-reloader") | ||
defer os.RemoveAll(dir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
glob := dir + "/*.yml" | ||
|
||
config := common.MustNewConfigFrom(common.MapStr{ | ||
"path": glob, | ||
"reload": common.MapStr{ | ||
"period": "1s", | ||
"enabled": true, | ||
}, | ||
}) | ||
// common.Config{} | ||
reloader := NewReloader(nil, config) | ||
retryCount := 10 | ||
|
||
go reloader.Run(nil) | ||
defer reloader.Stop() | ||
|
||
// wait until configScans >= 2 (which should happen after ~1 second) | ||
for i := 0; i < retryCount; i++ { | ||
if configScans.Get() >= 2 { | ||
break | ||
} | ||
// time interval is slightly more than a second so we don't slightly | ||
// undershoot the first iteration and wait a whole extra second. | ||
time.Sleep(1100 * time.Millisecond) | ||
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. I'm a bit worried about tests with Sleep as they tend to get flaky earlier or later. Is there an other way to achieve this "wait"? 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. I don't know a better way, though I'm open to suggestions :-) A spin lock would presumably be even worse. The difficulty is that the behavior being tested is itself timer-based. I did structure the logic here so that the test is only affected by the state changes and not other timing issues -- the only "race condition" here is that the unit test might wait several seconds without the reloader's 1-second-interval timer going off, in which case it has no way to tell whether anything's happening. But, the retry timeout was shorter than I meant it to be, so I just increased it to 10 seconds. 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. Sorry, I'm late on this one. Lets see if it shows up in flaky tests in the future ;-) What we did as workarounds in the past is:
In general I think it's problematic if a single unit tests increases the test suite by seconds and only spends the time waiting. To improve the above, perhaps there is a way to decrease the scan frequency and do checks every 10ms but retry 1000 times. |
||
} | ||
if configScans.Get() < 2 { | ||
assert.Fail(t, "Timed out waiting for configScans >= 2") | ||
} | ||
|
||
// The first scan should cause a reload, but additional ones should not, | ||
// so configReloads should still be 1. | ||
assert.Equal(t, int64(1), configReloads.Get()) | ||
|
||
// Write a file to the reloader path to trigger a real reload | ||
content := []byte("test\n") | ||
err = ioutil.WriteFile(dir+"/config1.yml", content, 0644) | ||
assert.NoError(t, err) | ||
|
||
// Wait for the number of scans to increase at least twice. This is somewhat | ||
// pedantic, but if we just wait for the next scan, it's possible to wake up | ||
// during the brief interval after configScans is updated but before | ||
// configReloads is, giving a false negative. Waiting two iterations | ||
// guarantees that the change from the first one has taken effect. | ||
targetScans := configScans.Get() + 2 | ||
for i := 0; i < retryCount; i++ { | ||
time.Sleep(time.Second) | ||
if configScans.Get() >= targetScans { | ||
break | ||
} | ||
} | ||
if configScans.Get() < targetScans { | ||
assert.Fail(t, | ||
fmt.Sprintf("Timed out waiting for configScans >= %d", targetScans)) | ||
} | ||
|
||
// The number of reloads should now have increased. It would be nicer to | ||
// check if the value is exactly 2, but we can't guarantee this: the glob | ||
// watcher includes an extra 1-second margin around the real modification | ||
// time, so changes that fall too close to a scan interval can be detected | ||
// twice. | ||
if configReloads.Get() < 2 { | ||
assert.Fail(t, | ||
fmt.Sprintf( | ||
"Reloader performed %d scans but only reloaded once", | ||
configScans.Get())) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
"starts": 0, | ||
"stops": 0 | ||
}, | ||
"scans": 1, | ||
"reloads": 1 | ||
}, | ||
"output": { | ||
|
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.
I like the renaming of the variable.