github.com/thanos-io/thanos@v0.32.5/pkg/compact/retention.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  	"fmt"
     9  	"time"
    10  
    11  	"github.com/go-kit/log"
    12  	"github.com/go-kit/log/level"
    13  	"github.com/oklog/ulid"
    14  	"github.com/pkg/errors"
    15  	"github.com/prometheus/client_golang/prometheus"
    16  	"github.com/thanos-io/objstore"
    17  
    18  	"github.com/thanos-io/thanos/pkg/block"
    19  	"github.com/thanos-io/thanos/pkg/block/metadata"
    20  )
    21  
    22  // ApplyRetentionPolicyByResolution removes blocks depending on the specified retentionByResolution based on blocks MaxTime.
    23  // A value of 0 disables the retention for its resolution.
    24  func ApplyRetentionPolicyByResolution(
    25  	ctx context.Context,
    26  	logger log.Logger,
    27  	bkt objstore.Bucket,
    28  	metas map[ulid.ULID]*metadata.Meta,
    29  	retentionByResolution map[ResolutionLevel]time.Duration,
    30  	blocksMarkedForDeletion prometheus.Counter,
    31  ) error {
    32  	level.Info(logger).Log("msg", "start optional retention")
    33  	for id, m := range metas {
    34  		retentionDuration := retentionByResolution[ResolutionLevel(m.Thanos.Downsample.Resolution)]
    35  		if retentionDuration.Seconds() == 0 {
    36  			continue
    37  		}
    38  
    39  		maxTime := time.Unix(m.MaxTime/1000, 0)
    40  		if time.Now().After(maxTime.Add(retentionDuration)) {
    41  			level.Info(logger).Log("msg", "applying retention: marking block for deletion", "id", id, "maxTime", maxTime.String())
    42  			if err := block.MarkForDeletion(ctx, logger, bkt, id, fmt.Sprintf("block exceeding retention of %v", retentionDuration), blocksMarkedForDeletion); err != nil {
    43  				return errors.Wrap(err, "delete block")
    44  			}
    45  		}
    46  	}
    47  	level.Info(logger).Log("msg", "optional retention apply done")
    48  	return nil
    49  }