tsdb/agent: fix validation of default options (#9876)

* tsdb/agent: fix application of defaults

MaxTS was being incorrectly constrained to the truncation interval

* add more tests to check validation

* force MaxWALTime = MinWALTime if min > max

Signed-off-by: Robert Fratto <robertfratto@gmail.com>
This commit is contained in:
Robert Fratto 2022-09-27 10:11:43 -04:00 committed by GitHub
parent d166da7b59
commit 448cfda6c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View file

@ -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

View file

@ -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
}