From 1c0d8c9b95d211922d87ff02e4f82fd0e5b9e703 Mon Sep 17 00:00:00 2001 From: yeya24 Date: Wed, 30 Jun 2021 22:51:23 -0700 Subject: [PATCH 1/3] add --delete-blocks flag to bucket rewrite tool to mark original blocks for deletion Signed-off-by: yeya24 --- cmd/thanos/tools_bucket.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/thanos/tools_bucket.go b/cmd/thanos/tools_bucket.go index 64185dab35..495331be4f 100644 --- a/cmd/thanos/tools_bucket.go +++ b/cmd/thanos/tools_bucket.go @@ -825,6 +825,7 @@ func registerBucketRewrite(app extkingpin.AppClause, objStoreConfig *extflag.Pat toRelabel := extflag.RegisterPathOrContent(cmd, "rewrite.to-relabel-config", "YAML file that contains relabel configs that will be applied to blocks", extflag.WithEnvSubstitution()) provideChangeLog := cmd.Flag("rewrite.add-change-log", "If specified, all modifications are written to new block directory. Disable if latency is to high.").Default("true").Bool() promBlocks := cmd.Flag("prom-blocks", "If specified, we assume the blocks to be uploaded are only used with Prometheus so we don't check external labels in this case.").Default("false").Bool() + deleteBlocks := cmd.Flag("delete-blocks", "whether to delete the original blocks after rewriting blocks successfully. Available in non dry-run mode only.").Default("false").Bool() cmd.Setup(func(g *run.Group, logger log.Logger, reg *prometheus.Registry, _ opentracing.Tracer, _ <-chan struct{}, _ bool) error { confContentYaml, err := objStoreConfig.Content() if err != nil { @@ -971,6 +972,12 @@ func registerBucketRewrite(app extkingpin.AppClause, objStoreConfig *extflag.Pat } } level.Info(logger).Log("msg", "uploaded", "source", id, "new", newID) + + if !*dryRun && *deleteBlocks { + if err := block.MarkForDeletion(ctx, logger, bkt, id, "block rewritten", promauto.With(nil).NewCounter(prometheus.CounterOpts{})); err != nil { + level.Error(logger).Log("msg", "failed to mark block for deletion", "id", id.String(), "err", err) + } + } } level.Info(logger).Log("msg", "rewrite done", "IDs", strings.Join(*blockIDs, ",")) return nil From dce3450d020aac4e52a23ef3bc361f5f2459ce2d Mon Sep 17 00:00:00 2001 From: yeya24 Date: Wed, 30 Jun 2021 23:00:32 -0700 Subject: [PATCH 2/3] add changelog Signed-off-by: yeya24 --- CHANGELOG.md | 1 + cmd/thanos/tools_bucket.go | 2 +- docs/components/tools.md | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3aa8ab0f5..8d3a40ae54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re - [#4327](https://github.com/thanos-io/thanos/pull/4327) Add environment variable substitution to all YAML configuration flags. - [#4239](https://github.com/thanos-io/thanos/pull/4239) Add penalty based deduplication mode for compactor. - [#4292](https://github.com/thanos-io/thanos/pull/4292) Receive: Enable exemplars ingestion and querying. +- [#4392](https://github.com/thanos-io/thanos/pull/4392) Tools: Added `--delete-blocks` to bucket rewrite tool to mark the original blocks for deletion after rewriting is done. ### Fixed diff --git a/cmd/thanos/tools_bucket.go b/cmd/thanos/tools_bucket.go index 495331be4f..3d20184dd6 100644 --- a/cmd/thanos/tools_bucket.go +++ b/cmd/thanos/tools_bucket.go @@ -825,7 +825,7 @@ func registerBucketRewrite(app extkingpin.AppClause, objStoreConfig *extflag.Pat toRelabel := extflag.RegisterPathOrContent(cmd, "rewrite.to-relabel-config", "YAML file that contains relabel configs that will be applied to blocks", extflag.WithEnvSubstitution()) provideChangeLog := cmd.Flag("rewrite.add-change-log", "If specified, all modifications are written to new block directory. Disable if latency is to high.").Default("true").Bool() promBlocks := cmd.Flag("prom-blocks", "If specified, we assume the blocks to be uploaded are only used with Prometheus so we don't check external labels in this case.").Default("false").Bool() - deleteBlocks := cmd.Flag("delete-blocks", "whether to delete the original blocks after rewriting blocks successfully. Available in non dry-run mode only.").Default("false").Bool() + deleteBlocks := cmd.Flag("delete-blocks", "Whether to delete the original blocks after rewriting blocks successfully. Available in non dry-run mode only.").Default("false").Bool() cmd.Setup(func(g *run.Group, logger log.Logger, reg *prometheus.Registry, _ opentracing.Tracer, _ <-chan struct{}, _ bool) error { confContentYaml, err := objStoreConfig.Content() if err != nil { diff --git a/docs/components/tools.md b/docs/components/tools.md index b3ba3998eb..86bd401f89 100644 --- a/docs/components/tools.md +++ b/docs/components/tools.md @@ -718,6 +718,9 @@ block for deletion to avoid overlaps. WARNING: This procedure is *IRREVERSIBLE* after certain time (delete delay), so do backup your blocks first. Flags: + --delete-blocks Whether to delete the original blocks after + rewriting blocks successfully. Available in non + dry-run mode only. --dry-run Prints the series changes instead of doing them. Defaults to true, for user to double check. (: Pass --no-dry-run to skip this. From 1071e72a9c44aa7fc59aeb37686abeb29c9c1bcc Mon Sep 17 00:00:00 2001 From: yeya24 Date: Wed, 30 Jun 2021 23:02:30 -0700 Subject: [PATCH 3/3] use stub counter Signed-off-by: yeya24 --- cmd/thanos/tools_bucket.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/thanos/tools_bucket.go b/cmd/thanos/tools_bucket.go index 3d20184dd6..cad437f2b9 100644 --- a/cmd/thanos/tools_bucket.go +++ b/cmd/thanos/tools_bucket.go @@ -888,6 +888,7 @@ func registerBucketRewrite(app extkingpin.AppClause, objStoreConfig *extflag.Pat g.Add(func() error { chunkPool := chunkenc.NewPool() changeLog := compactv2.NewChangeLog(ioutil.Discard) + stubCounter := promauto.With(nil).NewCounter(prometheus.CounterOpts{}) for _, id := range ids { // Delete series from block & modify. level.Info(logger).Log("msg", "downloading block", "source", id) @@ -974,7 +975,7 @@ func registerBucketRewrite(app extkingpin.AppClause, objStoreConfig *extflag.Pat level.Info(logger).Log("msg", "uploaded", "source", id, "new", newID) if !*dryRun && *deleteBlocks { - if err := block.MarkForDeletion(ctx, logger, bkt, id, "block rewritten", promauto.With(nil).NewCounter(prometheus.CounterOpts{})); err != nil { + if err := block.MarkForDeletion(ctx, logger, bkt, id, "block rewritten", stubCounter); err != nil { level.Error(logger).Log("msg", "failed to mark block for deletion", "id", id.String(), "err", err) } }