-
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 efe234b
Showing
5 changed files
with
163 additions
and
13 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,112 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package compact | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"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" | ||
) | ||
|
||
// ScheduleDeleteFilename is file to store compactor metadata | ||
// about when the block is scheduled to be deleted. | ||
const ScheduleDeleteFilename = "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"` | ||
} | ||
|
||
// 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 { | ||
return filepath.Walk(s.dir, func(path string, info os.FileInfo, err error) error { | ||
if strings.HasSuffix(path, ScheduleDeleteFilename) { | ||
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(id ulid.ULID) error { | ||
path := filepath.Join(s.dir, id.String(), ScheduleDeleteFilename) | ||
if _, err := os.Stat(path); err == nil { | ||
level.Warn(s.logger).Log("msg", "compactor-meta already exists for block id", id.String()) | ||
return nil | ||
} | ||
compactorMeta := &CompactorMeta{ | ||
ID: id, | ||
DeletionTime: time.Now().Unix(), | ||
} | ||
|
||
f, err := os.Create(path) | ||
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 | ||
} | ||
|
||
return nil | ||
} |