Alerting: Fix migration edge-case race condition for silences #81206
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.
What is this feature?
This change opts to bypass the unnecessary step of writing the silences to disk during the migration and instead write them directly to the kvstore.
Why do we need this feature?
If the db already has an entry in the kvstore for the silences of an alertmanager before the migration has taken place, then it's possible that the active alertmanager will overwrite the silence file created by the migration before it has a chance to load it into memory.
This should not happen normally but is possible in edge-cases.
The change avoids the race condition entirely and is more correct as we treat the database as the source of truth for AM state.
Special notes for your reviewer:
Can reproduce by adding an empty silence entry to the kvstore before migrating alerts that would normally create silences (such as alerts with NoData/Error set to keep_state).