Skip to content
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

Align tenant pruning according to wall clock #7299

Merged
merged 2 commits into from May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/thanos/receive.go
Expand Up @@ -418,7 +418,13 @@ func runReceive(
{
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
return runutil.Repeat(2*time.Hour, ctx.Done(), func() error {
pruneInterval := 2 * time.Duration(tsdbOpts.MaxBlockDuration) * time.Millisecond
return runutil.Repeat(time.Minute, ctx.Done(), func() error {
currentTime := time.Now()
currentTotalMinutes := currentTime.Hour()*60 + currentTime.Minute()
if currentTotalMinutes%int(pruneInterval.Minutes()) != 0 {
return nil
}
if err := dbs.Prune(ctx); err != nil {
level.Error(logger).Log("err", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/receive/multitsdb.go
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/prometheus/prometheus/tsdb"

"github.com/thanos-io/objstore"

"github.com/thanos-io/thanos/pkg/api/status"
"github.com/thanos-io/thanos/pkg/block/metadata"
"github.com/thanos-io/thanos/pkg/component"
Expand Down Expand Up @@ -334,6 +335,7 @@ func (t *MultiTSDB) Prune(ctx context.Context) error {
if t.tsdbOpts.RetentionDuration == 0 {
return nil
}
level.Info(t.logger).Log("msg", "Running pruning job")

var (
wg sync.WaitGroup
Expand All @@ -342,7 +344,6 @@ func (t *MultiTSDB) Prune(ctx context.Context) error {
prunedTenants []string
pmtx sync.Mutex
)

t.mtx.RLock()
for tenantID, tenantInstance := range t.tenants {
wg.Add(1)
Expand Down
8 changes: 4 additions & 4 deletions pkg/receive/multitsdb_test.go
Expand Up @@ -454,10 +454,10 @@ func TestMultiTSDBPrune(t *testing.T) {
)
defer func() { testutil.Ok(t, m.Close()) }()

for i := 0; i < 100; i++ {
testutil.Ok(t, appendSample(m, "deleted-tenant", time.UnixMilli(int64(10+i))))
testutil.Ok(t, appendSample(m, "compacted-tenant", time.Now().Add(-4*time.Hour)))
testutil.Ok(t, appendSample(m, "active-tenant", time.Now().Add(time.Duration(i)*time.Second)))
for step := time.Duration(0); step <= 2*time.Hour; step += time.Minute {
testutil.Ok(t, appendSample(m, "deleted-tenant", time.Now().Add(-9*time.Hour+step)))
testutil.Ok(t, appendSample(m, "compacted-tenant", time.Now().Add(-4*time.Hour+step)))
testutil.Ok(t, appendSample(m, "active-tenant", time.Now().Add(step)))
}
testutil.Equals(t, 3, len(m.TSDBLocalClients()))

Expand Down