lib/flagutil: rename Duration to RetentionDuration (#7284)

The purpose of this change is to reduce confusion between using
`flag.Duration` and `flagutils.Duration`. The reason is that
`flagutils.Duration` was mistakenly used for cases that required `m`
support. See
ab0d31a7b0

The change in name should clearly indicate the purpose of this data
type.

### Describe Your Changes

Please provide a brief description of the changes you made. Be as
specific as possible to help others understand the purpose and impact of
your modifications.

### Checklist

The following checks are **mandatory**:

- [ ] My change adheres [VictoriaMetrics contributing
guidelines](https://docs.victoriametrics.com/contributing/).

Signed-off-by: hagen1778 <roman@victoriametrics.com>
This commit is contained in:
Roman Khavronenko 2024-10-17 13:47:48 +02:00 committed by GitHub
parent f0d1db81dc
commit 05ac508fbf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 20 additions and 18 deletions

View file

@ -18,12 +18,12 @@ import (
) )
var ( var (
retentionPeriod = flagutil.NewDuration("retentionPeriod", "7d", "Log entries with timestamps older than now-retentionPeriod are automatically deleted; "+ retentionPeriod = flagutil.NewRetentionDuration("retentionPeriod", "7d", "Log entries with timestamps older than now-retentionPeriod are automatically deleted; "+
"log entries with timestamps outside the retention are also rejected during data ingestion; the minimum supported retention is 1d (one day); "+ "log entries with timestamps outside the retention are also rejected during data ingestion; the minimum supported retention is 1d (one day); "+
"see https://docs.victoriametrics.com/victorialogs/#retention ; see also -retention.maxDiskSpaceUsageBytes") "see https://docs.victoriametrics.com/victorialogs/#retention ; see also -retention.maxDiskSpaceUsageBytes")
maxDiskSpaceUsageBytes = flagutil.NewBytes("retention.maxDiskSpaceUsageBytes", 0, "The maximum disk space usage at -storageDataPath before older per-day "+ maxDiskSpaceUsageBytes = flagutil.NewBytes("retention.maxDiskSpaceUsageBytes", 0, "The maximum disk space usage at -storageDataPath before older per-day "+
"partitions are automatically dropped; see https://docs.victoriametrics.com/victorialogs/#retention-by-disk-space-usage ; see also -retentionPeriod") "partitions are automatically dropped; see https://docs.victoriametrics.com/victorialogs/#retention-by-disk-space-usage ; see also -retentionPeriod")
futureRetention = flagutil.NewDuration("futureRetention", "2d", "Log entries with timestamps bigger than now+futureRetention are rejected during data ingestion; "+ futureRetention = flagutil.NewRetentionDuration("futureRetention", "2d", "Log entries with timestamps bigger than now+futureRetention are rejected during data ingestion; "+
"see https://docs.victoriametrics.com/victorialogs/#retention") "see https://docs.victoriametrics.com/victorialogs/#retention")
storageDataPath = flag.String("storageDataPath", "victoria-logs-data", "Path to directory where to store VictoriaLogs data; "+ storageDataPath = flag.String("storageDataPath", "victoria-logs-data", "Path to directory where to store VictoriaLogs data; "+
"see https://docs.victoriametrics.com/victorialogs/#storage") "see https://docs.victoriametrics.com/victorialogs/#storage")

View file

@ -44,7 +44,7 @@ var (
"See also -search.logSlowQueryDuration and -search.maxMemoryPerQuery") "See also -search.logSlowQueryDuration and -search.maxMemoryPerQuery")
noStaleMarkers = flag.Bool("search.noStaleMarkers", false, "Set this flag to true if the database doesn't contain Prometheus stale markers, "+ noStaleMarkers = flag.Bool("search.noStaleMarkers", false, "Set this flag to true if the database doesn't contain Prometheus stale markers, "+
"so there is no need in spending additional CPU time on its handling. Staleness markers may exist only in data obtained from Prometheus scrape targets") "so there is no need in spending additional CPU time on its handling. Staleness markers may exist only in data obtained from Prometheus scrape targets")
minWindowForInstantRollupOptimization = flagutil.NewDuration("search.minWindowForInstantRollupOptimization", "3h", "Enable cache-based optimization for repeated queries "+ minWindowForInstantRollupOptimization = flag.Duration("search.minWindowForInstantRollupOptimization", time.Hour*3, "Enable cache-based optimization for repeated queries "+
"to /api/v1/query (aka instant queries), which contain rollup functions with lookbehind window exceeding the given value") "to /api/v1/query (aka instant queries), which contain rollup functions with lookbehind window exceeding the given value")
) )

View file

@ -27,11 +27,11 @@ import (
) )
var ( var (
retentionPeriod = flagutil.NewDuration("retentionPeriod", "1", "Data with timestamps outside the retentionPeriod is automatically deleted. The minimum retentionPeriod is 24h or 1d. See also -retentionFilter") retentionPeriod = flagutil.NewRetentionDuration("retentionPeriod", "1", "Data with timestamps outside the retentionPeriod is automatically deleted. The minimum retentionPeriod is 24h or 1d. See also -retentionFilter")
snapshotAuthKey = flagutil.NewPassword("snapshotAuthKey", "authKey, which must be passed in query string to /snapshot* pages. It overrides -httpAuth.*") snapshotAuthKey = flagutil.NewPassword("snapshotAuthKey", "authKey, which must be passed in query string to /snapshot* pages. It overrides -httpAuth.*")
forceMergeAuthKey = flagutil.NewPassword("forceMergeAuthKey", "authKey, which must be passed in query string to /internal/force_merge pages. It overrides -httpAuth.*") forceMergeAuthKey = flagutil.NewPassword("forceMergeAuthKey", "authKey, which must be passed in query string to /internal/force_merge pages. It overrides -httpAuth.*")
forceFlushAuthKey = flagutil.NewPassword("forceFlushAuthKey", "authKey, which must be passed in query string to /internal/force_flush pages. It overrides -httpAuth.*") forceFlushAuthKey = flagutil.NewPassword("forceFlushAuthKey", "authKey, which must be passed in query string to /internal/force_flush pages. It overrides -httpAuth.*")
snapshotsMaxAge = flagutil.NewDuration("snapshotsMaxAge", "0", "Automatically delete snapshots older than -snapshotsMaxAge if it is set to non-zero duration. Make sure that backup process has enough time to finish the backup before the corresponding snapshot is automatically deleted") snapshotsMaxAge = flagutil.NewRetentionDuration("snapshotsMaxAge", "0", "Automatically delete snapshots older than -snapshotsMaxAge if it is set to non-zero duration. Make sure that backup process has enough time to finish the backup before the corresponding snapshot is automatically deleted")
_ = flag.Duration("snapshotCreateTimeout", 0, "Deprecated: this flag does nothing") _ = flag.Duration("snapshotCreateTimeout", 0, "Deprecated: this flag does nothing")
precisionBits = flag.Int("precisionBits", 64, "The number of precision bits to store per each value. Lower precision bits improves data compression at the cost of precision loss") precisionBits = flag.Int("precisionBits", 64, "The number of precision bits to store per each value. Lower precision bits improves data compression at the cost of precision loss")

View file

@ -10,13 +10,13 @@ import (
"github.com/VictoriaMetrics/metricsql" "github.com/VictoriaMetrics/metricsql"
) )
// NewDuration returns new `duration` flag with the given name, defaultValue and description. // NewRetentionDuration returns new `duration` flag with the given name, defaultValue and description.
// //
// DefaultValue is in months. // DefaultValue is in months.
func NewDuration(name string, defaultValue string, description string) *Duration { func NewRetentionDuration(name string, defaultValue string, description string) *RetentionDuration {
description += "\nThe following optional suffixes are supported: s (second), h (hour), d (day), w (week), y (year). " + description += "\nThe following optional suffixes are supported: s (second), h (hour), d (day), w (week), y (year). " +
"If suffix isn't set, then the duration is counted in months" "If suffix isn't set, then the duration is counted in months"
d := &Duration{} d := &RetentionDuration{}
if err := d.Set(defaultValue); err != nil { if err := d.Set(defaultValue); err != nil {
panic(fmt.Sprintf("BUG: can not parse default value %s for flag %s", defaultValue, name)) panic(fmt.Sprintf("BUG: can not parse default value %s for flag %s", defaultValue, name))
} }
@ -24,8 +24,8 @@ func NewDuration(name string, defaultValue string, description string) *Duration
return d return d
} }
// Duration is a flag for holding duration. // RetentionDuration is a flag for holding duration for retention period.
type Duration struct { type RetentionDuration struct {
// msecs contains parsed duration in milliseconds. // msecs contains parsed duration in milliseconds.
msecs int64 msecs int64
@ -33,22 +33,24 @@ type Duration struct {
} }
// Duration returns d as time.Duration // Duration returns d as time.Duration
func (d *Duration) Duration() time.Duration { func (d *RetentionDuration) Duration() time.Duration {
return time.Millisecond * time.Duration(d.msecs) return time.Millisecond * time.Duration(d.msecs)
} }
// Milliseconds returns d in milliseconds // Milliseconds returns d in milliseconds
func (d *Duration) Milliseconds() int64 { func (d *RetentionDuration) Milliseconds() int64 {
return d.msecs return d.msecs
} }
// String implements flag.Value interface // String implements flag.Value interface
func (d *Duration) String() string { func (d *RetentionDuration) String() string {
return d.valueString return d.valueString
} }
// Set implements flag.Value interface // Set implements flag.Value interface
func (d *Duration) Set(value string) error { // It assumes that value without unit should be parsed as `month` duration.
// It returns an error iv value has `m` unit.
func (d *RetentionDuration) Set(value string) error {
if value == "" { if value == "" {
d.msecs = 0 d.msecs = 0
d.valueString = "" d.valueString = ""

View file

@ -9,7 +9,7 @@ import (
func TestDurationSetFailure(t *testing.T) { func TestDurationSetFailure(t *testing.T) {
f := func(value string) { f := func(value string) {
t.Helper() t.Helper()
var d Duration var d RetentionDuration
if err := d.Set(value); err == nil { if err := d.Set(value); err == nil {
t.Fatalf("expecting non-nil error in d.Set(%q)", value) t.Fatalf("expecting non-nil error in d.Set(%q)", value)
} }
@ -31,14 +31,14 @@ func TestDurationSetFailure(t *testing.T) {
f("-1") f("-1")
f("-34h") f("-34h")
// Duration in minutes is confused with duration in months // RetentionDuration in minutes is confused with duration in months
f("1m") f("1m")
} }
func TestDurationSetSuccess(t *testing.T) { func TestDurationSetSuccess(t *testing.T) {
f := func(value string, expectedMsecs int64) { f := func(value string, expectedMsecs int64) {
t.Helper() t.Helper()
var d Duration var d RetentionDuration
if err := d.Set(value); err != nil { if err := d.Set(value); err != nil {
t.Fatalf("unexpected error in d.Set(%q): %s", value, err) t.Fatalf("unexpected error in d.Set(%q): %s", value, err)
} }
@ -66,7 +66,7 @@ func TestDurationSetSuccess(t *testing.T) {
func TestDurationDuration(t *testing.T) { func TestDurationDuration(t *testing.T) {
f := func(value string, expected time.Duration) { f := func(value string, expected time.Duration) {
t.Helper() t.Helper()
var d Duration var d RetentionDuration
if err := d.Set(value); err != nil { if err := d.Set(value); err != nil {
t.Fatalf("unexpected error in d.Set(%q): %s", value, err) t.Fatalf("unexpected error in d.Set(%q): %s", value, err)
} }