Skip to content

Commit 8cce69c

Browse files
committed
Retention: switch number of days type uint16 -> float64
This allows specifying less than a day as threshold. That is especially useful if history doesn't mattter as much as disk space.
1 parent d0ef6cf commit 8cce69c

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

Diff for: internal/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func (f Flags) IsExplicitConfigPath() bool {
8181

8282
// RetentionConfig defines configuration for history retention.
8383
type RetentionConfig struct {
84-
HistoryDays uint16 `yaml:"history-days" env:"HISTORY_DAYS"`
85-
SlaDays uint16 `yaml:"sla-days" env:"SLA_DAYS"`
84+
HistoryDays float64 `yaml:"history-days" env:"HISTORY_DAYS"`
85+
SlaDays float64 `yaml:"sla-days" env:"SLA_DAYS"`
8686
Interval time.Duration `yaml:"interval" env:"INTERVAL" default:"1h"`
8787
Count uint64 `yaml:"count" env:"COUNT" default:"5000"`
8888
Options history.RetentionOptions `yaml:"options" env:"OPTIONS"`

Diff for: pkg/icingadb/history/retention.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/icinga/icingadb/pkg/icingaredis/telemetry"
1212
"github.com/pkg/errors"
1313
"go.uber.org/zap"
14+
"math"
1415
"strconv"
1516
"strings"
1617
"time"
@@ -97,7 +98,7 @@ var RetentionStatements = []retentionStatement{{
9798
}}
9899

99100
// RetentionOptions defines the non-default mapping of history categories with their retention period in days.
100-
type RetentionOptions map[string]uint16
101+
type RetentionOptions map[string]float64
101102

102103
// UnmarshalText implements [encoding.TextUnmarshaler] to allow RetentionOptions to be parsed by env.
103104
//
@@ -162,16 +163,16 @@ func (o *RetentionOptions) Validate() error {
162163
type Retention struct {
163164
db *database.DB
164165
logger *logging.Logger
165-
historyDays uint16
166-
slaDays uint16
166+
historyDays float64
167+
slaDays float64
167168
interval time.Duration
168169
count uint64
169170
options RetentionOptions
170171
}
171172

172173
// NewRetention returns a new Retention.
173174
func NewRetention(
174-
db *database.DB, historyDays, slaDays uint16, interval time.Duration,
175+
db *database.DB, historyDays, slaDays float64, interval time.Duration,
175176
count uint64, options RetentionOptions, logger *logging.Logger,
176177
) *Retention {
177178
return &Retention{
@@ -198,7 +199,7 @@ func (r *Retention) Start(ctx context.Context) error {
198199
errs := make(chan error, 1)
199200

200201
for _, stmt := range RetentionStatements {
201-
var days uint16
202+
var days float64
202203
switch stmt.RetentionType {
203204
case RetentionHistory:
204205
if d, ok := r.options[stmt.Category]; ok {
@@ -210,7 +211,7 @@ func (r *Retention) Start(ctx context.Context) error {
210211
days = r.slaDays
211212
}
212213

213-
if days < 1 {
214+
if days <= 0 {
214215
r.logger.Debugf("Skipping history retention for category %s", stmt.Category)
215216
continue
216217
}
@@ -219,12 +220,21 @@ func (r *Retention) Start(ctx context.Context) error {
219220
fmt.Sprintf("Starting history retention for category %s", stmt.Category),
220221
zap.Uint64("count", r.count),
221222
zap.Duration("interval", r.interval),
222-
zap.Uint16("retention-days", days),
223+
zap.Float64("retention-days", days),
223224
)
224225

225226
stmt := stmt
226227
periodic.Start(ctx, r.interval, func(tick periodic.Tick) {
227-
olderThan := tick.Time.AddDate(0, 0, -int(days))
228+
olderThan := tick.Time
229+
wholeDays, dayFraction := math.Modf(float64(days))
230+
231+
if wholeDays > 0 {
232+
olderThan = olderThan.AddDate(0, 0, -int(wholeDays))
233+
}
234+
235+
if dayFraction > 0 {
236+
olderThan = olderThan.Add(-time.Duration(dayFraction * float64(24*time.Hour)))
237+
}
228238

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

0 commit comments

Comments
 (0)