From 9c8f2968f348c6a3856e371d8ae15e4245ee8612 Mon Sep 17 00:00:00 2001 From: davidby-influx <72418212+davidby-influx@users.noreply.github.com> Date: Tue, 16 Jul 2024 08:37:40 -0700 Subject: [PATCH] fix: Store.validateArgs wrongfully overwriting start, end unix time (#25146) (#25163) When querying data before 1970-01-01 (UNIX time 0) validateArgs would set start to -in64 max and end to int64 max. closes https://github.com/influxdata/influxdb/issues/24669 Co-authored-by: Paul Hegenberg (cherry picked from commit c2b3e38a38e1e8a47b6008a332d4443b8fe5f38e) closes https://github.com/influxdata/influxdb/issues/25148 --- v1/services/storage/store.go | 4 +- v1/services/storage/store_test.go | 65 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/v1/services/storage/store.go b/v1/services/storage/store.go index af4ad09650..703f102868 100644 --- a/v1/services/storage/store.go +++ b/v1/services/storage/store.go @@ -138,10 +138,10 @@ func (s *Store) validateArgs(orgID, bucketID uint64, start, end int64) (string, return "", "", 0, 0, errors.New("invalid retention policy") } - if start <= 0 { + if start <= models.MinNanoTime { start = models.MinNanoTime } - if end <= 0 { + if end >= models.MaxNanoTime { end = models.MaxNanoTime } return database, rp, start, end, nil diff --git a/v1/services/storage/store_test.go b/v1/services/storage/store_test.go index cecf8a3f45..f5cd8c93e7 100644 --- a/v1/services/storage/store_test.go +++ b/v1/services/storage/store_test.go @@ -4,10 +4,75 @@ import ( "testing" "time" + "github.com/influxdata/influxdb/v2/internal" + "github.com/influxdata/influxdb/v2/models" "github.com/influxdata/influxdb/v2/v1/services/meta" "github.com/stretchr/testify/require" ) +func TestValidateArgs(t *testing.T) { + type inputParams struct { + orgID uint64 + bucketID uint64 + start int64 + end int64 + } + + type outputParams struct { + database string + rp string + start int64 + end int64 + err error + } + + testCases := []struct { + desc string + store *Store + input inputParams + expected outputParams + }{ + { + desc: "start not < models.MinNanoTime and end not > models.MaxNanoTime", + store: NewStore(nil, &internal.MetaClientMock{ + DatabaseFn: func(name string) *meta.DatabaseInfo { + return &meta.DatabaseInfo{ + Name: name, + RetentionPolicies: []meta.RetentionPolicyInfo{ + { + Name: meta.DefaultRetentionPolicyName, + }, + }, + } + }, + }), + input: inputParams{ + orgID: 1, + bucketID: 2, + start: models.MinNanoTime - 1, + end: models.MaxNanoTime + 1, + }, + expected: outputParams{ + database: "0000000000000002", + rp: meta.DefaultRetentionPolicyName, + start: models.MinNanoTime, + end: models.MaxNanoTime, + }, + }, + } + for _, tC := range testCases { + t.Run(tC.desc, func(t *testing.T) { + database, rp, start, end, err := tC.store.validateArgs(tC.input.orgID, tC.input.bucketID, tC.input.start, tC.input.end) + + require.Equal(t, tC.expected.database, database) + require.Equal(t, tC.expected.rp, rp) + require.Equal(t, tC.expected.start, start) + require.Equal(t, tC.expected.end, end) + require.Equal(t, tC.expected.err, err) + }) + } +} + func TestGroupShardsByTime(t *testing.T) { tests := []struct { name string