-
Notifications
You must be signed in to change notification settings - Fork 820
Implement periodic writing of alertmanager state to storage. #4031
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
31e59a1
Implement periodic writing of alertmanager state to storage.
stevesg 468eabd
Review comments (NewTimerService, interval config). Unit test cleanup.
stevesg 345b28f
Review comments: Update command line documentation.
stevesg 71e078c
Review comments: Move Validate/RegisterFlags functions.
stevesg 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package alertmanager | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"time" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/alertmanager/cluster/clusterpb" | ||
|
||
"github.com/cortexproject/cortex/pkg/alertmanager/alertspb" | ||
"github.com/cortexproject/cortex/pkg/alertmanager/alertstore" | ||
"github.com/cortexproject/cortex/pkg/util/services" | ||
) | ||
|
||
const ( | ||
defaultPersistTimeout = 30 * time.Second | ||
) | ||
|
||
var ( | ||
errInvalidPersistInterval = errors.New("invalid alertmanager persist interval, must be greater than zero") | ||
) | ||
|
||
type PersisterConfig struct { | ||
Interval time.Duration `yaml:"persist_interval"` | ||
} | ||
|
||
func (cfg *PersisterConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) { | ||
f.DurationVar(&cfg.Interval, prefix+".persist-interval", 15*time.Minute, "The interval between persisting the current alertmanager state (notification log and silences) to object storage. This is only used when sharding is enabled. This state is read when all replicas for a shard can not be contacted. In this scenario, having persisted the state more frequently will result in potentially fewer lost silences, and fewer duplicate notifications.") | ||
} | ||
|
||
func (cfg *PersisterConfig) Validate() error { | ||
if cfg.Interval <= 0 { | ||
return errInvalidPersistInterval | ||
} | ||
return nil | ||
} | ||
|
||
type PersistableState interface { | ||
State | ||
GetFullState() (*clusterpb.FullState, error) | ||
} | ||
|
||
// statePersister periodically writes the alertmanager state to persistent storage. | ||
type statePersister struct { | ||
services.Service | ||
|
||
state PersistableState | ||
store alertstore.AlertStore | ||
userID string | ||
logger log.Logger | ||
|
||
timeout time.Duration | ||
} | ||
|
||
// newStatePersister creates a new state persister. | ||
func newStatePersister(cfg PersisterConfig, userID string, state PersistableState, store alertstore.AlertStore, l log.Logger) *statePersister { | ||
|
||
s := &statePersister{ | ||
state: state, | ||
store: store, | ||
userID: userID, | ||
logger: l, | ||
timeout: defaultPersistTimeout, | ||
} | ||
|
||
s.Service = services.NewTimerService(cfg.Interval, s.starting, s.iteration, nil) | ||
|
||
return s | ||
} | ||
|
||
func (s *statePersister) starting(ctx context.Context) error { | ||
// Waits until the state replicator is settled, so that state is not | ||
// persisted before obtaining some initial state. | ||
return s.state.WaitReady(ctx) | ||
} | ||
|
||
func (s *statePersister) iteration(ctx context.Context) error { | ||
if err := s.persist(ctx); err != nil { | ||
level.Error(s.logger).Log("msg", "failed to persist state", "user", s.userID, "err", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *statePersister) persist(ctx context.Context) error { | ||
// Only the replica at position zero should write the state. | ||
if s.state.Position() != 0 { | ||
return nil | ||
} | ||
|
||
level.Debug(s.logger).Log("msg", "persisting state", "user", s.userID) | ||
|
||
fs, err := s.state.GetFullState() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(ctx, s.timeout) | ||
defer cancel() | ||
|
||
desc := alertspb.FullStateDesc{State: fs} | ||
if err := s.store.SetFullState(ctx, s.userID, desc); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.