From 1f3125676997c85b0efcc100a6bfe3eebf291560 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Mon, 23 Nov 2026 12:59:56 +0000 Subject: [PATCH] fix(storage): release replaced bytes when a key is overwritten Put added the new size to the usage counter without subtracting the file it replaced. Each overwrite inflated usage until restart and could trip the quota on a cache that was not full. --- pkg/storage/filesystem/filesystem.go | 9 ++++- .../filesystem/quota_overwrite_test.go | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 pkg/storage/filesystem/quota_overwrite_test.go diff --git a/pkg/storage/filesystem/filesystem.go b/pkg/storage/filesystem/filesystem.go index a49dad3..323cab8 100644 --- a/pkg/storage/filesystem/filesystem.go +++ b/pkg/storage/filesystem/filesystem.go @@ -162,7 +162,12 @@ func (fs *FilesystemStorage) Put(ctx context.Context, key string, data io.Reader // only successfully renamed files. Quota check happens before increment // to avoid transient inflation seen by concurrent Puts. fs.mu.Lock() - if fs.quota > 0 && fs.used+written > fs.quota { + // An overwrite releases the bytes of the file it replaces. + var replaced int64 + if info, statErr := root.Stat(relPath); statErr == nil { + replaced = info.Size() + } + if fs.quota > 0 && fs.used-replaced+written > fs.quota { fs.mu.Unlock() _ = root.Remove(tempPath) metrics.RecordStorageOperation("filesystem", "put", "quota_exceeded") @@ -174,7 +179,7 @@ func (fs *FilesystemStorage) Put(ctx context.Context, key string, data io.Reader metrics.RecordStorageOperation("filesystem", "put", "error") return errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to rename temp file") } - fs.used += written + fs.used += written - replaced currentUsed := fs.used fs.mu.Unlock() diff --git a/pkg/storage/filesystem/quota_overwrite_test.go b/pkg/storage/filesystem/quota_overwrite_test.go new file mode 100644 index 0000000..aa6a8a1 --- /dev/null +++ b/pkg/storage/filesystem/quota_overwrite_test.go @@ -0,0 +1,38 @@ +package filesystem + +import ( + "context" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +// Used bytes must equal the bytes on disk after a key is overwritten. +func TestPutOverwriteKeepsUsageExact(t *testing.T) { + ctx := context.Background() + fs, err := New(t.TempDir(), 1000) + require.NoError(t, err) + + require.NoError(t, fs.Put(ctx, "npm/a/metadata", strings.NewReader(strings.Repeat("x", 400)), nil)) + require.NoError(t, fs.Put(ctx, "npm/a/metadata", strings.NewReader(strings.Repeat("y", 300)), nil)) + + quota, err := fs.GetQuota(ctx) + require.NoError(t, err) + require.EqualValues(t, 300, quota.Used) + + require.NoError(t, fs.Delete(ctx, "npm/a/metadata")) + quota, err = fs.GetQuota(ctx) + require.NoError(t, err) + require.EqualValues(t, 0, quota.Used) +} + +// An overwrite that fits once the old bytes are released must not hit the quota. +func TestPutOverwriteWithinQuota(t *testing.T) { + ctx := context.Background() + fs, err := New(t.TempDir(), 500) + require.NoError(t, err) + + require.NoError(t, fs.Put(ctx, "npm/a/metadata", strings.NewReader(strings.Repeat("x", 400)), nil)) + require.NoError(t, fs.Put(ctx, "npm/a/metadata", strings.NewReader(strings.Repeat("y", 450)), nil)) +}