Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tsdb/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type Engine interface {
DiskSize() int64
IsIdle() (bool, string)
Free() error
// TimeRange will return the min/max time range of shard
TimeRange() (int64, int64)

io.WriterTo
}
Expand Down
5 changes: 5 additions & 0 deletions tsdb/engine/tsm1/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,11 @@ func (e *Engine) LastModified() time.Time {
return fsTime
}

// TimeRange returns the min and max time range for this shard
func (e *Engine) TimeRange() (int64, int64) {
return e.FileStore.TimeRange()
}

// EngineStatistics maintains statistics for the engine.
type EngineStatistics struct {
CacheCompactions int64 // Counter of cache compactions that have ever run.
Expand Down
21 changes: 21 additions & 0 deletions tsdb/engine/tsm1/file_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,27 @@ func (f *FileStore) LastModified() time.Time {
return f.lastModified
}

// TimeRange returns the minimum and maximum times across all TSM files in the FileStore.
// Returns (math.MaxInt64, math.MinInt64) if there are no files.
func (f *FileStore) TimeRange() (min, max int64) {
f.mu.RLock()
defer f.mu.RUnlock()

min, max = math.MaxInt64, math.MinInt64

for _, file := range f.files {
fileMin, fileMax := file.TimeRange()
if fileMin < min {
min = fileMin
}
if fileMax > max {
max = fileMax
}
}

return min, max
}

// We need to determine the possible files that may be accessed by this query given
// the time range.
func (f *FileStore) cost(key []byte, min, max int64) query.IteratorCost {
Expand Down
10 changes: 10 additions & 0 deletions tsdb/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ func (s *Shard) LastModifiedWithErr() (time.Time, error) {
return engine.LastModified(), nil
}

func (s *Shard) TimeRange() (int64, int64, error) {
engine, err := s.Engine()
if err != nil {
return int64(0), int64(0), fmt.Errorf("failed getting shard %d time range: %w", s.id, err)
}

minT, maxT := engine.TimeRange()
return minT, maxT, nil
}

// Index returns a reference to the underlying index. It returns an error if
// the index is nil.
func (s *Shard) Index() (Index, error) {
Expand Down
30 changes: 30 additions & 0 deletions tsdb/shard_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tsdb

import (
"fmt"
"math"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -301,3 +302,32 @@ func (sh *TempShard) MustWritePointsString(s string) {
panic(err)
}
}

func TestShard_TimeRange(t *testing.T) {
for _, index := range RegisteredIndexes() {
t.Run(index, func(t *testing.T) {
sh := NewTempShard(index)
defer CloseShard(t, sh)

require.NoError(t, sh.Open())

min, max, err := sh.TimeRange()
require.NoError(t, err)
require.Equal(t, int64(math.MaxInt64), min)
require.Equal(t, int64(math.MinInt64), max)

sh.MustWritePointsString(`
cpu value=100 1000000000
cpu value=200 2000000000
mem value=300 1500000000`)

// Force flush WAL to TSM files
require.NoError(t, sh.ScheduleFullCompaction())

min, max, err = sh.TimeRange()
require.NoError(t, err)
require.Equal(t, int64(1000000000000000000), min)
require.Equal(t, int64(2000000000000000000), max)
})
}
}