From 61c7f6beaefbb1a13fc39f3e8785b0bcd023eaaa Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 11 Apr 2022 12:25:30 +0300 Subject: [PATCH] app/vmselect/promql: allow calling InitRollupResultCache+StopRollupResultCache multiple times during tests Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2406 --- app/vmselect/promql/rollup_result_cache.go | 16 ++++++++++---- .../promql/rollup_result_cache_test.go | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/vmselect/promql/rollup_result_cache.go b/app/vmselect/promql/rollup_result_cache.go index 9ffd38a92..87f36fee8 100644 --- a/app/vmselect/promql/rollup_result_cache.go +++ b/app/vmselect/promql/rollup_result_cache.go @@ -97,6 +97,11 @@ var ( ) // InitRollupResultCache initializes the rollupResult cache +// +// if cachePath is empty, then the cache isn't stored to persistent disk. +// +// ResetRollupResultCache must be called when the cache must be reset. +// StopRollupResultCache must be called when the cache isn't needed anymore. func InitRollupResultCache(cachePath string) { rollupResultCachePath = cachePath startTime := time.Now() @@ -133,16 +138,19 @@ func InitRollupResultCache(cachePath string) { rollupResultCachePath, time.Since(startTime).Seconds(), fcs().EntriesCount, fcs().BytesSize) } - metrics.NewGauge(`vm_cache_entries{type="promql/rollupResult"}`, func() float64 { + // Use metrics.GetOrCreateGauge instead of metrics.NewGauge, + // so InitRollupResultCache+StopRollupResultCache could be called multiple times in tests. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2406 + metrics.GetOrCreateGauge(`vm_cache_entries{type="promql/rollupResult"}`, func() float64 { return float64(fcs().EntriesCount) }) - metrics.NewGauge(`vm_cache_size_bytes{type="promql/rollupResult"}`, func() float64 { + metrics.GetOrCreateGauge(`vm_cache_size_bytes{type="promql/rollupResult"}`, func() float64 { return float64(fcs().BytesSize) }) - metrics.NewGauge(`vm_cache_requests_total{type="promql/rollupResult"}`, func() float64 { + metrics.GetOrCreateGauge(`vm_cache_requests_total{type="promql/rollupResult"}`, func() float64 { return float64(fcs().GetCalls) }) - metrics.NewGauge(`vm_cache_misses_total{type="promql/rollupResult"}`, func() float64 { + metrics.GetOrCreateGauge(`vm_cache_misses_total{type="promql/rollupResult"}`, func() float64 { return float64(fcs().Misses) }) diff --git a/app/vmselect/promql/rollup_result_cache_test.go b/app/vmselect/promql/rollup_result_cache_test.go index de9a25e71..eac9224bb 100644 --- a/app/vmselect/promql/rollup_result_cache_test.go +++ b/app/vmselect/promql/rollup_result_cache_test.go @@ -3,11 +3,32 @@ package promql import ( "testing" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/fs" "github.com/VictoriaMetrics/VictoriaMetrics/lib/storage" "github.com/VictoriaMetrics/metricsql" ) +func TestRollupResultCacheInitStop(t *testing.T) { + t.Run("inmemory", func(t *testing.T) { + for i := 0; i < 5; i++ { + InitRollupResultCache("") + StopRollupResultCache() + } + }) + t.Run("file-based", func(t *testing.T) { + cacheFilePath := "test-rollup-result-cache" + for i := 0; i < 3; i++ { + InitRollupResultCache(cacheFilePath) + StopRollupResultCache() + } + fs.MustRemoveAll(cacheFilePath) + }) +} + func TestRollupResultCache(t *testing.T) { + InitRollupResultCache("") + defer StopRollupResultCache() + ResetRollupResultCache() window := int64(456) ec := &EvalConfig{