Skip to content

Commit

Permalink
userTSDB: Avoid race condition when setting db
Browse files Browse the repository at this point in the history
Signed-off-by: Arve Knudsen <[email protected]>
  • Loading branch information
aknuds1 committed Oct 22, 2024
1 parent a479a81 commit 88eea09
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/ingester/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -2720,7 +2720,7 @@ func (i *Ingester) createTSDB(userID string, walReplayConcurrency int) (*userTSD
return nil, errors.Wrapf(err, "failed to compact TSDB: %s", udir)
}

userDB.db = db
userDB.setDB(db)
// We set the limiter here because we don't want to limit
// series during WAL replay.
userDB.limiter = i.limiter
Expand Down
9 changes: 8 additions & 1 deletion pkg/ingester/user_tsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type ownedSeriesState struct {
}

type userTSDB struct {
hasDB atomic.Bool
db *tsdb.DB
userID string
activeSeries *activeseries.ActiveSeries
Expand Down Expand Up @@ -144,6 +145,12 @@ type userTSDB struct {
requiresOwnedSeriesUpdate atomic.String // Non-empty string means that we need to recompute "owned series" for the user. Value will be used in the log message.
}

// setDB sets u.db.
func (u *userTSDB) setDB(db *tsdb.DB) {
u.db = db
u.hasDB.Store(true)
}

func (u *userTSDB) Appender(ctx context.Context) storage.Appender {
return u.db.Appender(ctx)
}
Expand Down Expand Up @@ -361,7 +368,7 @@ func (u *userTSDB) PostDeletion(metrics map[chunks.HeadSeriesRef]labels.Labels)

// blocksToDelete filters the input blocks and returns the blocks which are safe to be deleted from the ingester.
func (u *userTSDB) blocksToDelete(blocks []*tsdb.Block) map[ulid.ULID]struct{} {
if u.db == nil {
if !u.hasDB.Load() {
return nil
}

Expand Down
35 changes: 35 additions & 0 deletions pkg/ingester/user_tsdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ingester
import (
"context"
"math"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -103,6 +104,40 @@ func TestUserTSDB_acquireAppendLock(t *testing.T) {
})
}

func TestUserTSDB_blocksToDelete(t *testing.T) {
t.Run("blocksToDelete is concurrency safe", func(t *testing.T) {
var wg sync.WaitGroup
wg.Add(2)

setDBStarted := make(chan struct{})
blockDeleteStarted := make(chan struct{})
var userDB userTSDB

go func() {
defer wg.Done()

<-setDBStarted

for i := 0; i < 1000; i++ {
userDB.blocksToDelete(nil)
if i == 0 {
close(blockDeleteStarted)
}
}
}()

go func() {
defer wg.Done()

close(setDBStarted)
<-blockDeleteStarted
userDB.setDB(&tsdb.DB{})
}()

wg.Wait()
})
}

func TestNextForcedHeadCompactionRange(t *testing.T) {
const blockDuration = 10

Expand Down

0 comments on commit 88eea09

Please sign in to comment.