Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c1eae5b

Browse files
committedJan 24, 2025·
Retention: switch number of days type uint16 -> float32
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 849e59d commit c1eae5b

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed
 

‎internal/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ type Flags struct {
5555

5656
// RetentionConfig defines configuration for history retention.
5757
type RetentionConfig struct {
58-
HistoryDays uint16 `yaml:"history-days"`
59-
SlaDays uint16 `yaml:"sla-days"`
58+
HistoryDays float32 `yaml:"history-days"`
59+
SlaDays float32 `yaml:"sla-days"`
6060
Interval time.Duration `yaml:"interval" default:"1h"`
6161
Count uint64 `yaml:"count" default:"5000"`
6262
Options history.RetentionOptions `yaml:"options"`

‎pkg/icingadb/history/retention.go

+17-7
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
"time"
1516
)
1617

@@ -95,7 +96,7 @@ var RetentionStatements = []retentionStatement{{
9596
}}
9697

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

100101
// Validate checks constraints in the supplied retention options and
101102
// returns an error if they are violated.
@@ -120,16 +121,16 @@ func (o RetentionOptions) Validate() error {
120121
type Retention struct {
121122
db *database.DB
122123
logger *logging.Logger
123-
historyDays uint16
124-
slaDays uint16
124+
historyDays float32
125+
slaDays float32
125126
interval time.Duration
126127
count uint64
127128
options RetentionOptions
128129
}
129130

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

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

183184
stmt := stmt
184185
periodic.Start(ctx, r.interval, func(tick periodic.Tick) {
185-
olderThan := tick.Time.AddDate(0, 0, -int(days))
186+
olderThan := tick.Time
187+
wholeDays, dayFraction := math.Modf(float64(days))
188+
189+
if wholeDays > 0 {
190+
olderThan = olderThan.AddDate(0, 0, -int(wholeDays))
191+
}
192+
193+
if dayFraction > 0 {
194+
olderThan = olderThan.Add(-time.Duration(dayFraction * float64(24*time.Hour)))
195+
}
186196

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

0 commit comments

Comments
 (0)
Please sign in to comment.