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

Memoize output of FileStore.Stats #7480

Merged
merged 1 commit into from
Oct 24, 2016
Merged

Memoize output of FileStore.Stats #7480

merged 1 commit into from
Oct 24, 2016

Conversation

e-dard
Copy link
Contributor

@e-dard e-dard commented Oct 18, 2016

Required for all non-trivial PRs
  • Rebased/mergable
  • Tests pass

This should significantly reduce the time spent planning compactions when there are large numbers of TSM files within a shard, by memoizing the output of FileStore.Stats. When new TSM files are added or removed the output of FileStore.Stats will need to be recalculated.

Before:

⇒  gt -run NONE -bench BenchmarkFileStore_Stats
BenchmarkFileStore_Stats-4          5000        215589 ns/op       82094 B/op          2 allocs/op
PASS
ok      github.com/influxdata/influxdb/tsdb/engine/tsm1 9.185s

After:

⇒  gt -run NONE -bench BenchmarkFileStore_Stats
BenchmarkFileStore_Stats-4      20000000            99.2 ns/op         0 B/op          0 allocs/op
PASS
ok      github.com/influxdata/influxdb/tsdb/engine/tsm1 15.388s

@jwilder @benbjohnson

/cc @beckettsean

@@ -258,6 +261,7 @@ func (f *FileStore) Add(files ...TSMFile) {
for _, file := range files {
atomic.AddInt64(&f.stats.DiskBytes, int64(file.Size()))
}
f.lastFileStats = nil // Will need to be recalculated on next call to Stats.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lower down in the readers, I just resliced this to f.lastFileStats = f.lastFileStats[:0] to avoid the extra allocations. Maybe do that here and check for len() == 0?

@@ -449,6 +454,7 @@ func (f *FileStore) Close() error {
file.Close()
}

f.lastFileStats = nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to be done in Replace which is the primary way that files are added to the FileStore via compactions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is in Close so I think it's OK to set it to nil.

@@ -598,6 +612,7 @@ func (f *FileStore) Replace(oldFiles, newFiles []string) error {
// Tell the purger about our in-use files we need to remove
f.purger.add(inuse)

f.lastFileStats = nil // Will need to be recalculated on next call to Stats.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[:0]

defer f.mu.RUnlock()
if len(f.lastFileStats) > 0 {
f.mu.RUnlock()
return f.lastFileStats
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to assign this under the read lock and return the assign var or else your accessing the slice outside the lock.

@e-dard e-dard force-pushed the er-fs-stats branch 6 times, most recently from 5c9ba86 to 72d716c Compare October 19, 2016 15:42
@e-dard e-dard changed the title [WIP] Memoize output of FileStore.Stats Memoize output of FileStore.Stats Oct 19, 2016
@jwilder jwilder added this to the 1.1.0 milestone Oct 20, 2016
@jwilder
Copy link
Contributor

jwilder commented Oct 20, 2016

The rename in your test fails on windows/appveyor because the file is open. Probably need to do something like: https://github.com/influxdata/influxdb/blob/master/tsdb/engine/tsm1/compact_test.go#L2175

@jwilder jwilder self-assigned this Oct 24, 2016
@jwilder jwilder force-pushed the er-fs-stats branch 2 times, most recently from 5fbf762 to e002ffc Compare October 24, 2016 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants