Skip to content

Commit

Permalink
fix(tsm1): Fix temp directory search bug
Browse files Browse the repository at this point in the history
The original code's intention is to scan a directory for the directory
with the higest value when converted to an integer.

So directories may be in the form:

  0.tmp
  1.tmp
  2.tmp
  30.tmp
  ...
  100.tmp

The loop should scan the directory, strip the basename and extension
from the file name to leave just a number, then store the higest number
it finds.

Before this patch, there is a bug that has the code only store the
higest value if there is an error converting the numeric value into an
integer.

This patch primarily fixes that logic.

In addition, this patch will save an indent level by inverting logic in
two places:

Instead if checkig if a file is a directory and has a suffix of ".tmp",
it is probably better to test if a file is NOT a directory OR does NOT
have an extension of ".tmp" then continue.

Also, instead of testig if len(ss) == 2, we can test if len(ss) != 2 and
continue if so.

Both of these save an indent level and keeps our "happy path" to the
left.

Finally, this patch will use string concatination instead of calling
fmt.Sprintf() to add periods to "tmp" and "tsm" extension.
  • Loading branch information
ayang64 committed Apr 8, 2020
1 parent f78b2aa commit 64debf6
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions tsdb/engine/tsm1/file_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,21 +482,31 @@ func (f *FileStore) Open() error {
if err != nil {
return err
}
ext := fmt.Sprintf(".%s", TmpTSMFileExtension)

// ascertain the current temp directory number by examining the existing
// directories and choosing the one with the higest basename when converted
// to an integer.
for _, fi := range tmpfiles {
if fi.IsDir() && strings.HasSuffix(fi.Name(), ext) {
ss := strings.Split(filepath.Base(fi.Name()), ".")
if len(ss) == 2 {
if i, err := strconv.Atoi(ss[0]); err != nil {
if i > f.currentTempDirID {
f.currentTempDirID = i
}
}
}
if !fi.IsDir() || !strings.HasSuffix(fi.Name(), "."+TmpTSMFileExtension) {
continue
}

ss := strings.Split(filepath.Base(fi.Name()), ".")
if len(ss) != 2 {
continue
}

i, err := strconv.Atoi(ss[0])
if err != nil || i <= f.currentTempDirID {
continue
}

// i must be a valid integer and greater than f.currentTempDirID at this
// point
f.currentTempDirID = i
}

files, err := filepath.Glob(filepath.Join(f.dir, fmt.Sprintf("*.%s", TSMFileExtension)))
files, err := filepath.Glob(filepath.Join(f.dir, "*."+TSMFileExtension))
if err != nil {
return err
}
Expand Down

0 comments on commit 64debf6

Please sign in to comment.