forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
per tenant retention #116
Merged
Merged
per tenant retention #116
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,19 +6,27 @@ package compact | |
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/oklog/ulid" | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/model" | ||
"github.com/thanos-io/objstore" | ||
|
||
"github.com/thanos-io/thanos/pkg/block" | ||
"github.com/thanos-io/thanos/pkg/block/metadata" | ||
) | ||
|
||
const ( | ||
// tenantRetentionRegex is the regex pattern for parsing tenant retention. | ||
// valid format is `<tenant>:(<yyyy-mm-dd>|<duration>d)` where <duration> > 0. | ||
tenantRetentionRegex = `^([\w-]+):((\d{4}-\d{2}-\d{2})|(\d+d))$` | ||
) | ||
|
||
// ApplyRetentionPolicyByResolution removes blocks depending on the specified retentionByResolution based on blocks MaxTime. | ||
// A value of 0 disables the retention for its resolution. | ||
func ApplyRetentionPolicyByResolution( | ||
|
@@ -47,3 +55,79 @@ func ApplyRetentionPolicyByResolution( | |
level.Info(logger).Log("msg", "optional retention apply done") | ||
return nil | ||
} | ||
|
||
type RetentionPolicy struct { | ||
CutoffDate time.Time | ||
RetentionDuration time.Duration | ||
} | ||
|
||
func (r RetentionPolicy) isExpired(blockMaxTime time.Time) bool { | ||
if r.CutoffDate.IsZero() { | ||
return time.Now().After(blockMaxTime.Add(r.RetentionDuration)) | ||
} | ||
return r.CutoffDate.After(blockMaxTime) | ||
} | ||
|
||
func ParesRetentionPolicyByTenant(logger log.Logger, retentionTenants []string) (map[string]RetentionPolicy, error) { | ||
pattern := regexp.MustCompile(tenantRetentionRegex) | ||
retentionByTenant := make(map[string]RetentionPolicy, len(retentionTenants)) | ||
for _, tenantRetention := range retentionTenants { | ||
matches := pattern.FindStringSubmatch(tenantRetention) | ||
invalidFormat := errors.Errorf("invalid retention format for tenant: %s, must be `<tenant>:(<yyyy-mm-dd>|<duration>d)`", tenantRetention) | ||
if len(matches) != 5 { | ||
return nil, errors.Wrapf(invalidFormat, "matched size %d", len(matches)) | ||
} | ||
tenant := matches[1] | ||
var policy RetentionPolicy | ||
if _, ok := retentionByTenant[tenant]; ok { | ||
return nil, errors.Errorf("duplicate retention policy for tenant: %s", tenant) | ||
} | ||
if cutoffDate, err := time.Parse(time.DateOnly, matches[3]); matches[3] != "" { | ||
if err != nil { | ||
return nil, errors.Wrapf(invalidFormat, "error parsing cutoff date: %v", err) | ||
} | ||
policy.CutoffDate = cutoffDate | ||
} | ||
if duration, err := model.ParseDuration(matches[4]); matches[4] != "" { | ||
if err != nil { | ||
return nil, errors.Wrapf(invalidFormat, "error parsing duration: %v", err) | ||
} else if duration == 0 { | ||
return nil, errors.Wrapf(invalidFormat, "duration must be greater than 0") | ||
} | ||
policy.RetentionDuration = time.Duration(duration) | ||
} | ||
level.Info(logger).Log("msg", "retention policy for tenant is enabled", "tenant", tenant, "retention policy", fmt.Sprintf("%v", policy)) | ||
retentionByTenant[tenant] = policy | ||
} | ||
return retentionByTenant, nil | ||
} | ||
|
||
// ApplyRetentionPolicyByTenant removes blocks depending on the specified retentionByTenant based on blocks MaxTime. | ||
func ApplyRetentionPolicyByTenant( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plan to add unit test for this as well |
||
ctx context.Context, | ||
logger log.Logger, | ||
bkt objstore.Bucket, | ||
metas map[ulid.ULID]*metadata.Meta, | ||
retentionByTenant map[string]RetentionPolicy, | ||
blocksMarkedForDeletion prometheus.Counter) error { | ||
if len(retentionByTenant) == 0 { | ||
level.Info(logger).Log("msg", "tenant retention is disabled due to no policy") | ||
return nil | ||
} | ||
level.Info(logger).Log("msg", "start tenant retention") | ||
for id, m := range metas { | ||
policy, ok := retentionByTenant[m.Thanos.GetTenant()] | ||
if !ok { | ||
continue | ||
} | ||
maxTime := time.Unix(m.MaxTime/1000, 0) | ||
if policy.isExpired(maxTime) { | ||
level.Info(logger).Log("msg", "applying retention: marking block for deletion", "id", id, "maxTime", maxTime.String()) | ||
if err := block.MarkForDeletion(ctx, logger, bkt, id, fmt.Sprintf("block exceeding retention of %v", policy), blocksMarkedForDeletion); err != nil { | ||
return errors.Wrap(err, "delete block") | ||
} | ||
} | ||
} | ||
level.Info(logger).Log("msg", "tenant retention apply done") | ||
return nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Give some examples in a piece of code comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples in unit tests already