From 682662b2ae1abb3ba7ce3aed76e98b5470c6abf0 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 13 Jul 2021 17:58:21 +0300 Subject: [PATCH] lib/storage: remove cache directory if it contains reset_cache_on_startup file See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1447 --- README.md | 5 +++++ docs/CHANGELOG.md | 1 + docs/README.md | 5 +++++ docs/Single-server-VictoriaMetrics.md | 5 +++++ lib/storage/storage.go | 12 ++++++++++++ 5 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 95d2e95bd..eed405a0a 100644 --- a/README.md +++ b/README.md @@ -1405,6 +1405,11 @@ These limits are approximate, so VictoriaMetrics can underflow/overflow the limi * VictoriaMetrics ignores `NaN` values during data ingestion. +## Cache removal + +VictoriaMetrics uses various internal caches. These caches are stored to `<-storageDataPath>/cache` directory during graceful shutdown (e.g. when VictoriaMetrics is stopped by sending `SIGINT` signal). The caches are read on the next VictoriaMetrics startup. Sometimes it is needed to remove such caches on the next startup. This can be performed by placing `reset_cache_on_startup` file inside the `<-storageDataPath>/cache` directory before the restart of VictoriaMetrics. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1447) for details. + + ## Data migration Use [vmctl](https://docs.victoriametrics.com/vmctl.html) for data migration. It supports the following data migration types: diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 8e63ab868..6e4e6252c 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -16,6 +16,7 @@ sort: 15 * FEATURE: support durations anywhere in [MetricsQL queries](https://docs.victoriametrics.com/MetricsQL.html). For example, `sum_over_time(m[1h]) / 1h` is a valid query, which is equivalent to `sum_over_time(m[1h]) / 3600`. * FEATURE: support durations without suffxies in [MetricsQL queries](https://docs.victoriametrics.com/MetricsQL.html). For example, `rate(m[3600])` is a valid query, which is equivalent to `rate(m[1h])`. * FEATURE: add `is_set` label to `flag` metrics. This allows determining explicitly set command-line flags with the query `flag{is_set="true"}`. +* FEATURE: add ability to remove caches stored inside `<-storageDataPath>/cache` on startup if `reset_cache_on_startup` file is present there. See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1447). * BUGFIX: vmagent: remove `{ %space %}` typo in `/targets` output. The typo has been introduced in v1.62.0. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1408). * BUGFIX: vmagent: fix CSS styles on `/targets` page. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1422). diff --git a/docs/README.md b/docs/README.md index 95d2e95bd..eed405a0a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1405,6 +1405,11 @@ These limits are approximate, so VictoriaMetrics can underflow/overflow the limi * VictoriaMetrics ignores `NaN` values during data ingestion. +## Cache removal + +VictoriaMetrics uses various internal caches. These caches are stored to `<-storageDataPath>/cache` directory during graceful shutdown (e.g. when VictoriaMetrics is stopped by sending `SIGINT` signal). The caches are read on the next VictoriaMetrics startup. Sometimes it is needed to remove such caches on the next startup. This can be performed by placing `reset_cache_on_startup` file inside the `<-storageDataPath>/cache` directory before the restart of VictoriaMetrics. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1447) for details. + + ## Data migration Use [vmctl](https://docs.victoriametrics.com/vmctl.html) for data migration. It supports the following data migration types: diff --git a/docs/Single-server-VictoriaMetrics.md b/docs/Single-server-VictoriaMetrics.md index fe9367247..0fca0f605 100644 --- a/docs/Single-server-VictoriaMetrics.md +++ b/docs/Single-server-VictoriaMetrics.md @@ -1409,6 +1409,11 @@ These limits are approximate, so VictoriaMetrics can underflow/overflow the limi * VictoriaMetrics ignores `NaN` values during data ingestion. +## Cache removal + +VictoriaMetrics uses various internal caches. These caches are stored to `<-storageDataPath>/cache` directory during graceful shutdown (e.g. when VictoriaMetrics is stopped by sending `SIGINT` signal). The caches are read on the next VictoriaMetrics startup. Sometimes it is needed to remove such caches on the next startup. This can be performed by placing `reset_cache_on_startup` file inside the `<-storageDataPath>/cache` directory before the restart of VictoriaMetrics. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1447) for details. + + ## Data migration Use [vmctl](https://docs.victoriametrics.com/vmctl.html) for data migration. It supports the following data migration types: diff --git a/lib/storage/storage.go b/lib/storage/storage.go index 9109d3832..84c4b67c8 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -158,6 +158,18 @@ func OpenStorage(path string, retentionMsecs int64, maxHourlySeries, maxDailySer return nil, fmt.Errorf("cannot create a directory for the storage at %q: %w", path, err) } + // Check whether the cache directory must be removed + // It is removed if it contains reset_cache_on_startup file. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1447 for details. + if fs.IsPathExist(s.cachePath + "/reset_cache_on_startup") { + logger.Infof("removing cache directory at %q, since it contains `reset_cache_on_startup` file...", s.cachePath) + var wg sync.WaitGroup + wg.Add(1) + fs.MustRemoveAllWithDoneCallback(s.cachePath, wg.Done) + wg.Wait() + logger.Infof("cache directory at %q has been successfully removed", s.cachePath) + } + // Protect from concurrent opens. flockF, err := fs.CreateFlockFile(path) if err != nil {