Skip to content

Commit

Permalink
addressed review comments
Browse files Browse the repository at this point in the history
Signed-off-by: nidhey60@gmail.com <nidhey.indurkar@infracloud.io>
  • Loading branch information
nidhey60@gmail.com committed Apr 26, 2023
1 parent aaabced commit c70237f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
27 changes: 15 additions & 12 deletions cmd/promtool/tsdb.go
Expand Up @@ -398,22 +398,25 @@ func openBlock(path, blockID string) (*tsdb.DBReadOnly, tsdb.BlockReader, error)
if err != nil {
return nil, nil, err
}

blocks, err := db.Blocks()
if err != nil {
return nil, nil, err
}
var block tsdb.BlockReader

if blockID == "" {
blockID, err = db.LastBlockID()
if err != nil {
return nil, nil, err
switch {
case blockID != "":
for _, b := range blocks {
if b.Meta().ULID.String() == blockID {
block = b
break
}
}
case len(blocks) > 0:
block = blocks[len(blocks)-1]
}

b, err := db.Block(blockID)
if err != nil {
return nil, nil, err
if block == nil {
return nil, nil, fmt.Errorf("block %s not found", blockID)
}
block = b

return db, block, nil
}

Expand Down
27 changes: 12 additions & 15 deletions tsdb/db.go
Expand Up @@ -611,21 +611,18 @@ func (db *DBReadOnly) LastBlockID() (string, error) {
max := uint64(0)

lastBlockID := ""
// Walk the blocks directory and find the latest subdirectory
for _, e := range entries {
// Check if dir is BLOCK dir or not
if isBlockDir(e) {
dirName := e.Name()
ulidObj, err := ulid.ParseStrict(dirName)
if err != nil {
return "", err
}
timestamp := ulidObj.Time()

if timestamp > max {
max = timestamp
lastBlockID = dirName
}
for _, e := range entries {
// Check if dir is a block dir or not.
dirName := e.Name()
ulidObj, err := ulid.ParseStrict(dirName)
if err != nil {
continue // Not a block dir.
}
timestamp := ulidObj.Time()
if timestamp > max {
max = timestamp
lastBlockID = dirName
}
}

Expand All @@ -646,7 +643,7 @@ func (db *DBReadOnly) Block(blockID string) (BlockReader, error) {

_, err := os.Stat(filepath.Join(db.dir, blockID))
if os.IsNotExist(err) {
return nil, errors.Errorf("Invalid Block ID %s", blockID)
return nil, errors.Errorf("invalid Block ID %s", blockID)
}

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

0 comments on commit c70237f

Please sign in to comment.