Skip to content

Commit 6fee816

Browse files
gouthamvepracucci
andauthored
Support 1w, 1y for retention and table period take 2 (#2252)
* Allow 1w in the period duration. Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com> * Add support for 1w, 1y to table manager retention Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com> * Update docs and changelog Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com> * Added model.Duration support to doc generator Signed-off-by: Marco Pracucci <marco@pracucci.com> * Fix infinite recursion in UnmarshalYAML. Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com> * Walk back integration config changes to support old versions Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com> * Final nits Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com> Co-authored-by: Marco Pracucci <marco@pracucci.com>
1 parent c105d22 commit 6fee816

File tree

12 files changed

+142
-23
lines changed

12 files changed

+142
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* `-flusher.concurrent-flushes` for number of concurrent flushes.
1111
* `-flusher.flush-op-timeout` is duration after which a flush should timeout.
1212
* [ENHANCEMENT] Experimental TSDB: Add support for local `filesystem` backend. #2245
13+
* [ENHANCEMENT] Allow 1w (where w denotes week) and 1y (where y denotes year) when setting table period and retention. #2252
1314

1415
## 0.7.0-rc.0 / 2020-03-09
1516

docs/configuration/config-file-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ To specify which configuration file to load, pass the `-config.file` flag at the
1818

1919
* `<boolean>`: a boolean that can take the values `true` or `false`
2020
* `<int>`: any integer matching the regular expression `[1-9]+[0-9]*`
21-
* `<duration>`: a duration matching the regular expression `[0-9]+(ns|us|µs|ms|[smh])`
21+
* `<duration>`: a duration matching the regular expression `[0-9]+(ns|us|µs|ms|s|m|h|d|w|y)` where y = 365 days.
2222
* `<string>`: a regular string
2323
* `<url>`: an URL
2424
* `<prefix>`: a CLI flag prefix based on the context (look at the parent configuration block to see which CLI flags prefix should be used)

docs/configuration/config-file-reference.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ To specify which configuration file to load, pass the `-config.file` flag at the
1818

1919
* `<boolean>`: a boolean that can take the values `true` or `false`
2020
* `<int>`: any integer matching the regular expression `[1-9]+[0-9]*`
21-
* `<duration>`: a duration matching the regular expression `[0-9]+(ns|us|µs|ms|[smh])`
21+
* `<duration>`: a duration matching the regular expression `[0-9]+(ns|us|µs|ms|s|m|h|d|w|y)` where y = 365 days.
2222
* `<string>`: a regular string
2323
* `<url>`: an URL
2424
* `<prefix>`: a CLI flag prefix based on the context (look at the parent configuration block to see which CLI flags prefix should be used)

docs/configuration/schema-config-reference.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Where `periodic_table_config` is
3333
```
3434
# The prefix to use for the tables.
3535
prefix: <string>
36-
# We typically run Cortex with new tables every week to keep the index size low and to make retention easier. This sets the period at which new tables are created and used. Typically 168h (1week).
36+
# We typically run Cortex with new tables every week to keep the index size low and to make retention easier. This sets the period at which new tables are created and used. Typically 1w (1week).
3737
period: <duration>
3838
# The tags that can be set on the dynamo table.
3939
tags: <map[string]string>
@@ -45,12 +45,12 @@ configs:
4545
- from: "2020-03-01" # Or typically a week before the Cortex cluster was created.
4646
schema: v9
4747
index:
48-
period: 168h
48+
period: 1w
4949
prefix: cortex_index_
5050
# Chunks section is optional and required only if you're not using a
5151
# separate object store.
5252
chunks:
53-
period: 168h
53+
period: 1w
5454
prefix: cortex_chunks
5555
store: aws-dynamo/bigtable-hashed/cassandra/boltdb
5656
object_store: <above options>/s3/gcs/azure/filesystem
@@ -66,21 +66,21 @@ configs:
6666
- from: "2018-08-23"
6767
schema: v9
6868
chunks:
69-
period: 168h0m0s
69+
period: 1w
7070
prefix: dev_chunks_
7171
index:
72-
period: 168h0m0s
72+
period: 1w
7373
prefix: dev_index_
7474
store: gcp-columnkey
7575

7676
# Starting 2018-02-13 we moved from BigTable to GCS for storing the chunks.
7777
- from: "2019-02-13"
7878
schema: v9
7979
chunks:
80-
period: 168h
80+
period: 1w
8181
prefix: dev_chunks_
8282
index:
83-
period: 168h
83+
period: 1w
8484
prefix: dev_index_
8585
object_store: gcs
8686
store: gcp-columnkey
@@ -90,10 +90,10 @@ configs:
9090
- from: "2019-02-24"
9191
schema: v9
9292
chunks:
93-
period: 168h
93+
period: 1w
9494
prefix: dev_chunks_
9595
index:
96-
period: 168h
96+
period: 1w
9797
prefix: dev_index_
9898
object_store: gcs
9999
store: bigtable-hashed
@@ -102,10 +102,10 @@ configs:
102102
- from: "2019-03-05"
103103
schema: v10
104104
chunks:
105-
period: 168h
105+
period: 1w
106106
prefix: dev_chunks_
107107
index:
108-
period: 168h
108+
period: 1w
109109
prefix: dev_index_
110110
object_store: gcs
111111
store: bigtable-hashed

docs/configuration/single-process-config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ schema:
6464
schema: v10
6565
index:
6666
prefix: index_
67-
period: 168h
67+
period: 1w
6868

6969
storage:
7070
boltdb:

docs/configuration/single-process-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ schema:
6161
schema: v10
6262
index:
6363
prefix: index_
64-
period: 168h
64+
period: 1w
6565

6666
storage:
6767
boltdb:

integration/configs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ const (
2727
schema: v9
2828
index:
2929
prefix: cortex_
30-
period: 168h0m0s
30+
period: 168h
3131
chunks:
3232
prefix: cortex_chunks_
33-
period: 168h0m0s
33+
period: 168h
3434
`
3535

3636
cortexAlertmanagerUserConfigYaml = `route:
@@ -109,7 +109,7 @@ storage:
109109
110110
table_manager:
111111
dynamodb_poll_interval: 1m
112-
retention_period: 168h
112+
retention_period: 168h
113113
114114
schema:
115115
{{.SchemaConfig}}

pkg/chunk/schema_config.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,42 @@ func (cfg *PeriodConfig) dailyBuckets(from, through model.Time, userID string) [
290290

291291
// PeriodicTableConfig is configuration for a set of time-sharded tables.
292292
type PeriodicTableConfig struct {
293-
Prefix string `yaml:"prefix"`
294-
Period time.Duration `yaml:"period,omitempty"`
295-
Tags Tags `yaml:"tags,omitempty"`
293+
Prefix string
294+
Period time.Duration
295+
Tags Tags
296+
}
297+
298+
// UnmarshalYAML implements the yaml.Unmarshaler interface.
299+
func (cfg *PeriodicTableConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
300+
g := struct {
301+
Prefix string `yaml:"prefix"`
302+
Period model.Duration `yaml:"period"`
303+
Tags Tags `yaml:"tags"`
304+
}{}
305+
if err := unmarshal(&g); err != nil {
306+
return err
307+
}
308+
309+
cfg.Prefix = g.Prefix
310+
cfg.Period = time.Duration(g.Period)
311+
cfg.Tags = g.Tags
312+
313+
return nil
314+
}
315+
316+
// MarshalYAML implements the yaml.Marshaler interface.
317+
func (cfg PeriodicTableConfig) MarshalYAML() (interface{}, error) {
318+
g := &struct {
319+
Prefix string `yaml:"prefix"`
320+
Period model.Duration `yaml:"period"`
321+
Tags Tags `yaml:"tags"`
322+
}{
323+
Prefix: cfg.Prefix,
324+
Period: model.Duration(cfg.Period),
325+
Tags: cfg.Tags,
326+
}
327+
328+
return g, nil
296329
}
297330

298331
// AutoScalingConfig for DynamoDB tables.

pkg/chunk/schema_config_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/prometheus/common/model"
88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
10+
yaml "gopkg.in/yaml.v2"
1011
)
1112

1213
func TestHourlyBuckets(t *testing.T) {
@@ -422,3 +423,30 @@ func MustParseDayTime(s string) DayTime {
422423
}
423424
return DayTime{model.TimeFromUnix(t.Unix())}
424425
}
426+
427+
func TestPeriodicTableConfigCustomUnmarshalling(t *testing.T) {
428+
yamlFile := `prefix: cortex_
429+
period: 1w
430+
tags:
431+
foo: bar
432+
`
433+
434+
cfg := PeriodicTableConfig{}
435+
err := yaml.Unmarshal([]byte(yamlFile), &cfg)
436+
require.NoError(t, err)
437+
438+
expectedCfg := PeriodicTableConfig{
439+
Prefix: "cortex_",
440+
Period: 7 * 24 * time.Hour,
441+
Tags: map[string]string{
442+
"foo": "bar",
443+
},
444+
}
445+
446+
require.Equal(t, expectedCfg, cfg)
447+
448+
yamlGenerated, err := yaml.Marshal(&cfg)
449+
require.NoError(t, err)
450+
451+
require.Equal(t, yamlFile, string(yamlGenerated))
452+
}

pkg/chunk/table_manager.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ type TableManagerConfig struct {
9090
RetentionDeletesEnabled bool `yaml:"retention_deletes_enabled"`
9191

9292
// How far back tables will be kept before they are deleted
93-
RetentionPeriod time.Duration `yaml:"retention_period"`
93+
RetentionPeriod time.Duration `yaml:"-"`
94+
// This is so that we can accept 1w, 1y in the YAML.
95+
RetentionPeriodModel model.Duration `yaml:"retention_period"`
9496

9597
// Period with which the table manager will poll for tables.
9698
DynamoDBPollInterval time.Duration `yaml:"dynamodb_poll_interval"`
@@ -102,6 +104,41 @@ type TableManagerConfig struct {
102104
ChunkTables ProvisionConfig `yaml:"chunk_tables_provisioning"`
103105
}
104106

107+
// UnmarshalYAML implements the yaml.Unmarshaler interface. To support RetentionPeriod.
108+
func (cfg *TableManagerConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
109+
110+
// If we call unmarshal on TableManagerConfig, it will call UnmarshalYAML leading to infinite recursion.
111+
// To make unmarshal fill the plain data struct rather than calling UnmarshalYAML
112+
// again, we have to hide it using a type indirection.
113+
type plain TableManagerConfig
114+
if err := unmarshal((*plain)(cfg)); err != nil {
115+
return err
116+
}
117+
118+
if cfg.RetentionPeriodModel > 0 {
119+
cfg.RetentionPeriod = time.Duration(cfg.RetentionPeriodModel)
120+
}
121+
122+
return nil
123+
}
124+
125+
// MarshalYAML implements the yaml.Marshaler interface. To support RetentionPeriod.
126+
func (cfg *TableManagerConfig) MarshalYAML() (interface{}, error) {
127+
cfg.RetentionPeriodModel = model.Duration(cfg.RetentionPeriod)
128+
return cfg, nil
129+
}
130+
131+
// Validate validates the config.
132+
func (cfg *TableManagerConfig) Validate() error {
133+
// We're setting this field because when using flags, you set the RetentionPeriodModel but not RetentionPeriod.
134+
// TODO(gouthamve): Its a hack, but I can't think of any other way :/
135+
if cfg.RetentionPeriodModel > 0 {
136+
cfg.RetentionPeriod = time.Duration(cfg.RetentionPeriodModel)
137+
}
138+
139+
return nil
140+
}
141+
105142
// ProvisionConfig holds config for provisioning capacity (on DynamoDB)
106143
type ProvisionConfig struct {
107144
ProvisionedThroughputOnDemandMode bool `yaml:"provisioned_throughput_on_demand_mode"`
@@ -123,7 +160,7 @@ type ProvisionConfig struct {
123160
func (cfg *TableManagerConfig) RegisterFlags(f *flag.FlagSet) {
124161
f.BoolVar(&cfg.ThroughputUpdatesDisabled, "table-manager.throughput-updates-disabled", false, "If true, disable all changes to DB capacity")
125162
f.BoolVar(&cfg.RetentionDeletesEnabled, "table-manager.retention-deletes-enabled", false, "If true, enables retention deletes of DB tables")
126-
f.DurationVar(&cfg.RetentionPeriod, "table-manager.retention-period", 0, "Tables older than this retention period are deleted. Note: This setting is destructive to data!(default: 0, which disables deletion)")
163+
f.Var(&cfg.RetentionPeriodModel, "table-manager.retention-period", "Tables older than this retention period are deleted. Note: This setting is destructive to data!(default: 0, which disables deletion)")
127164
f.DurationVar(&cfg.DynamoDBPollInterval, "dynamodb.poll-interval", 2*time.Minute, "How frequently to poll DynamoDB to learn our capacity.")
128165
f.DurationVar(&cfg.CreationGracePeriod, "dynamodb.periodic-table.grace-period", 10*time.Minute, "DynamoDB periodic tables grace period (duration which table will be created/deleted before/after it's needed).")
129166

0 commit comments

Comments
 (0)