Skip to content

Commit

Permalink
address remarks
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 6, 2023
1 parent 615f1cf commit ea8fca6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 63 deletions.
13 changes: 4 additions & 9 deletions cmd/promtool/tsdb.go
Expand Up @@ -408,17 +408,12 @@ func openBlock(path, blockID string) (*tsdb.DBReadOnly, tsdb.BlockReader, error)
}
}

if blockID != "" {
b, err := db.Block(nil, filepath.Join(blockID))
if err != nil {
return nil, nil, err
}
block = b
b, err := db.Block(blockID)
if err != nil {
return nil, nil, err
}
block = b

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

Expand Down
68 changes: 23 additions & 45 deletions tsdb/db.go
Expand Up @@ -603,75 +603,53 @@ func (db *DBReadOnly) Blocks() ([]BlockReader, error) {

// LastBlockID returns the BlockID of latest block.
func (db *DBReadOnly) LastBlockID(logger log.Logger) (string, error) {
select {
case <-db.closed:
return "", ErrClosed
default:
}
latestDirName, err := db.lastBlockDirName(logger)
if err != nil {
return "", err
}
// Open the latest block and get its ID
block, err := db.Block(logger, latestDirName)
if err != nil {
return "", err
}
return block.Meta().ULID.String(), nil
}

func (db *DBReadOnly) lastBlockDirName(logger log.Logger) (string, error) {
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 := ""
lastBlockID := ""
// Walk the blocks directory and find the latest subdirectory
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()
// 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
latestDirName = dirName
if timestamp > max {
max = timestamp
lastBlockID = dirName
}
}
}

if latestDirName == "" {
if lastBlockID == "" {
return "", errors.New("no blocks found")
}

return latestDirName, nil
return lastBlockID, nil
}

// Block returns a block reader by given block id.
func (db *DBReadOnly) Block(logger log.Logger, blockID string) (BlockReader, error) {
func (db *DBReadOnly) Block(blockID string) (BlockReader, error) {
select {
case <-db.closed:
return nil, ErrClosed
default:
}

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

block, err := OpenBlock(db.logger, filepath.Join(db.dir, blockID), nil)
if err != nil {
return nil, err
}
Expand Down
15 changes: 6 additions & 9 deletions tsdb/db_test.go
Expand Up @@ -2457,24 +2457,21 @@ func TestDBReadOnly(t *testing.T) {
require.Equal(t, expBlock.Meta(), blocks[i].Meta(), "block meta mismatch")
}
})
t.Run("analyse-block-id", func(t *testing.T) {
t.Run("block", func(t *testing.T) {
blockID := expBlock.meta.ULID.String()
block, err := dbReadOnly.Block(nil, blockID)
block, err := dbReadOnly.Block(blockID)
require.NoError(t, err)
require.Equal(t, expBlock.Meta(), block.Meta(), "block meta mismatch")
})
t.Run("invalid-block-id", func(t *testing.T) {
t.Run("invalid block ID", func(t *testing.T) {
blockID := "01GTDVZZF52NSWB5SXQF0P2PGF"
_, err := dbReadOnly.Block(nil, blockID)
_, err := dbReadOnly.Block(blockID)
require.Error(t, err)
})
t.Run("analyse-latest-block", func(t *testing.T) {
t.Run("last block ID", func(t *testing.T) {
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")
require.Equal(t, expBlocks[2].Meta().ULID.String(), blockID)
})
t.Run("querier", func(t *testing.T) {
// Open a read only db and ensure that the API returns the same result as the normal DB.
Expand Down

0 comments on commit ea8fca6

Please sign in to comment.