Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSM: Fix ShouldCompactCache without WAL #9972

Merged
merged 2 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/influx_inspect/buildtsi/buildtsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (cmd *Command) processShard(sfile *tsdb.SeriesFile, dbName, rpName string,

// Write out wal files.
cmd.Logger.Info("building cache from wal files")
cache := tsm1.NewCache(tsdb.DefaultCacheMaxMemorySize, "")
cache := tsm1.NewCache(tsdb.DefaultCacheMaxMemorySize)
loader := tsm1.NewCacheLoader(walPaths)
loader.WithLogger(cmd.Logger)
if err := loader.Load(cache); err != nil {
Expand Down
17 changes: 14 additions & 3 deletions tsdb/engine/tsm1/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ type Cache struct {
// This number is the number of pending or failed WriteSnaphot attempts since the last successful one.
snapshotAttempts int

stats *CacheStatistics
lastSnapshot time.Time
stats *CacheStatistics
lastSnapshot time.Time
lastWriteTime time.Time

// A one time synchronization used to initial the cache with a store. Since the store can allocate a
// a large amount memory across shards, we lazily create it.
Expand All @@ -208,7 +209,7 @@ type Cache struct {

// NewCache returns an instance of a cache which will use a maximum of maxSize bytes of memory.
// Only used for engine caches, never for snapshots.
func NewCache(maxSize uint64, path string) *Cache {
func NewCache(maxSize uint64) *Cache {
c := &Cache{
maxSize: maxSize,
store: emptyStore{},
Expand Down Expand Up @@ -363,6 +364,10 @@ func (c *Cache) WriteMulti(values map[string][]Value) error {
c.updateMemSize(int64(addedSize))
atomic.AddInt64(&c.stats.WriteOK, 1)

c.mu.Lock()
c.lastWriteTime = time.Now()
c.mu.Unlock()

return werr
}

Expand Down Expand Up @@ -762,6 +767,12 @@ func (cl *CacheLoader) WithLogger(log *zap.Logger) {
cl.Logger = log.With(zap.String("service", "cacheloader"))
}

func (c *Cache) LastWriteTime() time.Time {
c.mu.RLock()
defer c.mu.RUnlock()
return c.lastWriteTime
}

// UpdateAge updates the age statistic based on the current time.
func (c *Cache) UpdateAge() {
c.mu.RLock()
Expand Down
8 changes: 4 additions & 4 deletions tsdb/engine/tsm1/cache_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestCacheCheckConcurrentReadsAreSafe(t *testing.T) {
}

wg := sync.WaitGroup{}
c := tsm1.NewCache(1000000, "")
c := tsm1.NewCache(1000000)

ch := make(chan struct{})
for _, s := range series {
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestCacheRace(t *testing.T) {
}

wg := sync.WaitGroup{}
c := tsm1.NewCache(1000000, "")
c := tsm1.NewCache(1000000)

ch := make(chan struct{})
for _, s := range series {
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestCacheRace2Compacters(t *testing.T) {
}

wg := sync.WaitGroup{}
c := tsm1.NewCache(1000000, "")
c := tsm1.NewCache(1000000)

ch := make(chan struct{})
for _, s := range series {
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestCacheRace2Compacters(t *testing.T) {
c.ClearSnapshot(true)
mu.Lock()
defer mu.Unlock()
for k, _ := range myFiles {
for k := range myFiles {
if _, ok := mapFiles[k]; !ok {
errC <- fmt.Errorf("something else deleted one of my files")
return
Expand Down
76 changes: 38 additions & 38 deletions tsdb/engine/tsm1/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func TestCache_NewCache(t *testing.T) {
c := NewCache(100, "")
c := NewCache(100)
if c == nil {
t.Fatalf("failed to create new cache")
}
Expand All @@ -42,7 +42,7 @@ func TestCache_CacheWrite(t *testing.T) {
values := Values{v0, v1, v2}
valuesSize := uint64(v0.Size() + v1.Size() + v2.Size())

c := NewCache(3*valuesSize, "")
c := NewCache(3 * valuesSize)

if err := c.Write([]byte("foo"), values); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand All @@ -65,7 +65,7 @@ func TestCache_CacheWrite_TypeConflict(t *testing.T) {
values := Values{v0, v1}
valuesSize := v0.Size() + v1.Size()

c := NewCache(uint64(2*valuesSize), "")
c := NewCache(uint64(2 * valuesSize))

if err := c.Write([]byte("foo"), values[:1]); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand All @@ -87,7 +87,7 @@ func TestCache_CacheWriteMulti(t *testing.T) {
values := Values{v0, v1, v2}
valuesSize := uint64(v0.Size() + v1.Size() + v2.Size())

c := NewCache(30*valuesSize, "")
c := NewCache(30 * valuesSize)

if err := c.WriteMulti(map[string][]Value{"foo": values, "bar": values}); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand All @@ -104,19 +104,19 @@ func TestCache_CacheWriteMulti(t *testing.T) {
// Tests that the cache stats and size are correctly maintained during writes.
func TestCache_WriteMulti_Stats(t *testing.T) {
limit := uint64(1)
c := NewCache(limit, "")
c := NewCache(limit)
ms := NewTestStore()
c.store = ms

// Not enough room in the cache.
v := NewValue(1, 1.0)
values := map[string][]Value{"foo": []Value{v, v}}
values := map[string][]Value{"foo": {v, v}}
if got, exp := c.WriteMulti(values), ErrCacheMemorySizeLimitExceeded(uint64(v.Size()*2), limit); !reflect.DeepEqual(got, exp) {
t.Fatalf("got %q, expected %q", got, exp)
}

// Fail one of the values in the write.
c = NewCache(50, "")
c = NewCache(50)
c.init()
c.store = ms

Expand All @@ -127,7 +127,7 @@ func TestCache_WriteMulti_Stats(t *testing.T) {
return true, nil
}

values = map[string][]Value{"foo": []Value{v, v}, "bar": []Value{v}}
values = map[string][]Value{"foo": {v, v}, "bar": {v}}
if got, exp := c.WriteMulti(values), errors.New("write failed"); !reflect.DeepEqual(got, exp) {
t.Fatalf("got %v, expected %v", got, exp)
}
Expand All @@ -152,7 +152,7 @@ func TestCache_CacheWriteMulti_TypeConflict(t *testing.T) {
values := Values{v0, v1, v2}
valuesSize := uint64(v0.Size() + v1.Size() + v2.Size())

c := NewCache(3*valuesSize, "")
c := NewCache(3 * valuesSize)

if err := c.WriteMulti(map[string][]Value{"foo": values[:1], "bar": values[1:]}); err == nil {
t.Fatalf(" expected field type conflict")
Expand All @@ -174,7 +174,7 @@ func TestCache_Cache_DeleteRange(t *testing.T) {
values := Values{v0, v1, v2}
valuesSize := uint64(v0.Size() + v1.Size() + v2.Size())

c := NewCache(30*valuesSize, "")
c := NewCache(30 * valuesSize)

if err := c.WriteMulti(map[string][]Value{"foo": values, "bar": values}); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestCache_DeleteRange_NoValues(t *testing.T) {
values := Values{v0, v1, v2}
valuesSize := uint64(v0.Size() + v1.Size() + v2.Size())

c := NewCache(3*valuesSize, "")
c := NewCache(3 * valuesSize)

if err := c.WriteMulti(map[string][]Value{"foo": values}); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestCache_DeleteRange_NotSorted(t *testing.T) {
values := Values{v0, v1, v2}
valuesSize := uint64(v0.Size() + v1.Size() + v2.Size())

c := NewCache(3*valuesSize, "")
c := NewCache(3 * valuesSize)

if err := c.WriteMulti(map[string][]Value{"foo": values}); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestCache_Cache_Delete(t *testing.T) {
values := Values{v0, v1, v2}
valuesSize := uint64(v0.Size() + v1.Size() + v2.Size())

c := NewCache(30*valuesSize, "")
c := NewCache(30 * valuesSize)

if err := c.WriteMulti(map[string][]Value{"foo": values, "bar": values}); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestCache_Cache_Delete(t *testing.T) {
}

func TestCache_Cache_Delete_NonExistent(t *testing.T) {
c := NewCache(1024, "")
c := NewCache(1024)

c.Delete([][]byte{[]byte("bar")})

Expand All @@ -337,7 +337,7 @@ func TestCache_CacheWriteMulti_Duplicates(t *testing.T) {
v5 := NewValue(5, 3.0)
values1 := Values{v3, v4, v5}

c := NewCache(0, "")
c := NewCache(0)

if err := c.WriteMulti(map[string][]Value{"foo": values0}); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand Down Expand Up @@ -367,7 +367,7 @@ func TestCache_CacheValues(t *testing.T) {
v3 := NewValue(1, 1.0)
v4 := NewValue(4, 4.0)

c := NewCache(512, "")
c := NewCache(512)
if deduped := c.Values([]byte("no such key")); deduped != nil {
t.Fatalf("Values returned for no such key")
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestCache_CacheSnapshot(t *testing.T) {
v6 := NewValue(7, 5.0)
v7 := NewValue(2, 5.0)

c := NewCache(512, "")
c := NewCache(512)
if err := c.Write([]byte("foo"), Values{v0, v1, v2, v3}); err != nil {
t.Fatalf("failed to write 3 values, key foo to cache: %s", err.Error())
}
Expand Down Expand Up @@ -472,9 +472,9 @@ func TestCache_CacheSnapshot(t *testing.T) {
// Tests that Snapshot updates statistics correctly.
func TestCache_Snapshot_Stats(t *testing.T) {
limit := uint64(16)
c := NewCache(limit, "")
c := NewCache(limit)

values := map[string][]Value{"foo": []Value{NewValue(1, 1.0)}}
values := map[string][]Value{"foo": {NewValue(1, 1.0)}}
if err := c.WriteMulti(values); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -504,7 +504,7 @@ func TestCache_Snapshot_Stats(t *testing.T) {
}

func TestCache_CacheEmptySnapshot(t *testing.T) {
c := NewCache(512, "")
c := NewCache(512)

// Grab snapshot, and ensure it's as expected.
snapshot, err := c.Snapshot()
Expand All @@ -531,7 +531,7 @@ func TestCache_CacheWriteMemoryExceeded(t *testing.T) {
v0 := NewValue(1, 1.0)
v1 := NewValue(2, 2.0)

c := NewCache(uint64(v1.Size()), "")
c := NewCache(uint64(v1.Size()))

if err := c.Write([]byte("foo"), Values{v0}); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand Down Expand Up @@ -577,7 +577,7 @@ func TestCache_Deduplicate_Concurrent(t *testing.T) {
}

wg := sync.WaitGroup{}
c := NewCache(1000000, "")
c := NewCache(1000000)

wg.Add(1)
go func() {
Expand Down Expand Up @@ -611,9 +611,9 @@ func TestCacheLoader_LoadSingle(t *testing.T) {
p3 := NewValue(1, true)

values := map[string][]Value{
"foo": []Value{p1},
"bar": []Value{p2},
"baz": []Value{p3},
"foo": {p1},
"bar": {p2},
"baz": {p3},
}

entry := &WriteWALEntry{
Expand All @@ -629,7 +629,7 @@ func TestCacheLoader_LoadSingle(t *testing.T) {
}

// Load the cache using the segment.
cache := NewCache(1024, "")
cache := NewCache(1024)
loader := NewCacheLoader([]string{f.Name()})
if err := loader.Load(cache); err != nil {
t.Fatalf("failed to load cache: %s", err.Error())
Expand All @@ -652,7 +652,7 @@ func TestCacheLoader_LoadSingle(t *testing.T) {
}

// Reload the cache using the segment.
cache = NewCache(1024, "")
cache = NewCache(1024)
loader = NewCacheLoader([]string{f.Name()})
if err := loader.Load(cache); err != nil {
t.Fatalf("failed to load cache: %s", err.Error())
Expand Down Expand Up @@ -698,13 +698,13 @@ func TestCacheLoader_LoadDouble(t *testing.T) {
}

values := map[string][]Value{
"foo": []Value{p1},
"bar": []Value{p2},
"foo": {p1},
"bar": {p2},
}
segmentWrite(w1, values)
values = map[string][]Value{
"baz": []Value{p3},
"qux": []Value{p4},
"baz": {p3},
"qux": {p4},
}
segmentWrite(w2, values)

Expand All @@ -714,7 +714,7 @@ func TestCacheLoader_LoadDouble(t *testing.T) {
}

// Load the cache using the segments.
cache := NewCache(1024, "")
cache := NewCache(1024)
loader := NewCacheLoader([]string{f1.Name(), f2.Name()})
if err := loader.Load(cache); err != nil {
t.Fatalf("failed to load cache: %s", err.Error())
Expand Down Expand Up @@ -748,7 +748,7 @@ func TestCacheLoader_LoadDeleted(t *testing.T) {
p3 := NewValue(3, 3.0)

values := map[string][]Value{
"foo": []Value{p1, p2, p3},
"foo": {p1, p2, p3},
}

entry := &WriteWALEntry{
Expand Down Expand Up @@ -778,7 +778,7 @@ func TestCacheLoader_LoadDeleted(t *testing.T) {
}

// Load the cache using the segment.
cache := NewCache(1024, "")
cache := NewCache(1024)
loader := NewCacheLoader([]string{f.Name()})
if err := loader.Load(cache); err != nil {
t.Fatalf("failed to load cache: %s", err.Error())
Expand All @@ -790,7 +790,7 @@ func TestCacheLoader_LoadDeleted(t *testing.T) {
}

// Reload the cache using the segment.
cache = NewCache(1024, "")
cache = NewCache(1024)
loader = NewCacheLoader([]string{f.Name()})
if err := loader.Load(cache); err != nil {
t.Fatalf("failed to load cache: %s", err.Error())
Expand All @@ -809,7 +809,7 @@ func TestCache_Split(t *testing.T) {
values := Values{v0, v1, v2}
valuesSize := uint64(v0.Size() + v1.Size() + v2.Size())

c := NewCache(0, "")
c := NewCache(0)

if err := c.Write([]byte("foo"), values); err != nil {
t.Fatalf("failed to write key foo to cache: %s", err.Error())
Expand Down Expand Up @@ -898,7 +898,7 @@ func (s *TestStore) count() int { return s.c
var fvSize = uint64(NewValue(1, float64(1)).Size())

func BenchmarkCacheFloatEntries(b *testing.B) {
cache := NewCache(uint64(b.N)*fvSize, "")
cache := NewCache(uint64(b.N) * fvSize)
vals := make([][]Value, b.N)
for i := 0; i < b.N; i++ {
vals[i] = []Value{NewValue(1, float64(i))}
Expand All @@ -919,7 +919,7 @@ type points struct {

func BenchmarkCacheParallelFloatEntries(b *testing.B) {
c := b.N * runtime.GOMAXPROCS(0)
cache := NewCache(uint64(c)*fvSize*10, "")
cache := NewCache(uint64(c) * fvSize * 10)
vals := make([]points, c)
for i := 0; i < c; i++ {
v := make([]Value, 10)
Expand Down
Loading