Skip to content

Commit ad0d82c

Browse files
hongananchaudum
authored andcommitted
fix: Export exportTSInSecs field in TSDB identifier to make sure correct gap calculation (#13116)
In summary, the Meta(now in loki/pkg/storage/stores/shipper/bloomshipper/client.go) structure contains a Sources []tsdb.SingleTenantTSDBIdentifier field, and within tsdb.SingleTenantTSDBIdentifier, there is an exportTSInSecs field. When Meta is serialized and written to OSS storage, the exportTSInSecs field is being set to its default value, false, which is not the expected behavior. It should retain its actual value; otherwise, this will lead to issues during subsequent gap calculations. Co-authored-by: Christian Haudum <christian.haudum@gmail.com>
1 parent 0dc531f commit ad0d82c

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

‎pkg/storage/stores/shipper/indexshipper/tsdb/identifier.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func (p prefixedIdentifier) Name() string {
6464
// Identifier has all the information needed to resolve a TSDB index
6565
// Notably this abstracts away OS path separators, etc.
6666
type SingleTenantTSDBIdentifier struct {
67-
// exportTSInSecs tells whether creation timestamp should be exported in unix seconds instead of nanoseconds.
67+
// ExportTSInSecs tells whether creation timestamp should be exported in unix seconds instead of nanoseconds.
6868
// timestamp in filename could be a unix second or a unix nanosecond so
6969
// helps us to be able to reproduce filename back from parsed identifier.
7070
// Should be true ideally for older files with creation timestamp in seconds.
71-
exportTSInSecs bool
71+
ExportTSInSecs bool
7272
TS time.Time
7373
From, Through model.Time
7474
Checksum uint32
@@ -83,7 +83,7 @@ func (i SingleTenantTSDBIdentifier) Hash(h hash.Hash32) (err error) {
8383
// str builds filename with format <file-creation-ts> + `-` + `compactor` + `-` + <oldest-chunk-start-ts> + `-` + <latest-chunk-end-ts> `-` + <index-checksum>
8484
func (i SingleTenantTSDBIdentifier) str() string {
8585
ts := int64(0)
86-
if i.exportTSInSecs {
86+
if i.ExportTSInSecs {
8787
ts = i.TS.Unix()
8888
} else {
8989
ts = i.TS.UnixNano()
@@ -151,7 +151,7 @@ func ParseSingleTenantTSDBPath(p string) (id SingleTenantTSDBIdentifier, ok bool
151151
parsedTS = time.Unix(0, ts)
152152
}
153153
return SingleTenantTSDBIdentifier{
154-
exportTSInSecs: len(elems[0]) <= 10,
154+
ExportTSInSecs: len(elems[0]) <= 10,
155155
TS: parsedTS,
156156
From: model.Time(from),
157157
Through: model.Time(through),

‎pkg/storage/stores/shipper/indexshipper/tsdb/identifier_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tsdb
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"math"
67
"testing"
@@ -20,7 +21,7 @@ func TestParseSingleTenantTSDBPath(t *testing.T) {
2021
desc: "simple_works",
2122
input: "1-compactor-1-10-ff.tsdb",
2223
id: SingleTenantTSDBIdentifier{
23-
exportTSInSecs: true,
24+
ExportTSInSecs: true,
2425
TS: time.Unix(1, 0),
2526
From: 1,
2627
Through: 10,
@@ -32,7 +33,7 @@ func TestParseSingleTenantTSDBPath(t *testing.T) {
3233
desc: "simple_works_with_nanosecond",
3334
input: "1712534400000000000-compactor-1-10-ff.tsdb",
3435
id: SingleTenantTSDBIdentifier{
35-
exportTSInSecs: false,
36+
ExportTSInSecs: false,
3637
TS: time.Unix(0, 1712534400000000000),
3738
From: 1,
3839
Through: 10,
@@ -44,7 +45,7 @@ func TestParseSingleTenantTSDBPath(t *testing.T) {
4445
desc: "uint32_max_checksum_works",
4546
input: fmt.Sprintf("1-compactor-1-10-%x.tsdb", math.MaxUint32),
4647
id: SingleTenantTSDBIdentifier{
47-
exportTSInSecs: true,
48+
ExportTSInSecs: true,
4849
TS: time.Unix(1, 0),
4950
From: 1,
5051
Through: 10,
@@ -78,3 +79,29 @@ func TestParseSingleTenantTSDBPath(t *testing.T) {
7879
})
7980
}
8081
}
82+
83+
func TestSingleTenantTSDBIdentifierSerialization(t *testing.T) {
84+
for _, tc := range []struct {
85+
desc string
86+
input SingleTenantTSDBIdentifier
87+
}{
88+
{
89+
desc: "simple_works",
90+
input: SingleTenantTSDBIdentifier{ExportTSInSecs: true, TS: time.Unix(1, 0).UTC(), From: 1, Through: 10, Checksum: 255},
91+
},
92+
{
93+
desc: "simple_works_with_nanosecond",
94+
input: SingleTenantTSDBIdentifier{ExportTSInSecs: false, TS: time.Unix(0, 1712534400000000000).UTC(), From: 1, Through: 10, Checksum: 255},
95+
},
96+
} {
97+
t.Run(tc.desc, func(t *testing.T) {
98+
b, err := json.Marshal(tc.input)
99+
require.NoError(t, err)
100+
101+
var id SingleTenantTSDBIdentifier
102+
require.NoError(t, json.Unmarshal(b, &id))
103+
require.Equal(t, tc.input.Name(), id.Name())
104+
require.Equal(t, tc.input, id)
105+
})
106+
}
107+
}

0 commit comments

Comments
 (0)