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 0b6ef9a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
29 changes: 16 additions & 13 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 Expand Up @@ -686,4 +689,4 @@ func backfillOpenMetrics(path, outputDir string, humanReadable, quiet bool, maxB
}

return checkErr(backfill(5000, inputFile.Bytes(), outputDir, humanReadable, quiet, maxBlockDuration))
}
}

Check failure on line 692 in cmd/promtool/tsdb.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
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 0b6ef9a

Please sign in to comment.