Skip to content

Commit dc073d5

Browse files
committed
Add a flag to enable TSDB memorry snapshot on shutdown
Signed-off-by: wangguoliang <[email protected]>
1 parent 9db30fb commit dc073d5

File tree

7 files changed

+49
-14
lines changed

7 files changed

+49
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [FEATURE] Ingester: Add active series to all_user_stats page. #4972
99
* [FEATURE] Ingester: Added `-blocks-storage.tsdb.head-chunks-write-queue-size` allowing to configure the size of the in-memory queue used before flushing chunks to the disk . #5000
1010
* [FEATURE] Query Frontend: Log query params in query frontend even if error happens. #5005
11+
* [FEATURE] Ingester: Enable snapshotting of In-memory TSDB on disk during shutdown via `-blocks-storage.tsdb.memory-snapshot-on-shutdown`. #5011
1112
* [BUGFIX] Updated `golang.org/x/net` dependency to fix CVE-2022-27664. #5008
1213

1314
## 1.14.0 in progress

docs/blocks-storage/querier.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,4 +882,9 @@ blocks_storage:
882882
# will be stored. 0 or less means disabled.
883883
# CLI flag: -blocks-storage.tsdb.max-exemplars
884884
[max_exemplars: <int> | default = 0]
885+
886+
# True to enable snapshotting of in-memory TSDB data on disk when shutting
887+
# down.
888+
# CLI flag: -blocks-storage.tsdb.memory-snapshot-on-shutdown
889+
[memory_snapshot_on_shutdown: <boolean> | default = false]
885890
```

docs/blocks-storage/store-gateway.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,4 +946,9 @@ blocks_storage:
946946
# will be stored. 0 or less means disabled.
947947
# CLI flag: -blocks-storage.tsdb.max-exemplars
948948
[max_exemplars: <int> | default = 0]
949+
950+
# True to enable snapshotting of in-memory TSDB data on disk when shutting
951+
# down.
952+
# CLI flag: -blocks-storage.tsdb.memory-snapshot-on-shutdown
953+
[memory_snapshot_on_shutdown: <boolean> | default = false]
949954
```

docs/configuration/config-file-reference.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,6 +2669,11 @@ The `limits_config` configures default and per-tenant limits imposed by Cortex s
26692669
# CLI flag: -ingester.max-global-metadata-per-metric
26702670
[max_global_metadata_per_metric: <int> | default = 0]
26712671
2672+
# True to enable snapshotting in-memory TSDB data of per user on disk when
2673+
# shutting down.
2674+
# CLI flag: -blocks-storage.tsdb.memory-snapshot-on-shutdown-per-user
2675+
[memory_snapshot_on_shutdown_per_user: <boolean> | default = false]
2676+
26722677
# Maximum number of chunks that can be fetched in a single query from ingesters
26732678
# and long-term storage. This limit is enforced in the querier, ruler and
26742679
# store-gateway. 0 to disable.
@@ -3725,6 +3730,11 @@ tsdb:
37253730
# be stored. 0 or less means disabled.
37263731
# CLI flag: -blocks-storage.tsdb.max-exemplars
37273732
[max_exemplars: <int> | default = 0]
3733+
3734+
# True to enable snapshotting of in-memory TSDB data on disk when shutting
3735+
# down.
3736+
# CLI flag: -blocks-storage.tsdb.memory-snapshot-on-shutdown
3737+
[memory_snapshot_on_shutdown: <boolean> | default = false]
37283738
```
37293739

37303740
### `compactor_config`

pkg/ingester/ingester.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,20 +1833,21 @@ func (i *Ingester) createTSDB(userID string) (*userTSDB, error) {
18331833
}
18341834
// Create a new user database
18351835
db, err := tsdb.Open(udir, userLogger, tsdbPromReg, &tsdb.Options{
1836-
RetentionDuration: i.cfg.BlocksStorageConfig.TSDB.Retention.Milliseconds(),
1837-
MinBlockDuration: blockRanges[0],
1838-
MaxBlockDuration: blockRanges[len(blockRanges)-1],
1839-
NoLockfile: true,
1840-
StripeSize: i.cfg.BlocksStorageConfig.TSDB.StripeSize,
1841-
HeadChunksWriteBufferSize: i.cfg.BlocksStorageConfig.TSDB.HeadChunksWriteBufferSize,
1842-
WALCompression: i.cfg.BlocksStorageConfig.TSDB.WALCompressionEnabled,
1843-
WALSegmentSize: i.cfg.BlocksStorageConfig.TSDB.WALSegmentSizeBytes,
1844-
SeriesLifecycleCallback: userDB,
1845-
BlocksToDelete: userDB.blocksToDelete,
1846-
EnableExemplarStorage: enableExemplars,
1847-
IsolationDisabled: true,
1848-
MaxExemplars: int64(i.cfg.BlocksStorageConfig.TSDB.MaxExemplars),
1849-
HeadChunksWriteQueueSize: i.cfg.BlocksStorageConfig.TSDB.HeadChunksWriteQueueSize,
1836+
RetentionDuration: i.cfg.BlocksStorageConfig.TSDB.Retention.Milliseconds(),
1837+
MinBlockDuration: blockRanges[0],
1838+
MaxBlockDuration: blockRanges[len(blockRanges)-1],
1839+
NoLockfile: true,
1840+
StripeSize: i.cfg.BlocksStorageConfig.TSDB.StripeSize,
1841+
HeadChunksWriteBufferSize: i.cfg.BlocksStorageConfig.TSDB.HeadChunksWriteBufferSize,
1842+
WALCompression: i.cfg.BlocksStorageConfig.TSDB.WALCompressionEnabled,
1843+
WALSegmentSize: i.cfg.BlocksStorageConfig.TSDB.WALSegmentSizeBytes,
1844+
SeriesLifecycleCallback: userDB,
1845+
BlocksToDelete: userDB.blocksToDelete,
1846+
EnableExemplarStorage: enableExemplars,
1847+
IsolationDisabled: true,
1848+
MaxExemplars: int64(i.cfg.BlocksStorageConfig.TSDB.MaxExemplars),
1849+
HeadChunksWriteQueueSize: i.cfg.BlocksStorageConfig.TSDB.HeadChunksWriteQueueSize,
1850+
EnableMemorySnapshotOnShutdown: i.cfg.BlocksStorageConfig.TSDB.MemorySnapshotOnShutdown,
18501851
}, nil)
18511852
if err != nil {
18521853
return nil, errors.Wrapf(err, "failed to open TSDB: %s", udir)

pkg/storage/tsdb/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ type TSDBConfig struct {
151151

152152
// Positive value enables experiemental support for exemplars. 0 or less to disable.
153153
MaxExemplars int `yaml:"max_exemplars"`
154+
155+
// Enable snapshotting of in-memory TSDB data on disk when shutting down.
156+
MemorySnapshotOnShutdown bool `yaml:"memory_snapshot_on_shutdown"`
154157
}
155158

156159
// RegisterFlags registers the TSDBConfig flags.
@@ -176,6 +179,7 @@ func (cfg *TSDBConfig) RegisterFlags(f *flag.FlagSet) {
176179
f.DurationVar(&cfg.CloseIdleTSDBTimeout, "blocks-storage.tsdb.close-idle-tsdb-timeout", 0, "If TSDB has not received any data for this duration, and all blocks from TSDB have been shipped, TSDB is closed and deleted from local disk. If set to positive value, this value should be equal or higher than -querier.query-ingesters-within flag to make sure that TSDB is not closed prematurely, which could cause partial query results. 0 or negative value disables closing of idle TSDB.")
177180
f.IntVar(&cfg.MaxExemplars, "blocks-storage.tsdb.max-exemplars", 0, "Enables support for exemplars in TSDB and sets the maximum number that will be stored. 0 or less means disabled.")
178181
f.IntVar(&cfg.HeadChunksWriteQueueSize, "blocks-storage.tsdb.head-chunks-write-queue-size", chunks.DefaultWriteQueueSize, "The size of the in-memory queue used before flushing chunks to the disk.")
182+
f.BoolVar(&cfg.MemorySnapshotOnShutdown, "blocks-storage.tsdb.memory-snapshot-on-shutdown", false, "True to enable snapshotting of in-memory TSDB data on disk when shutting down.")
179183
}
180184

181185
// Validate the config.

pkg/util/validation/limits.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ type Limits struct {
6868
MaxLocalMetadataPerMetric int `yaml:"max_metadata_per_metric" json:"max_metadata_per_metric"`
6969
MaxGlobalMetricsWithMetadataPerUser int `yaml:"max_global_metadata_per_user" json:"max_global_metadata_per_user"`
7070
MaxGlobalMetadataPerMetric int `yaml:"max_global_metadata_per_metric" json:"max_global_metadata_per_metric"`
71+
// Snapshot
72+
MemorySnapshotOnShutdownPerUser bool `yaml:"memory_snapshot_on_shutdown_per_user" json:"memory_snapshot_on_shutdown_per_user"`
7173

7274
// Querier enforced limits.
7375
MaxChunksPerQuery int `yaml:"max_fetched_chunks_per_query" json:"max_fetched_chunks_per_query"`
@@ -147,6 +149,7 @@ func (l *Limits) RegisterFlags(f *flag.FlagSet) {
147149
f.IntVar(&l.MaxLocalSeriesPerMetric, "ingester.max-series-per-metric", 50000, "The maximum number of active series per metric name, per ingester. 0 to disable.")
148150
f.IntVar(&l.MaxGlobalSeriesPerUser, "ingester.max-global-series-per-user", 0, "The maximum number of active series per user, across the cluster before replication. 0 to disable. Supported only if -distributor.shard-by-all-labels is true.")
149151
f.IntVar(&l.MaxGlobalSeriesPerMetric, "ingester.max-global-series-per-metric", 0, "The maximum number of active series per metric name, across the cluster before replication. 0 to disable.")
152+
f.BoolVar(&l.MemorySnapshotOnShutdownPerUser, "blocks-storage.tsdb.memory-snapshot-on-shutdown-per-user", false, "True to enable snapshotting in-memory TSDB data of per user on disk when shutting down.")
150153

151154
f.IntVar(&l.MaxLocalMetricsWithMetadataPerUser, "ingester.max-metadata-per-user", 8000, "The maximum number of active metrics with metadata per user, per ingester. 0 to disable.")
152155
f.IntVar(&l.MaxLocalMetadataPerMetric, "ingester.max-metadata-per-metric", 10, "The maximum number of metadata per metric, per ingester. 0 to disable.")
@@ -391,6 +394,12 @@ func (o *Overrides) MaxGlobalSeriesPerMetric(userID string) int {
391394
return o.GetOverridesForUser(userID).MaxGlobalSeriesPerMetric
392395
}
393396

397+
// MemorySnapshotOnShutdownPerUser returns true when we should snapshot in-memory TSDB
398+
// data of per user on disk when shutting down.
399+
func (o *Overrides) MemorySnapshotOnShutdownPerUser(userID string) bool {
400+
return o.GetOverridesForUser(userID).MemorySnapshotOnShutdownPerUser
401+
}
402+
394403
// MaxChunksPerQueryFromStore returns the maximum number of chunks allowed per query when fetching
395404
// chunks from the long-term storage.
396405
func (o *Overrides) MaxChunksPerQueryFromStore(userID string) int {

0 commit comments

Comments
 (0)