What is this feature? This PR implements compressed periodic save for alert state storage, providing a more efficient alternative to regular periodic saves by grouping alert instances by rule UID and storing them using protobuf and snappy compression. When enabled via the state_compressed_periodic_save_enabled configuration option, the system groups alert instances by their alert rule, compresses each group using protobuf serialization and snappy compression, and processes all rules within a single database transaction at specified intervals instead of syncing after every alert evaluation cycle. Why do we need this feature? During discussions in PR #111357, we identified the need for a compressed approach to periodic alert state storage that could further reduce database load beyond the jitter mechanism. While the jitter feature distributes database operations over time, this compressed periodic save approach reduces the frequency of database operations by batching alert state updates at explicitly declared intervals rather than syncing after every alert evaluation cycle. This approach provides several key benefits: - Reduced Database Frequency: Instead of frequent sync operations tied to alert evaluation cycles, updates occur only at configured intervals - Storage Efficiency: Rule-based grouping with protobuf and snappy compression significantly reduces storage requirements The compressed periodic save complements the existing jitter mechanism by providing an alternative strategy focused on reducing overall database interaction frequency while maintaining data integrity through compression and batching. Who is this feature for? - Platform/Infrastructure teams managing large-scale Grafana deployments with high alert cardinality - Organizations looking to optimize storage costs and database performance for alerting workloads - Production environments with 1000+ alert rules where database write frequency is a concern
62 lines
2.4 KiB
Go
62 lines
2.4 KiB
Go
package state
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
history_model "github.com/grafana/grafana/pkg/services/ngalert/state/historian/model"
|
|
)
|
|
|
|
type AlertInstancesProvider interface {
|
|
GetAlertInstances() []models.AlertInstance
|
|
}
|
|
|
|
// InstanceStore represents the ability to fetch and write alert instances.
|
|
type InstanceStore interface {
|
|
InstanceReader
|
|
InstanceWriter
|
|
}
|
|
|
|
// InstanceReader provides methods to fetch alert instances.
|
|
type InstanceReader interface {
|
|
ListAlertInstances(ctx context.Context, cmd *models.ListAlertInstancesQuery) ([]*models.AlertInstance, error)
|
|
}
|
|
|
|
// InstanceWriter provides methods to write alert instances.
|
|
type InstanceWriter interface {
|
|
SaveAlertInstance(ctx context.Context, instance models.AlertInstance) error
|
|
DeleteAlertInstances(ctx context.Context, keys ...models.AlertInstanceKey) error
|
|
// SaveAlertInstancesForRule overwrites the state for the given rule.
|
|
SaveAlertInstancesForRule(ctx context.Context, key models.AlertRuleKeyWithGroup, instances []models.AlertInstance) error
|
|
DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKeyWithGroup) error
|
|
// FullSync performs a full synchronization of alert instances.
|
|
// If jitterFunc is provided, applies jitter delays between batches to distribute database load.
|
|
// If jitterFunc is nil, executes batches without delays.
|
|
FullSync(ctx context.Context, instances []models.AlertInstance, batchSize int, jitterFunc func(int) time.Duration) error
|
|
}
|
|
|
|
type OrgReader interface {
|
|
FetchOrgIds(ctx context.Context) ([]int64, error)
|
|
}
|
|
|
|
// RuleReader represents the ability to fetch alert rules.
|
|
type RuleReader interface {
|
|
ListAlertRules(ctx context.Context, query *models.ListAlertRulesQuery) (models.RulesGroup, error)
|
|
}
|
|
|
|
// Historian maintains an audit log of alert state history.
|
|
type Historian interface {
|
|
// RecordStates writes a number of state transitions for a given rule to state history. It returns a channel that
|
|
// is closed when writing the state transitions has completed. If an error has occurred, the channel will contain a
|
|
// non-nil error.
|
|
Record(ctx context.Context, rule history_model.RuleMeta, states []StateTransition) <-chan error
|
|
}
|
|
|
|
// ImageCapturer captures images.
|
|
//
|
|
//go:generate mockgen -destination=image_mock.go -package=state github.com/grafana/grafana/pkg/services/ngalert/state ImageCapturer
|
|
type ImageCapturer interface {
|
|
NewImage(ctx context.Context, r *models.AlertRule) (*models.Image, error)
|
|
}
|