-
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 a561c40
Showing
5 changed files
with
198 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
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,143 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package compact | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
"github.com/oklog/ulid" | ||
"github.com/pkg/errors" | ||
"github.com/thanos-io/thanos/pkg/block" | ||
"github.com/thanos-io/thanos/pkg/objstore" | ||
"github.com/thanos-io/thanos/pkg/runutil" | ||
) | ||
|
||
// DebugCompactorMetas is folder to store compactor metadata | ||
// about when the block is scheduled to be deleted. | ||
const DebugCompactorMetas = "debug/compactor-metas" | ||
|
||
// CompactorMeta stores block id and when block was marked for deletion. | ||
type CompactorMeta struct { | ||
ID ulid.ULID `json:"id"` | ||
DeletionTime int64 `json:"deletion_time"` | ||
} | ||
|
||
// 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 { | ||
compactorMetaPath := filepath.Join(s.dir, "download") | ||
defer func() { | ||
if err := os.RemoveAll(compactorMetaPath); err != nil { | ||
level.Error(s.logger).Log("msg", "failed to remove compactor meta dir", compactorMetaPath, "err", err) | ||
} | ||
}() | ||
|
||
if err := os.RemoveAll(compactorMetaPath); err != nil { | ||
return errors.Wrap(err, "clean compaction compactor meta dir") | ||
} | ||
|
||
if err := objstore.DownloadDir(ctx, s.logger, s.bkt, DebugCompactorMetas, compactorMetaPath); err != nil { | ||
return errors.Wrap(err, "downlad compactor-metas dir") | ||
} | ||
|
||
return filepath.Walk(compactorMetaPath, func(path string, fileInfo os.FileInfo, err error) error { | ||
_, file := filepath.Split(path) | ||
if _, err := ulid.Parse(strings.TrimSuffix(file, filepath.Ext(file))); err != nil { | ||
compactorMetaBytes, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return errors.Wrap(err, "read compactor meta") | ||
} | ||
|
||
compactorMeta := CompactorMeta{} | ||
|
||
if err := json.Unmarshal([]byte(compactorMetaBytes), &compactorMeta); err != nil { | ||
return errors.Wrap(err, "unmarshal compactor meta") | ||
} | ||
|
||
if time.Now().Unix()-compactorMeta.DeletionTime > s.deleteDelay.Milliseconds() { | ||
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 | ||
}) | ||
} | ||
|
||
// MarkBlockForDeletion creates a file | ||
// which stores information about when the block was marked for deletion. | ||
func (s *ScheduleBlockDelete) MarkBlockForDeletion(ctx context.Context, id ulid.ULID) error { | ||
compactorMetaExists, err := objstore.Exists(ctx, s.bkt, path.Join(DebugCompactorMetas, fmt.Sprintf("%s.json", id.String()))) | ||
if err != nil { | ||
return errors.Wrap(err, fmt.Sprint("check compactor meta for id %s in bucket", id.String())) | ||
} | ||
if compactorMetaExists { | ||
level.Info(s.logger).Log("msg", "compactor-meta already exists for block id", id.String()) | ||
return nil | ||
} | ||
|
||
compactorMetaPath := filepath.Join(s.dir, "upload", fmt.Sprintf("%s.json", id.String())) | ||
compactorMeta := &CompactorMeta{ | ||
ID: id, | ||
DeletionTime: time.Now().Unix(), | ||
} | ||
|
||
f, err := os.Create(compactorMetaPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
enc := json.NewEncoder(f) | ||
enc.SetIndent("", "\t") | ||
|
||
if err := enc.Encode(compactorMeta); err != nil { | ||
runutil.CloseWithLogOnErr(s.logger, f, "write meta file close") | ||
return err | ||
} | ||
if err := f.Close(); err != nil { | ||
return err | ||
} | ||
|
||
if err := objstore.UploadFile(ctx, s.logger, s.bkt, compactorMetaPath, path.Join(DebugCompactorMetas, fmt.Sprintf("%s.json", id.String()))); err != nil { | ||
return errors.Wrap(err, "upload meta file to debug dir") | ||
} | ||
|
||
if err := os.Remove(compactorMetaPath); err != nil { | ||
return errors.Wrap(err, "delete compactor-meta file") | ||
} | ||
|
||
return nil | ||
} |