Skip to content

Commit c88f1fa

Browse files
authored
Add min/max time filter to blockscopy and change min duration default to 0 (#261)
* Add min/max time filter to blockscopy and change min duration default to 0 Signed-off-by: Marco Pracucci <[email protected]> * Explain the time format for min/max time params Signed-off-by: Marco Pracucci <[email protected]> Signed-off-by: Marco Pracucci <[email protected]>
1 parent 57cffa0 commit c88f1fa

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

cmd/blockscopy/main.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type config struct {
3737
sourceBucket string
3838
destBucket string
3939
minBlockDuration time.Duration
40+
minTime flagext.Time
41+
maxTime flagext.Time
4042
tenantConcurrency int
4143
blocksConcurrency int
4244
copyPeriod time.Duration
@@ -50,7 +52,9 @@ type config struct {
5052
func (c *config) RegisterFlags(f *flag.FlagSet) {
5153
f.StringVar(&c.sourceBucket, "source-bucket", "", "Source GCS bucket with blocks.")
5254
f.StringVar(&c.destBucket, "destination-bucket", "", "Destination GCS bucket with blocks.")
53-
f.DurationVar(&c.minBlockDuration, "min-block-duration", 24*time.Hour, "If non-zero, ignore blocks that cover block range smaller than this.")
55+
f.DurationVar(&c.minBlockDuration, "min-block-duration", 0, "If non-zero, ignore blocks that cover block range smaller than this.")
56+
f.Var(&c.minTime, "min-time", fmt.Sprintf("If set, only blocks with MinTime >= this value are copied. The supported time format is %q.", time.RFC3339))
57+
f.Var(&c.maxTime, "max-time", fmt.Sprintf("If set, only blocks with MaxTime <= this value are copied. The supported time format is %q.", time.RFC3339))
5458
f.IntVar(&c.tenantConcurrency, "tenant-concurrency", 5, "How many tenants to process at once.")
5559
f.IntVar(&c.blocksConcurrency, "block-concurrency", 5, "How many blocks to copy at once per tenant.")
5660
f.DurationVar(&c.copyPeriod, "copy-period", 0, "How often to repeat the copy. If set to 0, copy is done once, and program stops. Otherwise program keeps running and copying blocks until terminated.")
@@ -207,29 +211,44 @@ func copyBlocks(ctx context.Context, cfg config, logger log.Logger, m *metrics)
207211

208212
logger := log.With(logger, "block", blockID)
209213

214+
// Skip if the block was already copied.
210215
if markers[blockID].copied {
211216
level.Debug(logger).Log("msg", "skipping block because it has been copied already")
212217
return nil
213218
}
214219

220+
blockMeta, err := loadMetaJSONFile(ctx, sourceBucket, tenantID, blockID)
221+
if err != nil {
222+
level.Error(logger).Log("msg", "skipping block, failed to read meta.json file", "err", err)
223+
return err
224+
}
225+
226+
// Add min/max time to each log entry. This is useful for debugging purposes.
227+
blockMinTime := time.Unix(0, blockMeta.MinTime*int64(time.Millisecond)).UTC()
228+
blockMaxTime := time.Unix(0, blockMeta.MaxTime*int64(time.Millisecond)).UTC()
229+
logger = log.With(logger, "block_min_time", blockMinTime, "block_max_time", blockMaxTime)
230+
215231
if markers[blockID].deletion {
216232
level.Debug(logger).Log("msg", "skipping block because it is marked for deletion")
217233
return nil
218234
}
219235

220-
if cfg.minBlockDuration > 0 {
221-
meta, err := loadMetaJSONFile(ctx, sourceBucket, tenantID, blockID)
222-
if err != nil {
223-
level.Error(logger).Log("msg", "skipping block, failed to read meta.json file", "err", err)
224-
return err
225-
}
236+
// If the min time filter is set, only blocks with MinTime >= the configured value are copied.
237+
if filterMinTime := time.Time(cfg.minTime); !filterMinTime.IsZero() && blockMinTime.Before(filterMinTime) {
238+
level.Debug(logger).Log("msg", "skipping block, block min time is lower than the configured min time filter", "configured_min_time", filterMinTime)
239+
return nil
240+
}
226241

227-
blockDuration := time.Millisecond * time.Duration(meta.MaxTime-meta.MinTime)
228-
if blockDuration < cfg.minBlockDuration {
229-
minTime := time.Unix(0, meta.MinTime*int64(time.Millisecond)).UTC()
230-
maxTime := time.Unix(0, meta.MaxTime*int64(time.Millisecond)).UTC()
242+
// If the max time filter is set, only blocks with MaxTime <= the configured value are copied.
243+
if filterMaxTime := time.Time(cfg.maxTime); !filterMaxTime.IsZero() && blockMaxTime.After(filterMaxTime) {
244+
level.Debug(logger).Log("msg", "skipping block, block max time is greater than the configured max time filter", "configured_max_time", filterMaxTime)
245+
return nil
246+
}
231247

232-
level.Debug(logger).Log("msg", "skipping block, block duration is smaller than minimum duration", "blockDuration", blockDuration, "minimumDuration", cfg.minBlockDuration, "min_time", minTime, "max_time", maxTime)
248+
if cfg.minBlockDuration > 0 {
249+
blockDuration := time.Millisecond * time.Duration(blockMeta.MaxTime-blockMeta.MinTime)
250+
if blockDuration < cfg.minBlockDuration {
251+
level.Debug(logger).Log("msg", "skipping block, block duration is smaller than minimum duration", "block_duration", blockDuration, "configured_min_duration", cfg.minBlockDuration)
233252
return nil
234253
}
235254
}

0 commit comments

Comments
 (0)