diff --git a/tsdb/engine/tsm1/cache.go b/tsdb/engine/tsm1/cache.go index 5ec2a68eec..9f317e9d31 100644 --- a/tsdb/engine/tsm1/cache.go +++ b/tsdb/engine/tsm1/cache.go @@ -103,13 +103,19 @@ type Cache struct { // NewCache returns an instance of a cache which will use a maximum of maxSize bytes of memory. func NewCache(maxSize uint64, path string) *Cache { - return &Cache{ + c := &Cache{ maxSize: maxSize, store: make(map[string]*entry), statMap: influxdb.NewStatistics("tsm1_cache:"+path, "tsm1_cache", map[string]string{"path": path}), path: path, lastSnapshot: time.Now(), } + c.UpdateAge() + c.UpdateCompactTime(0) + c.updateCachedBytes(0) + c.updateMemSize(0) + c.updateSnapshots() + return c } // Write writes the set of values for the key to the cache. This function is goroutine-safe. @@ -128,9 +134,7 @@ func (c *Cache) Write(key string, values []Value) error { c.size = newSize // Update the memory size stat - sizeStat := new(expvar.Int) - sizeStat.Set(int64(c.size)) - c.statMap.Set(statCacheMemoryBytes, sizeStat) + c.updateMemSize(newSize) return nil } @@ -160,9 +164,7 @@ func (c *Cache) WriteMulti(values map[string][]Value) error { c.mu.Unlock() // Update the memory size stat - sizeStat := new(expvar.Int) - sizeStat.Set(int64(newSize)) - c.statMap.Set(statCacheMemoryBytes, sizeStat) + c.updateMemSize(newSize) return nil } @@ -184,17 +186,9 @@ func (c *Cache) Snapshot() *Cache { c.snapshots = append(c.snapshots, snapshot) c.snapshotsSize += snapshot.size - // Update stats - memSizeStat := new(expvar.Int) - memSizeStat.Set(0) - c.statMap.Set(statCacheMemoryBytes, memSizeStat) - - diskSizeStat := new(expvar.Int) - diskSizeStat.Set(int64(c.snapshotsSize)) - c.statMap.Set(statCacheDiskBytes, diskSizeStat) - - c.statMap.Add(statCachedBytes, int64(snapshot.size)) - c.statMap.Add(statSnapshots, 1) + c.updateMemSize(0) + c.updateCachedBytes(snapshot.size) + c.updateSnapshots() return snapshot } @@ -221,12 +215,7 @@ func (c *Cache) ClearSnapshot(snapshot *Cache) { } } - // Update disk stats - diskSizeStat := new(expvar.Int) - diskSizeStat.Set(int64(c.snapshotsSize)) - c.statMap.Set(statCacheDiskBytes, diskSizeStat) - - c.statMap.Add(statSnapshots, -1) + c.updateSnapshots() } // Size returns the number of point-calcuated bytes the cache currently uses. @@ -457,3 +446,27 @@ func (c *Cache) UpdateAge() { func (c *Cache) UpdateCompactTime(d time.Duration) { c.statMap.Add(statWALCompactionTimeMs, int64(d/time.Millisecond)) } + +// Update the cachedBytes counter +func (c *Cache) updateCachedBytes(b uint64) { + c.statMap.Add(statCachedBytes, int64(b)) +} + +// Update the memSize level +func (c *Cache) updateMemSize(b uint64) { + memSizeStat := new(expvar.Int) + memSizeStat.Set(int64(b)) + c.statMap.Set(statCacheMemoryBytes, memSizeStat) +} + +// Update the snapshotsCount and the diskSize levels +func (c *Cache) updateSnapshots() { + // Update disk stats + diskSizeStat := new(expvar.Int) + diskSizeStat.Set(int64(c.snapshotsSize)) + c.statMap.Set(statCacheDiskBytes, diskSizeStat) + + snapshotsStat := new(expvar.Int) + snapshotsStat.Set(int64(len(c.snapshots))) + c.statMap.Set(statSnapshots, snapshotsStat) +}