fix(tsdb): Fix race calling filterShards outside a lock

Move filterShards inside the lock, as it enumerates the shards map,
which can result in data race when the map is written concurrently.
pull/10387/head
Stuart Carnie 2018-10-17 13:51:17 -07:00
parent 432f9eaf31
commit 9520b8d956
1 changed files with 6 additions and 6 deletions

View File

@ -660,6 +660,11 @@ func (s *Store) DeleteShard(shardID uint64) error {
}
delete(s.shards, 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))
s.mu.Unlock()
// Ensure the pending deletion flag is cleared on exit.
@ -677,12 +682,6 @@ func (s *Store) DeleteShard(shardID uint64) error {
ss := index.SeriesIDSet()
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))
s.walkShards(shards, func(sh *Shard) error {
index, err := sh.Index()
if err != nil {
@ -880,6 +879,7 @@ func (s *Store) DeleteMeasurement(database, name string) error {
// filterShards returns a slice of shards where fn returns true
// for the shard. If the provided predicate is nil then all shards are returned.
// filterShards should be called under a lock.
func (s *Store) filterShards(fn func(sh *Shard) bool) []*Shard {
var shards []*Shard
if fn == nil {