(maxt int64)
| 622 | } |
| 623 | |
| 624 | func (db *DBReadOnly) loadDataAsQueryable(maxt int64) (storage.SampleAndChunkQueryable, error) { |
| 625 | select { |
| 626 | case <-db.closed: |
| 627 | return nil, ErrClosed |
| 628 | default: |
| 629 | } |
| 630 | blockReaders, err := db.Blocks() |
| 631 | if err != nil { |
| 632 | return nil, err |
| 633 | } |
| 634 | blocks := make([]*Block, len(blockReaders)) |
| 635 | for i, b := range blockReaders { |
| 636 | b, ok := b.(*Block) |
| 637 | if !ok { |
| 638 | return nil, errors.New("unable to convert a read only block to a normal block") |
| 639 | } |
| 640 | blocks[i] = b |
| 641 | } |
| 642 | |
| 643 | opts := DefaultHeadOptions() |
| 644 | // Hard link the chunk files to a dir in db.sandboxDir in case the Head needs to truncate some of them |
| 645 | // or cut new ones while replaying the WAL. |
| 646 | // See https://github.com/prometheus/prometheus/issues/11618. |
| 647 | err = chunks.HardLinkChunkFiles(mmappedChunksDir(db.dir), mmappedChunksDir(db.sandboxDir)) |
| 648 | if err != nil { |
| 649 | return nil, err |
| 650 | } |
| 651 | opts.ChunkDirRoot = db.sandboxDir |
| 652 | head, err := NewHead(nil, db.logger, nil, nil, opts, NewHeadStats()) |
| 653 | if err != nil { |
| 654 | return nil, err |
| 655 | } |
| 656 | maxBlockTime := int64(math.MinInt64) |
| 657 | if len(blocks) > 0 { |
| 658 | maxBlockTime = blocks[len(blocks)-1].Meta().MaxTime |
| 659 | } |
| 660 | |
| 661 | // Also add the WAL if the current blocks don't cover the requests time range. |
| 662 | if maxBlockTime <= maxt { |
| 663 | if err := head.Close(); err != nil { |
| 664 | return nil, err |
| 665 | } |
| 666 | w, err := wlog.Open(db.logger, filepath.Join(db.dir, "wal")) |
| 667 | if err != nil { |
| 668 | return nil, err |
| 669 | } |
| 670 | var wbl *wlog.WL |
| 671 | wblDir := filepath.Join(db.dir, wlog.WblDirName) |
| 672 | if _, err := os.Stat(wblDir); !os.IsNotExist(err) { |
| 673 | wbl, err = wlog.Open(db.logger, wblDir) |
| 674 | if err != nil { |
| 675 | return nil, err |
| 676 | } |
| 677 | } |
| 678 | opts := DefaultHeadOptions() |
| 679 | opts.ChunkDirRoot = db.sandboxDir |
| 680 | head, err = NewHead(nil, db.logger, w, wbl, opts, NewHeadStats()) |
| 681 | if err != nil { |
no test coverage detected