diff --git a/tsdb/agent/db.go b/tsdb/agent/db.go index e210cdc99e..71470a85df 100644 --- a/tsdb/agent/db.go +++ b/tsdb/agent/db.go @@ -317,13 +317,16 @@ func validateOptions(opts *Options) *Options { opts.TruncateFrequency = DefaultTruncateFrequency } if opts.MinWALTime <= 0 { - opts.MinWALTime = 0 + opts.MinWALTime = DefaultMinWALTime } if opts.MaxWALTime <= 0 { opts.MaxWALTime = DefaultMaxWALTime } + if opts.MinWALTime > opts.MaxWALTime { + opts.MaxWALTime = opts.MinWALTime + } - if t := int64(opts.TruncateFrequency * time.Hour / time.Millisecond); opts.MaxWALTime < t { + if t := int64(opts.TruncateFrequency / time.Millisecond); opts.MaxWALTime < t { opts.MaxWALTime = t } return opts diff --git a/tsdb/agent/db_test.go b/tsdb/agent/db_test.go index 080913a866..9655533730 100644 --- a/tsdb/agent/db_test.go +++ b/tsdb/agent/db_test.go @@ -446,6 +446,25 @@ func Test_ExistingWAL_NextRef(t *testing.T) { require.Equal(t, uint64(seriesCount), db.nextRef.Load(), "nextRef should be equal to the number of series written across the entire WAL") } +func Test_validateOptions(t *testing.T) { + t.Run("Apply defaults to zero values", func(t *testing.T) { + opts := validateOptions(&Options{}) + require.Equal(t, DefaultOptions(), opts) + }) + + t.Run("Defaults are already valid", func(t *testing.T) { + require.Equal(t, DefaultOptions(), validateOptions(nil)) + }) + + t.Run("MaxWALTime should not be lower than TruncateFrequency", func(t *testing.T) { + opts := validateOptions(&Options{ + MaxWALTime: int64(time.Hour / time.Millisecond), + TruncateFrequency: 2 * time.Hour, + }) + require.Equal(t, int64(2*time.Hour/time.Millisecond), opts.MaxWALTime) + }) +} + func startTime() (int64, error) { return time.Now().Unix() * 1000, nil }