github.com/thanos-io/thanos@v0.32.5/pkg/compact/blocks_cleaner.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package compact
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	"github.com/go-kit/log"
    11  	"github.com/go-kit/log/level"
    12  	"github.com/pkg/errors"
    13  	"github.com/prometheus/client_golang/prometheus"
    14  	"github.com/thanos-io/objstore"
    15  
    16  	"github.com/thanos-io/thanos/pkg/block"
    17  )
    18  
    19  // BlocksCleaner is a struct that deletes blocks from bucket which are marked for deletion.
    20  type BlocksCleaner struct {
    21  	logger                   log.Logger
    22  	ignoreDeletionMarkFilter *block.IgnoreDeletionMarkFilter
    23  	bkt                      objstore.Bucket
    24  	deleteDelay              time.Duration
    25  	blocksCleaned            prometheus.Counter
    26  	blockCleanupFailures     prometheus.Counter
    27  }
    28  
    29  // NewBlocksCleaner creates a new BlocksCleaner.
    30  func NewBlocksCleaner(logger log.Logger, bkt objstore.Bucket, ignoreDeletionMarkFilter *block.IgnoreDeletionMarkFilter, deleteDelay time.Duration, blocksCleaned, blockCleanupFailures prometheus.Counter) *BlocksCleaner {
    31  	return &BlocksCleaner{
    32  		logger:                   logger,
    33  		ignoreDeletionMarkFilter: ignoreDeletionMarkFilter,
    34  		bkt:                      bkt,
    35  		deleteDelay:              deleteDelay,
    36  		blocksCleaned:            blocksCleaned,
    37  		blockCleanupFailures:     blockCleanupFailures,
    38  	}
    39  }
    40  
    41  // DeleteMarkedBlocks uses ignoreDeletionMarkFilter to gather the blocks that are marked for deletion and deletes those
    42  // if older than given deleteDelay.
    43  func (s *BlocksCleaner) DeleteMarkedBlocks(ctx context.Context) error {
    44  	level.Info(s.logger).Log("msg", "started cleaning of blocks marked for deletion")
    45  
    46  	deletionMarkMap := s.ignoreDeletionMarkFilter.DeletionMarkBlocks()
    47  	for _, deletionMark := range deletionMarkMap {
    48  		if time.Since(time.Unix(deletionMark.DeletionTime, 0)).Seconds() > s.deleteDelay.Seconds() {
    49  			if err := block.Delete(ctx, s.logger, s.bkt, deletionMark.ID); err != nil {
    50  				s.blockCleanupFailures.Inc()
    51  				return errors.Wrap(err, "delete block")
    52  			}
    53  			s.blocksCleaned.Inc()
    54  			level.Info(s.logger).Log("msg", "deleted block marked for deletion", "block", deletionMark.ID)
    55  		}
    56  	}
    57  
    58  	level.Info(s.logger).Log("msg", "cleaning of blocks marked for deletion done")
    59  	return nil
    60  }