Skip to content

Commit d2af577

Browse files
fix: improved shard deletion (#24602) (#24844) (#24848)
Avoid unnecessarily deleting series from the series file Log all errors on shard deletion Closes #24834 (cherry picked from commit 8ff06d5) closes #24836 (cherry picked from commit 2066c4b) closes #24837
1 parent e4fba98 commit d2af577

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

tsdb/store.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ func (s *Store) DeleteShard(shardID uint64) error {
764764
return nil
765765
}
766766

767-
// Remove the shard from Store so it's not returned to callers requesting
767+
// Remove the shard from Store, so it's not returned to callers requesting
768768
// shards. Also mark that this shard is currently being deleted in a separate
769769
// map so that we do not have to retain the global store lock while deleting
770770
// files.
@@ -792,7 +792,6 @@ func (s *Store) DeleteShard(shardID uint64) error {
792792
s.mu.Lock()
793793
defer s.mu.Unlock()
794794
delete(s.pendingShardDeletes, shardID)
795-
s.databases[db].removeIndexType(sh.IndexType())
796795
}()
797796

798797
// Get the shard's local bitset of series IDs.
@@ -806,6 +805,7 @@ func (s *Store) DeleteShard(shardID uint64) error {
806805
err = s.walkShards(shards, func(sh *Shard) error {
807806
index, err := sh.Index()
808807
if err != nil {
808+
s.Logger.Error("cannot find shard index", zap.Uint64("shard_id", sh.ID()), zap.Error(err))
809809
return err
810810
}
811811

@@ -814,7 +814,9 @@ func (s *Store) DeleteShard(shardID uint64) error {
814814
})
815815

816816
if err != nil {
817-
s.Logger.Error("error walking shards during DeleteShard operation", zap.Error(err))
817+
// We couldn't get the index for a shard. Rather than deleting series which may
818+
// exist in that shard as well as in the current shard, we stop the current deletion
819+
return err
818820
}
819821

820822
// Remove any remaining series in the set from the series file, as they don't
@@ -823,13 +825,15 @@ func (s *Store) DeleteShard(shardID uint64) error {
823825
sfile := s.seriesFile(db)
824826
if sfile != nil {
825827
ss.ForEach(func(id uint64) {
826-
err = sfile.DeleteSeriesID(id)
827-
if err != nil {
828-
s.Logger.Error("error deleting series id during DeleteShard operation", zap.Uint64("id", id), zap.Error(err))
828+
if err := sfile.DeleteSeriesID(id); err != nil {
829+
sfile.Logger.Error(
830+
"cannot delete series in shard",
831+
zap.Uint64("series_id", id),
832+
zap.Uint64("shard_id", shardID),
833+
zap.Error(err))
829834
}
830835
})
831836
}
832-
833837
}
834838

835839
// Close the shard.
@@ -840,9 +844,13 @@ func (s *Store) DeleteShard(shardID uint64) error {
840844
// Remove the on-disk shard data.
841845
if err := os.RemoveAll(sh.path); err != nil {
842846
return err
847+
} else if err = os.RemoveAll(sh.walPath); err != nil {
848+
return err
849+
} else {
850+
// Remove index type from the database on success
851+
s.databases[db].removeIndexType(sh.IndexType())
852+
return nil
843853
}
844-
845-
return os.RemoveAll(sh.walPath)
846854
}
847855

848856
// DeleteDatabase will close all shards associated with a database and remove the directory and files from disk.

0 commit comments

Comments
 (0)