-
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
[Heartbeat] States reloader and eslegclient #33405
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6958df7
Better loading / reloading of state tracker
andrewvc 4b07bde
Checkpoint
andrewvc 51b32fe
Checkpoint
andrewvc c3ac248
Refactor states loader to use eslegclient and reload config when runn…
emilioalvap e063101
Add deferred state loader for managed instances, fix some logs
emilioalvap f4a9720
Fix naming
emilioalvap cd25e6a
Fix linting
emilioalvap 2c5c3c1
Fix docs
emilioalvap 08bdd5a
Fix docs
emilioalvap 5f6e6ba
Fix integration tests
emilioalvap f242e6b
Review feedback, deferred loader unit test and changelog
emilioalvap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
package monitorstate | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
|
@@ -81,11 +83,13 @@ func (t *Tracker) getCurrentState(sf stdfields.StdMonitorFields) (state *State) | |
var err error | ||
for i := 0; i < tries; i++ { | ||
loadedState, err = t.stateLoader(sf) | ||
if err != nil { | ||
sleepFor := (time.Duration(i*i) * time.Second) + (time.Duration(rand.Intn(500)) * time.Millisecond) | ||
logp.L().Warnf("could not load last externally recorded state, will retry again in %d milliseconds: %w", sleepFor.Milliseconds(), err) | ||
time.Sleep(sleepFor) | ||
if err == nil { | ||
break | ||
} | ||
|
||
sleepFor := (time.Duration(i*i) * time.Second) + (time.Duration(rand.Intn(500)) * time.Millisecond) | ||
logp.L().Warnf("could not load last externally recorded state, will retry again in %d milliseconds: %w", sleepFor.Milliseconds(), err) | ||
time.Sleep(sleepFor) | ||
} | ||
if err != nil { | ||
logp.L().Warn("could not load prior state from elasticsearch after %d attempts, will create new state for monitor: %s", tries, sf.ID) | ||
|
@@ -104,3 +108,46 @@ func (t *Tracker) getCurrentState(sf stdfields.StdMonitorFields) (state *State) | |
func NilStateLoader(_ stdfields.StdMonitorFields) (*State, error) { | ||
return nil, nil | ||
} | ||
|
||
func AtomicStateLoader(inner StateLoader) (sl StateLoader, replace func(StateLoader)) { | ||
mtx := &sync.Mutex{} | ||
return func(currentSL stdfields.StdMonitorFields) (*State, error) { | ||
mtx.Lock() | ||
defer mtx.Unlock() | ||
|
||
return inner(currentSL) | ||
}, func(sl StateLoader) { | ||
mtx.Lock() | ||
defer mtx.Unlock() | ||
inner = sl | ||
logp.L().Info("Updated atomic state loader") | ||
} | ||
} | ||
|
||
func DeferredStateLoader(inner StateLoader, timeout time.Duration) (sl StateLoader, replace func(StateLoader)) { | ||
stateLoader, replaceStateLoader := AtomicStateLoader(inner) | ||
|
||
wg := sync.WaitGroup{} | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
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. Very idiomatic go concurrency! |
||
wg.Add(1) | ||
go func() { | ||
defer cancel() | ||
defer wg.Done() | ||
|
||
<-ctx.Done() | ||
|
||
if errors.Is(ctx.Err(), context.DeadlineExceeded) { | ||
logp.L().Warn("Timeout trying to defer state loader") | ||
} | ||
}() | ||
|
||
return func(currentSL stdfields.StdMonitorFields) (*State, error) { | ||
wg.Wait() | ||
|
||
return stateLoader(currentSL) | ||
}, func(sl StateLoader) { | ||
defer cancel() | ||
|
||
replaceStateLoader(sl) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should also add in the changes from https://github.com/elastic/beats/pull/33375/files#diff-3edc3a1c86f948ac04dc2aeef09be1c549a2e5cbedd80a76e49dde4d95fa6883R82 which adds a
heartbeat.states.enabled
config flag. The service won't have the manager enabled, so we'll have to enable that to have states work with the service.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 actually checks if heartbeat is in unmanaged mode (
!b.Manager.Enabled()
) to initialize a non-deferred states loader, so it should apply for the service as well.IIRC, states flag was motivated by #33357. IMO, this PR supersedes the original problem since we can now load the config on both modes. Is there an scenario where we would still like to disable states?