Skip to content

Commit

Permalink
fix(tsm1): Fix data race of seriesKeys in deleteSeriesRange
Browse files Browse the repository at this point in the history
  • Loading branch information
chengshiwen committed Sep 8, 2024
1 parent 1582b8a commit c8c8566
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tsdb/engine/tsm1/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,9 @@ func (e *Engine) deleteSeriesRange(seriesKeys [][]byte, min, max int64) error {
// would delete it from the index.
minKey := seriesKeys[0]

// Ensure seriesKeys slice is correctly read and written concurrently in the Apply func.
var seriesKeysLock sync.RWMutex

// Apply runs this func concurrently. The seriesKeys slice is mutated concurrently
// by different goroutines setting positions to nil.
if err := e.FileStore.Apply(func(r TSMFile) error {
Expand All @@ -1681,6 +1684,7 @@ func (e *Engine) deleteSeriesRange(seriesKeys [][]byte, min, max int64) error {
seriesKey, _ := SeriesAndFieldFromCompositeKey(indexKey)

// Skip over any deleted keys that are less than our tsm key
seriesKeysLock.RLock()
cmp := bytes.Compare(seriesKeys[j], seriesKey)
for j < len(seriesKeys) && cmp < 0 {
j++
Expand All @@ -1689,10 +1693,13 @@ func (e *Engine) deleteSeriesRange(seriesKeys [][]byte, min, max int64) error {
}
cmp = bytes.Compare(seriesKeys[j], seriesKey)
}
seriesKeysLock.RUnlock()

// We've found a matching key, cross it out so we do not remove it from the index.
if j < len(seriesKeys) && cmp == 0 {
seriesKeysLock.Lock()
seriesKeys[j] = emptyBytes
seriesKeysLock.Unlock()
j++
}
}
Expand Down

0 comments on commit c8c8566

Please sign in to comment.