From 41237319274fe647900561bebdb0698e890df60a Mon Sep 17 00:00:00 2001 From: Sandeep Sukhani Date: Tue, 9 Apr 2024 17:02:28 +0530 Subject: [PATCH] use a bool to turn on exporting creation timestamp in seconds --- .../shipper/indexshipper/tsdb/identifier.go | 29 ++++++++++-------- .../indexshipper/tsdb/identifier_test.go | 30 +++++++++---------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/pkg/storage/stores/shipper/indexshipper/tsdb/identifier.go b/pkg/storage/stores/shipper/indexshipper/tsdb/identifier.go index 208e37b168969..149d41bfa9441 100644 --- a/pkg/storage/stores/shipper/indexshipper/tsdb/identifier.go +++ b/pkg/storage/stores/shipper/indexshipper/tsdb/identifier.go @@ -64,13 +64,14 @@ func (p prefixedIdentifier) Name() string { // Identifier has all the information needed to resolve a TSDB index // Notably this abstracts away OS path separators, etc. type SingleTenantTSDBIdentifier struct { - // origTS holds original timestamp from filename which was parsed to TS as time.Time. + // exportTSInSecs tells whether creation timestamp should be exported in unix seconds instead of nanoseconds. // timestamp in filename could be a unix second or a unix nanosecond so - // retaining the original timestamp to be able to reproduce filename back from parsed identifier. - origTS int64 - TS time.Time - From, Through model.Time - Checksum uint32 + // helps us to be able to reproduce filename back from parsed identifier. + // Should be true ideally for older files with creation timestamp in seconds. + exportTSInSecs bool + TS time.Time + From, Through model.Time + Checksum uint32 } // implement Hash @@ -81,8 +82,10 @@ func (i SingleTenantTSDBIdentifier) Hash(h hash.Hash32) (err error) { // str builds filename with format + `-` + `compactor` + `-` + + `-` + `-` + func (i SingleTenantTSDBIdentifier) str() string { - ts := i.origTS - if ts == 0 { + ts := int64(0) + if i.exportTSInSecs { + ts = i.TS.Unix() + } else { ts = i.TS.UnixNano() } return fmt.Sprintf( @@ -148,11 +151,11 @@ func ParseSingleTenantTSDBPath(p string) (id SingleTenantTSDBIdentifier, ok bool parsedTS = time.Unix(0, ts) } return SingleTenantTSDBIdentifier{ - origTS: ts, - TS: parsedTS, - From: model.Time(from), - Through: model.Time(through), - Checksum: uint32(checksum), + exportTSInSecs: len(elems[0]) <= 10, + TS: parsedTS, + From: model.Time(from), + Through: model.Time(through), + Checksum: uint32(checksum), }, true } diff --git a/pkg/storage/stores/shipper/indexshipper/tsdb/identifier_test.go b/pkg/storage/stores/shipper/indexshipper/tsdb/identifier_test.go index f47990226750d..41e202be5e467 100644 --- a/pkg/storage/stores/shipper/indexshipper/tsdb/identifier_test.go +++ b/pkg/storage/stores/shipper/indexshipper/tsdb/identifier_test.go @@ -20,11 +20,11 @@ func TestParseSingleTenantTSDBPath(t *testing.T) { desc: "simple_works", input: "1-compactor-1-10-ff.tsdb", id: SingleTenantTSDBIdentifier{ - origTS: 1, - TS: time.Unix(1, 0), - From: 1, - Through: 10, - Checksum: 255, + exportTSInSecs: true, + TS: time.Unix(1, 0), + From: 1, + Through: 10, + Checksum: 255, }, ok: true, }, @@ -32,11 +32,11 @@ func TestParseSingleTenantTSDBPath(t *testing.T) { desc: "simple_works_with_nanosecond", input: "1712534400000000000-compactor-1-10-ff.tsdb", id: SingleTenantTSDBIdentifier{ - origTS: 1712534400000000000, - TS: time.Unix(0, 1712534400000000000), - From: 1, - Through: 10, - Checksum: 255, + exportTSInSecs: false, + TS: time.Unix(0, 1712534400000000000), + From: 1, + Through: 10, + Checksum: 255, }, ok: true, }, @@ -44,11 +44,11 @@ func TestParseSingleTenantTSDBPath(t *testing.T) { desc: "uint32_max_checksum_works", input: fmt.Sprintf("1-compactor-1-10-%x.tsdb", math.MaxUint32), id: SingleTenantTSDBIdentifier{ - origTS: 1, - TS: time.Unix(1, 0), - From: 1, - Through: 10, - Checksum: math.MaxUint32, + exportTSInSecs: true, + TS: time.Unix(1, 0), + From: 1, + Through: 10, + Checksum: math.MaxUint32, }, ok: true, },