@@ -227,9 +227,10 @@ func (p *Planner) runOne(ctx context.Context) error {
227
227
}
228
228
229
229
var (
230
- wg sync.WaitGroup
231
- start = time .Now ()
232
- status = statusFailure
230
+ wg sync.WaitGroup
231
+ start = time .Now ()
232
+ status = statusFailure
233
+ openTSDBs strategies.TSDBSet
233
234
)
234
235
defer func () {
235
236
p .metrics .buildCompleted .WithLabelValues (status ).Inc ()
@@ -238,6 +239,15 @@ func (p *Planner) runOne(ctx context.Context) error {
238
239
if status == statusSuccess {
239
240
p .metrics .buildLastSuccess .SetToCurrentTime ()
240
241
}
242
+
243
+ // Close all open TSDBs.
244
+ // These are used to get the chunkrefs for the series in the gaps.
245
+ // We populate the chunkrefs when we send the task to the builder.
246
+ for idx , reader := range openTSDBs {
247
+ if err := reader .Close (); err != nil {
248
+ level .Error (p .logger ).Log ("msg" , "failed to close tsdb" , "tsdb" , idx .Name (), "err" , err )
249
+ }
250
+ }
241
251
}()
242
252
243
253
p .metrics .buildStarted .Inc ()
@@ -275,7 +285,19 @@ func (p *Planner) runOne(ctx context.Context) error {
275
285
table : table ,
276
286
}
277
287
278
- tasks , existingMetas , err := p .computeTasks (ctx , table , tenant )
288
+ tsdbs , err := p .tsdbStore .ResolveTSDBs (ctx , table , tenant )
289
+ if err != nil {
290
+ level .Error (logger ).Log ("msg" , "failed to resolve tsdbs" , "err" , err )
291
+ continue
292
+ }
293
+
294
+ openTSDBs , err = openAllTSDBs (ctx , table , tenant , p .tsdbStore , tsdbs , openTSDBs )
295
+ if err != nil {
296
+ level .Error (logger ).Log ("msg" , "failed to open all tsdbs" , "err" , err )
297
+ continue
298
+ }
299
+
300
+ tasks , existingMetas , err := p .computeTasks (ctx , table , tenant , openTSDBs )
279
301
if err != nil {
280
302
level .Error (logger ).Log ("msg" , "failed to compute tasks" , "err" , err )
281
303
continue
@@ -286,7 +308,7 @@ func (p *Planner) runOne(ctx context.Context) error {
286
308
287
309
now := time .Now ()
288
310
for _ , task := range tasks {
289
- queueTask := NewQueueTask (ctx , now , task , resultsCh )
311
+ queueTask := NewQueueTask (ctx , now , task , openTSDBs [ task . TSDB ], resultsCh )
290
312
if err := p .enqueueTask (queueTask ); err != nil {
291
313
level .Error (logger ).Log ("msg" , "error enqueuing task" , "err" , err )
292
314
continue
@@ -374,7 +396,8 @@ func (p *Planner) computeTasks(
374
396
ctx context.Context ,
375
397
table config.DayTable ,
376
398
tenant string ,
377
- ) ([]* protos.Task , []bloomshipper.Meta , error ) {
399
+ tsdbs strategies.TSDBSet ,
400
+ ) ([]* strategies.Task , []bloomshipper.Meta , error ) {
378
401
strategy , err := strategies .NewStrategy (tenant , p .limits , p .logger )
379
402
if err != nil {
380
403
return nil , nil , fmt .Errorf ("error creating strategy: %w" , err )
@@ -402,29 +425,11 @@ func (p *Planner) computeTasks(
402
425
return nil , nil , fmt .Errorf ("failed to delete outdated metas during planning: %w" , err )
403
426
}
404
427
405
- // Resolve TSDBs
406
- tsdbs , err := p .tsdbStore .ResolveTSDBs (ctx , table , tenant )
407
- if err != nil {
408
- return nil , nil , fmt .Errorf ("failed to resolve tsdbs: %w" , err )
409
- }
410
-
411
428
if len (tsdbs ) == 0 {
412
429
return nil , metas , nil
413
430
}
414
431
415
- openTSDBs , err := openAllTSDBs (ctx , table , tenant , p .tsdbStore , tsdbs )
416
- if err != nil {
417
- return nil , nil , fmt .Errorf ("failed to open all tsdbs: %w" , err )
418
- }
419
- defer func () {
420
- for idx , reader := range openTSDBs {
421
- if err := reader .Close (); err != nil {
422
- level .Error (logger ).Log ("msg" , "failed to close index" , "err" , err , "tsdb" , idx .Name ())
423
- }
424
- }
425
- }()
426
-
427
- tasks , err := strategy .Plan (ctx , table , tenant , openTSDBs , metas )
432
+ tasks , err := strategy .Plan (ctx , table , tenant , tsdbs , metas )
428
433
if err != nil {
429
434
return nil , nil , fmt .Errorf ("failed to plan tasks: %w" , err )
430
435
}
@@ -506,18 +511,26 @@ func openAllTSDBs(
506
511
tenant string ,
507
512
store common.TSDBStore ,
508
513
tsdbs []tsdb.SingleTenantTSDBIdentifier ,
509
- ) (map [tsdb.SingleTenantTSDBIdentifier ]common.ClosableForSeries , error ) {
510
- openTSDBs := make (map [tsdb.SingleTenantTSDBIdentifier ]common.ClosableForSeries , len (tsdbs ))
514
+ alreadyOpen strategies.TSDBSet ,
515
+ ) (strategies.TSDBSet , error ) {
516
+ if len (alreadyOpen ) == 0 {
517
+ alreadyOpen = make (strategies.TSDBSet , len (tsdbs ))
518
+ }
519
+
511
520
for _ , idx := range tsdbs {
512
- tsdb , err := store .LoadTSDB (ctx , table , tenant , idx )
521
+ if _ , ok := alreadyOpen [idx ]; ok {
522
+ continue
523
+ }
524
+
525
+ reader , err := store .LoadTSDB (ctx , table , tenant , idx )
513
526
if err != nil {
514
527
return nil , fmt .Errorf ("failed to load tsdb: %w" , err )
515
528
}
516
529
517
- openTSDBs [idx ] = tsdb
530
+ alreadyOpen [idx ] = reader
518
531
}
519
532
520
- return openTSDBs , nil
533
+ return alreadyOpen , nil
521
534
}
522
535
523
536
// deleteOutdatedMetasAndBlocks filters out the outdated metas from the `metas` argument and deletes them from the store.
@@ -847,8 +860,13 @@ func (p *Planner) forwardTaskToBuilder(
847
860
builderID string ,
848
861
task * QueueTask ,
849
862
) (* protos.TaskResult , error ) {
863
+ protoTask , err := task .ToProtoTask (builder .Context ())
864
+ if err != nil {
865
+ return nil , fmt .Errorf ("error converting task to proto task: %w" , err )
866
+ }
867
+
850
868
msg := & protos.PlannerToBuilder {
851
- Task : task . ToProtoTask () ,
869
+ Task : protoTask ,
852
870
}
853
871
854
872
if err := builder .Send (msg ); err != nil {
0 commit comments