Skip to content

Commit

Permalink
Retention: switch number of days type uint16 -> float32
Browse files Browse the repository at this point in the history
This allows specifying less than a day as threshold.
That is especially useful if history doesn't mattter as much as disk space.
  • Loading branch information
Al2Klimov committed Jan 24, 2025
1 parent 849e59d commit c1eae5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type Flags struct {

// RetentionConfig defines configuration for history retention.
type RetentionConfig struct {
HistoryDays uint16 `yaml:"history-days"`
SlaDays uint16 `yaml:"sla-days"`
HistoryDays float32 `yaml:"history-days"`
SlaDays float32 `yaml:"sla-days"`
Interval time.Duration `yaml:"interval" default:"1h"`
Count uint64 `yaml:"count" default:"5000"`
Options history.RetentionOptions `yaml:"options"`
Expand Down
24 changes: 17 additions & 7 deletions pkg/icingadb/history/retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/icinga/icingadb/pkg/icingaredis/telemetry"
"github.com/pkg/errors"
"go.uber.org/zap"
"math"
"time"
)

Expand Down Expand Up @@ -95,7 +96,7 @@ var RetentionStatements = []retentionStatement{{
}}

// RetentionOptions defines the non-default mapping of history categories with their retention period in days.
type RetentionOptions map[string]uint16
type RetentionOptions map[string]float32

// Validate checks constraints in the supplied retention options and
// returns an error if they are violated.
Expand All @@ -120,16 +121,16 @@ func (o RetentionOptions) Validate() error {
type Retention struct {
db *database.DB
logger *logging.Logger
historyDays uint16
slaDays uint16
historyDays float32
slaDays float32
interval time.Duration
count uint64
options RetentionOptions
}

// NewRetention returns a new Retention.
func NewRetention(
db *database.DB, historyDays, slaDays uint16, interval time.Duration,
db *database.DB, historyDays, slaDays float32, interval time.Duration,
count uint64, options RetentionOptions, logger *logging.Logger,
) *Retention {
return &Retention{
Expand All @@ -156,7 +157,7 @@ func (r *Retention) Start(ctx context.Context) error {
errs := make(chan error, 1)

for _, stmt := range RetentionStatements {
var days uint16
var days float32
switch stmt.RetentionType {
case RetentionHistory:
if d, ok := r.options[stmt.Category]; ok {
Expand All @@ -177,12 +178,21 @@ func (r *Retention) Start(ctx context.Context) error {
fmt.Sprintf("Starting history retention for category %s", stmt.Category),
zap.Uint64("count", r.count),
zap.Duration("interval", r.interval),
zap.Uint16("retention-days", days),
zap.Float32("retention-days", days),
)

stmt := stmt
periodic.Start(ctx, r.interval, func(tick periodic.Tick) {
olderThan := tick.Time.AddDate(0, 0, -int(days))
olderThan := tick.Time
wholeDays, dayFraction := math.Modf(float64(days))

if wholeDays > 0 {
olderThan = olderThan.AddDate(0, 0, -int(wholeDays))
}

if dayFraction > 0 {
olderThan = olderThan.Add(-time.Duration(dayFraction * float64(24*time.Hour)))
}

r.logger.Debugf("Cleaning up historical data for category %s from table %s older than %s",
stmt.Category, stmt.Table, olderThan)
Expand Down

0 comments on commit c1eae5b

Please sign in to comment.