Skip to content

Commit

Permalink
Fixed lastest BlockID search logic, test cases
Browse files Browse the repository at this point in the history
Signed-off-by: nidhey27 <nidhey.indurkar@infracloud.io>
  • Loading branch information
nidhey27 committed Mar 6, 2023
1 parent 6da892a commit de6e437
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 43 deletions.
2 changes: 1 addition & 1 deletion cmd/promtool/tsdb.go
Expand Up @@ -432,7 +432,7 @@ func analyzeBlock(path, blockID string, limit int, runExtended bool) error {
}()

meta := block.Meta()

fmt.Printf("Block ID: %s\n", meta.ULID)
// Presume 1ms resolution that Prometheus uses.
fmt.Printf("Duration: %s\n", (time.Duration(meta.MaxTime-meta.MinTime) * 1e6).String())
fmt.Printf("Series: %d\n", meta.Stats.NumSeries)
Expand Down
66 changes: 25 additions & 41 deletions tsdb/db.go
Expand Up @@ -617,54 +617,39 @@ func (db *DBReadOnly) LastBlockID(logger log.Logger) (string, error) {
}

func (db *DBReadOnly) lastBlockDirName(logger log.Logger) (string, error) {
latestDirName := ""
entries, err := os.ReadDir(db.dir)
if err != nil {
return "", err
}
skipNames := map[string]struct{}{
"index": {},
"chunks_head": {},
"lock": {},
"queries.active": {},
"wal": {},
}
max := uint64(0)

latestDirName := ""
// Walk the blocks directory and find the latest subdirectory
err := filepath.Walk(filepath.Join(db.dir), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
for _, e := range entries {
dirName := e.Name()

if _, skip := skipNames[dirName]; skip {
// Skip the directory
continue
}
dirName := filepath.Base(path)

// Parse the directory name as a timestamp
entries, err := os.ReadDir(dirName)
ulidObj, err := ulid.Parse(dirName)
if err != nil {
return nil
return "", err
}
skipNames := map[string]struct{}{
"index": {},
"chunks_head": {},
"lock": {},
"queries.active": {},
"wal": {},
}
max := uint64(0)
for _, e := range entries {
dirName := e.Name()

if _, skip := skipNames[dirName]; skip {
// Skip the directory
continue
}

ulidObj, err := ulid.Parse(dirName)
if err != nil {
return err
}
timestamp := ulidObj.Time()
timestamp := ulidObj.Time()

if timestamp > max {
max = timestamp
latestDirName = dirName
}
if timestamp > max {
max = timestamp
latestDirName = dirName
}
return nil // Parse the directory name as a timestamp
})
if err != nil {
return "", err
}

if latestDirName == "" {
Expand All @@ -683,7 +668,6 @@ func (db *DBReadOnly) Block(logger log.Logger, blockID string) (BlockReader, err
}

block, err := OpenBlock(logger, filepath.Join(db.dir, blockID), nil)

if err != nil {
return nil, err
}
Expand Down
9 changes: 8 additions & 1 deletion tsdb/db_test.go
Expand Up @@ -2445,8 +2445,15 @@ func TestDBReadOnly(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expBlock.Meta(), block.Meta(), "block meta mismatch")
})
t.Run("invalid-block-id", func(t *testing.T) {
blockID := "01GTDVZZF52NSWB5SXQF0P2PGF"
_, err := dbReadOnly.Block(nil, blockID)
require.Error(t, err)
})
t.Run("analyse-latest-block", func(t *testing.T) {
blockID := ""
blockID, err := dbReadOnly.LastBlockID(nil)
expBlock = expBlocks[2]
require.NoError(t, err)
block, err := dbReadOnly.Block(nil, blockID)
require.NoError(t, err)
require.Equal(t, expBlock.Meta(), block.Meta(), "block meta mismatch")
Expand Down

0 comments on commit de6e437

Please sign in to comment.