Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
Fix race condition on index
Browse files Browse the repository at this point in the history
  • Loading branch information
salvacorts committed Mar 17, 2023
1 parent 113a96f commit da17f81
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/storage/stores/tsdb/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"path/filepath"
"sort"
"sync"
"unsafe"

"github.com/pkg/errors"
Expand Down Expand Up @@ -1388,7 +1389,8 @@ type Reader struct {

fingerprintOffsets FingerprintOffsets

secondaryIndexLabels map[string]secondaryIndexLabelInfo
secondaryIndexLabels map[string]secondaryIndexLabelInfo
secondaryIndexLabelsMtx sync.RWMutex

dec *Decoder

Expand Down Expand Up @@ -1576,6 +1578,8 @@ func newReader(b ByteSlice, c io.Closer) (*Reader, error) {
}

func (r *Reader) SecondaryIndexLabelNames() []string {
r.secondaryIndexLabelsMtx.Lock()
defer r.secondaryIndexLabelsMtx.Unlock()
lnames := make([]string, 0, len(r.secondaryIndexLabels))
for lname := range r.secondaryIndexLabels {
lnames = append(lnames, lname)
Expand All @@ -1585,6 +1589,8 @@ func (r *Reader) SecondaryIndexLabelNames() []string {
}

func (r *Reader) readSecondaryIndexLabels() error {
r.secondaryIndexLabelsMtx.Lock()
defer r.secondaryIndexLabelsMtx.Unlock()
if r.toc.SecondaryPostings == 0 {
r.secondaryIndexLabels = make(map[string]secondaryIndexLabelInfo, 0)
return nil
Expand Down Expand Up @@ -1618,6 +1624,8 @@ func (r *Reader) readSecondaryIndexLabels() error {
}

func (r *Reader) getOffsetForSecondaryLabelValue(d encoding.Decbuf, labelName, labelValue string) (*secondaryIndexLabelValueInfo, error) {
r.secondaryIndexLabelsMtx.Lock()
defer r.secondaryIndexLabelsMtx.Unlock()
labelInfo, ok := r.secondaryIndexLabels[labelName]
if !ok {
return nil, fmt.Errorf("label name %s not found", labelName)
Expand Down Expand Up @@ -1687,7 +1695,9 @@ func (r *Reader) SecondaryIndexChunks(labelName, labelValue string, seriesRef *s
chks := ChunkMetasPool.Get()
defer ChunkMetasPool.Put(chks)

r.secondaryIndexLabelsMtx.RLock()
labelInfo, ok := r.secondaryIndexLabels[labelName]
r.secondaryIndexLabelsMtx.RUnlock()
if !ok {
return nil
}
Expand Down

0 comments on commit da17f81

Please sign in to comment.