Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make the tsdb filenames correctly reproducible from the identifier #12536

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/storage/stores/shipper/indexshipper/tsdb/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ 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.
// 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
Expand All @@ -77,9 +81,13 @@ func (i SingleTenantTSDBIdentifier) Hash(h hash.Hash32) (err error) {

// str builds filename with format <file-creation-ts> + `-` + `compactor` + `-` + <oldest-chunk-start-ts> + `-` + <latest-chunk-end-ts> `-` + <index-checksum>
func (i SingleTenantTSDBIdentifier) str() string {
ts := i.origTS
if ts == 0 {
ts = i.TS.UnixNano()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I find this a bit hacky. I think a bool flag writeTSAsSecs would read better.

}
return fmt.Sprintf(
"%d-%s-%d-%d-%x.tsdb",
i.TS.UnixNano(),
ts,
compactedFileUploader,
i.From,
i.Through,
Expand Down Expand Up @@ -140,6 +148,7 @@ 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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,
Expand All @@ -31,6 +32,7 @@ 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,
Expand All @@ -42,6 +44,7 @@ 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,
Expand Down Expand Up @@ -69,6 +72,9 @@ func TestParseSingleTenantTSDBPath(t *testing.T) {
id, ok := ParseSingleTenantTSDBPath(tc.input)
require.Equal(t, tc.ok, ok)
require.Equal(t, tc.id, id)
if ok {
require.Equal(t, tc.input, id.Name())
}
})
}
}
Loading