-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compact: add schedule-delete and delete-delay
Signed-off-by: khyatisoneji <[email protected]>
- Loading branch information
1 parent
7515974
commit 3d21ab1
Showing
8 changed files
with
185 additions
and
16 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package metadata | ||
|
||
import "github.com/oklog/ulid" | ||
|
||
// CompactorMetaFilename is the known json filename to store compactor metadata. | ||
const CompactorMetaFilename = "compactor-meta.json" | ||
|
||
// CompactorMeta stores block id and when block was marked for deletion. | ||
type CompactorMeta struct { | ||
ID ulid.ULID `json:"id"` | ||
DeletionTime int64 `json:"deletion_time"` | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package compact | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
"github.com/pkg/errors" | ||
"github.com/thanos-io/thanos/pkg/block" | ||
"github.com/thanos-io/thanos/pkg/block/metadata" | ||
"github.com/thanos-io/thanos/pkg/objstore" | ||
"github.com/thanos-io/thanos/pkg/runutil" | ||
) | ||
|
||
// ScheduleBlockDelete marks the block to be deleted. | ||
type ScheduleBlockDelete struct { | ||
dir string | ||
logger log.Logger | ||
deleteDelay time.Duration | ||
bkt objstore.Bucket | ||
} | ||
|
||
// NewScheduleBlockDelete creates a new ScheduleBlockDelete. | ||
func NewScheduleBlockDelete(logger log.Logger, dir string, bkt objstore.Bucket, deleteDelay time.Duration) *ScheduleBlockDelete { | ||
return &ScheduleBlockDelete{ | ||
dir: dir, | ||
logger: logger, | ||
deleteDelay: deleteDelay, | ||
bkt: bkt, | ||
} | ||
} | ||
|
||
// ScheduleDelete deletes blocks from bucket | ||
// deleteDelay duration after block is marked for deletion. | ||
func (s *ScheduleBlockDelete) ScheduleDelete(ctx context.Context) error { | ||
defer func() { | ||
if err := os.RemoveAll(s.dir); err != nil { | ||
level.Error(s.logger).Log("msg", "failed to remove compactor meta dir", s.dir, "err", err) | ||
} | ||
}() | ||
|
||
if err := os.RemoveAll(s.dir); err != nil { | ||
return errors.Wrap(err, "clean compaction compactor meta dir") | ||
} | ||
|
||
return s.bkt.Iter(ctx, "", func(name string) error { | ||
id, ok := block.IsBlockDir(name) | ||
if ok { | ||
compactorMetaFile := path.Join(id.String(), metadata.CompactorMetaFilename) | ||
|
||
r, err := s.bkt.Get(ctx, compactorMetaFile) | ||
if s.bkt.IsObjNotFoundErr(err) { | ||
return nil | ||
} | ||
if err != nil { | ||
return errors.Wrapf(err, "get compactor-meta file: %v", compactorMetaFile) | ||
} | ||
|
||
defer runutil.CloseWithLogOnErr(s.logger, r, "close bkt compactor-meta get") | ||
|
||
metaContent, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return errors.Wrapf(err, "read compactor-meta file: %v", compactorMetaFile) | ||
} | ||
|
||
compactorMeta := metadata.CompactorMeta{} | ||
if err := json.Unmarshal(metaContent, &compactorMeta); err != nil { | ||
return errors.Wrap(err, "unmarshal compactor meta") | ||
} | ||
|
||
if time.Since(time.Unix(compactorMeta.DeletionTime, 0)) > s.deleteDelay { | ||
if err := block.Delete(ctx, s.logger, s.bkt, compactorMeta.ID); err != nil { | ||
return errors.Wrap(err, "delete block") | ||
} | ||
|
||
if err := os.RemoveAll(filepath.Join(s.dir, compactorMeta.ID.String())); err != nil { | ||
return errors.Wrap(err, "delete compactor-meta.json") | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
} |