lib/workingsetcache: properly initialize new cache when the stored cache has unexpected size

This is a follow-up for 9bc541587b
This commit is contained in:
Aliaksandr Valialkin
2025-11-10 12:48:09 +01:00
parent 5336091785
commit b2cd3bf1f2
6 changed files with 11 additions and 9 deletions

4
go.mod
View File

@@ -9,7 +9,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.2
github.com/VictoriaMetrics/VictoriaLogs v1.36.2-0.20251008164716-21c0fb3de84d
github.com/VictoriaMetrics/easyproto v0.1.4
github.com/VictoriaMetrics/fastcache v1.13.0
github.com/VictoriaMetrics/fastcache v1.13.1
github.com/VictoriaMetrics/metrics v1.40.2
github.com/VictoriaMetrics/metricsql v0.84.8
github.com/aws/aws-sdk-go-v2 v1.39.2
@@ -33,6 +33,7 @@ require (
github.com/valyala/gozstd v1.23.2
github.com/valyala/histogram v1.2.0
github.com/valyala/quicktemplate v1.8.0
go.yaml.in/yaml/v3 v3.0.4
golang.org/x/net v0.46.0
golang.org/x/oauth2 v0.32.0
golang.org/x/sys v0.37.0
@@ -152,7 +153,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.43.0 // indirect
golang.org/x/exp v0.0.0-20251002181428-27f1f14c8bb9 // indirect
golang.org/x/sync v0.17.0 // indirect

4
go.sum
View File

@@ -56,8 +56,8 @@ github.com/VictoriaMetrics/VictoriaLogs v1.36.2-0.20251008164716-21c0fb3de84d h1
github.com/VictoriaMetrics/VictoriaLogs v1.36.2-0.20251008164716-21c0fb3de84d/go.mod h1:JKZK8LZ9O38pW3+CbBSqL64nswBg6nJ0GE788b0Ps/8=
github.com/VictoriaMetrics/easyproto v0.1.4 h1:r8cNvo8o6sR4QShBXQd1bKw/VVLSQma/V2KhTBPf+Sc=
github.com/VictoriaMetrics/easyproto v0.1.4/go.mod h1:QlGlzaJnDfFd8Lk6Ci/fuLxfTo3/GThPs2KH23mv710=
github.com/VictoriaMetrics/fastcache v1.13.0 h1:AW4mheMR5Vd9FkAPUv+NH6Nhw+fmbTMGMsNAoA/+4G0=
github.com/VictoriaMetrics/fastcache v1.13.0/go.mod h1:hHXhl4DA2fTL2HTZDJFXWgW0LNjo6B+4aj2Wmng3TjU=
github.com/VictoriaMetrics/fastcache v1.13.1 h1:+DkWqdIpjm7G04zynEcDzx6JYtFDD5I9J3HJ3L4voFM=
github.com/VictoriaMetrics/fastcache v1.13.1/go.mod h1:hHXhl4DA2fTL2HTZDJFXWgW0LNjo6B+4aj2Wmng3TjU=
github.com/VictoriaMetrics/metrics v1.40.2 h1:OVSjKcQEx6JAwGeu8/KQm9Su5qJ72TMEW4xYn5vw3Ac=
github.com/VictoriaMetrics/metrics v1.40.2/go.mod h1:XE4uudAAIRaJE614Tl5HMrtoEU6+GDZO4QTnNSsZRuA=
github.com/VictoriaMetrics/metricsql v0.84.8 h1:5JXrvPJiYkYNqJVT7+hMZmpAwRHd3txBdlVIw4rJ1VM=

View File

@@ -100,9 +100,9 @@ func loadFromFileOrNew(filePath string, maxBytes int) *fastcache.Cache {
if errors.Is(err, os.ErrNotExist) {
logger.Infof("cache at path %s missing files; init new cache", filePath)
} else if strings.Contains(err.Error(), "contains maxBytes") {
} else if strings.Contains(err.Error(), "unexpected number of bucket chunks") {
// covers the cache reset due to max memory size change at
// https://github.com/VictoriaMetrics/fastcache/blob/198c85ee90a1f65127126b5904c191e70f083cbf/file.go#L133
// https://github.com/VictoriaMetrics/fastcache/blob/9bc541587b1df2a9198cb2a0425b9ada4005a505/file.go#L147
logger.Warnf("%s; init new cache", err)
} else {
logger.Errorf("cache at path %s is invalid: %s; init new cache", filePath, err)

View File

@@ -20,6 +20,8 @@ func TestLoadFromFileOrNewError(t *testing.T) {
defer fs.MustRemoveDir(t.Name())
f := func(path string, expErr string) {
t.Helper()
logBuffer := &bytes.Buffer{}
logger.SetOutputForTests(logBuffer)
defer logger.ResetOutputForTest()
@@ -52,7 +54,7 @@ func TestLoadFromFileOrNewError(t *testing.T) {
f(path, "invalid: cannot read maxBucketChunks")
path = initCacheForTest(t, `cacheMismatch`, 87654321)
f(path, "contains maxBytes=10000; want 33554432; init new cache")
f(path, "unexpected number of bucket chunks; got 2; want 1; init new cache")
}
func TestLoadFromFileOrNewOK(t *testing.T) {

View File

@@ -144,7 +144,7 @@ func load(filePath string, maxBytes int) (*Cache, error) {
maxBucketBytes := uint64((maxBytes + bucketsCount - 1) / bucketsCount)
expectedBucketChunks := (maxBucketBytes + chunkSize - 1) / chunkSize
if maxBucketChunks != expectedBucketChunks {
return nil, fmt.Errorf("cache file %s contains maxBytes=%d; want %d", filePath, maxBytes, expectedBucketChunks*chunkSize*bucketsCount)
return nil, fmt.Errorf("cache file %s contains unexpected number of bucket chunks; got %d; want %d", filePath, maxBucketChunks, expectedBucketChunks)
}
}

2
vendor/modules.txt vendored
View File

@@ -139,7 +139,7 @@ github.com/VictoriaMetrics/VictoriaLogs/lib/prefixfilter
# github.com/VictoriaMetrics/easyproto v0.1.4
## explicit; go 1.18
github.com/VictoriaMetrics/easyproto
# github.com/VictoriaMetrics/fastcache v1.13.0
# github.com/VictoriaMetrics/fastcache v1.13.1
## explicit; go 1.24.0
github.com/VictoriaMetrics/fastcache
# github.com/VictoriaMetrics/metrics v1.40.2