Skip to content

Commit 88317d8

Browse files
author
Thor
committed
parallelize the opening of TSDB
Signed-off-by: Thor <[email protected]>
1 parent af05e55 commit 88317d8

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

pkg/ingester/ingester_v2.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,23 @@ func (i *Ingester) getOrCreateTSDB(userID string, force bool) (*tsdb.DB, error)
388388
return nil, fmt.Errorf(errTSDBCreateIncompatibleState, ingesterState)
389389
}
390390

391+
// Create the database and a shipper for a user
392+
db, err := i.createTSDB(userID)
393+
if err != nil {
394+
return nil, err
395+
}
396+
397+
// Add the db to list of user databases
398+
i.TSDBState.dbs[userID] = db
399+
return db, nil
400+
}
401+
402+
// createTSDB creates a TSDB for a given userID, and returns the created db.
403+
func (i *Ingester) createTSDB(userID string) (*tsdb.DB, error) {
391404
udir := i.cfg.TSDBConfig.BlocksDir(userID)
392405

393406
// Create a new user database
394-
var err error
395-
db, err = tsdb.Open(udir, util.Logger, nil, &tsdb.Options{
407+
db, err := tsdb.Open(udir, util.Logger, nil, &tsdb.Options{
396408
RetentionDuration: uint64(i.cfg.TSDBConfig.Retention / time.Millisecond),
397409
BlockRanges: i.cfg.TSDBConfig.BlockRanges.ToMillisecondRanges(),
398410
NoLockfile: true,
@@ -424,8 +436,6 @@ func (i *Ingester) getOrCreateTSDB(userID string, force bool) (*tsdb.DB, error)
424436
})
425437
}()
426438

427-
i.TSDBState.dbs[userID] = db
428-
429439
return db, nil
430440
}
431441

@@ -505,10 +515,17 @@ func (i *Ingester) openExistingTSDB(ctx context.Context) error {
505515
go func(userID string) {
506516
defer wg.Done()
507517
defer openGate.Done()
508-
_, err := i.getOrCreateTSDB(userID, true) // force create the TSDB due to the lifecycler not having started yet
518+
db, err := i.createTSDB(userID)
509519
if err != nil {
510520
level.Error(util.Logger).Log("msg", "unable to open user TSDB", "err", err, "user", userID)
521+
return
511522
}
523+
524+
// Add the database to the map of user databases
525+
i.userStatesMtx.Lock()
526+
defer i.userStatesMtx.Unlock()
527+
i.TSDBState.dbs[userID] = db
528+
512529
}(userID)
513530

514531
return filepath.SkipDir // Don't descend into directories

0 commit comments

Comments
 (0)