-
Notifications
You must be signed in to change notification settings - Fork 544
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
copyblocks: support copying between tenants #10110
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ import ( | |||||||
"os" | ||||||||
"os/signal" | ||||||||
"path/filepath" | ||||||||
"slices" | ||||||||
"strings" | ||||||||
"syscall" | ||||||||
"time" | ||||||||
|
@@ -24,6 +25,7 @@ import ( | |||||||
"github.com/go-kit/log/level" | ||||||||
"github.com/grafana/dskit/concurrency" | ||||||||
"github.com/grafana/dskit/flagext" | ||||||||
"github.com/grafana/dskit/tenant" | ||||||||
"github.com/oklog/ulid" | ||||||||
"github.com/pkg/errors" | ||||||||
"github.com/prometheus/client_golang/prometheus" | ||||||||
|
@@ -44,6 +46,7 @@ type config struct { | |||||||
copyPeriod time.Duration | ||||||||
enabledUsers flagext.StringSliceCSV | ||||||||
disabledUsers flagext.StringSliceCSV | ||||||||
crossTenantMapping flagext.StringSliceCSV | ||||||||
dryRun bool | ||||||||
skipNoCompactBlockDurationCheck bool | ||||||||
httpListen string | ||||||||
|
@@ -59,6 +62,7 @@ func (c *config) registerFlags(f *flag.FlagSet) { | |||||||
f.DurationVar(&c.copyPeriod, "copy-period", 0, "How often to repeat the copy. If set to 0, copy is done once, and the program stops. Otherwise, the program keeps running and copying blocks until it is terminated.") | ||||||||
f.Var(&c.enabledUsers, "enabled-users", "If not empty, only blocks for these users are copied.") | ||||||||
f.Var(&c.disabledUsers, "disabled-users", "If not empty, blocks for these users are not copied.") | ||||||||
f.Var(&c.crossTenantMapping, "cross-tenant-mapping", "A comma-separated list of (source tenant):(destination tenant). If a source tenant is not mapped then its destination tenant is assumed to be identical.") | ||||||||
f.BoolVar(&c.dryRun, "dry-run", false, "Don't perform any copy; only log what would happen.") | ||||||||
f.BoolVar(&c.skipNoCompactBlockDurationCheck, "skip-no-compact-block-duration-check", false, "If set, blocks marked as no-compact are not checked against min-block-duration") | ||||||||
f.StringVar(&c.httpListen, "http-listen-address", ":8080", "HTTP listen address.") | ||||||||
|
@@ -80,6 +84,23 @@ func (c *config) validate() error { | |||||||
return nil | ||||||||
} | ||||||||
|
||||||||
func (c *config) parseCrossTenantMapping() (map[string]string, error) { | ||||||||
m := make(map[string]string, len(c.crossTenantMapping)) | ||||||||
for _, mapping := range c.crossTenantMapping { | ||||||||
splitMapping := strings.Split(mapping, ":") | ||||||||
if len(splitMapping) != 2 || slices.Contains(splitMapping, "") { | ||||||||
return nil, fmt.Errorf("invalid tenant mapping: %s", mapping) | ||||||||
} | ||||||||
for _, id := range splitMapping { | ||||||||
if err := tenant.ValidTenantID(id); err != nil { | ||||||||
return nil, err | ||||||||
} | ||||||||
} | ||||||||
m[splitMapping[0]] = splitMapping[1] | ||||||||
} | ||||||||
return m, nil | ||||||||
} | ||||||||
|
||||||||
type metrics struct { | ||||||||
copyCyclesSucceeded prometheus.Counter | ||||||||
copyCyclesFailed prometheus.Counter | ||||||||
|
@@ -126,6 +147,12 @@ func main() { | |||||||
os.Exit(1) | ||||||||
} | ||||||||
|
||||||||
crossTenantMapping, err := cfg.parseCrossTenantMapping() | ||||||||
if err != nil { | ||||||||
fmt.Fprintln(os.Stderr, err.Error()) | ||||||||
os.Exit(1) | ||||||||
} | ||||||||
|
||||||||
logger := log.NewLogfmtLogger(os.Stdout) | ||||||||
logger = log.With(logger, "ts", log.DefaultTimestampUTC) | ||||||||
|
||||||||
|
@@ -144,7 +171,7 @@ func main() { | |||||||
} | ||||||||
}() | ||||||||
|
||||||||
success := runCopy(ctx, cfg, logger, m) | ||||||||
success := runCopy(ctx, cfg, crossTenantMapping, logger, m) | ||||||||
if cfg.copyPeriod <= 0 { | ||||||||
if success { | ||||||||
os.Exit(0) | ||||||||
|
@@ -158,14 +185,14 @@ func main() { | |||||||
for ctx.Err() == nil { | ||||||||
select { | ||||||||
case <-t.C: | ||||||||
_ = runCopy(ctx, cfg, logger, m) | ||||||||
_ = runCopy(ctx, cfg, crossTenantMapping, logger, m) | ||||||||
case <-ctx.Done(): | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func runCopy(ctx context.Context, cfg config, logger log.Logger, m *metrics) bool { | ||||||||
err := copyBlocks(ctx, cfg, logger, m) | ||||||||
func runCopy(ctx context.Context, cfg config, crossTenantMapping map[string]string, logger log.Logger, m *metrics) bool { | ||||||||
err := copyBlocks(ctx, cfg, crossTenantMapping, logger, m) | ||||||||
if err != nil { | ||||||||
m.copyCyclesFailed.Inc() | ||||||||
level.Error(logger).Log("msg", "failed to copy blocks", "err", err, "dryRun", cfg.dryRun) | ||||||||
|
@@ -177,7 +204,7 @@ func runCopy(ctx context.Context, cfg config, logger log.Logger, m *metrics) boo | |||||||
return true | ||||||||
} | ||||||||
|
||||||||
func copyBlocks(ctx context.Context, cfg config, logger log.Logger, m *metrics) error { | ||||||||
func copyBlocks(ctx context.Context, cfg config, crossTenantMapping map[string]string, logger log.Logger, m *metrics) error { | ||||||||
sourceBucket, destBucket, copyFunc, err := cfg.copyConfig.ToBuckets(ctx) | ||||||||
if err != nil { | ||||||||
return err | ||||||||
|
@@ -197,23 +224,28 @@ func copyBlocks(ctx context.Context, cfg config, logger log.Logger, m *metrics) | |||||||
disabledUsers[u] = struct{}{} | ||||||||
} | ||||||||
|
||||||||
return concurrency.ForEachUser(ctx, tenants, cfg.tenantConcurrency, func(ctx context.Context, tenantID string) error { | ||||||||
if !isAllowedUser(enabledUsers, disabledUsers, tenantID) { | ||||||||
return concurrency.ForEachUser(ctx, tenants, cfg.tenantConcurrency, func(ctx context.Context, sourceTenantID string) error { | ||||||||
if !isAllowedUser(enabledUsers, disabledUsers, sourceTenantID) { | ||||||||
return nil | ||||||||
} | ||||||||
|
||||||||
logger := log.With(logger, "tenantID", tenantID) | ||||||||
destinationTenantID, ok := crossTenantMapping[sourceTenantID] | ||||||||
if !ok { | ||||||||
destinationTenantID = sourceTenantID | ||||||||
} | ||||||||
|
||||||||
logger := log.With(logger, "sourceTenantID", sourceTenantID, "destinationTenantID", destinationTenantID) | ||||||||
|
||||||||
blocks, err := listBlocksForTenant(ctx, sourceBucket, tenantID) | ||||||||
blocks, err := listBlocksForTenant(ctx, sourceBucket, sourceTenantID) | ||||||||
if err != nil { | ||||||||
level.Error(logger).Log("msg", "failed to list blocks for tenant", "err", err) | ||||||||
return errors.Wrapf(err, "failed to list blocks for tenant %v", tenantID) | ||||||||
return errors.Wrapf(err, "failed to list blocks for tenant %v", sourceTenantID) | ||||||||
} | ||||||||
|
||||||||
markers, err := listBlockMarkersForTenant(ctx, sourceBucket, tenantID, destBucket.Name()) | ||||||||
markers, err := listBlockMarkersForTenant(ctx, sourceBucket, sourceTenantID, destBucket.Name()) | ||||||||
if err != nil { | ||||||||
level.Error(logger).Log("msg", "failed to list blocks markers for tenant", "err", err) | ||||||||
return errors.Wrapf(err, "failed to list block markers for tenant %v", tenantID) | ||||||||
return errors.Wrapf(err, "failed to list block markers for tenant %v", sourceTenantID) | ||||||||
} | ||||||||
|
||||||||
var blockIDs []string | ||||||||
|
@@ -243,7 +275,7 @@ func copyBlocks(ctx context.Context, cfg config, logger log.Logger, m *metrics) | |||||||
return nil | ||||||||
} | ||||||||
|
||||||||
blockMeta, err := loadMetaJSONFile(ctx, sourceBucket, tenantID, blockID) | ||||||||
blockMeta, err := loadMetaJSONFile(ctx, sourceBucket, sourceTenantID, blockID) | ||||||||
if err != nil { | ||||||||
level.Error(logger).Log("msg", "skipping block, failed to read meta.json file", "err", err) | ||||||||
return err | ||||||||
|
@@ -287,7 +319,7 @@ func copyBlocks(ctx context.Context, cfg config, logger log.Logger, m *metrics) | |||||||
|
||||||||
level.Info(logger).Log("msg", "copying block") | ||||||||
|
||||||||
err = copySingleBlock(ctx, tenantID, blockID, markers[blockID], sourceBucket, copyFunc) | ||||||||
err = copySingleBlock(ctx, sourceTenantID, destinationTenantID, blockID, markers[blockID], sourceBucket, copyFunc) | ||||||||
if err != nil { | ||||||||
m.blocksCopyFailed.Inc() | ||||||||
level.Error(logger).Log("msg", "failed to copy block", "err", err) | ||||||||
|
@@ -297,7 +329,7 @@ func copyBlocks(ctx context.Context, cfg config, logger log.Logger, m *metrics) | |||||||
m.blocksCopied.Inc() | ||||||||
level.Info(logger).Log("msg", "block copied successfully") | ||||||||
|
||||||||
err = uploadCopiedMarkerFile(ctx, sourceBucket, tenantID, blockID, destBucket.Name()) | ||||||||
err = uploadCopiedMarkerFile(ctx, sourceBucket, sourceTenantID, blockID, destBucket.Name()) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit) Maybe we can add a note about the behaviour around markers, just it was clear from the code
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reworded it a bit and added the comment:
|
||||||||
if err != nil { | ||||||||
level.Error(logger).Log("msg", "failed to upload copied-marker file for block", "block", blockID.String(), "err", err) | ||||||||
return err | ||||||||
|
@@ -324,13 +356,13 @@ func isAllowedUser(enabled map[string]struct{}, disabled map[string]struct{}, te | |||||||
} | ||||||||
|
||||||||
// This method copies files within single TSDB block to a destination bucket. | ||||||||
func copySingleBlock(ctx context.Context, tenantID string, blockID ulid.ULID, markers blockMarkers, srcBkt objtools.Bucket, copyFunc objtools.CopyFunc) error { | ||||||||
func copySingleBlock(ctx context.Context, sourceTenantID, destinationTenantID string, blockID ulid.ULID, markers blockMarkers, srcBkt objtools.Bucket, copyFunc objtools.CopyFunc) error { | ||||||||
result, err := srcBkt.List(ctx, objtools.ListOptions{ | ||||||||
Prefix: tenantID + objtools.Delim + blockID.String(), | ||||||||
Prefix: sourceTenantID + objtools.Delim + blockID.String(), | ||||||||
Recursive: true, | ||||||||
}) | ||||||||
if err != nil { | ||||||||
return errors.Wrapf(err, "copySingleBlock: failed to list block files for %v/%v", tenantID, blockID.String()) | ||||||||
return errors.Wrapf(err, "copySingleBlock: failed to list block files for %v/%v", sourceTenantID, blockID.String()) | ||||||||
} | ||||||||
paths := result.ToNames() | ||||||||
|
||||||||
|
@@ -346,11 +378,21 @@ func copySingleBlock(ctx context.Context, tenantID string, blockID ulid.ULID, ma | |||||||
|
||||||||
// Copy global markers too (skipping deletion mark because deleted blocks are not copied by this tool). | ||||||||
if markers.noCompact { | ||||||||
paths = append(paths, tenantID+objtools.Delim+block.NoCompactMarkFilepath(blockID)) | ||||||||
paths = append(paths, sourceTenantID+objtools.Delim+block.NoCompactMarkFilepath(blockID)) | ||||||||
} | ||||||||
|
||||||||
isCrossTenant := sourceTenantID != destinationTenantID | ||||||||
|
||||||||
for _, fullPath := range paths { | ||||||||
err := copyFunc(ctx, fullPath, objtools.CopyOptions{}) | ||||||||
options := objtools.CopyOptions{} | ||||||||
if isCrossTenant { | ||||||||
after, found := strings.CutPrefix(fullPath, sourceTenantID) | ||||||||
if !found { | ||||||||
return fmt.Errorf("unexpected object path that does not begin with sourceTenantID: path=%s, sourceTenantID=%s", fullPath, sourceTenantID) | ||||||||
} | ||||||||
options.DestinationObjectName = destinationTenantID + after | ||||||||
} | ||||||||
err := copyFunc(ctx, fullPath, options) | ||||||||
if err != nil { | ||||||||
return errors.Wrapf(err, "copySingleBlock: failed to copy %v", fullPath) | ||||||||
} | ||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) Maybe it's better to stick to "users" on the inputs level? The command has
--enabled-users
and--disabled-users
flags. So it feels confusing if one doesn't know that those are the same. Can we name the flag--user-mapping
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, thanks. I changed it to
--user-mapping
and updated the documentation to reflect that.