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

Speed up drop database #6623

Merged
merged 1 commit into from
May 13, 2016
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [#3541](https://github.com/influxdata/influxdb/issues/3451): Update SHOW FIELD KEYS to return the field type with the field key.
- [#6609](https://github.com/influxdata/influxdb/pull/6609): Add support for JWT token authentication.
- [#6559](https://github.com/influxdata/influxdb/issues/6559): Teach the http service how to enforce connection limits.
- [#6623](https://github.com/influxdata/influxdb/pull/6623): Speed up drop database

### Bugfixes

Expand Down
33 changes: 27 additions & 6 deletions tsdb/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,40 @@ func (s *Store) ShardIteratorCreator(id uint64) influxql.IteratorCreator {

// DeleteDatabase will close all shards associated with a database and remove the directory and files from disk.
func (s *Store) DeleteDatabase(name string) error {
s.mu.Lock()
defer s.mu.Unlock()
type resp struct {
shardID uint64
err error
}

s.mu.RLock()
responses := make(chan resp, len(s.shards))
var wg sync.WaitGroup
// Close and delete all shards on the database.
for shardID, sh := range s.shards {
if sh.database == name {
// Delete the shard from disk.
if err := s.deleteShard(shardID); err != nil {
return err
}
wg.Add(1)
go func(shardID uint64) {
defer wg.Done()
err := sh.Close()
responses <- resp{shardID, err}
}(shardID)
}
}
s.mu.RUnlock()
wg.Wait()
close(responses)
Copy link
Contributor

@e-dard e-dard May 13, 2016

Choose a reason for hiding this comment

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

You could further improve this by getting rid of the WaitGroup and counting how many responses you have received in your for loop further down. Once you have len(s.shards) responses you can close the channel and exit the loop.

This has the added benefit that you don't have to wait for all the shards to attempt to close themselves, before finding out that one of them early on errored out anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, thinking about it I guess you would probably want to carry on deleting the shards after an error anyway right? So then the waitgroup does make sense..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Wanted to have them all try and either complete or fail before returning an error.


for r := range responses {
if r.err != nil {
return r.err
}
s.mu.Lock()
delete(s.shards, r.shardID)
s.mu.Unlock()
}

s.mu.Lock()
defer s.mu.Unlock()
if err := os.RemoveAll(filepath.Join(s.path, name)); err != nil {
return err
}
Expand Down