|
38 | 38 | // ErrMultipleIndexTypes is returned when trying to do deletes on a database with |
39 | 39 | // multiple index types. |
40 | 40 | ErrMultipleIndexTypes = errors.New("cannot delete data. DB contains shards using both inmem and tsi1 indexes. Please convert all shards to use the same index type to delete data.") |
| 41 | + // ErrNothingToDelete is returned when where is nothing to do for DeleteSeries |
| 42 | + // this error is a noop |
| 43 | + ErrNothingToDelete = errors.New("nothing to delete") |
41 | 44 | ) |
42 | 45 |
|
43 | 46 | // Statistics gathered by the store. |
@@ -1649,41 +1652,50 @@ func (s *Store) DeleteSeries(database string, sources []influxql.Source, conditi |
1649 | 1652 | max = influxql.MaxTime |
1650 | 1653 | } |
1651 | 1654 |
|
1652 | | - s.mu.RLock() |
1653 | | - if s.databases[database].hasMultipleIndexTypes() { |
1654 | | - s.mu.RUnlock() |
1655 | | - return ErrMultipleIndexTypes |
1656 | | - } |
1657 | | - sfile := s.sfiles[database] |
1658 | | - if sfile == nil { |
1659 | | - s.mu.RUnlock() |
1660 | | - // No series file means nothing has been written to this DB and thus nothing to delete. |
1661 | | - return nil |
1662 | | - } |
1663 | | - |
1664 | | - shardFilterFn := byDatabase(database) |
1665 | | - if len(sources) != 0 { |
1666 | | - var rp string |
1667 | | - for idx, source := range sources { |
1668 | | - if measurement, ok := source.(*influxql.Measurement); ok { |
1669 | | - if idx == 0 { |
1670 | | - rp = measurement.RetentionPolicy |
1671 | | - } else if rp != measurement.RetentionPolicy { |
1672 | | - return fmt.Errorf("mixed retention policies not supported, wanted %q got %q", rp, measurement.RetentionPolicy) |
| 1655 | + getEpochsAndShards := func() (error, []*Shard, map[uint64]*epochTracker, *SeriesFile) { |
| 1656 | + s.mu.RLock() |
| 1657 | + defer s.mu.RUnlock() |
| 1658 | + if s.databases[database].hasMultipleIndexTypes() { |
| 1659 | + return ErrMultipleIndexTypes, nil, nil, nil |
| 1660 | + } |
| 1661 | + sfile := s.sfiles[database] |
| 1662 | + if sfile == nil { |
| 1663 | + // No series file means nothing has been written to this DB and thus nothing to delete. |
| 1664 | + return ErrNothingToDelete, nil, nil, nil |
| 1665 | + } |
| 1666 | + |
| 1667 | + shardFilterFn := byDatabase(database) |
| 1668 | + if len(sources) != 0 { |
| 1669 | + var rp string |
| 1670 | + for idx, source := range sources { |
| 1671 | + if measurement, ok := source.(*influxql.Measurement); ok { |
| 1672 | + if idx == 0 { |
| 1673 | + rp = measurement.RetentionPolicy |
| 1674 | + } else if rp != measurement.RetentionPolicy { |
| 1675 | + return fmt.Errorf("mixed retention policies not supported, wanted %q got %q", rp, measurement.RetentionPolicy), nil, nil, nil |
| 1676 | + } |
| 1677 | + } else { |
| 1678 | + return fmt.Errorf("unsupported source type in delete %v", source), nil, nil, nil |
1673 | 1679 | } |
1674 | | - } else { |
1675 | | - return fmt.Errorf("unsupported source type in delete %v", source) |
1676 | 1680 | } |
1677 | | - } |
1678 | 1681 |
|
1679 | | - if rp != "" { |
1680 | | - shardFilterFn = ComposeShardFilter(shardFilterFn, byRetentionPolicy(rp)) |
| 1682 | + if rp != "" { |
| 1683 | + shardFilterFn = ComposeShardFilter(shardFilterFn, byRetentionPolicy(rp)) |
| 1684 | + } |
1681 | 1685 | } |
| 1686 | + |
| 1687 | + shards := s.filterShards(shardFilterFn) |
| 1688 | + epochs := s.epochsForShards(shards) |
| 1689 | + return nil, shards, epochs, sfile |
1682 | 1690 | } |
1683 | | - shards := s.filterShards(shardFilterFn) |
1684 | 1691 |
|
1685 | | - epochs := s.epochsForShards(shards) |
1686 | | - s.mu.RUnlock() |
| 1692 | + err, shards, epochs, sfile := getEpochsAndShards() |
| 1693 | + if err != nil && !errors.Is(err, ErrNothingToDelete) { |
| 1694 | + s.Logger.Error("DeleteSeries failed", zap.String("error", err.Error())) |
| 1695 | + return err |
| 1696 | + } else if errors.Is(err, ErrNothingToDelete) { |
| 1697 | + return nil |
| 1698 | + } |
1687 | 1699 |
|
1688 | 1700 | // Limit deletes for each shard since expanding the measurement into the list |
1689 | 1701 | // of series keys can be very memory intensive if run concurrently. |
|
0 commit comments