Skip to content

Commit

Permalink
fix(tsi1): wait deleting epoch before dropping shard
Browse files Browse the repository at this point in the history
  • Loading branch information
dengzhi.ldz committed Mar 16, 2020
1 parent eddae02 commit 1cbcb19
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions tsdb/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,19 +713,20 @@ func (s *Store) DeleteShard(shardID uint64) error {
return nil
}
delete(s.shards, shardID)
delete(s.epochs, shardID)
s.pendingShardDeletes[shardID] = struct{}{}

db := sh.Database()
// Determine if the shard contained any series that are not present in any
// other shards in the database.
shards := s.filterShards(byDatabase(db))
epoch := s.epochs[shardID]
s.mu.Unlock()

// Ensure the pending deletion flag is cleared on exit.
defer func() {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.epochs, shardID)
delete(s.pendingShardDeletes, shardID)
s.databases[db].removeIndexType(sh.IndexType())
}()
Expand Down Expand Up @@ -786,6 +787,15 @@ func (s *Store) DeleteShard(shardID uint64) error {

}

// enter the epoch tracker
guards, gen := epoch.StartWrite()
defer epoch.EndWrite(gen)

// wait for any guards before closing the shard
for _, guard := range guards {
guard.Wait()
}

// Close the shard.
if err := sh.Close(); err != nil {
return err
Expand All @@ -807,16 +817,25 @@ func (s *Store) DeleteDatabase(name string) error {
// no files locally, so nothing to do
return nil
}
shards := s.filterShards(func(sh *Shard) bool {
return sh.database == name
})
shards := s.filterShards(byDatabase(name))
epochs := s.epochsForShards(shards)
s.mu.RUnlock()

if err := s.walkShards(shards, func(sh *Shard) error {
if sh.database != name {
return nil
}

epoch := epochs[sh.id]
// enter the epoch tracker
guards, gen := epoch.StartWrite()
defer epoch.EndWrite(gen)

// wait for any guards before closing the shard
for _, guard := range guards {
guard.Wait()
}

return sh.Close()
}); err != nil {
return err
Expand Down

0 comments on commit 1cbcb19

Please sign in to comment.