This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Krasi Georgiev <kgeorgie@redhat.com>
- Loading branch information
Krasi Georgiev
committed
May 9, 2018
1 parent
7c89db0
commit 000c29c
Showing
3 changed files
with
148 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package tsdb | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type ErrorOpen string | ||
|
||
func (e ErrorOpen) Error() string { | ||
return fmt.Sprintf("block open error: %v", e) | ||
} | ||
|
||
type ErrorSequence string | ||
|
||
func (e ErrorSequence) Error() string { | ||
return fmt.Sprintf("block sequence error %v", e) | ||
} | ||
|
||
type ErrorTmp struct{} | ||
|
||
func (e ErrorTmp) Error() string { | ||
return fmt.Sprintf("abandoned tmp files") | ||
} | ||
|
||
// Scan checks the integrity of the tsdb for a given directory and returns a list of files and folders that can be deleted. | ||
// It returns a map of error to explain the reason and slice of dirs for further processing. | ||
func Scan(dir string) (map[error][]string, error) { | ||
useless := make(map[error][]string) | ||
|
||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
|
||
db := &DB{ | ||
dir: dir, | ||
logger: nil, | ||
opts: &Options{}, | ||
} | ||
|
||
dirs, err := blockDirs(db.dir) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "find blocks") | ||
} | ||
|
||
// Scan for blocks that can't be opened. | ||
var errOpen ErrorOpen | ||
for _, dir := range dirs { | ||
b, err := OpenBlock(dir, nil) | ||
if err != nil { | ||
errOpen = ErrorOpen(err.Error()) | ||
useless[errOpen] = append(useless[errOpen], dir) | ||
break | ||
} | ||
|
||
db.blocks = append(db.blocks, b) | ||
} | ||
|
||
// Scan for overlaping blocks. | ||
if overlaps := validateBlockSequence(db.blocks); overlaps != nil { | ||
var dirs []string | ||
for _, b := range db.blocks { | ||
dirs = append(dirs, b.Dir()) | ||
} | ||
useless[ErrorSequence(overlaps.String())] = dirs | ||
} | ||
|
||
// Scan for temporary files. | ||
var tmpFiles []string | ||
filepath.Walk(db.dir, func(path string, f os.FileInfo, _ error) error { | ||
if !f.IsDir() { | ||
if filepath.Ext(path) == ".tmp" { | ||
tmpFiles = append(tmpFiles, path) | ||
} | ||
} | ||
return nil | ||
}) | ||
if len(tmpFiles) > 0 { | ||
useless[ErrorTmp{}] = tmpFiles | ||
} | ||
return useless, nil | ||
} |