mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
fix(cache): retry once when a purge removes the blob during a hit
Get probed the blob in getOrFetch and opened it again after the singleflight. A purge between the two returned a storage error to the client. Get now resolves the entry a second time, which sees the miss and fetches again.
This commit is contained in:
Vendored
+38
-28
@@ -49,7 +49,6 @@ type Manager struct {
|
|||||||
sf singleflight.Group
|
sf singleflight.Group
|
||||||
config Config
|
config Config
|
||||||
cleanupWG sync.WaitGroup
|
cleanupWG sync.WaitGroup
|
||||||
mu sync.RWMutex
|
|
||||||
bcMu sync.RWMutex
|
bcMu sync.RWMutex
|
||||||
closeOnce sync.Once
|
closeOnce sync.Once
|
||||||
evictMu sync.Mutex // serialises LRU eviction; a second caller waits and re-checks its quota
|
evictMu sync.Mutex // serialises LRU eviction; a second caller waits and re-checks its quota
|
||||||
@@ -97,6 +96,9 @@ const defaultScanWaitTimeout = 30 * time.Second
|
|||||||
// cleanupBatchSize bounds how many rows one cleanup query reads.
|
// cleanupBatchSize bounds how many rows one cleanup query reads.
|
||||||
const cleanupBatchSize = metadata.DefaultBatchSize
|
const cleanupBatchSize = metadata.DefaultBatchSize
|
||||||
|
|
||||||
|
// getAttempts bounds how often Get re-resolves an entry whose blob vanished.
|
||||||
|
const getAttempts = 2
|
||||||
|
|
||||||
// Eviction metric reasons.
|
// Eviction metric reasons.
|
||||||
const (
|
const (
|
||||||
evictReasonTTL = "ttl"
|
evictReasonTTL = "ttl"
|
||||||
@@ -221,36 +223,44 @@ func (m *Manager) Get(ctx context.Context, registry, name, version string, fetch
|
|||||||
// Use singleflight to deduplicate concurrent requests
|
// Use singleflight to deduplicate concurrent requests
|
||||||
key := fmt.Sprintf("%s/%s/%s", registry, name, version)
|
key := fmt.Sprintf("%s/%s/%s", registry, name, version)
|
||||||
|
|
||||||
result, err, _ := m.sf.Do(key, func() (interface{}, error) {
|
var openErr error
|
||||||
// getOrFetch returns a CacheEntry with Data == nil. Each caller
|
// A purge can delete the blob between the cache-hit probe and the open
|
||||||
// re-opens its own storage reader below to avoid sharing a
|
// below. The second pass sees the miss and fetches again.
|
||||||
// single io.ReadCloser across concurrent waiters.
|
for attempt := 0; attempt < getAttempts; attempt++ {
|
||||||
return m.getOrFetch(ctx, registry, name, version, fetchFunc)
|
result, err, _ := m.sf.Do(key, func() (interface{}, error) {
|
||||||
})
|
// getOrFetch returns a CacheEntry with Data == nil. Each caller
|
||||||
|
// re-opens its own storage reader below to avoid sharing a
|
||||||
|
// single io.ReadCloser across concurrent waiters.
|
||||||
|
return m.getOrFetch(ctx, registry, name, version, fetchFunc)
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := result.(*CacheEntry)
|
||||||
|
if entry == nil || entry.Package == nil {
|
||||||
|
return nil, errors.New(errors.ErrCodeStorageFailure, "cache entry missing package metadata")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open a fresh ReadCloser per caller from storage.
|
||||||
|
data, err := m.storage.Get(ctx, entry.Package.StorageKey)
|
||||||
|
if err != nil {
|
||||||
|
openErr = err
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a copy so concurrent waiters don't share the Data field.
|
||||||
|
return &CacheEntry{
|
||||||
|
Data: data,
|
||||||
|
Package: entry.Package,
|
||||||
|
UpstreamURL: entry.UpstreamURL,
|
||||||
|
CacheControl: entry.CacheControl,
|
||||||
|
FromCache: entry.FromCache,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
entry := result.(*CacheEntry)
|
return nil, errors.Wrap(openErr, errors.ErrCodeStorageFailure, "failed to retrieve cached package")
|
||||||
if entry == nil || entry.Package == nil {
|
|
||||||
return nil, errors.New(errors.ErrCodeStorageFailure, "cache entry missing package metadata")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open a fresh ReadCloser per caller from storage.
|
|
||||||
data, err := m.storage.Get(ctx, entry.Package.StorageKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to retrieve cached package")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return a copy so concurrent waiters don't share the Data field.
|
|
||||||
return &CacheEntry{
|
|
||||||
Data: data,
|
|
||||||
Package: entry.Package,
|
|
||||||
UpstreamURL: entry.UpstreamURL,
|
|
||||||
CacheControl: entry.CacheControl,
|
|
||||||
FromCache: entry.FromCache,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getOrFetch implements the actual get-or-fetch logic
|
// getOrFetch implements the actual get-or-fetch logic
|
||||||
|
|||||||
Vendored
+54
@@ -0,0 +1,54 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// racyStorage deletes the blob and the row when the second Get opens it, as a purge would.
|
||||||
|
type racyStorage struct {
|
||||||
|
*memStorage
|
||||||
|
meta *memMetadata
|
||||||
|
gets atomic.Int32
|
||||||
|
armed atomic.Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *racyStorage) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||||
|
if s.armed.Load() && s.gets.Add(1) == 2 {
|
||||||
|
_ = s.Delete(ctx, key)
|
||||||
|
_ = s.meta.DeletePackage(ctx, "npm", "left-pad", "1.0.0")
|
||||||
|
}
|
||||||
|
return s.memStorage.Get(ctx, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A purge between the cache-hit probe and the caller's open must not fail the request.
|
||||||
|
func TestGetRecoversFromPurgeBetweenProbeAndOpen(t *testing.T) {
|
||||||
|
clock := newFakeClock()
|
||||||
|
meta := newMemMetadata(clock)
|
||||||
|
store := &racyStorage{memStorage: newMemStorage(), meta: meta}
|
||||||
|
|
||||||
|
mgr, err := New(store, meta, nil, nil, Config{Now: clock.Now, CleanupInterval: 24 * time.Hour})
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() { _ = mgr.Close() })
|
||||||
|
|
||||||
|
fetch := func(context.Context) (*FetchResult, error) {
|
||||||
|
return &FetchResult{Data: io.NopCloser(strings.NewReader("payload")), UpstreamURL: "https://upstream/x"}, nil
|
||||||
|
}
|
||||||
|
entry, err := mgr.Get(context.Background(), "npm", "left-pad", "1.0.0", fetch)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_ = entry.Data.Close()
|
||||||
|
|
||||||
|
store.armed.Store(true)
|
||||||
|
entry, err = mgr.Get(context.Background(), "npm", "left-pad", "1.0.0", fetch)
|
||||||
|
require.NoError(t, err)
|
||||||
|
body, err := io.ReadAll(entry.Data)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_ = entry.Data.Close()
|
||||||
|
require.Equal(t, "payload", string(body))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user