Skip to content

[release-2.53 LTS] Backward compatibility with upcoming index v3 #16762

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

Merged
merged 1 commit into from
Jun 25, 2025
Merged
Changes from all commits
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
28 changes: 16 additions & 12 deletions tsdb/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ const (
// HeaderLen represents number of bytes reserved of index for header.
HeaderLen = 5

// FormatV1 represents 1 version of index.
// FormatV1 represents version 1 of index.
FormatV1 = 1
// FormatV2 represents 2 version of index.
// FormatV2 represents version 2 of index.
FormatV2 = 2
// FormatV3 represents version 3 of index.
FormatV3 = 3

indexFilename = "index"

Expand Down Expand Up @@ -1192,7 +1194,9 @@ func newReader(b ByteSlice, c io.Closer) (*Reader, error) {
}
r.version = int(r.b.Range(4, 5)[0])

if r.version != FormatV1 && r.version != FormatV2 {
switch r.version {
case FormatV1, FormatV2, FormatV3:
default:
return nil, fmt.Errorf("unknown index file version %d", r.version)
}

Expand Down Expand Up @@ -1350,7 +1354,9 @@ func (s Symbols) Lookup(o uint32) (string, error) {
B: s.bs.Range(0, s.bs.Len()),
}

if s.version == FormatV2 {
if s.version == FormatV1 {
d.Skip(int(o))
} else {
if int(o) >= s.seen {
return "", fmt.Errorf("unknown symbol offset %d", o)
}
Expand All @@ -1359,8 +1365,6 @@ func (s Symbols) Lookup(o uint32) (string, error) {
for i := o - (o / symbolFactor * symbolFactor); i > 0; i-- {
d.UvarintBytes()
}
} else {
d.Skip(int(o))
}
sym := d.UvarintStr()
if d.Err() != nil {
Expand Down Expand Up @@ -1406,10 +1410,10 @@ func (s Symbols) ReverseLookup(sym string) (uint32, error) {
if lastSymbol != sym {
return 0, fmt.Errorf("unknown symbol %q", sym)
}
if s.version == FormatV2 {
return uint32(res), nil
if s.version == FormatV1 {
return uint32(s.bs.Len() - lastLen), nil
}
return uint32(s.bs.Len() - lastLen), nil
return uint32(res), nil
}

func (s Symbols) Size() int {
Expand Down Expand Up @@ -1562,7 +1566,7 @@ func (r *Reader) LabelNamesFor(ctx context.Context, ids ...storage.SeriesRef) ([
offset := id
// In version 2 series IDs are no longer exact references but series are 16-byte padded
// and the ID is the multiple of 16 of the actual position.
if r.version == FormatV2 {
if r.version != FormatV1 {
offset = id * seriesByteAlign
}

Expand Down Expand Up @@ -1601,7 +1605,7 @@ func (r *Reader) LabelValueFor(ctx context.Context, id storage.SeriesRef, label
offset := id
// In version 2 series IDs are no longer exact references but series are 16-byte padded
// and the ID is the multiple of 16 of the actual position.
if r.version == FormatV2 {
if r.version != FormatV1 {
offset = id * seriesByteAlign
}
d := encoding.NewDecbufUvarintAt(r.b, int(offset), castagnoliTable)
Expand All @@ -1627,7 +1631,7 @@ func (r *Reader) Series(id storage.SeriesRef, builder *labels.ScratchBuilder, ch
offset := id
// In version 2 series IDs are no longer exact references but series are 16-byte padded
// and the ID is the multiple of 16 of the actual position.
if r.version == FormatV2 {
if r.version != FormatV1 {
offset = id * seriesByteAlign
}
d := encoding.NewDecbufUvarintAt(r.b, int(offset), castagnoliTable)
Expand Down
Loading