diff --git a/tsdb/engine/tsm1/cache.go b/tsdb/engine/tsm1/cache.go index 9f317e9d31..2d056575be 100644 --- a/tsdb/engine/tsm1/cache.go +++ b/tsdb/engine/tsm1/cache.go @@ -122,19 +122,21 @@ func NewCache(maxSize uint64, path string) *Cache { // It returns an error if the cache has exceeded its max size. func (c *Cache) Write(key string, values []Value) error { c.mu.Lock() - defer c.mu.Unlock() // Enough room in the cache? - newSize := c.size + uint64(Values(values).Size()) + addedSize := Values(values).Size() + newSize := c.size + uint64(addedSize) if c.maxSize > 0 && newSize+c.snapshotsSize > c.maxSize { + c.mu.Unlock() return ErrCacheMemoryExceeded } c.write(key, values) c.size = newSize + c.mu.Unlock() // Update the memory size stat - c.updateMemSize(newSize) + c.updateMemSize(int64(addedSize)) return nil } @@ -164,7 +166,7 @@ func (c *Cache) WriteMulti(values map[string][]Value) error { c.mu.Unlock() // Update the memory size stat - c.updateMemSize(newSize) + c.updateMemSize(int64(totalSz)) return nil } @@ -186,7 +188,7 @@ func (c *Cache) Snapshot() *Cache { c.snapshots = append(c.snapshots, snapshot) c.snapshotsSize += snapshot.size - c.updateMemSize(0) + c.updateMemSize(-int64(snapshot.size)) c.updateCachedBytes(snapshot.size) c.updateSnapshots() @@ -453,10 +455,8 @@ func (c *Cache) updateCachedBytes(b uint64) { } // Update the memSize level -func (c *Cache) updateMemSize(b uint64) { - memSizeStat := new(expvar.Int) - memSizeStat.Set(int64(b)) - c.statMap.Set(statCacheMemoryBytes, memSizeStat) +func (c *Cache) updateMemSize(b int64) { + c.statMap.Add(statCacheMemoryBytes, b) } // Update the snapshotsCount and the diskSize levels