Skip to content

Commit

Permalink
Simplify Cache.Snapshot
Browse files Browse the repository at this point in the history
This simplifies the cache.Snapshot func to swap the hot cache to
the snapshot cache instead of copy and appending entries.  This
reduces the amount of time the cache is write locked which should
reduce cache contention for the read only code paths.
  • Loading branch information
jwilder committed Jan 11, 2017
1 parent bcdb0a7 commit ae838ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
30 changes: 11 additions & 19 deletions tsdb/engine/tsm1/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,27 +345,19 @@ func (c *Cache) Snapshot() (*Cache, error) {
}
}

// Append the current cache values to the snapshot. Because we're accessing
// the Cache we need to call f on each partition in serial.
if err := c.store.applySerial(func(k string, e *entry) error {
e.mu.RLock()
defer e.mu.RUnlock()
snapshotEntry, ok := c.snapshot.store.entry(k)
if ok {
if err := snapshotEntry.add(e.values); err != nil {
return err
}
} else {
c.snapshot.store.add(k, e)
snapshotEntry = e
}
atomic.AddUint64(&c.snapshotSize, uint64(Values(e.values).Size()))
return nil
}); err != nil {
return nil, err
// Did a prior snapshot exist that failed? If so, return the existing
// snapshot to retry.
if c.snapshot.Size() > 0 {
return c.snapshot, nil
}

snapshotSize := c.Size() // record the number of bytes written into a snapshot
c.snapshot.store, c.store = c.store, c.snapshot.store
snapshotSize := c.Size()

// Save the size of the snapshot on the snapshot cache
c.snapshot.size = snapshotSize
// Save the size of the snapshot on the live cache
c.snapshotSize = snapshotSize

// Reset the cache's store.
c.store.reset()
Expand Down
4 changes: 2 additions & 2 deletions tsdb/engine/tsm1/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func TestCache_CacheWriteMemoryExceeded(t *testing.T) {
t.Fatalf("cache keys incorrect after writes, exp %v, got %v", exp, keys)
}
if err := c.Write("bar", Values{v1}); err == nil || !strings.Contains(err.Error(), "cache-max-memory-size") {
t.Fatalf("wrong error writing key bar to cache")
t.Fatalf("wrong error writing key bar to cache: %v", err)
}

// Grab snapshot, write should still fail since we're still using the memory.
Expand All @@ -434,7 +434,7 @@ func TestCache_CacheWriteMemoryExceeded(t *testing.T) {
t.Fatalf("failed to snapshot cache: %v", err)
}
if err := c.Write("bar", Values{v1}); err == nil || !strings.Contains(err.Error(), "cache-max-memory-size") {
t.Fatalf("wrong error writing key bar to cache")
t.Fatalf("wrong error writing key bar to cache: %v", err)
}

// Clear the snapshot and the write should now succeed.
Expand Down

0 comments on commit ae838ef

Please sign in to comment.