From e66e73a00d722a1d1444099d741d483e6a62e52d Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Fri, 18 Jan 2019 15:14:51 +0530 Subject: [PATCH 1/4] Add flag for size based retention Signed-off-by: Goutham Veeramachaneni --- cmd/prometheus/main.go | 11 +++++++---- docs/storage.md | 3 +++ storage/tsdb/tsdb.go | 8 ++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 75efeca80c7..93d54d6b222 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -41,10 +41,10 @@ import ( "github.com/prometheus/common/model" "github.com/prometheus/common/version" prom_runtime "github.com/prometheus/prometheus/pkg/runtime" - "gopkg.in/alecthomas/kingpin.v2" + kingpin "gopkg.in/alecthomas/kingpin.v2" "k8s.io/klog" - "github.com/mwitkow/go-conntrack" + conntrack "github.com/mwitkow/go-conntrack" "github.com/prometheus/common/promlog" promlogflag "github.com/prometheus/common/promlog/flag" "github.com/prometheus/prometheus/config" @@ -172,7 +172,10 @@ func main() { Hidden().PlaceHolder("").BytesVar(&cfg.tsdb.WALSegmentSize) a.Flag("storage.tsdb.retention", "How long to retain samples in storage."). - Default("15d").SetValue(&cfg.tsdb.Retention) + Default("15d").SetValue(&cfg.tsdb.RetentionDuration) + + a.Flag("storage.tsdb.retention.size", "[EXPERIMENTAL] Maximum number of bytes that can be stored for blocks. This flag is experimental and can be changed in future releases."). + Default("0").Int64Var(&cfg.tsdb.MaxBytes) a.Flag("storage.tsdb.no-lockfile", "Do not create lockfile in data directory."). Default("false").BoolVar(&cfg.tsdb.NoLockfile) @@ -245,7 +248,7 @@ func main() { cfg.web.RoutePrefix = "/" + strings.Trim(cfg.web.RoutePrefix, "/") if cfg.tsdb.MaxBlockDuration == 0 { - cfg.tsdb.MaxBlockDuration = cfg.tsdb.Retention / 10 + cfg.tsdb.MaxBlockDuration = cfg.tsdb.RetentionDuration / 10 } promql.LookbackDelta = time.Duration(cfg.lookbackDelta) diff --git a/docs/storage.md b/docs/storage.md index 21cac5e893d..b6c83199b64 100644 --- a/docs/storage.md +++ b/docs/storage.md @@ -53,6 +53,7 @@ Prometheus has several flags that allow configuring the local storage. The most * `--storage.tsdb.path`: This determines where Prometheus writes its database. Defaults to `data/`. * `--storage.tsdb.retention`: This determines when to remove old data. Defaults to `15d`. +* `--storage.tsdb.retention.size`: [EXPERIMENTAL] This determines the maximum number of bytes that storage blocks can use (note that this does not include the WAL size, which can be substantial). The oldest data will be removed first. Defaults to `0` or disabled. This flag is experimental and can be changed in future releases. On average, Prometheus uses only around 1-2 bytes per sample. Thus, to plan the capacity of a Prometheus server, you can use the rough formula: @@ -64,6 +65,8 @@ To tune the rate of ingested samples per second, you can either reduce the numbe If your local storage becomes corrupted for whatever reason, your best bet is to shut down Prometheus and remove the entire storage directory. However, you can also try removing individual block directories to resolve the problem. This means losing a time window of around two hours worth of data per block directory. Again, Prometheus's local storage is not meant as durable long-term storage. +If both time and size retention policies are specified, whichever policy triggers first will be used at that instant. + ## Remote storage integrations Prometheus's local storage is limited by single nodes in its scalability and durability. Instead of trying to solve clustered storage in Prometheus itself, Prometheus has a set of interfaces that allow integrating with remote storage systems. diff --git a/storage/tsdb/tsdb.go b/storage/tsdb/tsdb.go index f18036216b5..b8230e60e89 100644 --- a/storage/tsdb/tsdb.go +++ b/storage/tsdb/tsdb.go @@ -119,7 +119,10 @@ type Options struct { WALSegmentSize units.Base2Bytes // Duration for how long to retain data. - Retention model.Duration + RetentionDuration model.Duration + + // Maximum number of bytes to be retained. + MaxBytes int64 // Disable creation and consideration of lockfile. NoLockfile bool @@ -183,7 +186,8 @@ func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*t db, err := tsdb.Open(path, l, r, &tsdb.Options{ WALSegmentSize: int(opts.WALSegmentSize), - RetentionDuration: uint64(time.Duration(opts.Retention).Seconds() * 1000), + RetentionDuration: uint64(time.Duration(opts.RetentionDuration).Seconds() * 1000), + MaxBytes: opts.MaxBytes, BlockRanges: rngs, NoLockfile: opts.NoLockfile, }) From 0179b7be62293f65a0a055917fc32064820ff58f Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Fri, 18 Jan 2019 16:02:19 +0530 Subject: [PATCH 2/4] Deprecate the old retention flag for a new one. Signed-off-by: Goutham Veeramachaneni --- cmd/prometheus/main.go | 13 ++++++++++++- docs/storage.md | 3 ++- storage/tsdb/tsdb.go | 32 +++++++++++++++++++++++++++++++- storage/tsdb/tsdb_test.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 93d54d6b222..49ebb732bcb 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -171,7 +171,10 @@ func main() { "Size at which to split the tsdb WAL segment files (e.g. 100MB)"). Hidden().PlaceHolder("").BytesVar(&cfg.tsdb.WALSegmentSize) - a.Flag("storage.tsdb.retention", "How long to retain samples in storage."). + a.Flag("storage.tsdb.retention", "[DEPRECATED] How long to retain samples in storage. This flag has been deprecated, use \"storage.tsdb.retention.time\" instead"). + Default("15d").SetValue(&cfg.tsdb.Retention) + + a.Flag("storage.tsdb.retention.time", "How long to retain samples in storage."). Default("15d").SetValue(&cfg.tsdb.RetentionDuration) a.Flag("storage.tsdb.retention.size", "[EXPERIMENTAL] Maximum number of bytes that can be stored for blocks. This flag is experimental and can be changed in future releases."). @@ -256,6 +259,14 @@ func main() { logger := promlog.New(&cfg.promlogConfig) + defaultDuration, err := model.ParseDuration("15d") + if err != nil { + panic(err) + } + if cfg.tsdb.Retention != defaultDuration { + level.Warn(logger).Log("deprecation_notice", `"storage.tsdb.retention" flag is deprecated use "storage.tsdb.retention.time" instead.`) + } + // Above level 6, the k8s client would log bearer tokens in clear-text. klog.ClampLevel(6) klog.SetLogger(log.With(logger, "component", "k8s_client_runtime")) diff --git a/docs/storage.md b/docs/storage.md index b6c83199b64..9a9196bb529 100644 --- a/docs/storage.md +++ b/docs/storage.md @@ -52,8 +52,9 @@ For further details on file format, see [TSDB format](https://github.com/prometh Prometheus has several flags that allow configuring the local storage. The most important ones are: * `--storage.tsdb.path`: This determines where Prometheus writes its database. Defaults to `data/`. -* `--storage.tsdb.retention`: This determines when to remove old data. Defaults to `15d`. +* `--storage.tsdb.retention.time`: This determines when to remove old data. Defaults to `15d`. * `--storage.tsdb.retention.size`: [EXPERIMENTAL] This determines the maximum number of bytes that storage blocks can use (note that this does not include the WAL size, which can be substantial). The oldest data will be removed first. Defaults to `0` or disabled. This flag is experimental and can be changed in future releases. +* `--storage.tsdb.retention`: This flag has been deprecated in favour of `storage.tsdb.retention.time`. On average, Prometheus uses only around 1-2 bytes per sample. Thus, to plan the capacity of a Prometheus server, you can use the rough formula: diff --git a/storage/tsdb/tsdb.go b/storage/tsdb/tsdb.go index b8230e60e89..1ec58a20cfe 100644 --- a/storage/tsdb/tsdb.go +++ b/storage/tsdb/tsdb.go @@ -108,6 +108,9 @@ type adapter struct { // Options of the DB storage. type Options struct { + // The interval at which the write ahead log is flushed to disc. + WALFlushInterval time.Duration + // The timestamp range of head blocks after which they get persisted. // It's the minimum duration of any persisted block. MinBlockDuration model.Duration @@ -118,6 +121,9 @@ type Options struct { // The maximum size of each WAL segment file. WALSegmentSize units.Base2Bytes + // Deprecated, use RetentionDuration. + Retention model.Duration + // Duration for how long to retain data. RetentionDuration model.Duration @@ -170,6 +176,9 @@ func registerMetrics(db *tsdb.DB, r prometheus.Registerer) { // Open returns a new storage backed by a TSDB database that is configured for Prometheus. func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*tsdb.DB, error) { + + retention := ChooseRetention(opts.Retention, opts.RetentionDuration) + if opts.MinBlockDuration > opts.MaxBlockDuration { opts.MaxBlockDuration = opts.MinBlockDuration } @@ -186,7 +195,7 @@ func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*t db, err := tsdb.Open(path, l, r, &tsdb.Options{ WALSegmentSize: int(opts.WALSegmentSize), - RetentionDuration: uint64(time.Duration(opts.RetentionDuration).Seconds() * 1000), + RetentionDuration: uint64(time.Duration(retention).Seconds() * 1000), MaxBytes: opts.MaxBytes, BlockRanges: rngs, NoLockfile: opts.NoLockfile, @@ -199,6 +208,27 @@ func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*t return db, nil } +// ChooseRetention is some roundabout code to support both RetentionDuration and Retention (for different flags). +// If Retention is 15d, then it means that the default value is set and the value of RetentionDuration is used. +func ChooseRetention(oldFlagDuration, newFlagDuration model.Duration) model.Duration { + defaultDuration, err := model.ParseDuration("15d") + if err != nil { + panic(err) + } + + retention := oldFlagDuration + if retention == defaultDuration { + retention = newFlagDuration + } + + // Further if both the flags are set, then RetentionDuration takes precedence. + if newFlagDuration != defaultDuration { + retention = newFlagDuration + } + + return retention +} + // StartTime implements the Storage interface. func (a adapter) StartTime() (int64, error) { var startTime int64 diff --git a/storage/tsdb/tsdb_test.go b/storage/tsdb/tsdb_test.go index 85a8e43a901..8afaec4fa46 100644 --- a/storage/tsdb/tsdb_test.go +++ b/storage/tsdb/tsdb_test.go @@ -61,3 +61,34 @@ func TestMetrics(t *testing.T) { testutil.Equals(t, 0.003, metrics.Gauge.GetValue()) } + +func TestChooseRetention(t *testing.T) { + defaultRetention, err := model.ParseDuration("15d") + testutil.Ok(t, err) + + retention1, err := model.ParseDuration("20d") + testutil.Ok(t, err) + retention2, err := model.ParseDuration("30d") + testutil.Ok(t, err) + + cases := []struct { + oldFlagRetention model.Duration + newFlagRetention model.Duration + + chosen model.Duration + }{ + // Case 1: both are default (unset flags). + {defaultRetention, defaultRetention, defaultRetention}, + // Case 2: old flag is set and new flag is unset. + {retention1, defaultRetention, retention1}, + // Case 3: old flag is unset and new flag is set. + {defaultRetention, retention2, retention2}, + // Case 4: both flags are set. + {retention1, retention2, retention2}, + } + + for _, tc := range cases { + retention := tsdb.ChooseRetention(tc.oldFlagRetention, tc.newFlagRetention) + testutil.Equals(t, tc.chosen, retention) + } +} From c1b98287a3e44af54fdd0e6b62ef2aa5e560dd0d Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Fri, 18 Jan 2019 16:54:37 +0530 Subject: [PATCH 3/4] Add ability to take a suffix for size flag Signed-off-by: Goutham Veeramachaneni --- cmd/prometheus/main.go | 4 ++-- docs/storage.md | 2 +- storage/tsdb/tsdb.go | 7 ++----- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 49ebb732bcb..45224888239 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -177,8 +177,8 @@ func main() { a.Flag("storage.tsdb.retention.time", "How long to retain samples in storage."). Default("15d").SetValue(&cfg.tsdb.RetentionDuration) - a.Flag("storage.tsdb.retention.size", "[EXPERIMENTAL] Maximum number of bytes that can be stored for blocks. This flag is experimental and can be changed in future releases."). - Default("0").Int64Var(&cfg.tsdb.MaxBytes) + a.Flag("storage.tsdb.retention.size", "[EXPERIMENTAL] Maximum number of bytes that can be stored for blocks. Units supported: KB, MB, GB, TB, PB. This flag is experimental and can be changed in future releases."). + Default("0").BytesVar(&cfg.tsdb.MaxBytes) a.Flag("storage.tsdb.no-lockfile", "Do not create lockfile in data directory."). Default("false").BoolVar(&cfg.tsdb.NoLockfile) diff --git a/docs/storage.md b/docs/storage.md index 9a9196bb529..ea730b1f139 100644 --- a/docs/storage.md +++ b/docs/storage.md @@ -53,7 +53,7 @@ Prometheus has several flags that allow configuring the local storage. The most * `--storage.tsdb.path`: This determines where Prometheus writes its database. Defaults to `data/`. * `--storage.tsdb.retention.time`: This determines when to remove old data. Defaults to `15d`. -* `--storage.tsdb.retention.size`: [EXPERIMENTAL] This determines the maximum number of bytes that storage blocks can use (note that this does not include the WAL size, which can be substantial). The oldest data will be removed first. Defaults to `0` or disabled. This flag is experimental and can be changed in future releases. +* `--storage.tsdb.retention.size`: [EXPERIMENTAL] This determines the maximum number of bytes that storage blocks can use (note that this does not include the WAL size, which can be substantial). The oldest data will be removed first. Defaults to `0` or disabled. This flag is experimental and can be changed in future releases. Units supported: KB, MB, GB, PB. Ex: "512MB" * `--storage.tsdb.retention`: This flag has been deprecated in favour of `storage.tsdb.retention.time`. On average, Prometheus uses only around 1-2 bytes per sample. Thus, to plan the capacity of a Prometheus server, you can use the rough formula: diff --git a/storage/tsdb/tsdb.go b/storage/tsdb/tsdb.go index 1ec58a20cfe..b6c3021d2ec 100644 --- a/storage/tsdb/tsdb.go +++ b/storage/tsdb/tsdb.go @@ -108,9 +108,6 @@ type adapter struct { // Options of the DB storage. type Options struct { - // The interval at which the write ahead log is flushed to disc. - WALFlushInterval time.Duration - // The timestamp range of head blocks after which they get persisted. // It's the minimum duration of any persisted block. MinBlockDuration model.Duration @@ -128,7 +125,7 @@ type Options struct { RetentionDuration model.Duration // Maximum number of bytes to be retained. - MaxBytes int64 + MaxBytes units.Base2Bytes // Disable creation and consideration of lockfile. NoLockfile bool @@ -196,7 +193,7 @@ func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*t db, err := tsdb.Open(path, l, r, &tsdb.Options{ WALSegmentSize: int(opts.WALSegmentSize), RetentionDuration: uint64(time.Duration(retention).Seconds() * 1000), - MaxBytes: opts.MaxBytes, + MaxBytes: int64(opts.MaxBytes), BlockRanges: rngs, NoLockfile: opts.NoLockfile, }) From b875ecbd00fd1eb981a543ee1989ca01235161c7 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Fri, 18 Jan 2019 17:09:36 +0530 Subject: [PATCH 4/4] Address feedback Signed-off-by: Goutham Veeramachaneni --- cmd/prometheus/main.go | 48 +++++++++++++++++++++++++++++-------- cmd/prometheus/main_test.go | 29 ++++++++++++++++++++++ docs/storage.md | 2 +- storage/tsdb/tsdb.go | 29 +--------------------- storage/tsdb/tsdb_test.go | 31 ------------------------ 5 files changed, 69 insertions(+), 70 deletions(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 45224888239..25426b9a3fc 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -41,10 +41,10 @@ import ( "github.com/prometheus/common/model" "github.com/prometheus/common/version" prom_runtime "github.com/prometheus/prometheus/pkg/runtime" - kingpin "gopkg.in/alecthomas/kingpin.v2" + "gopkg.in/alecthomas/kingpin.v2" "k8s.io/klog" - conntrack "github.com/mwitkow/go-conntrack" + "github.com/mwitkow/go-conntrack" "github.com/prometheus/common/promlog" promlogflag "github.com/prometheus/common/promlog/flag" "github.com/prometheus/prometheus/config" @@ -71,10 +71,19 @@ var ( Name: "prometheus_config_last_reload_success_timestamp_seconds", Help: "Timestamp of the last successful configuration reload.", }) + + defaultRetentionString = "15d" + defaultRetentionDuration model.Duration ) func init() { prometheus.MustRegister(version.NewCollector("prometheus")) + + var err error + defaultRetentionDuration, err = model.ParseDuration(defaultRetentionString) + if err != nil { + panic(err) + } } func main() { @@ -83,6 +92,11 @@ func main() { runtime.SetMutexProfileFraction(20) } + var ( + oldFlagRetentionDuration model.Duration + newFlagRetentionDuration model.Duration + ) + cfg := struct { configFile string @@ -172,10 +186,10 @@ func main() { Hidden().PlaceHolder("").BytesVar(&cfg.tsdb.WALSegmentSize) a.Flag("storage.tsdb.retention", "[DEPRECATED] How long to retain samples in storage. This flag has been deprecated, use \"storage.tsdb.retention.time\" instead"). - Default("15d").SetValue(&cfg.tsdb.Retention) + Default(defaultRetentionString).SetValue(&oldFlagRetentionDuration) - a.Flag("storage.tsdb.retention.time", "How long to retain samples in storage."). - Default("15d").SetValue(&cfg.tsdb.RetentionDuration) + a.Flag("storage.tsdb.retention.time", "How long to retain samples in storage. Overrides \"storage.tsdb.retention\" if this flag is set to anything other than default."). + Default(defaultRetentionString).SetValue(&newFlagRetentionDuration) a.Flag("storage.tsdb.retention.size", "[EXPERIMENTAL] Maximum number of bytes that can be stored for blocks. Units supported: KB, MB, GB, TB, PB. This flag is experimental and can be changed in future releases."). Default("0").BytesVar(&cfg.tsdb.MaxBytes) @@ -250,6 +264,8 @@ func main() { // RoutePrefix must always be at least '/'. cfg.web.RoutePrefix = "/" + strings.Trim(cfg.web.RoutePrefix, "/") + cfg.tsdb.RetentionDuration = chooseRetention(oldFlagRetentionDuration, newFlagRetentionDuration) + if cfg.tsdb.MaxBlockDuration == 0 { cfg.tsdb.MaxBlockDuration = cfg.tsdb.RetentionDuration / 10 } @@ -259,11 +275,7 @@ func main() { logger := promlog.New(&cfg.promlogConfig) - defaultDuration, err := model.ParseDuration("15d") - if err != nil { - panic(err) - } - if cfg.tsdb.Retention != defaultDuration { + if oldFlagRetentionDuration != defaultRetentionDuration { level.Warn(logger).Log("deprecation_notice", `"storage.tsdb.retention" flag is deprecated use "storage.tsdb.retention.time" instead.`) } @@ -771,3 +783,19 @@ func sendAlerts(s sender, externalURL string) rules.NotifyFunc { } } } + +// chooseRetention is some roundabout code to support both RetentionDuration and Retention (for different flags). +// If Retention is 15d, then it means that the default value is set and the value of RetentionDuration is used. +func chooseRetention(oldFlagDuration, newFlagDuration model.Duration) model.Duration { + retention := oldFlagDuration + if retention == defaultRetentionDuration { + retention = newFlagDuration + } + + // Further newFlag takes precedence if it's set to anything other than default. + if newFlagDuration != defaultRetentionDuration { + retention = newFlagDuration + } + + return retention +} diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index e82e55bc32b..0671d759ec6 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -25,6 +25,7 @@ import ( "testing" "time" + "github.com/prometheus/common/model" "github.com/prometheus/prometheus/notifier" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/rules" @@ -284,3 +285,31 @@ func TestWALSegmentSizeBounds(t *testing.T) { } } } + +func TestChooseRetention(t *testing.T) { + retention1, err := model.ParseDuration("20d") + testutil.Ok(t, err) + retention2, err := model.ParseDuration("30d") + testutil.Ok(t, err) + + cases := []struct { + oldFlagRetention model.Duration + newFlagRetention model.Duration + + chosen model.Duration + }{ + // Both are default (unset flags). + {defaultRetentionDuration, defaultRetentionDuration, defaultRetentionDuration}, + // Old flag is set and new flag is unset. + {retention1, defaultRetentionDuration, retention1}, + // Old flag is unset and new flag is set. + {defaultRetentionDuration, retention2, retention2}, + // Both flags are set. + {retention1, retention2, retention2}, + } + + for _, tc := range cases { + retention := chooseRetention(tc.oldFlagRetention, tc.newFlagRetention) + testutil.Equals(t, tc.chosen, retention) + } +} diff --git a/docs/storage.md b/docs/storage.md index ea730b1f139..9f6b0e9134b 100644 --- a/docs/storage.md +++ b/docs/storage.md @@ -52,7 +52,7 @@ For further details on file format, see [TSDB format](https://github.com/prometh Prometheus has several flags that allow configuring the local storage. The most important ones are: * `--storage.tsdb.path`: This determines where Prometheus writes its database. Defaults to `data/`. -* `--storage.tsdb.retention.time`: This determines when to remove old data. Defaults to `15d`. +* `--storage.tsdb.retention.time`: This determines when to remove old data. Defaults to `15d`. Overrides `storage.tsdb.retention` if this flag is set to anything other than default. * `--storage.tsdb.retention.size`: [EXPERIMENTAL] This determines the maximum number of bytes that storage blocks can use (note that this does not include the WAL size, which can be substantial). The oldest data will be removed first. Defaults to `0` or disabled. This flag is experimental and can be changed in future releases. Units supported: KB, MB, GB, PB. Ex: "512MB" * `--storage.tsdb.retention`: This flag has been deprecated in favour of `storage.tsdb.retention.time`. diff --git a/storage/tsdb/tsdb.go b/storage/tsdb/tsdb.go index b6c3021d2ec..4b45dd47bcd 100644 --- a/storage/tsdb/tsdb.go +++ b/storage/tsdb/tsdb.go @@ -118,9 +118,6 @@ type Options struct { // The maximum size of each WAL segment file. WALSegmentSize units.Base2Bytes - // Deprecated, use RetentionDuration. - Retention model.Duration - // Duration for how long to retain data. RetentionDuration model.Duration @@ -173,9 +170,6 @@ func registerMetrics(db *tsdb.DB, r prometheus.Registerer) { // Open returns a new storage backed by a TSDB database that is configured for Prometheus. func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*tsdb.DB, error) { - - retention := ChooseRetention(opts.Retention, opts.RetentionDuration) - if opts.MinBlockDuration > opts.MaxBlockDuration { opts.MaxBlockDuration = opts.MinBlockDuration } @@ -192,7 +186,7 @@ func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*t db, err := tsdb.Open(path, l, r, &tsdb.Options{ WALSegmentSize: int(opts.WALSegmentSize), - RetentionDuration: uint64(time.Duration(retention).Seconds() * 1000), + RetentionDuration: uint64(time.Duration(opts.RetentionDuration).Seconds() * 1000), MaxBytes: int64(opts.MaxBytes), BlockRanges: rngs, NoLockfile: opts.NoLockfile, @@ -205,27 +199,6 @@ func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*t return db, nil } -// ChooseRetention is some roundabout code to support both RetentionDuration and Retention (for different flags). -// If Retention is 15d, then it means that the default value is set and the value of RetentionDuration is used. -func ChooseRetention(oldFlagDuration, newFlagDuration model.Duration) model.Duration { - defaultDuration, err := model.ParseDuration("15d") - if err != nil { - panic(err) - } - - retention := oldFlagDuration - if retention == defaultDuration { - retention = newFlagDuration - } - - // Further if both the flags are set, then RetentionDuration takes precedence. - if newFlagDuration != defaultDuration { - retention = newFlagDuration - } - - return retention -} - // StartTime implements the Storage interface. func (a adapter) StartTime() (int64, error) { var startTime int64 diff --git a/storage/tsdb/tsdb_test.go b/storage/tsdb/tsdb_test.go index 8afaec4fa46..85a8e43a901 100644 --- a/storage/tsdb/tsdb_test.go +++ b/storage/tsdb/tsdb_test.go @@ -61,34 +61,3 @@ func TestMetrics(t *testing.T) { testutil.Equals(t, 0.003, metrics.Gauge.GetValue()) } - -func TestChooseRetention(t *testing.T) { - defaultRetention, err := model.ParseDuration("15d") - testutil.Ok(t, err) - - retention1, err := model.ParseDuration("20d") - testutil.Ok(t, err) - retention2, err := model.ParseDuration("30d") - testutil.Ok(t, err) - - cases := []struct { - oldFlagRetention model.Duration - newFlagRetention model.Duration - - chosen model.Duration - }{ - // Case 1: both are default (unset flags). - {defaultRetention, defaultRetention, defaultRetention}, - // Case 2: old flag is set and new flag is unset. - {retention1, defaultRetention, retention1}, - // Case 3: old flag is unset and new flag is set. - {defaultRetention, retention2, retention2}, - // Case 4: both flags are set. - {retention1, retention2, retention2}, - } - - for _, tc := range cases { - retention := tsdb.ChooseRetention(tc.oldFlagRetention, tc.newFlagRetention) - testutil.Equals(t, tc.chosen, retention) - } -}