Skip to content

Commit

Permalink
Add disk stats for FileStore
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-rushakoff committed Feb 20, 2016
1 parent d99c09c commit 602043e
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion tsdb/engine/tsm1/file_store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tsm1

import (
"expvar"
"fmt"
"log"
"os"
Expand All @@ -10,6 +11,8 @@ import (
"strings"
"sync"
"time"

"github.com/influxdata/influxdb"
)

type TSMFile interface {
Expand Down Expand Up @@ -79,6 +82,11 @@ type TSMFile interface {
BlockIterator() *BlockIterator
}

// Statistics gathered by the FileStore.
const (
statFileStoreBytes = "diskBytes"
)

type FileStore struct {
mu sync.RWMutex
lastModified time.Time
Expand All @@ -90,6 +98,8 @@ type FileStore struct {

Logger *log.Logger
traceLogging bool

statMap *expvar.Map
}

type FileStat struct {
Expand Down Expand Up @@ -118,7 +128,8 @@ func NewFileStore(dir string) *FileStore {
return &FileStore{
dir: dir,
lastModified: time.Now(),
Logger: log.New(os.Stderr, "[filestore]", log.LstdFlags),
Logger: log.New(os.Stderr, "[filestore] ", log.LstdFlags),
statMap: influxdb.NewStatistics("tsm1_filestore:"+dir, "tsm1_filestore", map[string]string{"path": dir}),
}
}

Expand Down Expand Up @@ -154,6 +165,9 @@ func (f *FileStore) NextGeneration() int {
func (f *FileStore) Add(files ...TSMFile) {
f.mu.Lock()
defer f.mu.Unlock()
for _, file := range files {
f.statMap.Add(statFileStoreBytes, int64(file.Size()))
}
f.files = append(f.files, files...)
sort.Sort(tsmReaders(f.files))
}
Expand All @@ -175,6 +189,9 @@ func (f *FileStore) Remove(paths ...string) {

if keep {
active = append(active, file)
} else {
// Removing the file, remove the file size from the total file store bytes
f.statMap.Add(statFileStoreBytes, -int64(file.Size()))
}
}
f.files = active
Expand Down Expand Up @@ -263,6 +280,11 @@ func (f *FileStore) Open() error {
return fmt.Errorf("error opening file %s: %v", fn, err)
}

// Accumulate file store size stat
if fi, err := file.Stat(); err == nil {
f.statMap.Add(statFileStoreBytes, fi.Size())
}

go func(idx int, file *os.File) {
start := time.Now()
df, err := NewTSMReaderWithOptions(TSMReaderOptions{
Expand Down Expand Up @@ -412,6 +434,15 @@ func (f *FileStore) Replace(oldFiles, newFiles []string) error {
f.files = active
sort.Sort(tsmReaders(f.files))

// Recalculate the disk size stat
var totalSize int64
for _, file := range f.files {
totalSize += int64(file.Size())
}
sizeStat := new(expvar.Int)
sizeStat.Set(totalSize)
f.statMap.Set(statFileStoreBytes, sizeStat)

return nil
}

Expand Down

0 comments on commit 602043e

Please sign in to comment.